Fixed rotation wheel

This commit is contained in:
Yousef Khadadeh 2026-03-31 20:52:46 +03:00
parent ed36036aea
commit 80afacfcbd
9 changed files with 50 additions and 33 deletions

View file

@ -368,10 +368,10 @@ void DrawingProgram::toolbar_gui() {
tool_button("Zoom Canvas Toolbar Button", "data/icons/zoom.svg", DrawingProgramToolType::ZOOM); tool_button("Zoom Canvas Toolbar Button", "data/icons/zoom.svg", DrawingProgramToolType::ZOOM);
tool_button("Pan Canvas Toolbar Button", "data/icons/hand.svg", DrawingProgramToolType::PAN); tool_button("Pan Canvas Toolbar Button", "data/icons/hand.svg", DrawingProgramToolType::PAN);
std::shared_ptr<double> oldRotationAngle = std::make_shared<double>(world.drawData.cam.c.rotation); std::shared_ptr<double> newRotationAngle = std::make_shared<double>(world.drawData.cam.c.rotation);
gui.element<RotateWheel>("Canvas Rotate Wheel", &world.drawData.cam.c.rotation, [&, oldRotationAngle] { gui.element<RotateWheel>("Canvas Rotate Wheel", newRotationAngle.get(), [&, newRotationAngle] {
world.drawData.cam.c.rotate_about(world.drawData.cam.c.from_space(world.main.window.size.cast<float>() * 0.5f), world.drawData.cam.c.rotation - *oldRotationAngle); world.drawData.cam.c.rotate_about(world.drawData.cam.c.from_space(world.main.window.size.cast<float>() * 0.5f), *newRotationAngle - world.drawData.cam.c.rotation);
*oldRotationAngle = world.drawData.cam.c.rotation; *newRotationAngle = world.drawData.cam.c.rotation;
}); });
} }
}); });

View file

@ -18,7 +18,8 @@ void text_button(GUIManager& gui, const char* id, std::string_view text, const T
}; };
CLAY_AUTO_ID({ CLAY_AUTO_ID({
.layout = { .layout = {
.sizing = {.width = options.wide ? CLAY_SIZING_GROW(0) : CLAY_SIZING_FIT(0), .height = CLAY_SIZING_FIT(0) } .sizing = {.width = options.wide ? CLAY_SIZING_GROW(0) : CLAY_SIZING_FIT(0), .height = CLAY_SIZING_FIT(0) },
.childAlignment = {.x = options.centered ? CLAY_ALIGN_X_CENTER : CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}
} }
}) { }) {
gui.element<SelectableButton>(id, d); gui.element<SelectableButton>(id, d);

View file

@ -14,7 +14,7 @@ void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_Siz
}, },
.backgroundColor = convert_vec4<Clay_Color>(gui.io.theme->backColor1), .backgroundColor = convert_vec4<Clay_Color>(gui.io.theme->backColor1),
.cornerRadius = CLAY_CORNER_RADIUS(10), .cornerRadius = CLAY_CORNER_RADIUS(10),
.floating = { .attachTo = CLAY_ATTACH_TO_PARENT }, .floating = { .zIndex = gui.zIndex, .attachTo = CLAY_ATTACH_TO_PARENT },
.border = {.color = convert_vec4<Clay_Color>(gui.io.theme->fillColor2), .width = CLAY_BORDER_OUTSIDE(1)} .border = {.color = convert_vec4<Clay_Color>(gui.io.theme->fillColor2), .width = CLAY_BORDER_OUTSIDE(1)}
}) { }) {
innerContent(); innerContent();

View file

@ -18,6 +18,7 @@ template <typename T> void ColorPickerButton<T>::layout(const Clay_ElementId& id
} }
}); });
if(isOpen) { if(isOpen) {
gui.zIndex++;
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]() {
ElementHelpers::color_picker_items(gui, "c", val, { ElementHelpers::color_picker_items(gui, "c", val, {
.hasAlpha = data.hasAlpha, .hasAlpha = data.hasAlpha,
@ -25,11 +26,14 @@ template <typename T> void ColorPickerButton<T>::layout(const Clay_ElementId& id
}); });
}, { }, {
.mouseButton = [&](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { .mouseButton = [&](const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) {
if(!mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) if(!mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) {
isOpen = false; isOpen = false;
gui.set_to_layout();
}
return mouseHovering; return mouseHovering;
} }
}); });
gui.zIndex--;
} }
} }

View file

