Added separate touch events for GUI

This commit is contained in:
Yousef Khadadeh 2026-04-28 20:13:47 +03:00
parent c9c61a8b68
commit 973014cb5f
8 changed files with 66 additions and 12 deletions

View file

@ -163,7 +163,9 @@ void GUIHolder::input_mouse_wheel_callback(const InputManager::MouseWheelCallbac
} }
void GUIHolder::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) { void GUIHolder::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
gui.input_finger_touch_callback(touch);
} }
void GUIHolder::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) { void GUIHolder::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) {
gui.input_finger_motion_callback(motion);
} }

View file

@ -14,6 +14,8 @@ namespace GUIStuff {
void Element::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { } void Element::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { }
void Element::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { } void Element::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { }
void Element::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) { } void Element::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) { }
void Element::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) { }
void Element::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) { }
void Element::input_key_callback(const InputManager::KeyCallbackArgs& key) {} void Element::input_key_callback(const InputManager::KeyCallbackArgs& key) {}
void Element::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {} void Element::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {}

View file

@ -32,6 +32,8 @@ namespace GUIStuff {
virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button); virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button);
virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion); virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion);
virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel); virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel);
virtual void input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch);
virtual void input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion);
virtual void input_key_callback(const InputManager::KeyCallbackArgs& key); virtual void input_key_callback(const InputManager::KeyCallbackArgs& key);
protected: protected:
static SCollision::AABB<float> get_bb_from_command(Clay_RenderCommand* command); static SCollision::AABB<float> get_bb_from_command(Clay_RenderCommand* command);

View file

@ -21,7 +21,7 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
else if(d.onClick) d.onClick(); else if(d.onClick) d.onClick();
}; };
if(isHeld || ((d.isSelected || mouseHovering) && d.drawType == DrawType::TRANSPARENT_BORDER)) if(isHeld || ((d.isSelected || isHovering) && d.drawType == DrawType::TRANSPARENT_BORDER))
borderColor = io.theme->fillColor1; borderColor = io.theme->fillColor1;
else if(d.drawType == DrawType::TRANSPARENT_BORDER) else if(d.drawType == DrawType::TRANSPARENT_BORDER)
borderColor = io.theme->backColor2; borderColor = io.theme->backColor2;
@ -30,7 +30,7 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
if(d.isSelected) if(d.isSelected)
backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.4f); backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.4f);
else if(mouseHovering || isHeld) else if(isHovering || isHeld)
backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.2f); backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.2f);
else else
backgroundColorHighlight = {0.0f, 0.0f, 0.0f, 0.0f}; backgroundColorHighlight = {0.0f, 0.0f, 0.0f, 0.0f};
@ -63,7 +63,7 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
.cornerRadius = CLAY_CORNER_RADIUS(4) .cornerRadius = CLAY_CORNER_RADIUS(4)
}) { }) {
if(d.innerContent) if(d.innerContent)
d.innerContent({.isSelected = d.isSelected, .isHovering = mouseHovering, .isHeld = isHeld}); d.innerContent({.isSelected = d.isSelected, .isHovering = isHovering, .isHeld = isHeld});
} }
} }
} }
@ -81,15 +81,42 @@ void SelectableButton::input_mouse_button_callback(const InputManager::MouseButt
gui.set_post_callback_func(onClick); gui.set_post_callback_func(onClick);
gui.set_to_layout(); gui.set_to_layout();
} }
else if(mouseHovering != oldIsHovering || isHeld != oldIsHeld) else if(mouseHovering != isHovering || isHeld != oldIsHeld)
gui.set_to_layout(); gui.set_to_layout();
oldIsHovering = mouseHovering; isHovering = mouseHovering;
} }
void SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { void SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
if(mouseHovering != oldIsHovering) if(mouseHovering != isHovering)
gui.set_to_layout(); gui.set_to_layout();
oldIsHovering = mouseHovering; isHovering = mouseHovering;
}
void SelectableButton::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
bool oldIsHeld = isHeld;
bool oldIsHovering = isHovering;
isHeld = mouseHovering && touch.down;
isHovering = mouseHovering && touch.down;
if(isHeld) {
if(instantResponse)
gui.set_post_callback_func(onClick);
gui.set_to_layout();
}
else if(mouseHovering && oldIsHeld && !touch.down) {
if(!instantResponse)
gui.set_post_callback_func(onClick);
gui.set_to_layout();
}
else if(oldIsHovering != isHovering || isHeld != oldIsHeld)
gui.set_to_layout();
}
void SelectableButton::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) {
if(isHovering || isHeld) {
isHovering = false;
isHeld = false;
gui.set_to_layout();
}
} }
} }

