Emulate touch events as mouse button events in layoutelement

This commit is contained in:
Yousef Khadadeh 2026-04-28 20:28:21 +03:00
parent 973014cb5f
commit 4c30939882
6 changed files with 33 additions and 11 deletions

View file

@ -493,7 +493,7 @@ void DrawingProgram::right_click_action_menu(Vector2f popupPos, const std::funct
innerContent();
}
}, LayoutElement::Callbacks{
.mouseButton = [&](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(button.down && button.button != InputManager::MouseButton::RIGHT && !l->mouseHovering)
clear_right_click_popup();
}

View file

@ -13,6 +13,7 @@ void LayoutElement::layout(const Clay_ElementId& id, const std::function<void(La
void LayoutElement::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
if(c.mouseButton) c.mouseButton(this, button);
if(c.onClick) c.onClick(this, button);
}
void LayoutElement::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
@ -23,6 +24,23 @@ void LayoutElement::input_mouse_wheel_callback(const InputManager::MouseWheelCal
if(c.mouseWheel) c.mouseWheel(this, wheel);
}
void LayoutElement::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
if(c.fingerTouch) c.fingerTouch(this, touch);
if(c.onClick) {
c.onClick(this, {
.deviceType = InputManager::MouseDeviceType::TOUCH,
.button = InputManager::MouseButton::LEFT,
.down = touch.down,
.clicks = static_cast<uint8_t>(touch.fingerTapCount),
.pos = touch.pos
});
}
}
void LayoutElement::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) {
if(c.fingerMotion) c.fingerMotion(this, motion);
}
void LayoutElement::input_key_callback(const InputManager::KeyCallbackArgs& key) {
if(c.key) c.key(this, key);
}

View file

@ -10,12 +10,17 @@ class LayoutElement : public Element {
std::function<void(LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button)> mouseButton;
std::function<void(LayoutElement* l, const InputManager::MouseMotionCallbackArgs& motion)> mouseMotion;
std::function<void(LayoutElement* l, const InputManager::MouseWheelCallbackArgs& wheel)> mouseWheel;
std::function<void(LayoutElement* l, const InputManager::FingerTouchCallbackArgs& touch)> fingerTouch;
std::function<void(LayoutElement* l, const InputManager::FingerMotionCallbackArgs& motion)> fingerMotion;
std::function<void(LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button)> onClick;
std::function<void(LayoutElement* l, const InputManager::KeyCallbackArgs& key)> key;
};
void layout(const Clay_ElementId& id, const std::function<void(LayoutElement*, const Clay_ElementId&)>& layout, const Callbacks& c = {});
virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override;
virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override;
virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) override;
virtual void input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) override;
virtual void input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) override;
virtual void input_key_callback(const InputManager::KeyCallbackArgs& key) override;
private:
Callbacks c;

View file

@ -491,7 +491,7 @@ void FileSelectScreen::title_bar() {
}
}
}, LayoutElement::Callbacks{
.mouseButton = [&](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& m) {
.onClick = [&](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& m) {
if(!l->mouseHovering && m.down) {
moreOptionsMenu = MoreOptionsMenu::CLOSED;
main.g.gui.set_to_layout();
@ -720,7 +720,7 @@ void FileSelectScreen::menu_black_box() {
}) {
}
}, LayoutElement::Callbacks{
.mouseButton = [&] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& m) {
.onClick = [&] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& m) {
if(m.down && m.button == InputManager::MouseButton::LEFT && l->mouseHovering) {
mainMenuOpenAnim->animation_trigger_reverse();
gui.set_to_layout();

View file

@ -2,7 +2,6 @@
#include "../MainProgram.hpp"
#include "DrawingProgramScreen.hpp"
#include "Helpers/ConvertVec.hpp"
#include "../GUIStuff/Elements/LayoutElement.hpp"
#include "../GUIStuff/ElementHelpers/ButtonHelpers.hpp"
#include "../GUIStuff/ElementHelpers/LayoutHelpers.hpp"
#include "../GUIStuff/ElementHelpers/TextLabelHelpers.hpp"

View file

@ -606,7 +606,7 @@ void Toolbar::top_toolbar() {
#endif
}
}, LayoutElement::Callbacks {
.mouseButton = [&, mainMenuButton](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, mainMenuButton](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(!l->mouseHovering && button.down && !mainMenuButton->mouseHovering) {
menuPopUpOpen = false;
gui.set_to_layout();
@ -846,7 +846,7 @@ void Toolbar::grid_menu(Element* gridMenuButton) {
}
}
}, LayoutElement::Callbacks {
.mouseButton = [&, i] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, i] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(l->mouseHovering && button.down && button.button == InputManager::MouseButton::LEFT) {
gridMenu.selectedGrid = i;
if(button.clicks == 2) {
@ -870,7 +870,7 @@ void Toolbar::grid_menu(Element* gridMenuButton) {
});
}
}, LayoutElement::Callbacks {
.mouseButton = [&, gridMenuButton] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, gridMenuButton] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(!l->mouseHovering && !l->childMouseHovering && !gridMenuButton->mouseHovering && button.down)
stop_displaying_grid_menu();
}
@ -928,7 +928,7 @@ void Toolbar::bookmark_menu(Element* bookmarkMenuButton) {
main.world->bMan.setup_list_gui();
}
}, LayoutElement::Callbacks {
.mouseButton = [&, bookmarkMenuButton] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, bookmarkMenuButton] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(!l->mouseHovering && !l->childMouseHovering && !bookmarkMenuButton->mouseHovering && button.down)
stop_displaying_bookmark_menu();
}
@ -958,7 +958,7 @@ void Toolbar::layer_menu(Element* layerMenuButton) {
main.world->drawProg.layerMan.listGUI.setup_list_gui();
}
}, LayoutElement::Callbacks {
.mouseButton = [&, layerMenuButton] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, layerMenuButton] (LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(!l->mouseHovering && !l->childMouseHovering && !layerMenuButton->mouseHovering && button.down)
stop_displaying_layer_menu();
}
@ -1216,7 +1216,7 @@ void Toolbar::color_picker_window(const char* id, Vector4f** color, GUIStuff::El
});
}
}, LayoutElement::Callbacks{
.mouseButton = [&, b, color](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, b, color](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(!l->mouseHovering && !l->childMouseHovering && !b->mouseHovering && button.down) {
*color = nullptr;
main.g.gui.set_to_layout();
@ -2111,7 +2111,7 @@ void Toolbar::file_picker_gui() {
text_label(gui, entry.filename().string());
}
}, LayoutElement::Callbacks {
.mouseButton = [&, selectedEntry, entry](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
.onClick = [&, selectedEntry, entry](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) {
if(l->mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) {
gui.set_post_callback_func([&, button, selectedEntry, entry] {
if(selectedEntry && button.clicks >= 2) {