Merge pull request #731 from MihailRis/update-mouse-events

Update mouse events
This commit is contained in:
MihailRis 2025-12-10 21:18:46 +03:00 • committed by GitHub
commit 2668b8e17b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 52 additions and 14 deletions

View file

@ -42,8 +42,11 @@ Examples:
- `ondoubleclick` - lua function called when you double click on an element.
- `onfocus` - lua function called when focusing on an element.
- `ondefocus` - lua function called when the element loses focus.
- `onmouseover` - lua function called when the cursor enters an element.
- `onmouseover` - lua function called when the cursor exits an element.
- `onmouseenter` - lua function called when the cursor enters an element.
- `onmouseleave` - lua function called when the cursor leaves an element.
- `onmouseover` - lua function called when the cursor hovers over an element.
- `onmouseout` - lua function called when the cursor exits an element.
onmouseenter and onmouseleave do not respect the elements hierarchy.
- `tooltip` - tooltip text
- `tooltip-delay` - tooltip show-up delay
- `gravity` - automatic positioning of the element in the container. (Does not work in automatic containers like panel). Values: *top-left, top-center, top-right, center-left, center-center, center-right, bottom-left, bottom-center, bottom-right*.

View file

@ -46,8 +46,11 @@
- `ondoubleclick` - lua функция вызываемая при двойном нажатии на элемент.
- `onfocus` - lua функция вызываемая при фокусировке на элемент.
- `ondefocus` - lua функция вызываемая при потере фокуса элеметом.
- `onmouseover` - lua функция вызываемая при входе курсора в элемент.
- `onmouseover` - lua функция вызываемая при выходе курсора из элемента.
- `onmouseenter` - lua функция вызываемая при входе курсора в элемент.
- `onmouseleave` - lua функция вызываемая при выходе курсора из элемента.
- `onmouseover` - lua функция вызываемая при наведении курсора на элемент.
- `onmouseout` - lua функция вызываемая при выходе курсора из элемента.
onmouseenter и onmouseleave не учитывают иерархию элементов.
- `tooltip` - текст всплывающей подсказки
- `tooltip-delay` - задержка появления всплывающей подсказки
- `gravity` - автоматическое позиционирование элемента в контейнере. (Не работает в автоматических контейнерах, как panel). Значения: *top-left, top-center, top-right, center-left, center-center, center-right, bottom-left, bottom-center, bottom-right*.

View file

@ -133,10 +133,10 @@ void GUI::actMouse(float delta, const CursorState& cursor) {
auto hover = container->getAt(cursor.pos);
if (this->hover && this->hover != hover) {
this->hover->setHover(false);
this->hover->setMouseEnter(false);
}
if (hover) {
hover->setHover(true);
if (hover && hover != this->hover) {
hover->setMouseEnter(true);
int scroll = input.getScroll();
if (scroll) {
@ -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) {
@ -219,7 +241,7 @@ void GUI::act(float delta, const glm::uvec2& vp) {
actMouse(delta, cursor);
} else {
if (hover) {
hover->setHover(false);
hover->setMouseEnter(false);
hover = nullptr;
}
}

View file

@ -2,6 +2,7 @@
#include "delegates.hpp"
#include <set>
#include <queue>
#include <memory>
#include <vector>
@ -80,6 +81,7 @@ namespace gui {
std::unique_ptr<Camera> uicamera;
std::shared_ptr<Menu> menu;
std::queue<runnable> postRunnables;
std::set<std::shared_ptr<UINode>> mouseOver;
PageLoaderFunc pagesLoader;

View file

@ -47,10 +47,11 @@ void UINode::setAlign(Align align) {
this->align = align;
}
void UINode::setHover(bool flag) {
if (hover == flag) {
return;
}
void UINode::setMouseEnter(bool flag) {
actions.notify(flag ? UIAction::MOUSE_ENTER : UIAction::MOUSE_LEAVE, gui);
}
void UINode::setMouseOver(bool flag) {
hover = flag;
actions.notify(flag ? UIAction::MOUSE_OVER : UIAction::MOUSE_OUT, gui);
}

View file

@ -59,7 +59,9 @@ namespace gui {
DEFOCUS,
RIGHT_CLICK,
MOUSE_OVER,
MOUSE_OUT
MOUSE_OUT,
MOUSE_ENTER,
MOUSE_LEAVE,
};
using ActionsSet = TaggedCallbacksSet<UIAction, GUI&>;
@ -167,11 +169,14 @@ namespace gui {
virtual void setAlign(Align align);
Align getAlign() const;
virtual void setHover(bool flag);
virtual void setMouseEnter(bool flag);
bool isHover() const;
void setMouseOver(bool flag);
virtual void setParent(UINode* node);
UINode* getParent() const;
std::shared_ptr<UINode> getParentShared() const;
/// @brief Set element color (doesn't affect inner elements).
/// Also replaces hover color to avoid adding extra properties

View file

@ -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(