View file

@ -31,11 +31,14 @@ class SelectableButton : public Element {
void layout(const Clay_ElementId& id, const Data& d); void layout(const Clay_ElementId& id, const Data& d);
virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; 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_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override;
virtual void input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) override;
virtual void input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) override;
private: private:
bool instantResponse = false; bool instantResponse = false;
bool isHeld = false; bool isHeld = false;
bool oldIsHovering = false; bool isHovering = false;
bool moved = false;
std::function<void()> onClick; std::function<void()> onClick;
}; };

View file

@ -690,13 +690,17 @@ void GUIManager::input_key_callback(const InputManager::KeyCallbackArgs& key) {
} }
void GUIManager::input_mouse_button_callback(InputManager::MouseButtonCallbackArgs button) { void GUIManager::input_mouse_button_callback(InputManager::MouseButtonCallbackArgs button) {
button.pos /= io.guiScaleMultiplier; if(button.deviceType != InputManager::MouseDeviceType::TOUCH) {
mouse_callback(button.pos, [&button] (ElementContainer* e) { e->elem->input_mouse_button_callback(button); }); button.pos /= io.guiScaleMultiplier;
mouse_callback(button.pos, [&button] (ElementContainer* e) { e->elem->input_mouse_button_callback(button); });
}
} }
void GUIManager::input_mouse_motion_callback(InputManager::MouseMotionCallbackArgs motion) { void GUIManager::input_mouse_motion_callback(InputManager::MouseMotionCallbackArgs motion) {
motion.pos /= io.guiScaleMultiplier; if(motion.deviceType != InputManager::MouseDeviceType::TOUCH) {
mouse_callback(motion.pos, [&motion] (ElementContainer* e) { e->elem->input_mouse_motion_callback(motion); }); motion.pos /= io.guiScaleMultiplier;
mouse_callback(motion.pos, [&motion] (ElementContainer* e) { e->elem->input_mouse_motion_callback(motion); });
}
} }
void GUIManager::input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs wheel) { void GUIManager::input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs wheel) {
@ -704,6 +708,16 @@ void GUIManager::input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs
mouse_callback(wheel.mousePos, [&wheel] (ElementContainer* e) { e->elem->input_mouse_wheel_callback(wheel); }); mouse_callback(wheel.mousePos, [&wheel] (ElementContainer* e) { e->elem->input_mouse_wheel_callback(wheel); });
} }
void GUIManager::input_finger_touch_callback(InputManager::FingerTouchCallbackArgs touch) {
touch.pos /= io.guiScaleMultiplier;
mouse_callback(touch.pos, [&touch] (ElementContainer* e) { e->elem->input_finger_touch_callback(touch); });
}
void GUIManager::input_finger_motion_callback(InputManager::FingerMotionCallbackArgs motion) {
motion.pos /= io.guiScaleMultiplier;
mouse_callback(motion.pos, [&motion] (ElementContainer* e) { e->elem->input_finger_motion_callback(motion); });
}
void GUIManager::mouse_callback(const Vector2f& mousePos, const std::function<void(ElementContainer*)>& f) { void GUIManager::mouse_callback(const Vector2f& mousePos, const std::function<void(ElementContainer*)>& f) {
cursorObstructed = false; cursorObstructed = false;
int16_t zIndexObstructed = 0; int16_t zIndexObstructed = 0;

View file

@ -101,6 +101,8 @@ class GUIManager {
void input_mouse_button_callback(InputManager::MouseButtonCallbackArgs button); void input_mouse_button_callback(InputManager::MouseButtonCallbackArgs button);
void input_mouse_motion_callback(InputManager::MouseMotionCallbackArgs motion); void input_mouse_motion_callback(InputManager::MouseMotionCallbackArgs motion);
void input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs wheel); void input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs wheel);
void input_finger_touch_callback(InputManager::FingerTouchCallbackArgs touch);
void input_finger_motion_callback(InputManager::FingerMotionCallbackArgs motion);
bool cursor_obstructed() const; bool cursor_obstructed() const;
private: private:

View file

@ -397,11 +397,13 @@ void MainProgram::input_multi_finger_motion_callback(const InputManager::MultiFi
} }
void MainProgram::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) { void MainProgram::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
g.input_finger_touch_callback(touch);
screen->input_finger_touch_callback(touch); screen->input_finger_touch_callback(touch);
post_callback(); post_callback();
} }
void MainProgram::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) { void MainProgram::input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) {
g.input_finger_motion_callback(motion);
screen->input_finger_motion_callback(motion); screen->input_finger_motion_callback(motion);
post_callback(); post_callback();
} }