infinipaint/src/GUIStuff/ElementHelpers/ButtonHelpers.hpp
2026-04-02 18:23:36 +03:00

66 lines
2.4 KiB
C++

#pragma once
#include "../GUIManager.hpp"
#include "../Elements/SelectableButton.hpp"
#include "../Elements/ColorRectangleDisplay.hpp"
namespace GUIStuff { namespace ElementHelpers {
constexpr static float BIG_BUTTON_SIZE = 30;
constexpr static float SMALL_BUTTON_SIZE = 20;
template <typename OptT> SelectableButton::Data selectable_button_options_to_data(const OptT& options) {
SelectableButton::Data toRet;
toRet.onClick = options.onClick;
toRet.isSelected = options.isSelected;
toRet.drawType = options.drawType;
return toRet;
}
struct TextButtonOptions {
SelectableButton::DrawType drawType = SelectableButton::DrawType::FILLED;
bool isSelected = false;
bool wide = false;
bool centered = true;
std::function<void()> onClick;
};
struct SVGButtonOptions {
SelectableButton::DrawType drawType = SelectableButton::DrawType::FILLED;
bool isSelected = false;
float size = BIG_BUTTON_SIZE;
std::function<void()> onClick;
};
struct FixedSizeColorButtonOptions {
SelectableButton::DrawType drawType = SelectableButton::DrawType::FILLED;
bool isSelected = false;
bool hasAlpha = true;
float size = BIG_BUTTON_SIZE;
std::function<void()> onClick;
};
void text_button(GUIManager& gui, const char* id, std::string_view text, const TextButtonOptions& options = {});
void text_button_sized(GUIManager& gui, const char* id, std::string_view text, Clay_SizingAxis x, Clay_SizingAxis y, const TextButtonOptions& options = {});
void svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath, const SVGButtonOptions& options = {});
template <typename T> void color_button(GUIManager& gui, const char* id, T* val, const FixedSizeColorButtonOptions& options = {}) {
gui.new_id(id, [&] {
SelectableButton::Data d = selectable_button_options_to_data(options);
d.innerContent = [&] (const SelectableButton::InnerContentCallbackParameters&) {
gui.element<ColorRectangleDisplay>("color display", [val, hasAlpha = options.hasAlpha] {
if(hasAlpha)
return SkColor4f{(*val)[0], (*val)[1], (*val)[2], (*val)[3]};
else
return SkColor4f{(*val)[0], (*val)[1], (*val)[2], 1.0f};
});
};
CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(options.size), .height = CLAY_SIZING_FIXED(options.size) } } }) {
gui.element<SelectableButton>("button", d);
}
});
}
}}