2025-09-28 16:32:57 +03:00
|
|
|
#include "RotateWheel.hpp"
|
|
|
|
|
#include "Helpers/ConvertVec.hpp"
|
2026-03-27 09:51:53 +03:00
|
|
|
#include "../GUIManager.hpp"
|
2025-09-28 16:32:57 +03:00
|
|
|
|
|
|
|
|
namespace GUIStuff {
|
|
|
|
|
|
2025-11-02 09:38:15 +03:00
|
|
|
constexpr float WHEEL_WIDTH = 13.0f;
|
2025-09-28 16:32:57 +03:00
|
|
|
|
|
|
|
|
constexpr double ROTATE_BAR_SNAP_DISTANCE = 0.3;
|
|
|
|
|
constexpr double ROTATE_BAR_SNAP_DISTRIBUTION = std::numbers::pi * 0.25;
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
RotateWheel::RotateWheel(GUIManager& gui):
|
|
|
|
|
Element(gui) {}
|
2025-09-28 16:32:57 +03:00
|
|
|
|
2026-03-31 17:33:09 +03:00
|
|
|
void RotateWheel::layout(const Clay_ElementId& id, double* rotateAngle, const std::function<void()>& onChange) {
|
2026-03-27 09:51:53 +03:00
|
|
|
this->rotateAngle = rotateAngle;
|
|
|
|
|
this->onChange = onChange;
|
|
|
|
|
|
2026-03-31 17:33:09 +03:00
|
|
|
CLAY(id, {
|
2026-03-27 09:51:53 +03:00
|
|
|
.layout = {
|
2025-09-28 16:32:57 +03:00
|
|
|
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}
|
|
|
|
|
},
|
2026-03-30 19:41:18 +03:00
|
|
|
.aspectRatio = {1.0f},
|
2025-09-28 16:32:57 +03:00
|
|
|
.custom = { .customData = this }
|
|
|
|
|
}) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RotateWheel::wheel_start() {
|
|
|
|
|
return wheel_end() - WHEEL_WIDTH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RotateWheel::wheel_end() {
|
2026-03-27 09:51:53 +03:00
|
|
|
return boundingBox.value().width() * 0.5f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RotateWheel::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) {
|
|
|
|
|
isHovering = mouseHovering;
|
|
|
|
|
isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down;
|
|
|
|
|
update_paint_circle_menu_mouse(button.pos, button.button == InputManager::MouseButton::LEFT && button.down);
|
|
|
|
|
return Element::input_mouse_button_callback(button, mouseHovering);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool RotateWheel::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) {
|
|
|
|
|
update_paint_circle_menu_mouse(motion.pos, false);
|
|
|
|
|
return Element::input_mouse_motion_callback(motion, mouseHovering);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RotateWheel::update_paint_circle_menu_mouse(const Vector2f& p, bool leftClicked) {
|
|
|
|
|
if(boundingBox.has_value()) {
|
|
|
|
|
Vector2f vecFromCenter = (p - boundingBox.value().center()).normalized();
|
|
|
|
|
float distFromCenter = vec_distance(p, boundingBox.value().center());
|
|
|
|
|
isRotateBarHovered = isHovering && distFromCenter > wheel_start() && distFromCenter < wheel_end();
|
|
|
|
|
isRotateBarHeld = isRotateBarHovered && isHeld;
|
|
|
|
|
|
|
|
|
|
if(isRotateBarHeld) {
|
|
|
|
|
gui.set_post_callback_func([&, vecFromCenter] {
|
|
|
|
|
auto& rotationAngle = *rotateAngle;
|
|
|
|
|
rotationAngle = std::atan2(vecFromCenter.y(), -vecFromCenter.x()) + std::numbers::pi;
|
|
|
|
|
if(isRotateBarHovered) { // If we're hovering over the rotation bar, we should try snapping to specific angles
|
|
|
|
|
for(double snapPos = 0.0; snapPos < std::numbers::pi * 2.0 + 0.001; snapPos += ROTATE_BAR_SNAP_DISTRIBUTION) {
|
|
|
|
|
if(std::fabs(rotationAngle - snapPos) < ROTATE_BAR_SNAP_DISTANCE) {
|
|
|
|
|
rotationAngle = snapPos;
|
|
|
|
|
if(rotationAngle >= std::numbers::pi * 2.0 - 0.001) // Set 2pi back to 0
|
|
|
|
|
rotationAngle = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-28 16:32:57 +03:00
|
|
|
}
|
|
|
|
|
|
2026-03-20 20:53:59 +03:00
|
|
|
void RotateWheel::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {
|
2026-03-27 09:51:53 +03:00
|
|
|
auto& bb = boundingBox.value();
|
|
|
|
|
|
2025-09-28 16:32:57 +03:00
|
|
|
canvas->save();
|
|
|
|
|
canvas->translate(bb.center().x(), bb.center().y());
|
|
|
|
|
|
2026-03-20 20:53:59 +03:00
|
|
|
draw_rotate_wheel(canvas, io, skiaAA);
|
2025-09-28 16:32:57 +03:00
|
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-20 20:53:59 +03:00
|
|
|
void RotateWheel::draw_rotate_wheel(SkCanvas* canvas, UpdateInputData& io, bool skiaAA) {
|
2025-09-28 16:32:57 +03:00
|
|
|
float wheelStart = wheel_start();
|
|
|
|
|
float wheelEnd = wheel_end();
|
|
|
|
|
|
|
|
|
|
float rotateBarMiddleRadius = (wheelEnd + wheelStart) / 2.0f;
|
|
|
|
|
SkPaint rotateBarFill(io.theme->backColor2);
|
|
|
|
|
rotateBarFill.setStroke(true);
|
|
|
|
|
rotateBarFill.setStrokeWidth(wheelEnd - wheelStart);
|
|
|
|
|
canvas->drawCircle(0.0f, 0.0f, rotateBarMiddleRadius, rotateBarFill);
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
if(isRotateBarHovered) {
|
2025-09-28 16:32:57 +03:00
|
|
|
for(double snapPos = 0.0; snapPos < std::numbers::pi * 2.0; snapPos += ROTATE_BAR_SNAP_DISTRIBUTION) {
|
|
|
|
|
Vector2f lineDir{std::cos(snapPos), -std::sin(snapPos)};
|
|
|
|
|
SkPaint p(snapPos == 0.0 ? io.theme->fillColor1 : io.theme->frontColor2);
|
2026-03-20 20:53:59 +03:00
|
|
|
p.setAntiAlias(skiaAA);
|
2025-11-01 20:47:34 +03:00
|
|
|
p.setStrokeWidth(1.0f);
|
2025-09-28 16:32:57 +03:00
|
|
|
canvas->drawLine(lineDir.x() * wheelStart, lineDir.y() * wheelStart, lineDir.x() * wheelEnd, lineDir.y() * wheelEnd, p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SkPaint rotateBarHolderFill;
|
2026-03-20 20:53:59 +03:00
|
|
|
rotateBarHolderFill.setAntiAlias(skiaAA);
|
2025-09-28 16:32:57 +03:00
|
|
|
if(selection.held)
|
|
|
|
|
rotateBarHolderFill.setColor4f(io.theme->fillColor1);
|
|
|
|
|
else if(selection.hovered)
|
2025-11-01 20:47:34 +03:00
|
|
|
rotateBarHolderFill.setColor4f(io.theme->fillColor2);
|
2025-09-28 16:32:57 +03:00
|
|
|
else
|
|
|
|
|
rotateBarHolderFill.setColor4f(io.theme->frontColor2);
|
|
|
|
|
float rotateBarHolderRadius = (wheelEnd - wheelStart) / 2.0f;
|
2026-03-27 09:51:53 +03:00
|
|
|
Vector2f rotateBarHolderPos{std::cos(*rotateAngle) * rotateBarMiddleRadius, -std::sin(*rotateAngle) * rotateBarMiddleRadius};
|
2025-09-28 16:32:57 +03:00
|
|
|
canvas->drawCircle(rotateBarHolderPos.x(), rotateBarHolderPos.y(), rotateBarHolderRadius, rotateBarHolderFill);
|
|
|
|
|
SkPaint rotateBarHolderOutline(io.theme->frontColor1);
|
2026-03-20 20:53:59 +03:00
|
|
|
rotateBarHolderOutline.setAntiAlias(skiaAA);
|
2025-09-28 16:32:57 +03:00
|
|
|
rotateBarHolderOutline.setStroke(true);
|
|
|
|
|
rotateBarHolderOutline.setStrokeWidth(2.0f);
|
|
|
|
|
canvas->drawCircle(rotateBarHolderPos.x(), rotateBarHolderPos.y(), rotateBarHolderRadius, rotateBarHolderOutline);
|
|
|
|
|
|
|
|
|
|
SkPaint rotateBarOutline(io.theme->frontColor2);
|
2026-03-20 20:53:59 +03:00
|
|
|
rotateBarOutline.setAntiAlias(skiaAA);
|
2025-09-28 16:32:57 +03:00
|
|
|
rotateBarOutline.setStroke(true);
|
2025-11-01 20:47:34 +03:00
|
|
|
rotateBarOutline.setStrokeWidth(1.0f);
|
2025-09-28 16:32:57 +03:00
|
|
|
canvas->drawCircle(0.0f, 0.0f, wheelStart, rotateBarOutline);
|
|
|
|
|
canvas->drawCircle(0.0f, 0.0f, wheelEnd, rotateBarOutline);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|