@ -37,21 +37,22 @@ float RotateWheel::wheel_end() {
bool RotateWheel::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) { bool RotateWheel::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) {
isHovering = mouseHovering; isHovering = mouseHovering;
isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down; isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down;
update_paint_circle_menu_mouse(button.pos, button.button == InputManager::MouseButton::LEFT && button.down); isRotateBarHeld = isRotateBarHovered && isHeld;
update_paint_circle_menu_mouse(button.pos);
return Element::input_mouse_button_callback(button, mouseHovering); return Element::input_mouse_button_callback(button, mouseHovering);
} }
bool RotateWheel::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) { bool RotateWheel::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) {
update_paint_circle_menu_mouse(motion.pos, false); isHovering = mouseHovering;
update_paint_circle_menu_mouse(motion.pos);
return Element::input_mouse_motion_callback(motion, mouseHovering); return Element::input_mouse_motion_callback(motion, mouseHovering);
} }
void RotateWheel::update_paint_circle_menu_mouse(const Vector2f& p, bool leftClicked) { void RotateWheel::update_paint_circle_menu_mouse(const Vector2f& p) {
if(boundingBox.has_value()) { if(boundingBox.has_value()) {
Vector2f vecFromCenter = (p - boundingBox.value().center()).normalized(); Vector2f vecFromCenter = (p - boundingBox.value().center()).normalized();
float distFromCenter = vec_distance(p, boundingBox.value().center()); float distFromCenter = vec_distance(p, boundingBox.value().center());
isRotateBarHovered = isHovering && distFromCenter > wheel_start() && distFromCenter < wheel_end(); isRotateBarHovered = isHovering && distFromCenter > wheel_start() && distFromCenter < wheel_end();
isRotateBarHeld = isRotateBarHovered && isHeld;
if(isRotateBarHeld) { if(isRotateBarHeld) {
gui.set_post_callback_func([&, vecFromCenter] { gui.set_post_callback_func([&, vecFromCenter] {
@ -67,6 +68,7 @@ void RotateWheel::update_paint_circle_menu_mouse(const Vector2f& p, bool leftCli
} }
} }
} }
onChange();
}); });
} }
} }
@ -105,9 +107,9 @@ void RotateWheel::draw_rotate_wheel(SkCanvas* canvas, UpdateInputData& io, bool
SkPaint rotateBarHolderFill; SkPaint rotateBarHolderFill;
rotateBarHolderFill.setAntiAlias(skiaAA); rotateBarHolderFill.setAntiAlias(skiaAA);
if(selection.held) if(isRotateBarHeld)
rotateBarHolderFill.setColor4f(io.theme->fillColor1); rotateBarHolderFill.setColor4f(io.theme->fillColor1);
else if(selection.hovered) else if(isRotateBarHovered)
rotateBarHolderFill.setColor4f(io.theme->fillColor2); rotateBarHolderFill.setColor4f(io.theme->fillColor2);
else else
rotateBarHolderFill.setColor4f(io.theme->frontColor2); rotateBarHolderFill.setColor4f(io.theme->frontColor2);

View file

@ -10,7 +10,7 @@ class RotateWheel : public Element {
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override; virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override;
virtual bool input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override; virtual bool input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override;
virtual bool input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override; virtual bool input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) override;
SelectionHelper selection;
private: private:
float wheel_start(); float wheel_start();
float wheel_end(); float wheel_end();
@ -23,7 +23,7 @@ class RotateWheel : public Element {
bool isRotateBarHovered = false; bool isRotateBarHovered = false;
std::function<void()> onChange; std::function<void()> onChange;
void update_paint_circle_menu_mouse(const Vector2f& p, bool leftClicked); void update_paint_circle_menu_mouse(const Vector2f& p);
}; };
} }

View file

