Fix OnMouseEnter/Leave inside Panels

Also remove OnMouseMovedInside and the dx and dy parameters of OnMouseMoved because nothing used these for anything.
This commit is contained in:
Tamás Bálint Misius 2024-02-06 14:56:40 +01:00
parent 588fe293ec
commit e6e36a6b7c
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
18 changed files with 78 additions and 89 deletions

View File

@ -1055,10 +1055,17 @@ void GameView::updateToolButtonScroll()
{
for (auto *button : toolButtons)
{
if (button->Position.X < x && button->Position.X + button->Size.X > x)
auto inside = button->Position.X < x && button->Position.X + button->Size.X > x;
if (inside && !button->MouseInside)
{
button->MouseInside = true;
button->OnMouseEnter(x, y);
else
}
if (!inside && button->MouseInside)
{
button->MouseInside = false;
button->OnMouseLeave(x, y);
}
}
}
}

View File

@ -189,11 +189,7 @@ void Component::OnMouseHover(int localx, int localy)
{
}
void Component::OnMouseMoved(int localx, int localy, int dx, int dy)
{
}
void Component::OnMouseMovedInside(int localx, int localy, int dx, int dy)
void Component::OnMouseMoved(int localx, int localy)
{
}

View File

@ -43,6 +43,7 @@ namespace ui
bool Enabled;
bool Visible;
bool DoesTextInput;
bool MouseInside;
bool MouseDownInside;
ui::Appearance Appearance;
@ -96,20 +97,8 @@ namespace ui
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// dx: Mouse X delta.
// dy: Mouse Y delta.
///
virtual void OnMouseMoved(int localx, int localy, int dx, int dy);
///
// Called: When the mouse moves.
// Params:
// localx: Local mouse X position.
// localy: Local mouse Y position.
// dx: Mouse X delta.
// dy: Mouse Y delta.
///
virtual void OnMouseMovedInside(int localx, int localy, int dx, int dy);
virtual void OnMouseMoved(int localx, int localy);
///
// Called: When the mouse moves on top of the item.

View File

