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) {}
|
|
|
|
|
|
2026-03-31 17:33:09 +03:00
|
|
|
void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
|
2026-03-31 13:17:58 +03:00
|
|
|
auto& io = gui.io;
|
2026-03-27 09:51:53 +03:00
|
|
|
|
2025-11-02 09:38:15 +03:00
|
|
|
SkColor4f borderColor;
|
2025-11-01 16:07:19 +03:00
|
|
|
SkColor4f backgroundColorHighlight;
|
|
|
|
|
SkColor4f backgroundColor;
|
|
|
|
|
|
2026-04-09 16:30:28 +03:00
|
|
|
instantResponse = d.instantResponse;
|
2026-04-06 12:23:46 +03:00
|
|
|
onClick = [&, this, d] {
|
|
|
|
|
if(d.onClickButton)
|
|
|
|
|
d.onClickButton(this);
|
|
|
|
|
else if(d.onClick) d.onClick();
|
|
|
|
|
};
|
2026-03-27 09:51:53 +03:00
|
|
|
|
2026-04-02 21:57:26 +03:00
|
|
|
if(isHeld || ((d.isSelected || mouseHovering) && 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-04-02 21:57:26 +03:00
|
|
|
else if(mouseHovering || 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;
|
|
|
|
|
|
2026-03-31 17:33:09 +03:00
|
|
|
CLAY(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)
|
2026-04-02 21:57:26 +03:00
|
|
|
d.innerContent({.isSelected = d.isSelected, .isHovering = mouseHovering, .isHeld = isHeld});
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-02 21:57:26 +03:00
|
|
|
void SelectableButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
|
2026-03-27 09:51:53 +03:00
|
|
|
bool oldIsHeld = isHeld;
|
2026-04-02 21:57:26 +03:00
|
|
|
isHeld = mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down;
|
2026-04-09 16:30:28 +03:00
|
|
|
if(isHeld) {
|
2026-04-19 13:37:16 +03:00
|
|
|
if(instantResponse)
|
2026-04-09 16:30:28 +03:00
|
|
|
gui.set_post_callback_func(onClick);
|
2026-04-06 12:23:46 +03:00
|
|
|
gui.set_to_layout();
|
2026-04-09 16:30:28 +03:00
|
|
|
}
|
2026-04-06 12:23:46 +03:00
|
|
|
else if(mouseHovering && oldIsHeld && button.button == InputManager::MouseButton::LEFT && !button.down) {
|
2026-04-09 16:30:28 +03:00
|
|
|
if(!instantResponse)
|
|
|
|
|
gui.set_post_callback_func(onClick);
|
2026-03-27 09:51:53 +03:00
|
|
|
gui.set_to_layout();
|
|
|
|
|
}
|
2026-04-02 21:57:26 +03:00
|
|
|
else if(mouseHovering != oldIsHovering || isHeld != oldIsHeld)
|
2026-03-27 09:51:53 +03:00
|
|
|
gui.set_to_layout();
|
2026-04-02 21:57:26 +03:00
|
|
|
oldIsHovering = mouseHovering;
|
2026-03-27 09:51:53 +03:00
|
|
|
}
|
|
|
|
|
|
2026-04-02 21:57:26 +03:00
|
|
|
void SelectableButton::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion) {
|
|
|
|
|
if(mouseHovering != oldIsHovering)
|
2026-03-27 09:51:53 +03:00
|
|
|
gui.set_to_layout();
|
2026-04-02 21:57:26 +03:00
|
|
|
oldIsHovering = mouseHovering;
|
2025-07-06 20:43:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|