Added phone tool settings

This commit is contained in:
Yousef Khadadeh 2026-04-30 12:09:03 +03:00
parent f1d7cbf896
commit c90c09b169
42 changed files with 479 additions and 57 deletions

View file

@ -22,7 +22,7 @@ void CheckBox::layout(const Clay_ElementId& id, const std::function<bool()>& isT
}
void CheckBox::update() {
if(smooth_two_way_animation_time_check_for_change(hoverAnimation, gui.io.deltaTime, mouseHovering, CHECKBOX_ANIMATION_TIME))
if(smooth_two_way_animation_time_check_for_change(hoverAnimation, gui.io.deltaTime, is_hovering_animation(), CHECKBOX_ANIMATION_TIME))
gui.invalidate_draw_element(this);
if(oldIsTicked != isTicked()) {
gui.invalidate_draw_element(this);
@ -30,9 +30,26 @@ void CheckBox::update() {
}
}
bool CheckBox::is_hovering_animation() {
return mouseHovering && (!gui.last_interaction_is_touch() || isHeld);
}
void CheckBox::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down)
if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) {
gui.set_post_callback_func([&](){if(onClick) onClick();});
isHeld = true;
}
else
isHeld = false;
}
void CheckBox::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
if(mouseHovering && touch.down) {
gui.set_post_callback_func([&](){if(onClick) onClick();});
isHeld = true;
}
else
isHeld = false;
}
void CheckBox::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {
@ -53,7 +70,7 @@ void CheckBox::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderComma
}
else {
SkRect checkBox = SkRect::MakeLTRB(-0.45f, -0.45f, 0.45f, 0.45f);
p.setColor4f(convert_vec4<SkColor4f>(mouseHovering ? io.theme->fillColor1 : io.theme->fillColor2));
p.setColor4f(convert_vec4<SkColor4f>(is_hovering_animation() ? io.theme->fillColor1 : io.theme->fillColor2));
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(0.15f);
canvas->drawRoundRect(checkBox, 0.25f, 0.25f, p);

View file

@ -9,6 +9,7 @@ class CheckBox : public Element {
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override;
virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override;
virtual void input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) override;
virtual void update() override;
void layout(const Clay_ElementId& id, const std::function<bool()>& isTicked, const std::function<void()>& onClick);
@ -16,6 +17,8 @@ class CheckBox : public Element {
static constexpr float CHECKBOX_ANIMATION_TIME = 0.3;
float hoverAnimation = 0.0;
bool oldIsTicked = false;
bool isHeld = false;
bool is_hovering_animation();
std::function<bool()> isTicked;
std::function<void()> onClick;
};

View file

@ -1,6 +1,7 @@
#pragma once
#include "Element.hpp"
#include "../GUIManager.hpp"
#include "../../TimePoint.hpp"
namespace GUIStuff {
@ -35,7 +36,7 @@ template <typename T> class NumberSlider : public Element {
virtual void update() override {
smooth_two_way_animation_time(dd.holdAnimation, gui.io.deltaTime, dd.isHeld, HOLD_ANIMATION_TIME);
smooth_two_way_animation_time(dd.hoverAnimation, gui.io.deltaTime, mouseHovering, gui.io.theme->hoverExpandTime);
smooth_two_way_animation_time(dd.hoverAnimation, gui.io.deltaTime, mouseHovering && !gui.last_interaction_is_touch(), gui.io.theme->hoverExpandTime);
if(oldDD != dd) {
gui.invalidate_draw_element(this, {
.top = 10.0f,
@ -113,6 +114,23 @@ template <typename T> class NumberSlider : public Element {
update_slider_pos(motion.pos, false);
}
virtual void input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) override {
bool oldIsHeld = dd.isHeld;
dd.isHeld = mouseHovering && touch.down;
if(oldIsHeld && !dd.isHeld) {
gui.set_post_callback_func([&] {
if(config.onRelease) config.onRelease();
});
}
else if(dd.isHeld && boundingBox.has_value())
update_slider_pos(touch.pos, true);
}
virtual void input_finger_motion_callback(const InputManager::FingerMotionCallbackArgs& motion) override {
if(dd.isHeld && boundingBox.has_value())
update_slider_pos(motion.pos, false);
}
private:
void update_slider_pos(const Vector2f& p, bool justHeld) {
gui.set_post_callback_func([&, p, justHeld] {

View file

@ -20,7 +20,7 @@ void RadioButton::layout(const Clay_ElementId& id, const std::function<bool()>&
}
void RadioButton::update() {
if(smooth_two_way_animation_time_check_for_change(hoverAnimation, gui.io.deltaTime, mouseHovering, RADIOBUTTON_ANIMATION_TIME))
if(smooth_two_way_animation_time_check_for_change(hoverAnimation, gui.io.deltaTime, is_hovering_animation(), RADIOBUTTON_ANIMATION_TIME))
gui.invalidate_draw_element(this);
if(oldIsTicked != isTicked()) {
gui.invalidate_draw_element(this);
@ -28,9 +28,26 @@ void RadioButton::update() {
}
}
bool RadioButton::is_hovering_animation() {
return mouseHovering && (!gui.last_interaction_is_touch() || isHeld);
}
void RadioButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down)
if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) {
gui.set_post_callback_func([&] { if(onClick) onClick(); });
isHeld = true;
}
else
isHeld = false;
}
void RadioButton::input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) {
if(mouseHovering && touch.down) {
gui.set_post_callback_func([&] { if(onClick) onClick(); });
isHeld = true;
}
else
isHeld = false;
}
void RadioButton::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {
@ -62,7 +79,7 @@ void RadioButton::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCo
else {
SkPaint p;
p.setAntiAlias(skiaAA);
p.setColor4f(convert_vec4<SkColor4f>(mouseHovering ? io.theme->fillColor1 : io.theme->backColor2));
p.setColor4f(convert_vec4<SkColor4f>(is_hovering_animation() ? io.theme->fillColor1 : io.theme->backColor2));
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(0.15f);
canvas->drawCircle(0.0f, 0.0f, 0.5f, p);

View file

@ -8,12 +8,15 @@ class RadioButton : public Element {
RadioButton(GUIManager& gui);
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override;
virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override;
virtual void input_finger_touch_callback(const InputManager::FingerTouchCallbackArgs& touch) override;
virtual void update() override;
void layout(const Clay_ElementId& id, const std::function<bool()>& isTicked, const std::function<void()>& onClick);
private:
static constexpr float RADIOBUTTON_ANIMATION_TIME = 0.3;
bool oldIsTicked = false;
bool isHeld = false;
bool is_hovering_animation();
float hoverAnimation = 0.0;
std::function<bool()> isTicked;
std::function<void()> onClick;

View file

@ -27,6 +27,7 @@ GUIManager::GUIManager()
Clay_SetMeasureTextFunction(clay_skia_measure_text, this);
setToLayout = true;
postCallbackFuncIsHighPriority = false;
lastInteractionIsTouch = false;
}
Clay_Dimensions GUIManager::clay_skia_measure_text(Clay_StringSlice str, Clay_TextElementConfig* config, void* userData) {
@ -671,6 +672,10 @@ void GUIManager::invalidate_draw_in_area(const SCollision::AABB<float>& bb) {
invalidDrawBB = bb;
}
bool GUIManager::last_interaction_is_touch() {
return lastInteractionIsTouch;
}
bool GUIManager::cursor_obstructed() const {
return cursorObstructed;
}
@ -702,6 +707,7 @@ void GUIManager::input_key_callback(const InputManager::KeyCallbackArgs& key) {
void GUIManager::input_mouse_button_callback(InputManager::MouseButtonCallbackArgs button) {
if(button.deviceType != InputManager::MouseDeviceType::TOUCH) {
lastInteractionIsTouch = false;
button.pos /= io.guiScaleMultiplier;
mouse_callback(button.pos, [&button] (ElementContainer* e) { e->elem->input_mouse_button_callback(button); });
}
@ -709,6 +715,7 @@ void GUIManager::input_mouse_button_callback(InputManager::MouseButtonCallbackAr
void GUIManager::input_mouse_motion_callback(InputManager::MouseMotionCallbackArgs motion) {
if(motion.deviceType != InputManager::MouseDeviceType::TOUCH) {
lastInteractionIsTouch = false;
motion.pos /= io.guiScaleMultiplier;
motion.move /= io.guiScaleMultiplier;
mouse_callback(motion.pos, [&motion] (ElementContainer* e) { e->elem->input_mouse_motion_callback(motion); });
@ -721,11 +728,13 @@ void GUIManager::input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs
}
void GUIManager::input_finger_touch_callback(InputManager::FingerTouchCallbackArgs touch) {
lastInteractionIsTouch = true;
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) {
lastInteractionIsTouch = true;
motion.pos /= io.guiScaleMultiplier;
motion.move /= io.guiScaleMultiplier;
mouse_callback(motion.pos, [&motion] (ElementContainer* e) { e->elem->input_finger_motion_callback(motion); });

View file

@ -47,6 +47,8 @@ class GUIManager {
void invalidate_draw_element(Element* element, const BorderData& extraPadding = {});
void invalidate_draw_in_area(const SCollision::AABB<float>& bb);
bool last_interaction_is_touch();
template <typename ElementType, typename... Args> ElementType* element(const char* id, const Args&... a) {
push_id(id);
@ -161,6 +163,7 @@ class GUIManager {
void calculate_new_clip_rect(std::vector<SCollision::AABB<float>>& clipRectStack, std::optional<SCollision::AABB<float>>& clipRect, bool& clipNoDraw);
void clip_rect_transform(SkCanvas* canvas, std::vector<SCollision::AABB<float>>& clipRectStack, std::optional<SCollision::AABB<float>>& clipRect, bool& clipNoDraw);
bool lastInteractionIsTouch;
bool setToLayout;
bool setToUpdateInvalidateDrawAreaFromLayout;
bool cursorObstructed;