infinipaint/src/GUIStuff/Elements/RadioButton.cpp

72 lines
2.5 KiB
C++
Raw Normal View History

2025-07-06 20:43:53 +03:00
#include "RadioButton.hpp"
#include "../GUIManager.hpp"
2025-07-06 20:43:53 +03:00
namespace GUIStuff {
RadioButton::RadioButton(GUIManager& gui):
Element(gui) {}
void RadioButton::layout(const std::function<bool()>& isTicked, const std::function<void()>& onClick) {
this->onClick = onClick;
this->isTicked = isTicked;
CLAY_AUTO_ID({
2025-07-06 20:43:53 +03:00
.layout = {
.sizing = {.width = CLAY_SIZING_FIXED(20), .height = CLAY_SIZING_FIXED(20)}
},
.custom = { .customData = this }
}) {}
}
bool RadioButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) {
isHovering = mouseHovering;
if(isHovering && button.button == InputManager::MouseButton::LEFT && button.down)
gui.set_post_callback_func([&] { if(onClick) onClick(); });
return Element::input_mouse_button_callback(button, mouseHovering);
}
bool RadioButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) {
isHovering = mouseHovering;
return Element::input_mouse_motion_callback(motion, mouseHovering);
2025-07-06 20:43:53 +03:00
}
void RadioButton::clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) {
auto& bb = boundingBox.value();
2025-07-06 20:43:53 +03:00
canvas->save();
canvas->translate(bb.min.x(), bb.min.y());
canvas->scale(bb.width(), bb.height());
canvas->translate(0.5f, 0.5f);
if(isTicked()) {
2025-07-06 20:43:53 +03:00
SkPaint p;
p.setAntiAlias(skiaAA);
2025-07-06 20:43:53 +03:00
p.setColor4f(convert_vec4<SkColor4f>(io.theme->fillColor1));
p.setStyle(SkPaint::kFill_Style);
canvas->drawCircle(0.0f, 0.0f, 0.5f, p);
SkPaint innerCircleP;
innerCircleP.setAntiAlias(skiaAA);
2025-07-06 20:43:53 +03:00
innerCircleP.setColor4f(convert_vec4<SkColor4f>(io.theme->backColor2));
innerCircleP.setStyle(SkPaint::kFill_Style);
static BezierEasing easeRadius(0.68, -2.55, 0.265, 3.55);
float lerpTime2 = easeRadius(smooth_two_way_time(hoverAnimation2, io.deltaTime, isHovering, RADIOBUTTON_ANIMATION_TIME));
2025-07-06 20:43:53 +03:00
float innerCircleRadius = lerp_vec(0.3f, 0.2f, lerpTime2);
canvas->drawCircle(0.0f, 0.0f, innerCircleRadius, innerCircleP);
}
else {
SkPaint p;
p.setAntiAlias(skiaAA);
p.setColor4f(convert_vec4<SkColor4f>(isHovering ? io.theme->fillColor1 : io.theme->backColor2));
2025-07-06 20:43:53 +03:00
p.setStyle(SkPaint::kStroke_Style);
p.setStrokeWidth(0.15f);
canvas->drawCircle(0.0f, 0.0f, 0.5f, p);
}
canvas->restore();
}
}