From 78e0ac5a9513962fd8f0856cf15c4f22cd461101 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 10 Dec 2025 20:03:41 +0300 Subject: [PATCH] add 'onmouseenter', 'onmouseleave' ui events --- src/graphics/ui/GUI.cpp | 22 ++++++++++++++++++++++ src/graphics/ui/GUI.hpp | 2 ++ src/graphics/ui/elements/UINode.cpp | 4 ++++ src/graphics/ui/elements/UINode.hpp | 7 ++++++- src/graphics/ui/gui_xml.cpp | 2 ++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/graphics/ui/GUI.cpp b/src/graphics/ui/GUI.cpp index 22ae5b908..82f798fe4 100644 --- a/src/graphics/ui/GUI.cpp +++ b/src/graphics/ui/GUI.cpp @@ -144,6 +144,28 @@ void GUI::actMouse(float delta, const CursorState& cursor) { } } this->hover = hover; + auto node = hover; + while (node) { + if (mouseOver.find(hover) != mouseOver.end()) { + break; + } + mouseOver.insert(node); + node->setMouseOver(true); + auto parent = node->getParent(); + if (parent) { + node = parent->shared_from_this(); + } + } + + for (auto it = mouseOver.begin(); it != mouseOver.end(); ) { + auto node = *it; + if (node->isInside(cursor.pos)) { + ++it; + continue; + } + node->setMouseOver(false); + it = mouseOver.erase(it); + } if (input.jclicked(Mousecode::BUTTON_1)) { if (pressed == nullptr && this->hover) { diff --git a/src/graphics/ui/GUI.hpp b/src/graphics/ui/GUI.hpp index 16d8e358c..8a12c3cd4 100644 --- a/src/graphics/ui/GUI.hpp +++ b/src/graphics/ui/GUI.hpp @@ -2,6 +2,7 @@ #include "delegates.hpp" +#include #include #include #include @@ -80,6 +81,7 @@ namespace gui { std::unique_ptr uicamera; std::shared_ptr menu; std::queue postRunnables; + std::set> mouseOver; PageLoaderFunc pagesLoader; diff --git a/src/graphics/ui/elements/UINode.cpp b/src/graphics/ui/elements/UINode.cpp index cbcb1c909..f4b4c5cb8 100644 --- a/src/graphics/ui/elements/UINode.cpp +++ b/src/graphics/ui/elements/UINode.cpp @@ -52,6 +52,10 @@ void UINode::setHover(bool flag) { return; } hover = flag; + actions.notify(flag ? UIAction::MOUSE_ENTER : UIAction::MOUSE_LEAVE, gui); +} + +void UINode::setMouseOver(bool flag) { actions.notify(flag ? UIAction::MOUSE_OVER : UIAction::MOUSE_OUT, gui); } diff --git a/src/graphics/ui/elements/UINode.hpp b/src/graphics/ui/elements/UINode.hpp index cbd43b380..f1c378fa1 100644 --- a/src/graphics/ui/elements/UINode.hpp +++ b/src/graphics/ui/elements/UINode.hpp @@ -59,7 +59,9 @@ namespace gui { DEFOCUS, RIGHT_CLICK, MOUSE_OVER, - MOUSE_OUT + MOUSE_OUT, + MOUSE_ENTER, + MOUSE_LEAVE, }; using ActionsSet = TaggedCallbacksSet; @@ -170,8 +172,11 @@ namespace gui { virtual void setHover(bool flag); bool isHover() const; + void setMouseOver(bool flag); + virtual void setParent(UINode* node); UINode* getParent() const; + std::shared_ptr getParentShared() const; /// @brief Set element color (doesn't affect inner elements). /// Also replaces hover color to avoid adding extra properties diff --git a/src/graphics/ui/gui_xml.cpp b/src/graphics/ui/gui_xml.cpp index 6011373fb..967fd6bca 100644 --- a/src/graphics/ui/gui_xml.cpp +++ b/src/graphics/ui/gui_xml.cpp @@ -186,6 +186,8 @@ static void read_uinode( register_action(node, reader, element, "ondoubleclick", UIAction::DOUBLE_CLICK); register_action(node, reader, element, "onmouseover", UIAction::MOUSE_OVER); register_action(node, reader, element, "onmouseout", UIAction::MOUSE_OUT); + register_action(node, reader, element, "onmouseenter", UIAction::MOUSE_ENTER); + register_action(node, reader, element, "onmouseleave", UIAction::MOUSE_LEAVE); } static void read_container_impl(