@ -133,7 +133,7 @@ void DirectionSelector::Draw(const ui::Point& screenPos)
g->BlendEllipse(center + value.offset, { handleRadius, handleRadius }, borderColor);
}
void DirectionSelector::OnMouseMoved(int x, int y, int dx, int dy)
void DirectionSelector::OnMouseMoved(int x, int y)
{
if (mouseDown)
{

View File

@ -78,7 +78,7 @@ public:
void SetValues(float x, float y);
void Draw(const ui::Point& screenPos) override;
void OnMouseMoved(int x, int y, int dx, int dy) override;
void OnMouseMoved(int x, int y) override;
void OnMouseDown(int x, int y, unsigned int button) override;
void OnMouseUp(int x, int y, unsigned button) override;
inline void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override { altDown = alt; }

View File

@ -146,7 +146,7 @@ void Label::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bo
}
}
void Label::OnMouseMoved(int localx, int localy, int dx, int dy)
void Label::OnMouseMoved(int localx, int localy)
{
if (selecting)
{

View File

@ -60,7 +60,7 @@ namespace ui
void OnContextMenuAction(int item) override;
virtual void OnMouseDown(int x, int y, unsigned button) override;
void OnMouseUp(int x, int y, unsigned button) override;
void OnMouseMoved(int localx, int localy, int dx, int dy) override;
void OnMouseMoved(int localx, int localy) override;
void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override;
void Draw(const Point& screenPos) override;
void Tick(float dt) override;

View File

@ -10,8 +10,7 @@ using namespace ui;
Panel::Panel(Point position, Point size):
Component(position, size),
InnerSize(size),
ViewportPosition(0, 0),
mouseInside(false)
ViewportPosition(0, 0)
{
}
@ -27,6 +26,7 @@ void Panel::AddChild(Component* c)
{
c->SetParent(this);
c->SetParentWindow(this->GetParentWindow());
c->MouseInside = false;
c->MouseDownInside = false;
}
@ -165,25 +165,26 @@ void Panel::OnMouseHover(int localx, int localy)
XOnMouseHover(localx, localy);
}
void Panel::OnMouseMoved(int localx, int localy, int dx, int dy)
void Panel::OnMouseMoved(int localx, int localy)
{
XOnMouseMoved(localx, localy, dx, dy);
PropagateMouseMove();
XOnMouseMoved(localx, localy);
for (size_t i = 0; i < children.size(); ++i)
{
if(children[i]->Enabled)
children[i]->OnMouseMoved(localx - children[i]->Position.X - ViewportPosition.X, localy - children[i]->Position.Y - ViewportPosition.Y, dx, dy);
children[i]->OnMouseMoved(localx - children[i]->Position.X - ViewportPosition.X, localy - children[i]->Position.Y - ViewportPosition.Y);
}
}
void Panel::OnMouseMovedInside(int localx, int localy, int dx, int dy)
void Panel::PropagateMouseMove()
{
mouseInside = true;
auto localx = ui::Engine::Ref().GetMouseX() - GetScreenPos().X;
auto localy = ui::Engine::Ref().GetMouseY() - GetScreenPos().Y;
for (size_t i = 0; i < children.size(); ++i)
{
if (children[i]->Enabled)
{
Point local (localx - children[i]->Position.X - ViewportPosition.X, localy - children[i]->Position.Y - ViewportPosition.Y)
, prevlocal (local.X - dx, local.Y - dy);
Point local (localx - children[i]->Position.X - ViewportPosition.X, localy - children[i]->Position.Y - ViewportPosition.Y);
// mouse currently inside?
if( local.X >= 0 &&
@ -191,14 +192,12 @@ void Panel::OnMouseMovedInside(int localx, int localy, int dx, int dy)
local.X < children[i]->Size.X &&
local.Y < children[i]->Size.Y )
{
children[i]->OnMouseMovedInside(localx - children[i]->Position.X - ViewportPosition.X, localy - children[i]->Position.Y - ViewportPosition.Y, dx, dy);
children[i]->OnMouseMoved(localx - children[i]->Position.X - ViewportPosition.X, localy - children[i]->Position.Y - ViewportPosition.Y);
// was the mouse outside?
if(!(prevlocal.X >= 0 &&
prevlocal.Y >= 0 &&
prevlocal.X < children[i]->Size.X &&
prevlocal.Y < children[i]->Size.Y ) )
if (!children[i]->MouseInside)
{
children[i]->MouseInside = true;
children[i]->OnMouseEnter(local.X, local.Y);
}
}
@ -206,31 +205,24 @@ void Panel::OnMouseMovedInside(int localx, int localy, int dx, int dy)
else
{
// was the mouse inside?
if( prevlocal.X >= 0 &&
prevlocal.Y >= 0 &&
prevlocal.X < children[i]->Size.X &&
prevlocal.Y < children[i]->Size.Y )
if (children[i]->MouseInside)
{
children[i]->MouseInside = false;
children[i]->OnMouseLeave(local.X, local.Y);
}
}
}
}
// always allow hover on parent (?)
XOnMouseMovedInside(localx, localy, dx, dy);
}
void Panel::OnMouseEnter(int localx, int localy)
{
mouseInside = true;
XOnMouseEnter(localx, localy);
}
void Panel::OnMouseLeave(int localx, int localy)
{
mouseInside = false;
XOnMouseLeave(localx, localy);
}
@ -332,11 +324,7 @@ void Panel::XOnMouseHover(int localx, int localy)
{
}
void Panel::XOnMouseMoved(int localx, int localy, int dx, int dy)
{
}
void Panel::XOnMouseMovedInside(int localx, int localy, int dx, int dy)
void Panel::XOnMouseMoved(int localx, int localy)
{
}

View File

@ -51,8 +51,7 @@ namespace ui
void Draw(const Point& screenPos) override;
void OnMouseHover(int localx, int localy) override;
void OnMouseMoved(int localx, int localy, int dx, int dy) override;
void OnMouseMovedInside(int localx, int localy, int dx, int dy) override;
void OnMouseMoved(int localx, int localy) override;
void OnMouseEnter(int localx, int localy) override;
void OnMouseLeave(int localx, int localy) override;
void OnMouseDown(int x, int y, unsigned button) override;
@ -66,7 +65,6 @@ namespace ui
protected:
// child components
std::vector<ui::Component*> children;
bool mouseInside;
// Overridable. Called by XComponent::Tick()
virtual void XTick(float dt);
@ -79,10 +77,7 @@ namespace ui
virtual void XOnMouseHover(int localx, int localy);
// Overridable. Called by XComponent::OnMouseMoved()
virtual void XOnMouseMoved(int localx, int localy, int dx, int dy);
// Overridable. Called by XComponent::OnMouseMovedInside()
virtual void XOnMouseMovedInside(int localx, int localy, int dx, int dy);
virtual void XOnMouseMoved(int localx, int localy);
// Overridable. Called by XComponent::OnMouseEnter()
virtual void XOnMouseEnter(int localx, int localy);
@ -110,6 +105,8 @@ namespace ui
// Overridable. Called by XComponent::OnKeyRelease()
virtual void XOnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void PropagateMouseMove();
};
}

