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

@ -14,6 +14,8 @@ namespace GUIStuff {
void Element::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { }
void Element::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { }
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::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_motion_callback(const InputManager::MouseMotionCallbackArgs& motion);
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);
protected:
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();
};
if(isHeld || ((d.isSelected || mouseHovering) && d.drawType == DrawType::TRANSPARENT_BORDER))
if(isHeld || ((d.isSelected || isHovering) && d.drawType == DrawType::TRANSPARENT_BORDER))
borderColor = io.theme->fillColor1;
else if(d.drawType == DrawType::TRANSPARENT_BORDER)
borderColor = io.theme->backColor2;
@ -30,7 +30,7 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
if(d.isSelected)
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);
else
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)
}) {
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_to_layout();
}
else if(mouseHovering != oldIsHovering || isHeld != oldIsHeld)
else if(mouseHovering != isHovering || isHeld != oldIsHeld)
gui.set_to_layout();
oldIsHovering = mouseHovering;
isHovering = mouseHovering;
}
void SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
if(mouseHovering != oldIsHovering)
if(mouseHovering != isHovering)
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);
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_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) override;
virtual void input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) override;
private:
bool instantResponse = false;
bool isHeld = false;
bool oldIsHovering = false;
bool isHovering = false;
bool moved = false;
std::function<void()> onClick;
};