@ -19,6 +19,8 @@ void ScrollArea::layout(const Clay_ElementId& id, const Options& options) {
containerDimensions = {scrollData.scrollContainerDimensions.width, scrollData.scrollContainerDimensions.height}; containerDimensions = {scrollData.scrollContainerDimensions.width, scrollData.scrollContainerDimensions.height};
} }
clamp_scroll();
CLAY(localID, { CLAY(localID, {
.layout = { .layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}, .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)},
@ -103,12 +105,18 @@ bool ScrollArea::input_mouse_motion_callback(const InputManager::MouseMotionCall
return Element::input_mouse_motion_callback(motion, mouseHovering); return Element::input_mouse_motion_callback(motion, mouseHovering);
} }
void ScrollArea::clamp_scroll() {
scrollOffset.x() = std::clamp(scrollOffset.x(), -std::max(0.0f, contentDimensions.x() - containerDimensions.x()), 0.0f);
scrollOffset.y() = std::clamp(scrollOffset.y(), -std::max(0.0f, contentDimensions.y() - containerDimensions.y()), 0.0f);
}
bool ScrollArea::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) { bool ScrollArea::input_mouse_wheel_callback(const InputManager::MouseWheelCallbackArgs& wheel, bool mouseHovering) {
if(mouseHovering) { if(mouseHovering) {
if(opts.scrollVertical) if(opts.scrollVertical)
scrollOffset.y() += wheel.amount.y(); scrollOffset.y() = scrollOffset.y() + wheel.amount.y() * 10.0f;
if(opts.scrollHorizontal) if(opts.scrollHorizontal)
scrollOffset.x() += wheel.amount.x(); scrollOffset.y() = scrollOffset.x() + wheel.amount.x() * 10.0f;
clamp_scroll();
gui.set_to_layout(); gui.set_to_layout();
} }
return Element::input_mouse_wheel_callback(wheel, mouseHovering); return Element::input_mouse_wheel_callback(wheel, mouseHovering);

View file

@ -37,6 +37,7 @@ class ScrollArea : public Element {
Vector2f contentDimensions = {0.0f, 0.0f}; Vector2f contentDimensions = {0.0f, 0.0f};
Vector2f containerDimensions = {0.0f, 0.0f}; Vector2f containerDimensions = {0.0f, 0.0f};
Vector2f scrollOffset = {0.0f, 0.0f}; Vector2f scrollOffset = {0.0f, 0.0f};
void clamp_scroll();
}; };
} }

View file

@ -425,18 +425,18 @@ void Toolbar::top_toolbar() {
auto& gui = main.g.gui; auto& gui = main.g.gui;
auto& io = gui.io; auto& io = gui.io;
CLAY_AUTO_ID({ gui.element<LayoutElement>("top menu bar", [&] (const Clay_ElementId& lId) {
.layout = { CLAY(lId, {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) }, .layout = {
.padding = CLAY_PADDING_ALL(static_cast<uint16_t>(io.theme->padding1 / 2)), .sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) },
.childGap = static_cast<uint16_t>(io.theme->childGap1 / 2), .padding = CLAY_PADDING_ALL(static_cast<uint16_t>(io.theme->padding1 / 2)),
.childAlignment = { .x = CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}, .childGap = static_cast<uint16_t>(io.theme->childGap1 / 2),
.layoutDirection = CLAY_LEFT_TO_RIGHT .childAlignment = { .x = CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER},
}, .layoutDirection = CLAY_LEFT_TO_RIGHT
.backgroundColor = convert_vec4<Clay_Color>(io.theme->backColor1), },
.cornerRadius = CLAY_CORNER_RADIUS(io.theme->windowCorners1) .backgroundColor = convert_vec4<Clay_Color>(io.theme->backColor1),
}) { .cornerRadius = CLAY_CORNER_RADIUS(io.theme->windowCorners1)
gui.new_id("menu top toolbar", [&] { }) {
global_log(); global_log();
bool bookmarkMenuPopUpJustOpen = false; bool bookmarkMenuPopUpJustOpen = false;
bool gridMenuPopUpJustOpen = false; bool gridMenuPopUpJustOpen = false;
@ -551,7 +551,7 @@ void Toolbar::top_toolbar() {
.drawType = SelectableButton::DrawType::TRANSPARENT_ALL, .drawType = SelectableButton::DrawType::TRANSPARENT_ALL,
.wide = true, .wide = true,
.centered = false, .centered = false,
.onClick = [&]{ .onClick = [&, onClick]{
onClick(); onClick();
menuPopUpOpen = false; menuPopUpOpen = false;
} }
@ -638,8 +638,8 @@ void Toolbar::top_toolbar() {
} }
}); });
} }
}); }
} });
} }
void Toolbar::web_version_welcome() { void Toolbar::web_version_welcome() {
@ -1658,7 +1658,8 @@ void Toolbar::general_settings_inner_gui() {
.drawType = SelectableButton::DrawType::TRANSPARENT_ALL, .drawType = SelectableButton::DrawType::TRANSPARENT_ALL,
.isSelected = generalSettingsOptions == opt, .isSelected = generalSettingsOptions == opt,
.wide = true, .wide = true,
.onClick = [&] { .centered = false,
.onClick = [&, opt] {
main.g.load_theme(main.configPath, main.conf.themeCurrentlyLoaded); main.g.load_theme(main.configPath, main.conf.themeCurrentlyLoaded);
themeData.selectedThemeIndex = std::nullopt; themeData.selectedThemeIndex = std::nullopt;
generalSettingsOptions = opt; generalSettingsOptions = opt;