View File

@ -355,17 +355,18 @@ void SaveButton::OnMouseDown(int x, int y, unsigned int button)
}
}
void SaveButton::OnMouseMovedInside(int x, int y, int dx, int dy)
void SaveButton::OnMouseMoved(int x, int y)
{
if(y > Size.Y-11)
isMouseInsideAuthor = true;
else
isMouseInsideAuthor = false;
isMouseInsideAuthor = false;
isMouseInsideHistory = false;
if (MouseInside)
{
if(y > Size.Y-11)
isMouseInsideAuthor = true;
if(showVotes && y > Size.Y-29 && y < Size.Y - 18 && x > 0 && x < 9)
isMouseInsideHistory = true;
else
isMouseInsideHistory = false;
if(showVotes && y > Size.Y-29 && y < Size.Y - 18 && x > 0 && x < 9)
isMouseInsideHistory = true;
}
}
void SaveButton::OnMouseEnter(int x, int y)

View File

@ -53,7 +53,7 @@ public:
void OnMouseEnter(int x, int y) override;
void OnMouseLeave(int x, int y) override;
void OnMouseMovedInside(int x, int y, int dx, int dy) override;
void OnMouseMoved(int x, int y) override;
void AddContextMenu(int menuType);
void OnContextMenuAction(int item) override;

View File

@ -108,10 +108,11 @@ void ScrollPanel::XOnMouseUp(int x, int y, unsigned int button)
scrollbarClickLocation = 0;
}
void ScrollPanel::XOnMouseMoved(int x, int y, int dx, int dy)
void ScrollPanel::XOnMouseMoved(int x, int y)
{
if(maxOffset.Y>0 && InnerSize.Y>0)
{
auto oldViewportPositionY = ViewportPosition.Y;
float scrollHeight = float(Size.Y)*(float(Size.Y)/float(InnerSize.Y));
float scrollPos = 0;
if (-ViewportPosition.Y>0)
@ -155,11 +156,18 @@ void ScrollPanel::XOnMouseMoved(int x, int y, int dx, int dy)
}
else
isMouseInsideScrollbar = false;
if (oldViewportPositionY != ViewportPosition.Y)
{
PropagateMouseMove();
}
}
}
void ScrollPanel::XTick(float dt)
{
auto oldViewportPositionY = ViewportPosition.Y;
if (panning)
{
auto scrollY = initialOffsetY + scrollbarInitialYClick - (Engine::Ref().GetMouseY() - GetScreenPos().Y);
@ -229,9 +237,9 @@ void ScrollPanel::XTick(float dt)
}
}
if (mouseInside && scrollBarWidth < 6)
if (MouseInside && scrollBarWidth < 6)
scrollBarWidth++;
else if (!mouseInside && scrollBarWidth > 0 && !scrollbarSelected)
else if (!MouseInside && scrollBarWidth > 0 && !scrollbarSelected)
scrollBarWidth--;
if (isMouseInsideScrollbarArea && scrollbarClickLocation && !scrollbarSelected)
@ -251,4 +259,9 @@ void ScrollPanel::XTick(float dt)
offsetY += scrollbarClickLocation*scrollHeight/10;
ViewportPosition.Y -= int(scrollbarClickLocation*scrollHeight/10);
}
if (oldViewportPositionY != ViewportPosition.Y)
{
PropagateMouseMove();
}
}

View File

