diff --git a/src/DrawingProgram/DrawingProgram.cpp b/src/DrawingProgram/DrawingProgram.cpp index 29186a8..dd53c5f 100644 --- a/src/DrawingProgram/DrawingProgram.cpp +++ b/src/DrawingProgram/DrawingProgram.cpp @@ -334,7 +334,7 @@ void DrawingProgram::toolbar_gui() { auto& t = world.main.toolbar; gui.new_id("Drawing Program Toolbar GUI", [&] { - gui.element("Drawing Program Toolbar GUI", [&] (const Clay_ElementId& lId) { + gui.element("Drawing Program Toolbar GUI", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_FIT(0), .height = CLAY_SIZING_FIT(0)}, @@ -450,7 +450,7 @@ void DrawingProgram::selection_action_menu(Vector2f popupPos) { }); } }, LayoutElement::Callbacks{ - .mouseButton = [&](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { + .mouseButton = [&](LayoutElement*, const InputManager::MouseButtonCallbackArgs& button) { if(button.down && button.button != InputManager::MouseButton::RIGHT) clear_right_click_popup(); } @@ -473,7 +473,7 @@ void DrawingProgram::tool_options_gui() { auto& io = gui.io; float minGUIWidth = drawTool->get_type() == DrawingProgramToolType::SCREENSHOT ? 300 : 200; - gui.element("Drawing program tool options gui", [&] (const Clay_ElementId& lId) { + gui.element("Drawing program tool options gui", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_FIT(minGUIWidth), .height = CLAY_SIZING_FIT(0)}, diff --git a/src/GUIStuff/ElementHelpers/ButtonHelpers.cpp b/src/GUIStuff/ElementHelpers/ButtonHelpers.cpp index 1203404..36366e2 100644 --- a/src/GUIStuff/ElementHelpers/ButtonHelpers.cpp +++ b/src/GUIStuff/ElementHelpers/ButtonHelpers.cpp @@ -41,14 +41,16 @@ void text_button_sized(GUIManager& gui, const char* id, std::string_view text, C } } -void svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath, const SVGButtonOptions& options) { +SelectableButton* svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath, const SVGButtonOptions& options) { SelectableButton::Data d = selectable_button_options_to_data(options); d.innerContent = [&gui, &svgPath] (const SelectableButton::InnerContentCallbackParameters& p) { gui.element("icon", svgPath, p.isSelected || p.isHovering || p.isHeld); }; + SelectableButton* toRet; CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(options.size), .height = CLAY_SIZING_FIXED(options.size) } } }) { - gui.element(id, d); + toRet = gui.element(id, d); } + return toRet; } }} diff --git a/src/GUIStuff/ElementHelpers/ButtonHelpers.hpp b/src/GUIStuff/ElementHelpers/ButtonHelpers.hpp index e9fc478..abdbc51 100644 --- a/src/GUIStuff/ElementHelpers/ButtonHelpers.hpp +++ b/src/GUIStuff/ElementHelpers/ButtonHelpers.hpp @@ -44,7 +44,7 @@ struct FixedSizeColorButtonOptions { void text_button(GUIManager& gui, const char* id, std::string_view text, const TextButtonOptions& options = {}); void text_button_sized(GUIManager& gui, const char* id, std::string_view text, Clay_SizingAxis x, Clay_SizingAxis y, const TextButtonOptions& options = {}); -void svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath, const SVGButtonOptions& options = {}); +SelectableButton* svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath, const SVGButtonOptions& options = {}); template void color_button(GUIManager& gui, const char* id, T* val, const FixedSizeColorButtonOptions& options = {}) { gui.new_id(id, [&] { diff --git a/src/GUIStuff/ElementHelpers/LayoutHelpers.cpp b/src/GUIStuff/ElementHelpers/LayoutHelpers.cpp index b3db554..9713703 100644 --- a/src/GUIStuff/ElementHelpers/LayoutHelpers.cpp +++ b/src/GUIStuff/ElementHelpers/LayoutHelpers.cpp @@ -3,8 +3,8 @@ namespace GUIStuff { namespace ElementHelpers { -void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_SizingAxis x, Clay_SizingAxis y, const std::function& innerContent, const LayoutElement::Callbacks& callbacks) { - gui.element(id, [&] (const Clay_ElementId& lId) { +void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_SizingAxis x, Clay_SizingAxis y, const std::function& innerContent, const LayoutElement::Callbacks& callbacks) { + gui.element(id, [&] (LayoutElement* l, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = x, .height = y }, @@ -17,7 +17,7 @@ void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_Siz .floating = { .zIndex = gui.get_z_index(), .attachTo = CLAY_ATTACH_TO_PARENT }, .border = {.color = convert_vec4(gui.io.theme->fillColor2), .width = CLAY_BORDER_OUTSIDE(1)} }) { - innerContent(); + innerContent(l); } }, callbacks); } diff --git a/src/GUIStuff/ElementHelpers/LayoutHelpers.hpp b/src/GUIStuff/ElementHelpers/LayoutHelpers.hpp index 851895c..0fda635 100644 --- a/src/GUIStuff/ElementHelpers/LayoutHelpers.hpp +++ b/src/GUIStuff/ElementHelpers/LayoutHelpers.hpp @@ -4,7 +4,7 @@ namespace GUIStuff { namespace ElementHelpers { -void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_SizingAxis x, Clay_SizingAxis y, const std::function& innerContent, const LayoutElement::Callbacks& callbacks); +void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_SizingAxis x, Clay_SizingAxis y, const std::function& innerContent, const LayoutElement::Callbacks& callbacks); void left_to_right_layout(GUIManager& gui, Clay_SizingAxis x, Clay_SizingAxis y, const std::function& innerContent); void left_to_right_line_layout(GUIManager& gui, const std::function& innerContent); void left_to_right_line_centered_layout(GUIManager& gui, const std::function& innerContent); diff --git a/src/GUIStuff/Elements/CheckBox.cpp b/src/GUIStuff/Elements/CheckBox.cpp index e67865f..1fa0dea 100644 --- a/src/GUIStuff/Elements/CheckBox.cpp +++ b/src/GUIStuff/Elements/CheckBox.cpp @@ -20,16 +20,9 @@ void CheckBox::layout(const Clay_ElementId& id, const std::function& isT }) {} } -void CheckBox::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - isHovering = mouseHovering; - if(isHovering && button.button == InputManager::MouseButton::LEFT && button.down) +void CheckBox::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { + if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) gui.set_post_callback_func([&](){if(onClick) onClick();}); - return Element::input_mouse_button_callback(button, mouseHovering); -} - -void CheckBox::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - isHovering = mouseHovering; - return Element::input_mouse_motion_callback(motion, mouseHovering); } void CheckBox::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) { @@ -50,7 +43,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(isHovering ? io.theme->fillColor1 : io.theme->fillColor2)); + p.setColor4f(convert_vec4(mouseHovering ? io.theme->fillColor1 : io.theme->fillColor2)); p.setStyle(SkPaint::kStroke_Style); p.setStrokeWidth(0.15f); canvas->drawRoundRect(checkBox, 0.25f, 0.25f, p); @@ -77,7 +70,7 @@ void CheckBox::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderComma static BezierEasing anim{0.445, -0.733, 0.575, 1.627}; - float lerpTime2 = anim(smooth_two_way_time(hoverAnimation2, io.deltaTime, isHovering, CHECKBOX_ANIMATION_TIME)); + float lerpTime2 = anim(smooth_two_way_time(hoverAnimation2, io.deltaTime, mouseHovering, CHECKBOX_ANIMATION_TIME)); std::array points; diff --git a/src/GUIStuff/Elements/CheckBox.hpp b/src/GUIStuff/Elements/CheckBox.hpp index 0601ad0..c9c7b3a 100644 --- a/src/GUIStuff/Elements/CheckBox.hpp +++ b/src/GUIStuff/Elements/CheckBox.hpp @@ -8,13 +8,11 @@ class CheckBox : public Element { CheckBox(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, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; + virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; void layout(const Clay_ElementId& id, const std::function& isTicked, const std::function& onClick); private: static constexpr float CHECKBOX_ANIMATION_TIME = 0.3; - bool isHovering = false; float hoverAnimation2 = 0.0; std::function isTicked; std::function onClick; diff --git a/src/GUIStuff/Elements/ColorPicker.decl.hpp b/src/GUIStuff/Elements/ColorPicker.decl.hpp index 5a35210..4608eba 100644 --- a/src/GUIStuff/Elements/ColorPicker.decl.hpp +++ b/src/GUIStuff/Elements/ColorPicker.decl.hpp @@ -63,8 +63,8 @@ template class ColorPicker : public Element { ColorPicker(GUIManager& gui); void layout(const Clay_ElementId& id, T* data, bool selectAlpha, const std::function& onChange); virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA); - void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; - void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; + void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; + void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override; private: void update_color_picker_pos(const Vector2f& p); float get_sv_selection_area_size(); diff --git a/src/GUIStuff/Elements/ColorPicker.impl.hpp b/src/GUIStuff/Elements/ColorPicker.impl.hpp index 214c3ef..0709735 100644 --- a/src/GUIStuff/Elements/ColorPicker.impl.hpp +++ b/src/GUIStuff/Elements/ColorPicker.impl.hpp @@ -82,7 +82,7 @@ template void ColorPicker::clay_draw(SkCanvas* canvas, UpdateInp canvas->restore(); } -template void ColorPicker::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { +template void ColorPicker::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { held = HeldBar::NONE; if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down && boundingBox.has_value()) { auto& bb = boundingBox.value(); @@ -103,12 +103,10 @@ template void ColorPicker::input_mouse_button_callback(const Inp } } update_color_picker_pos(button.pos); - Element::input_mouse_button_callback(button, mouseHovering); } -template void ColorPicker::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { +template void ColorPicker::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { update_color_picker_pos(motion.pos); - Element::input_mouse_motion_callback(motion, mouseHovering); } template void ColorPicker::update_color_picker_pos(const Vector2f& p) { diff --git a/src/GUIStuff/Elements/ColorPickerButton.impl.hpp b/src/GUIStuff/Elements/ColorPickerButton.impl.hpp index b03595e..bf4a871 100644 --- a/src/GUIStuff/Elements/ColorPickerButton.impl.hpp +++ b/src/GUIStuff/Elements/ColorPickerButton.impl.hpp @@ -19,18 +19,17 @@ template void ColorPickerButton::layout(const Clay_ElementId& id }); if(isOpen) { gui.set_z_index(gui.get_z_index() + 1, [&] { - ElementHelpers::top_to_bottom_window_popup_layout(gui, "Color Picker", CLAY_SIZING_FIT(300), CLAY_SIZING_FIT(0), [&, val, data = data]() { + ElementHelpers::top_to_bottom_window_popup_layout(gui, "Color Picker", CLAY_SIZING_FIT(300), CLAY_SIZING_FIT(0), [&, val, data = data](LayoutElement*) { ElementHelpers::color_picker_items(gui, "c", val, { .hasAlpha = data.hasAlpha, .onEdit = data.onEdit }); }, { - .mouseButton = [&](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - if(!mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) { + .mouseButton = [&](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) { + if(!l->mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) { isOpen = false; gui.set_to_layout(); } - return mouseHovering; } }); }); diff --git a/src/GUIStuff/Elements/DropDown.hpp b/src/GUIStuff/Elements/DropDown.hpp index 49ca55d..67fac05 100644 --- a/src/GUIStuff/Elements/DropDown.hpp +++ b/src/GUIStuff/Elements/DropDown.hpp @@ -53,7 +53,7 @@ template class DropDown : public Element { if(isOpen) { gui.set_z_index(gui.get_z_index() + 1, [&] { - gui.element("DROPDOWN", [&](const Clay_ElementId& lId) { + gui.element("DROPDOWN", [&](LayoutElement*, const Clay_ElementId& lId) { Clay_ElementData dropdownElemData = Clay_GetElementData(lId); float calculatedDropdownMaxHeight = 0.0f; if(dropdownElemData.found) @@ -87,7 +87,7 @@ template class DropDown : public Element { .clipHorizontal = true, .elementContent = [&](size_t i) { bool selectedEntry = static_cast(*data) == i; - gui.element("elem", [&] (const Clay_ElementId& lId) { + gui.element("elem", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_FIXED(250), .height = CLAY_SIZING_FIXED(18)}, @@ -117,9 +117,9 @@ template class DropDown : public Element { } } }, LayoutElement::Callbacks { - .mouseButton = [&, i](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - update_hovering_over(i, mouseHovering); - if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) { + .mouseButton = [&, i](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) { + update_hovering_over(i, l->mouseHovering); + if(l->mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) { gui.set_post_callback_func([&, i] { *d = i; isOpen = false; @@ -127,11 +127,9 @@ template class DropDown : public Element { }); gui.set_to_layout(); } - return mouseHovering; }, - .mouseMotion = [&, i](const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - update_hovering_over(i, mouseHovering); - return mouseHovering; + .mouseMotion = [&, i](LayoutElement* l, const InputManager::MouseMotionCallbackArgs& motion) { + update_hovering_over(i, l->mouseHovering); } }); } diff --git a/src/GUIStuff/Elements/Element.cpp b/src/GUIStuff/Elements/Element.cpp index d99350f..044d5ff 100644 --- a/src/GUIStuff/Elements/Element.cpp +++ b/src/GUIStuff/Elements/Element.cpp @@ -6,9 +6,9 @@ namespace GUIStuff { void Element::tick_update() {} - void Element::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { } - void Element::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { } - void Element::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) { } + 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_key_callback(const InputManager::KeyCallbackArgs& key) {} void Element::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {} diff --git a/src/GUIStuff/Elements/Element.hpp b/src/GUIStuff/Elements/Element.hpp index 875af85..64364bd 100644 --- a/src/GUIStuff/Elements/Element.hpp +++ b/src/GUIStuff/Elements/Element.hpp @@ -17,13 +17,17 @@ namespace GUIStuff { virtual ~Element() = default; int16_t zIndex; + bool mouseHovering = false; + bool childMouseHovering = false; + + Element* parent = nullptr; virtual void tick_update(); // Return true when mouse obstructs what's behind it - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering); - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering); - virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering); + 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_key_callback(const InputManager::KeyCallbackArgs& key); protected: static SCollision::AABB get_bb_from_command(Clay_RenderCommand* command); diff --git a/src/GUIStuff/Elements/LayoutElement.cpp b/src/GUIStuff/Elements/LayoutElement.cpp index 74250cf..e8d3fcf 100644 --- a/src/GUIStuff/Elements/LayoutElement.cpp +++ b/src/GUIStuff/Elements/LayoutElement.cpp @@ -5,28 +5,25 @@ namespace GUIStuff { LayoutElement::LayoutElement(GUIManager& gui): Element(gui) {} -void LayoutElement::layout(const Clay_ElementId& id, const std::function& layout, const Callbacks& c) { +void LayoutElement::layout(const Clay_ElementId& id, const std::function& layout, const Callbacks& c) { this->c = c; - layout(id); + layout(this, id); } -void LayoutElement::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - if(c.mouseButton) c.mouseButton(button, mouseHovering); - return Element::input_mouse_button_callback(button, mouseHovering); +void LayoutElement::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { + if(c.mouseButton) c.mouseButton(this, button); } -void LayoutElement::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - if(c.mouseMotion) c.mouseMotion(motion, mouseHovering); - return Element::input_mouse_motion_callback(motion, mouseHovering); +void LayoutElement::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { + if(c.mouseMotion) c.mouseMotion(this, motion); } -void LayoutElement::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) { - if(c.mouseWheel) c.mouseWheel(wheel, mouseHovering); - return Element::input_mouse_wheel_callback(wheel, mouseHovering); +void LayoutElement::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) { + if(c.mouseWheel) c.mouseWheel(this, wheel); } void LayoutElement::input_key_callback(const InputManager::KeyCallbackArgs& key) { - if(c.key) c.key(key); + if(c.key) c.key(this, key); } } diff --git a/src/GUIStuff/Elements/LayoutElement.hpp b/src/GUIStuff/Elements/LayoutElement.hpp index 2823c2f..beba2e5 100644 --- a/src/GUIStuff/Elements/LayoutElement.hpp +++ b/src/GUIStuff/Elements/LayoutElement.hpp @@ -7,15 +7,15 @@ class LayoutElement : public Element { public: LayoutElement(GUIManager& gui); struct Callbacks { - std::function mouseButton; - std::function mouseMotion; - std::function mouseWheel; - std::function key; + std::function mouseButton; + std::function mouseMotion; + std::function mouseWheel; + std::function key; }; - void layout(const Clay_ElementId& id, const std::function& layout, const Callbacks& c = {}); - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; - virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) override; + void layout(const Clay_ElementId& id, const std::function& 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_key_callback(const InputManager::KeyCallbackArgs& key) override; private: Callbacks c; diff --git a/src/GUIStuff/Elements/NumberSlider.hpp b/src/GUIStuff/Elements/NumberSlider.hpp index 71cc256..c3cef08 100644 --- a/src/GUIStuff/Elements/NumberSlider.hpp +++ b/src/GUIStuff/Elements/NumberSlider.hpp @@ -30,7 +30,7 @@ template class NumberSlider : public Element { canvas->save(); canvas->translate(bb.min.x(), bb.min.y()); - float lerpTimeHover = smooth_two_way_time(hoverAnimation, io.deltaTime, isHovering, io.theme->hoverExpandTime); + float lerpTimeHover = smooth_two_way_time(hoverAnimation, io.deltaTime, mouseHovering, io.theme->hoverExpandTime); static BezierEasing easeHeight(0.68, -1.55, 0.265, 2.55); @@ -73,19 +73,15 @@ template class NumberSlider : public Element { canvas->restore(); } - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override { - isHovering = mouseHovering; - isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down; + virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override { + isHeld = mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down; if(isHeld && boundingBox.has_value()) update_slider_pos(button.pos); - Element::input_mouse_button_callback(button, mouseHovering); } - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override { - isHovering = mouseHovering; + virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override { if(isHeld && boundingBox.has_value()) update_slider_pos(motion.pos); - Element::input_mouse_motion_callback(motion, mouseHovering); } private: @@ -98,7 +94,6 @@ template class NumberSlider : public Element { } bool isHeld = false; - bool isHovering = false; T* data = nullptr; T minData = 0.0; T maxData = 1.0; diff --git a/src/GUIStuff/Elements/PaintCircleMenu.cpp b/src/GUIStuff/Elements/PaintCircleMenu.cpp index 4ecff19..4e0816c 100644 --- a/src/GUIStuff/Elements/PaintCircleMenu.cpp +++ b/src/GUIStuff/Elements/PaintCircleMenu.cpp @@ -30,27 +30,23 @@ bool PaintCircleMenu::collides_with_point(const Vector2f& p) const { return boundingBox.has_value() && SCollision::collide(SCollision::Circle(boundingBox.value().center(), CIRCLE_END), p) && !SCollision::collide(SCollision::Circle(boundingBox.value().center(), PALETTE_START), p); } -void PaintCircleMenu::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { +void PaintCircleMenu::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { if(d.mouseButton) d.mouseButton(button, mouseHovering); - isHovering = mouseHovering; - isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down; + isHeld = mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down; update_paint_circle_menu_mouse_hover(button.pos); isRotateBarHeld = isRotateBarHovered && isHeld; update_paint_circle_menu_mouse(button.pos, button.button == InputManager::MouseButton::LEFT && button.down); - Element::input_mouse_button_callback(button, mouseHovering); } -void PaintCircleMenu::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - isHovering = mouseHovering; +void PaintCircleMenu::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { update_paint_circle_menu_mouse_hover(motion.pos); update_paint_circle_menu_mouse(motion.pos, false); - Element::input_mouse_motion_callback(motion, mouseHovering); } void PaintCircleMenu::update_paint_circle_menu_mouse_hover(const Vector2f& p) { float distFromCenter = vec_distance(p, boundingBox.value().center()); - isRotateBarHovered = isHovering && distFromCenter > ROTATE_START && distFromCenter < CIRCLE_END; - isColorBarHovered = isHovering && distFromCenter > PALETTE_START && distFromCenter < ROTATE_START; + isRotateBarHovered = mouseHovering && distFromCenter > ROTATE_START && distFromCenter < CIRCLE_END; + isColorBarHovered = mouseHovering && distFromCenter > PALETTE_START && distFromCenter < ROTATE_START; } void PaintCircleMenu::update_paint_circle_menu_mouse(const Vector2f& p, bool leftClicked) { diff --git a/src/GUIStuff/Elements/PaintCircleMenu.hpp b/src/GUIStuff/Elements/PaintCircleMenu.hpp index 48bfb25..8dd13e5 100644 --- a/src/GUIStuff/Elements/PaintCircleMenu.hpp +++ b/src/GUIStuff/Elements/PaintCircleMenu.hpp @@ -17,14 +17,13 @@ class PaintCircleMenu : public Element { void layout(const Clay_ElementId& id, const Data& data); virtual bool collides_with_point(const Vector2f& p) const override; virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override; - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; + virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; + virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override; private: void draw_rotate_bar(SkCanvas* canvas, UpdateInputData& io, bool skiaAA); void draw_palette_bar(SkCanvas* canvas, UpdateInputData& io, bool skiaAA); Data d; unsigned colorSelectionIndex = 0; - bool isHovering = false; bool isRotateBarHeld = false; bool isRotateBarHovered = false; bool isColorBarHovered = false; diff --git a/src/GUIStuff/Elements/PositionAdjustingPopupMenu.cpp b/src/GUIStuff/Elements/PositionAdjustingPopupMenu.cpp index b1429a9..cd9ebbf 100644 --- a/src/GUIStuff/Elements/PositionAdjustingPopupMenu.cpp +++ b/src/GUIStuff/Elements/PositionAdjustingPopupMenu.cpp @@ -15,7 +15,7 @@ void PositionAdjustingPopupMenu::layout(const Clay_ElementId& id, Vector2f popup popupPos.x() -= bb.width(); } - layoutElement = gui.element("popup", [&] (const Clay_ElementId& lId) { + layoutElement = gui.element("popup", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_FIT(100), .height = CLAY_SIZING_FIT(0)}, diff --git a/src/GUIStuff/Elements/RadioButton.cpp b/src/GUIStuff/Elements/RadioButton.cpp index 2cdfc7b..09d52e4 100644 --- a/src/GUIStuff/Elements/RadioButton.cpp +++ b/src/GUIStuff/Elements/RadioButton.cpp @@ -18,16 +18,9 @@ void RadioButton::layout(const Clay_ElementId& id, const std::function& }) {} } -void RadioButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - isHovering = mouseHovering; - if(isHovering && button.button == InputManager::MouseButton::LEFT && button.down) +void RadioButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { + if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) gui.set_post_callback_func([&] { if(onClick) onClick(); }); - Element::input_mouse_button_callback(button, mouseHovering); -} - -void RadioButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - isHovering = mouseHovering; - Element::input_mouse_motion_callback(motion, mouseHovering); } void RadioButton::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) { diff --git a/src/GUIStuff/Elements/RadioButton.hpp b/src/GUIStuff/Elements/RadioButton.hpp index 2a79a10..a4c9dab 100644 --- a/src/GUIStuff/Elements/RadioButton.hpp +++ b/src/GUIStuff/Elements/RadioButton.hpp @@ -7,8 +7,7 @@ class RadioButton : public Element { public: 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, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; + virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; void layout(const Clay_ElementId& id, const std::function& isTicked, const std::function& onClick); private: diff --git a/src/GUIStuff/Elements/RotateWheel.cpp b/src/GUIStuff/Elements/RotateWheel.cpp index fb799df..11430bb 100644 --- a/src/GUIStuff/Elements/RotateWheel.cpp +++ b/src/GUIStuff/Elements/RotateWheel.cpp @@ -34,25 +34,23 @@ float RotateWheel::wheel_end() { return boundingBox.value().width() * 0.5f; } -void RotateWheel::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - isHovering = mouseHovering; - isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down; +void RotateWheel::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { + isHeld = mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down; update_rotate_wheel_mouse_hover(button.pos); isRotateBarHeld = isRotateBarHovered && isHeld; update_rotate_wheel_mouse(button.pos); - Element::input_mouse_button_callback(button, mouseHovering); + Element::input_mouse_button_callback(button); } -void RotateWheel::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - isHovering = mouseHovering; +void RotateWheel::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { update_rotate_wheel_mouse_hover(motion.pos); update_rotate_wheel_mouse(motion.pos); - Element::input_mouse_motion_callback(motion, mouseHovering); + Element::input_mouse_motion_callback(motion); } void RotateWheel::update_rotate_wheel_mouse_hover(const Vector2f& p) { float distFromCenter = vec_distance(p, boundingBox.value().center()); - isRotateBarHovered = isHovering && distFromCenter > wheel_start() && distFromCenter < wheel_end(); + isRotateBarHovered = mouseHovering && distFromCenter > wheel_start() && distFromCenter < wheel_end(); } void RotateWheel::update_rotate_wheel_mouse(const Vector2f& p) { diff --git a/src/GUIStuff/Elements/RotateWheel.hpp b/src/GUIStuff/Elements/RotateWheel.hpp index 26aed43..a209993 100644 --- a/src/GUIStuff/Elements/RotateWheel.hpp +++ b/src/GUIStuff/Elements/RotateWheel.hpp @@ -8,8 +8,8 @@ class RotateWheel : public Element { RotateWheel(GUIManager& gui); void layout(const Clay_ElementId& id, double* rotateAngle, const std::function& onChange); virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override; - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; + virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; + virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override; private: float wheel_start(); @@ -18,7 +18,6 @@ class RotateWheel : public Element { double* rotateAngle = nullptr; bool isHeld = false; - bool isHovering = false; bool isRotateBarHeld = false; bool isRotateBarHovered = false; std::function onChange; diff --git a/src/GUIStuff/Elements/ScrollArea.cpp b/src/GUIStuff/Elements/ScrollArea.cpp index 47831f7..408c2e6 100644 --- a/src/GUIStuff/Elements/ScrollArea.cpp +++ b/src/GUIStuff/Elements/ScrollArea.cpp @@ -48,7 +48,7 @@ void ScrollArea::layout(const Clay_ElementId& id, const Options& options) { float scrollerPos = std::fabs(scrollOffset.y() / scrollPosMax); float areaAboveScrollerSize = scrollerPos * (sAreaDim - scrollerSize); - gui.element("scroll bar", [&] (const Clay_ElementId& lId) { + gui.element("scroll bar", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_FIXED(12), .height = CLAY_SIZING_GROW(0)}, @@ -71,28 +71,27 @@ void ScrollArea::layout(const Clay_ElementId& id, const Options& options) { } }) {} - gui.element("scroller", [&] (const Clay_ElementId& lId2) { + gui.element("scroller", [&] (LayoutElement*, const Clay_ElementId& lId2) { CLAY(lId2, { .layout = {.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(scrollerSize)}}, .backgroundColor = convert_vec4(scrollerColor), .cornerRadius = CLAY_CORNER_RADIUS(3), }) {} }, LayoutElement::Callbacks{ - .mouseMotion = [&] (const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - if(isHoveringOverScrollerY != mouseHovering) { - isHoveringOverScrollerY = mouseHovering; + .mouseMotion = [&] (LayoutElement* t, const InputManager::MouseMotionCallbackArgs& motion) { + if(isHoveringOverScrollerY != t->mouseHovering) { + isHoveringOverScrollerY = t->mouseHovering; gui.set_to_layout(); } - return mouseHovering; } }); CLAY_AUTO_ID({ .layout = {.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}}}) {} } }, LayoutElement::Callbacks{ - .mouseButton = [&, scrollPosMax, sAreaStart, sAreaDim, scrollerSize, areaAboveScrollerSize](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { + .mouseButton = [&, scrollPosMax, sAreaStart, sAreaDim, scrollerSize, areaAboveScrollerSize](LayoutElement* t, const InputManager::MouseButtonCallbackArgs& button) { if(button.button == InputManager::MouseButton::LEFT) { - if(button.down && mouseHovering) { + if(button.down && t->mouseHovering) { isScrollbarHeldY = true; if(isHoveringOverScrollerY) scrollerStartPosY = sAreaStart + areaAboveScrollerSize + scrollerSize * 0.5f; @@ -112,11 +111,10 @@ void ScrollArea::layout(const Clay_ElementId& id, const Options& options) { gui.set_to_layout(); } } - return mouseHovering; }, - .mouseMotion = [&, scrollPosMax, sAreaStart, sAreaDim, scrollerSize](const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - if(isScrollbarHoveredY != mouseHovering) { - isScrollbarHoveredY = mouseHovering; + .mouseMotion = [&, scrollPosMax, sAreaStart, sAreaDim, scrollerSize](LayoutElement* t, const InputManager::MouseMotionCallbackArgs& motion) { + if(isScrollbarHoveredY != t->mouseHovering) { + isScrollbarHoveredY = t->mouseHovering; gui.set_to_layout(); } if(isScrollbarHeldY) { @@ -131,7 +129,6 @@ void ScrollArea::layout(const Clay_ElementId& id, const Options& options) { if(oldScrollOffset != scrollOffset.y()) gui.set_to_layout(); } - return mouseHovering; } }); } @@ -147,7 +144,7 @@ void ScrollArea::clamp_scroll() { scrollOffset.y() = std::clamp(scrollOffset.y(), -std::max(0.0f, contentDimensions.y() - containerDimensions.y()), 0.0f); } -void ScrollArea::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) { +void ScrollArea::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) { if(mouseHovering) { Vector2f oldScrollOffset = scrollOffset; @@ -160,7 +157,6 @@ void ScrollArea::input_mouse_wheel_callback(const InputManager::MouseWheelCallba if(oldScrollOffset != scrollOffset) gui.set_to_layout(); } - Element::input_mouse_wheel_callback(wheel, mouseHovering); } } diff --git a/src/GUIStuff/Elements/ScrollArea.hpp b/src/GUIStuff/Elements/ScrollArea.hpp index 3d74f56..4524d95 100644 --- a/src/GUIStuff/Elements/ScrollArea.hpp +++ b/src/GUIStuff/Elements/ScrollArea.hpp @@ -28,7 +28,7 @@ class ScrollArea : public Element { void layout(const Clay_ElementId& id, const Options& options); void reset_scroll(); - virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) override; + virtual void input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel) override; private: Options opts; bool isScrollbarHeldY = false; diff --git a/src/GUIStuff/Elements/SelectableButton.cpp b/src/GUIStuff/Elements/SelectableButton.cpp index c016065..7c321dc 100644 --- a/src/GUIStuff/Elements/SelectableButton.cpp +++ b/src/GUIStuff/Elements/SelectableButton.cpp @@ -16,7 +16,7 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) { onClick = d.onClick; - if(isHeld || ((d.isSelected || isHovering) && d.drawType == DrawType::TRANSPARENT_BORDER)) + if(isHeld || ((d.isSelected || mouseHovering) && d.drawType == DrawType::TRANSPARENT_BORDER)) borderColor = io.theme->fillColor1; else if(d.drawType == DrawType::TRANSPARENT_BORDER) borderColor = io.theme->backColor2; @@ -25,7 +25,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(isHovering || isHeld) + else if(mouseHovering || isHeld) backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.2f); else backgroundColorHighlight = {0.0f, 0.0f, 0.0f, 0.0f}; @@ -58,31 +58,27 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) { .cornerRadius = CLAY_CORNER_RADIUS(4) }) { if(d.innerContent) - d.innerContent({.isSelected = d.isSelected, .isHovering = isHovering, .isHeld = isHeld}); + d.innerContent({.isSelected = d.isSelected, .isHovering = mouseHovering, .isHeld = isHeld}); } } } -void SelectableButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - bool oldIsHovering = isHovering; +void SelectableButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { bool oldIsHeld = isHeld; - isHovering = mouseHovering; - isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down; + isHeld = mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down; if(isHeld) { gui.set_post_callback_func([&]{ if(onClick) onClick(); }); gui.set_to_layout(); } - else if(isHovering != oldIsHovering || isHeld != oldIsHeld) + else if(mouseHovering != oldIsHovering || isHeld != oldIsHeld) gui.set_to_layout(); - Element::input_mouse_button_callback(button, mouseHovering); + oldIsHovering = mouseHovering; } -void SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { - bool oldIsHovering = isHovering; - isHovering = mouseHovering; - if(isHovering != oldIsHovering) +void SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { + if(mouseHovering != oldIsHovering) gui.set_to_layout(); - Element::input_mouse_motion_callback(motion, mouseHovering); + oldIsHovering = mouseHovering; } } diff --git a/src/GUIStuff/Elements/SelectableButton.hpp b/src/GUIStuff/Elements/SelectableButton.hpp index 339b28d..06b4777 100644 --- a/src/GUIStuff/Elements/SelectableButton.hpp +++ b/src/GUIStuff/Elements/SelectableButton.hpp @@ -27,12 +27,12 @@ class SelectableButton : public Element { SelectableButton(GUIManager& gui); void layout(const Clay_ElementId& id, const Data& d); - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; + virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) override; + virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) override; private: - bool isHovering = false; bool isHeld = false; + bool oldIsHovering = false; std::function onClick; }; diff --git a/src/GUIStuff/Elements/TextBox.decl.hpp b/src/GUIStuff/Elements/TextBox.decl.hpp index 62915f9..6902a7e 100644 --- a/src/GUIStuff/Elements/TextBox.decl.hpp +++ b/src/GUIStuff/Elements/TextBox.decl.hpp @@ -26,8 +26,8 @@ template class TextBox : public Element { void layout(const Clay_ElementId& id, const TextBoxData& userInfo); virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override; - virtual void input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; - virtual void input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) 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_key_callback(const InputManager::KeyCallbackArgs& key) override; ~TextBox(); private: diff --git a/src/GUIStuff/Elements/TextBox.impl.hpp b/src/GUIStuff/Elements/TextBox.impl.hpp index 50c91b0..de3b9fd 100644 --- a/src/GUIStuff/Elements/TextBox.impl.hpp +++ b/src/GUIStuff/Elements/TextBox.impl.hpp @@ -61,7 +61,7 @@ template void TextBox::clay_draw(SkCanvas* canvas, UpdateInputDa canvas->restore(); } -template void TextBox::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { +template void TextBox::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) { auto& io = gui.io; if(button.button == InputManager::MouseButton::LEFT && boundingBox.has_value()) { @@ -84,13 +84,11 @@ template void TextBox::input_mouse_button_callback(const InputMa textbox->process_mouse_left_button(*cur, button.pos - boundingBox.value().min, 0, false, gui.io.input->key(InputManager::KEY_TEXT_SHIFT).held); } } - Element::input_mouse_button_callback(button, mouseHovering); } -template void TextBox::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { +template void TextBox::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) { if(boundingBox.has_value()) textbox->process_mouse_left_button(*cur, motion.pos - boundingBox.value().min, 0, gui.io.input->mouse.leftDown, gui.io.input->key(InputManager::KEY_TEXT_SHIFT).held); - Element::input_mouse_motion_callback(motion, mouseHovering); } template void TextBox::input_key_callback(const InputManager::KeyCallbackArgs& key) { diff --git a/src/GUIStuff/GUIManager.cpp b/src/GUIStuff/GUIManager.cpp index 21ed329..dc66a37 100644 --- a/src/GUIStuff/GUIManager.cpp +++ b/src/GUIStuff/GUIManager.cpp @@ -353,31 +353,42 @@ void GUIManager::input_key_callback(const InputManager::KeyCallbackArgs& key) { void GUIManager::input_mouse_button_callback(InputManager::MouseButtonCallbackArgs button) { button.pos *= io.guiScaleMultiplier; - mouse_callback(button.pos, [&button] (ElementContainer* e, bool mouseHovering) { e->elem->input_mouse_button_callback(button, mouseHovering); }); + mouse_callback(button.pos, [&button] (ElementContainer* e) { e->elem->input_mouse_button_callback(button); }); } void GUIManager::input_mouse_motion_callback(InputManager::MouseMotionCallbackArgs motion) { motion.pos *= io.guiScaleMultiplier; - mouse_callback(motion.pos, [&motion] (ElementContainer* e, bool mouseHovering) { e->elem->input_mouse_motion_callback(motion, mouseHovering); }); + mouse_callback(motion.pos, [&motion] (ElementContainer* e) { e->elem->input_mouse_motion_callback(motion); }); } void GUIManager::input_mouse_wheel_callback(InputManager::MouseWheelCallbackArgs wheel) { wheel.mousePos *= io.guiScaleMultiplier; - mouse_callback(wheel.mousePos, [&wheel] (ElementContainer* e, bool mouseHovering) { e->elem->input_mouse_wheel_callback(wheel, mouseHovering); }); + mouse_callback(wheel.mousePos, [&wheel] (ElementContainer* e) { e->elem->input_mouse_wheel_callback(wheel); }); } -void GUIManager::mouse_callback(const Vector2f& mousePos, const std::function& f) { +void GUIManager::mouse_callback(const Vector2f& mousePos, const std::function& f) { cursorObstructed = false; int16_t zIndexObstructed = 0; + + for(ElementContainer* e : orderedElements) + e->elem->childMouseHovering = false; + for(ElementContainer* e : orderedElements) { if((!cursorObstructed || zIndexObstructed == e->elem->zIndex) && e->elem->collides_with_point(mousePos)) { zIndexObstructed = e->elem->zIndex; cursorObstructed = true; - f(e, true); + e->elem->mouseHovering = true; + Element* nextParent = e->elem->parent; + while(nextParent) + nextParent->childMouseHovering |= true; } else - f(e, false); + e->elem->mouseHovering = false; } + + for(ElementContainer* e : orderedElements) + f(e); + run_post_callback_func(); layout_if_necessary(); } diff --git a/src/GUIStuff/GUIManager.hpp b/src/GUIStuff/GUIManager.hpp index c19b708..d01896a 100644 --- a/src/GUIStuff/GUIManager.hpp +++ b/src/GUIStuff/GUIManager.hpp @@ -37,18 +37,23 @@ class GUIManager { template ElementType* element(const char* id, const Args&... a) { push_id(id); + Element* oldParent = parentElement; ElementType* elem = insert_element(); + elem->parent = parentElement; Clay_ElementId clayId = strArena.elem_id_from_id_stack(idStack); elem->set_parent_clipping_region(clippingRegion); elem->layout(clayId, a...); elem->set_bounding_box_from_elem_data(Clay_GetElementData(clayId)); // Setting bounding box after layout ensures that the element will have its bounding box set if it's set to be drawn. + parentElement = oldParent; pop_id(); return elem; } template ElementType* clipping_element(const char* id, const Args&... a) { push_id(id); + Element* oldParent = parentElement; ElementType* elem = insert_element(); + elem->parent = parentElement; Clay_ElementId clayId = strArena.elem_id_from_id_stack(idStack); elem->set_parent_clipping_region(clippingRegion); auto oldClippingRegion = clippingRegion; @@ -61,6 +66,7 @@ class GUIManager { elem->layout(clayId, a...); elem->set_bounding_box_from_elem_data(Clay_GetElementData(clayId)); clippingRegion = oldClippingRegion; + parentElement = oldParent; pop_id(); return elem; } @@ -79,7 +85,7 @@ class GUIManager { void layout_end(); void single_layout_run(); - void mouse_callback(const Vector2f& mousePos, const std::function& f); + void mouse_callback(const Vector2f& mousePos, const std::function& f); GUIManagerIDStack idStack; std::vector orderedElements; @@ -104,6 +110,7 @@ class GUIManager { void pop_id(); int16_t zIndex = 0; + Element* parentElement = nullptr; std::optional> clippingRegion; std::function postCallbackFunc; diff --git a/src/Toolbar.cpp b/src/Toolbar.cpp index 8f80247..e561406 100644 --- a/src/Toolbar.cpp +++ b/src/Toolbar.cpp @@ -453,7 +453,7 @@ void Toolbar::top_toolbar() { auto& gui = main.g.gui; auto& io = gui.io; - gui.element("top menu bar", [&] (const Clay_ElementId& lId) { + gui.element("top menu bar", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) }, @@ -471,19 +471,15 @@ void Toolbar::top_toolbar() { bool layerMenuPopUpJustOpen = false; auto icon_button_top_toolbar = [&](const char* id, const std::string& svgPath, bool isSelected, const std::function& onClick) { - svg_icon_button(gui, id, svgPath, { + return svg_icon_button(gui, id, svgPath, { .drawType = SelectableButton::DrawType::TRANSPARENT_ALL, .isSelected = isSelected, .onClick = onClick }); }; - menuPopupOpenFlipped = false; - icon_button_top_toolbar("Main Menu Button", "data/icons/menu.svg", menuPopUpOpen, [&] { - if(!menuPopupOpenFlipped) { - menuPopUpOpen = !menuPopUpOpen; - menuPopupOpenFlipped = false; - } + Element* mainMenuButton = icon_button_top_toolbar("Main Menu Button", "data/icons/menu.svg", menuPopUpOpen, [&] { + menuPopUpOpen = !menuPopUpOpen; }); @@ -562,7 +558,7 @@ void Toolbar::top_toolbar() { } if(menuPopUpOpen) { gui.set_z_index(5, [&] { - gui.element("main menu popup", [&] (const Clay_ElementId& id) { + gui.element("main menu popup", [&] (LayoutElement*, const Clay_ElementId& id) { CLAY(id, { .layout = { .sizing = {.width = CLAY_SIZING_FIT(100), .height = CLAY_SIZING_FIT(0) }, @@ -657,10 +653,9 @@ void Toolbar::top_toolbar() { #endif } }, LayoutElement::Callbacks { - .mouseButton = [&](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - if(!mouseHovering && button.down && !menuPopupOpenFlipped) { + .mouseButton = [&, mainMenuButton](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) { + if(!l->mouseHovering && button.down && !mainMenuButton->mouseHovering) { menuPopUpOpen = false; - menuPopupOpenFlipped = true; gui.set_to_layout(); } } @@ -1225,7 +1220,7 @@ void Toolbar::color_picker_window(const char* id, Vector4f** color, bool* colorJ .padding = {.top = 40, .bottom = 40} } }) { - main.g.gui.element(id, [&](const Clay_ElementId& lId) { + main.g.gui.element(id, [&](LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_FIT(300), .height = CLAY_SIZING_FIT(0)}, @@ -1237,17 +1232,14 @@ void Toolbar::color_picker_window(const char* id, Vector4f** color, bool* colorJ .backgroundColor = convert_vec4(gui.io.theme->backColor1), .cornerRadius = CLAY_CORNER_RADIUS(gui.io.theme->windowCorners1) }) { - //template void color_picker_items(GUIManager& gui, const char* id, T* val, const ColorPickerItemsOptions& options = {}) { color_picker_items(gui, "colorpicker", *color, { .onEdit = onChange }); - //isUpdatingColorLeft |= color_palette("colorpickerleftpalette", colorLeft, hoveringOnDropdown); - //if(!Clay_Hovered() && !justAssignedColorLeft && !hoveringOnDropdown && io.mouse.leftClick) - // colorLeft = nullptr; + color_palette("colorpickerleftpalette", colorLeft, onChange); } }, LayoutElement::Callbacks{ - .mouseButton = [&, colorJustDisabled](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - if(!mouseHovering && button.down) { + .mouseButton = [&, colorJustDisabled](LayoutElement* l, const InputManager::MouseButtonCallbackArgs& button) { + if(!l->mouseHovering && button.down) { *colorJustDisabled = true; main.g.gui.set_to_layout(); } @@ -1256,7 +1248,7 @@ void Toolbar::color_picker_window(const char* id, Vector4f** color, bool* colorJ } } -bool Toolbar::color_palette(const char* id, Vector4f* color, bool& hoveringOnDropdown) { +bool Toolbar::color_palette(const char* id, Vector4f* color, const std::function& onChange) { auto& gui = main.g.gui; auto& io = gui.io; @@ -1528,7 +1520,7 @@ void Toolbar::center_obstructing_window_gui(const char* id, Clay_SizingAxis x, C auto& io = gui.io; gui.set_z_index(100, [&] { - gui.element(id, [&] (const Clay_ElementId& id) { + gui.element(id, [&] (LayoutElement*, const Clay_ElementId& id) { CLAY(id, { .layout = { .sizing = {.width = x, .height = y }, @@ -2123,7 +2115,7 @@ void Toolbar::file_picker_gui() { .elementContent = [&] (size_t i) { const std::filesystem::path& entry = filePicker.entries[i]; bool selectedEntry = filePicker.currentSelectedPath == entry; - gui.element("elem", [&] (const Clay_ElementId& lId) { + gui.element("elem", [&] (LayoutElement*, const Clay_ElementId& lId) { CLAY(lId, { .layout = { .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(entryHeight)}, @@ -2146,8 +2138,8 @@ void Toolbar::file_picker_gui() { text_label(gui, entry.filename().string()); } }, LayoutElement::Callbacks { - .mouseButton = [&, selectedEntry, entry](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { - if(mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) { + .mouseButton = [&, 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) { if(std::filesystem::is_directory(entry)) { diff --git a/src/Toolbar.hpp b/src/Toolbar.hpp index ac8419d..e900568 100644 --- a/src/Toolbar.hpp +++ b/src/Toolbar.hpp @@ -63,6 +63,7 @@ class Toolbar { bool colorLeftJustDisabled = false; Vector4f* colorLeftJustEnabled = nullptr; std::function onColorLeftChange; + Vector4f* colorRight = nullptr; bool colorRightJustDisabled = false; Vector4f* colorRightJustEnabled = nullptr; @@ -89,7 +90,7 @@ class Toolbar { void file_picker_gui(); void performance_metrics(); void color_picker_window(const char* id, Vector4f** color, bool* colorJustDisabled, const std::function& onChange); - bool color_palette(const char* id, Vector4f* color, bool& hoveringOnDropdown); + bool color_palette(const char* id, Vector4f* color, const std::function& onChange); void open_world_file(bool isClient, const std::string& netSource, const std::string& serverLocalID); void load_default_palette(); void load_default_theme(); @@ -151,7 +152,6 @@ class Toolbar { bool showPerformance = false; bool menuPopUpOpen = false; - bool menuPopupOpenFlipped = false; bool optionsMenuOpen = false; bool playerMenuOpen = false;