2025-07-06 20:43:53 +03:00
|
|
|
#include "SelectableButton.hpp"
|
|
|
|
|
#include "Helpers/ConvertVec.hpp"
|
2026-03-27 09:51:53 +03:00
|
|
|
#include "../GUIManager.hpp"
|
2025-07-06 20:43:53 +03:00
|
|
|
|
|
|
|
|
namespace GUIStuff {
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
SelectableButton::SelectableButton(GUIManager& gui):
|
|
|
|
|
Element(gui) {}
|
|
|
|
|
|
|
|
|
|
void SelectableButton::layout(const Data& d) {
|
|
|
|
|
auto& io = *gui.io;
|
|
|
|
|
|
2025-11-02 09:38:15 +03:00
|
|
|
SkColor4f borderColor;
|
2025-11-01 16:07:19 +03:00
|
|
|
SkColor4f backgroundColorHighlight;
|
|
|
|
|
SkColor4f backgroundColor;
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
onClick = d.onClick;
|
|
|
|
|
|
|
|
|
|
if(isHeld || ((d.isSelected || isHovering) && d.drawType == DrawType::TRANSPARENT_BORDER))
|
2025-11-02 09:38:15 +03:00
|
|
|
borderColor = io.theme->fillColor1;
|
2026-03-27 09:51:53 +03:00
|
|
|
else if(d.drawType == DrawType::TRANSPARENT_BORDER)
|
2025-11-02 09:38:15 +03:00
|
|
|
borderColor = io.theme->backColor2;
|
|
|
|
|
else
|
|
|
|
|
borderColor = SkColor4f{0.0f, 0.0f, 0.0f, 0.0f};
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
if(d.isSelected)
|
2025-11-01 16:07:19 +03:00
|
|
|
backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.4f);
|
2026-03-27 09:51:53 +03:00
|
|
|
else if(isHovering || isHeld)
|
2025-11-01 16:07:19 +03:00
|
|
|
backgroundColorHighlight = color_mul_alpha(io.theme->fillColor1, 0.2f);
|
|
|
|
|
else
|
|
|
|
|
backgroundColorHighlight = {0.0f, 0.0f, 0.0f, 0.0f};
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
if(d.drawType == DrawType::TRANSPARENT_ALL || d.drawType == DrawType::TRANSPARENT_BORDER)
|
2025-11-01 16:07:19 +03:00
|
|
|
backgroundColor = {0.0f, 0.0f, 0.0f, 0.0f};
|
2026-03-27 09:51:53 +03:00
|
|
|
else if(d.drawType == DrawType::FILLED)
|
2025-11-01 16:07:19 +03:00
|
|
|
backgroundColor = io.theme->backColor2;
|
|
|
|
|
else
|
|
|
|
|
backgroundColor = io.theme->fillColor2;
|
|
|
|
|
|
2025-12-02 16:09:30 +03:00
|
|
|
CLAY_AUTO_ID({.layout = {
|
2025-11-01 16:07:19 +03:00
|
|
|
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)},
|
|
|
|
|
.childGap = 0,
|
|
|
|
|
.childAlignment = { .x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER }
|
|
|
|
|
},
|
|
|
|
|
.backgroundColor = convert_vec4<Clay_Color>(backgroundColor),
|
|
|
|
|
.cornerRadius = CLAY_CORNER_RADIUS(4),
|
|
|
|
|
.border = {
|
|
|
|
|
.color = convert_vec4<Clay_Color>(borderColor),
|
2025-11-04 10:58:26 +03:00
|
|
|
.width = CLAY_BORDER_OUTSIDE(2)
|
2025-11-01 16:07:19 +03:00
|
|
|
}
|
|
|
|
|
}) {
|
2025-12-02 16:09:30 +03:00
|
|
|
CLAY_AUTO_ID({.layout = {
|
2025-07-06 20:43:53 +03:00
|
|
|
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)},
|
2026-03-27 09:51:53 +03:00
|
|
|
.padding = (d.drawType == DrawType::TRANSPARENT_BORDER) ? CLAY_PADDING_ALL(0) : CLAY_PADDING_ALL(4),
|
2025-07-06 20:43:53 +03:00
|
|
|
.childAlignment = { .x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER },
|
|
|
|
|
},
|
2025-11-01 16:07:19 +03:00
|
|
|
.backgroundColor = convert_vec4<Clay_Color>(backgroundColorHighlight),
|
|
|
|
|
.cornerRadius = CLAY_CORNER_RADIUS(4)
|
2025-07-06 20:43:53 +03:00
|
|
|
}) {
|
2026-03-27 09:51:53 +03:00
|
|
|
if(d.innerContent)
|
|
|
|
|
d.innerContent({.isSelected = d.isSelected, .isHovering = isHovering, .isHeld = isHeld});
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 09:51:53 +03:00
|
|
|
bool SelectableButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) {
|
|
|
|
|
bool oldIsHovering = isHovering;
|
|
|
|
|
bool oldIsHeld = isHeld;
|
|
|
|
|
isHovering = mouseHovering;
|
|
|
|
|
isHeld = isHovering && button.button == InputManager::MouseButton::LEFT && button.down;
|
|
|
|
|
if(isHeld) {
|
|
|
|
|
gui.set_post_callback_func([&]{onClick();});
|
|
|
|
|
gui.set_to_layout();
|
|
|
|
|
}
|
|
|
|
|
else if(isHovering != oldIsHovering || isHeld != oldIsHeld)
|
|
|
|
|
gui.set_to_layout();
|
|
|
|
|
return Element::input_mouse_button_callback(button, mouseHovering);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) {
|
|
|
|
|
bool oldIsHovering = isHovering;
|
|
|
|
|
isHovering = mouseHovering;
|
|
|
|
|
if(isHovering != oldIsHovering)
|
|
|
|
|
gui.set_to_layout();
|
|
|
|
|
return Element::input_mouse_motion_callback(motion, mouseHovering);
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|