@ -41,6 +41,6 @@ namespace ui
void XOnMouseWheelInside(int localx, int localy, int d) override;
void XOnMouseDown(int localx, int localy, unsigned int button) override;
void XOnMouseUp(int x, int y, unsigned int button) override;
void XOnMouseMoved(int localx, int localy, int dx, int dy) override;
void XOnMouseMoved(int localx, int localy) override;
};
}

View File

@ -40,7 +40,7 @@ void Slider::updatePosition(int position)
}
}
void Slider::OnMouseMoved(int x, int y, int dx, int dy)
void Slider::OnMouseMoved(int x, int y)
{
if(isMouseDown)
{

View File

@ -24,7 +24,7 @@ public:
Slider(Point position, Point size, int steps);
virtual ~Slider() = default;
void OnMouseMoved(int x, int y, int dx, int dy) override;
void OnMouseMoved(int x, int y) override;
void OnMouseDown(int x, int y, unsigned button) override;
void OnMouseUp(int x, int y, unsigned button) override;
void Draw(const Point& screenPos) override;

View File

@ -592,7 +592,7 @@ void Textbox::OnMouseUp(int x, int y, unsigned button)
Label::OnMouseUp(x, y, button);
}
void Textbox::OnMouseMoved(int localx, int localy, int dx, int dy)
void Textbox::OnMouseMoved(int localx, int localy)
{
if(mouseDown)
{
@ -601,7 +601,7 @@ void Textbox::OnMouseMoved(int localx, int localy, int dx, int dy)
cursor = index.raw_index;
resetCursorPosition();
}
Label::OnMouseMoved(localx, localy, dx, dy);
Label::OnMouseMoved(localx, localy);
}
void Textbox::OnDefocus()

View File

@ -55,7 +55,7 @@ public:
void OnContextMenuAction(int item) override;
void OnMouseDown(int x, int y, unsigned button) override;
void OnMouseUp(int x, int y, unsigned button) override;
void OnMouseMoved(int localx, int localy, int dx, int dy) override;
void OnMouseMoved(int localx, int localy) override;
void OnKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override;
void OnVKeyPress(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt);
void OnKeyRelease(int key, int scan, bool repeat, bool shift, bool ctrl, bool alt) override;

View File

@ -43,12 +43,16 @@ void Window::AddComponent(Component* c)
if (c->GetParentWindow() == NULL)
{
c->SetParentWindow(this);
c->MouseInside = false;
c->MouseDownInside = false;
Components.push_back(c);
if (Engine::Ref().GetMouseX() > Position.X + c->Position.X && Engine::Ref().GetMouseX() < Position.X + c->Position.X + c->Size.X &&
Engine::Ref().GetMouseY() > Position.Y + c->Position.Y && Engine::Ref().GetMouseY() < Position.Y + c->Position.Y + c->Size.Y)
{
c->MouseInside = true;
c->OnMouseEnter(Engine::Ref().GetMouseX() - (Position.X + c->Position.X), Engine::Ref().GetMouseY() - (Position.Y + c->Position.Y));
}
}
else
{
@ -486,21 +490,17 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
Point local(x - Components[i]->Position.X, y - Components[i]->Position.Y);
Point a(local.X - dx, local.Y - dy);
Components[i]->OnMouseMoved(local.X, local.Y, dx, dy);
Components[i]->OnMouseMoved(local.X, local.Y);
if (local.X >= 0 &&
local.Y >= 0 &&
local.X < Components[i]->Size.X &&
local.Y < Components[i]->Size.Y && !halt)
{
Components[i]->OnMouseMovedInside(local.X, local.Y, dx, dy);
// entering?
if (!(a.X >= 0 &&
a.Y >= 0 &&
a.X < Components[i]->Size.X &&
a.Y < Components[i]->Size.Y ))
if (!Components[i]->MouseInside)
{
Components[i]->MouseInside = true;
Components[i]->OnMouseEnter(local.X, local.Y);
}
if (Components[i]->Enabled)
@ -509,11 +509,9 @@ void Window::DoMouseMove(int x_, int y_, int dx, int dy)
else if (!halt)
{
// leaving?
if (a.X >= 0 &&
a.Y >= 0 &&
a.X < Components[i]->Size.X &&
a.Y < Components[i]->Size.Y )
if (Components[i]->MouseInside)
{
Components[i]->MouseInside = false;
Components[i]->OnMouseLeave(local.X, local.Y);
}