Starting work on moving GUI to event system (doesnt compile)

This commit is contained in:
Yousef Khadadeh 2026-03-27 09:51:53 +03:00
parent f1a855edc3
commit ef7dfcef05
69 changed files with 4296 additions and 3709 deletions

View file

@ -0,0 +1,47 @@
#include "ButtonHelpers.hpp"
#include "../Elements/SVGIcon.hpp"
#include "TextLabelHelpers.hpp"
namespace GUIStuff { namespace ElementHelpers {
void text_button(GUIManager& gui, const char* id, std::string_view text, const TextButtonOptions& options) {
SelectableButton::Data d = selectable_button_options_to_data(options);
d.innerContent = [&gui, text] (const SelectableButton::InnerContentCallbackParameters&) {
text_label(gui, text);
};
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = options.wide ? CLAY_SIZING_GROW(0) : CLAY_SIZING_FIT(0), .height = CLAY_SIZING_FIT(0) },
.childAlignment = {.x = options.centered ? CLAY_ALIGN_X_CENTER : CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}
}
}) {
gui.element<SelectableButton>(id, options);
}
}
void text_button_sized(GUIManager& gui, const char* id, std::string_view text, Clay_SizingAxis x, Clay_SizingAxis y, const TextButtonOptions& options) {
SelectableButton::Data d = selectable_button_options_to_data(options);
d.innerContent = [&gui, text] (const SelectableButton::InnerContentCallbackParameters&) {
text_label(gui, text);
};
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = x, .height = y },
.childAlignment = {.x = options.centered ? CLAY_ALIGN_X_CENTER : CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}
}
}) {
gui.element<SelectableButton>(id, options);
}
}
void svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath, const SVGButtonOptions& options) {
SelectableButton::Data d = selectable_button_options_to_data(options);
d.innerContent = [&gui, &svgPath] (const SelectableButton::InnerContentCallbackParameters& p) {
gui.element<SVGIcon>("icon", svgPath, p.isSelected || p.isHovering || p.isHeld);
};
CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(options.size), .height = CLAY_SIZING_FIXED(options.size) } } }) {
gui.element<SelectableButton>(id, options);
}
}
}}

View file

@ -0,0 +1,64 @@
#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) {
SelectableButton::Data d = selectable_button_options_to_data(options);
d.innerContent = [&] (const SelectableButton::InnerContentCallbackParameters&) {
gui.element<ColorRectangleDisplay>([val, hasAlpha = options.hasAlpha] {
if(hasAlpha)
return SkColor4f{(*val)[0], (*val)[1], (*val)[2], 1.0f};
else
return SkColor4f{(*val)[0], (*val)[1], (*val)[2], (*val)[3]};
});
};
CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(options.size), .height = CLAY_SIZING_FIXED(options.size) } } }) {
gui.element<SelectableButton>(id, options);
}
}
}}

View file

@ -0,0 +1,30 @@
#include "CheckBoxHelpers.hpp"
#include "LayoutHelpers.hpp"
#include "TextLabelHelpers.hpp"
#include "../Elements/CheckBox.hpp"
namespace GUIStuff { namespace ElementHelpers {
void checkbox_boolean(GUIManager& gui, const char* id, bool* d, const std::function<void()>& onClick) {
gui.element<CheckBox>(id, [d]{ return *d; }, [d, onClick] {
*d = !(*d);
if(onClick)
onClick();
});
}
void checkbox_field(GUIManager& gui, const char* id, std::string_view name, const std::function<bool()>& isTicked, const std::function<void()>& onClick) {
left_to_right_line_layout(gui, [&]() {
gui.element<CheckBox>(id, isTicked, onClick);
text_label(gui, name);
});
}
void checkbox_boolean_field(GUIManager& gui, const char* id, std::string_view name, bool* d, const std::function<void()>& onClick) {
left_to_right_line_layout(gui, [&]() {
checkbox_boolean(gui, id, d, onClick);
text_label(gui, name);
});
}
}}

View file

@ -0,0 +1,10 @@
#pragma once
#include "../GUIManager.hpp"
namespace GUIStuff { namespace ElementHelpers {
void checkbox_boolean(GUIManager& gui, const char* id, bool* d, const std::function<void()>& onClick = nullptr);
void checkbox_field(GUIManager& gui, const char* id, std::string_view name, const std::function<bool()>& isTicked, const std::function<void()>& onClick);
void checkbox_boolean_field(GUIManager& gui, const char* id, std::string_view name, bool* d, const std::function<void()>& onClick = nullptr);
}}

View file

@ -0,0 +1,47 @@
#pragma once
#include "../GUIManager.hpp"
#include "TextBoxHelpers.hpp"
#include "../Elements/ColorPicker.hpp"
#include "../Elements/ColorPickerButton.decl.hpp"
namespace GUIStuff { namespace ElementHelpers {
struct ColorPickerItemsOptions {
bool hasAlpha = true;
std::function<void()> onEdit;
};
template <typename T> void color_picker_items(GUIManager& gui, const char* id, T* val, const ColorPickerItemsOptions& options = {}) {
gui.push_id(id);
auto fullOnEdit = [&gui, oE = options.onEdit, val]() {
oE();
gui.set_to_layout();
};
gui.element<ColorPicker>("c", val, options.hasAlpha, fullOnEdit);
left_to_right_line_layout([&]() {
text_label(gui, "R");
input_color_component_255("r", &(*val)[0], { .onEdit = fullOnEdit });
text_label(gui, "G");
input_color_component_255("g", &(*val)[1], { .onEdit = fullOnEdit });
text_label(gui, "B");
input_color_component_255("b", &(*val)[2], { .onEdit = fullOnEdit });
if(options.hasAlpha) {
text_label(gui, "A");
input_color_component_255("a", &(*val)[3], { .onEdit = fullOnEdit });
}
});
left_to_right_line_layout([&]() {
text_label(gui, "Hex");
input_color_hex("h", val, { .onEdit = fullOnEdit, .hasAlpha = options.hasAlpha });
});
gui.pop_id();
}
template <typename T> void color_picker_button_field(GUIManager& gui, const char* id, std::string_view name, T* val, const ColorPickerButtonData& options = {}) {
left_to_right_line_layout([&]() {
gui.element<ColorPickerButton<T>>(id, val, options);
text_label(gui, name);
});
}
} }

View file

@ -0,0 +1,53 @@
#include "LayoutHelpers.hpp"
#include "../Elements/LayoutElement.hpp"
namespace GUIStuff { namespace ElementHelpers {
void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_SizingAxis x, Clay_SizingAxis y, const std::function<void()>& innerContent, const LayoutElement::Callbacks& callbacks) {
gui.element<LayoutElement>(id, [&] {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = x, .height = y },
.padding = CLAY_PADDING_ALL(gui.io->theme->padding1),
.childGap = gui.io->theme->childGap1,
.layoutDirection = CLAY_TOP_TO_BOTTOM
},
.backgroundColor = convert_vec4<Clay_Color>(gui.io->theme->backColor1),
.cornerRadius = CLAY_CORNER_RADIUS(10),
.floating = { .attachTo = CLAY_ATTACH_TO_PARENT },
.border = {.color = convert_vec4<Clay_Color>(gui.io->theme->fillColor2), .width = CLAY_BORDER_OUTSIDE(1)}
}) {
innerContent();
}
}, callbacks);
}
void left_to_right_layout(GUIManager& gui, Clay_SizingAxis x, Clay_SizingAxis y, const std::function<void()>& innerContent) {
CLAY_AUTO_ID({.layout = {
.sizing = {.width = x, .height = y },
.childGap = gui.io->theme->childGap1,
.childAlignment = {.x = CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER},
.layoutDirection = CLAY_LEFT_TO_RIGHT,
}
}) {
innerContent();
}
}
void left_to_right_line_layout(GUIManager& gui, const std::function<void()>& innerContent) {
left_to_right_layout(gui, CLAY_SIZING_GROW(0), CLAY_SIZING_FIT(0), innerContent);
}
void left_to_right_line_centered_layout(GUIManager& gui, const std::function<void()>& innerContent) {
CLAY_AUTO_ID({.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) },
.childGap = gui.io->theme->childGap1,
.childAlignment = {.x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER},
.layoutDirection = CLAY_LEFT_TO_RIGHT,
}
}) {
innerContent();
}
}
}}

View file

@ -0,0 +1,12 @@
#pragma once
#include "../GUIManager.hpp"
#include "../Elements/LayoutElement.hpp"
namespace GUIStuff { namespace ElementHelpers {
void top_to_bottom_window_popup_layout(GUIManager& gui, const char* id, Clay_SizingAxis x, Clay_SizingAxis y, const std::function<void()>& innerContent, const LayoutElement::Callbacks& callbacks);
void left_to_right_layout(GUIManager& gui, Clay_SizingAxis x, Clay_SizingAxis y, const std::function<void()>& innerContent);
void left_to_right_line_layout(GUIManager& gui, const std::function<void()>& innerContent);
void left_to_right_line_centered_layout(GUIManager& gui, const std::function<void()>& innerContent);
}}

View file

@ -0,0 +1,20 @@
#pragma once
#include "../GUIManager.hpp"
#include "TextBoxHelpers.hpp"
#include "../Elements/NumberSlider.hpp"
namespace GUIStuff { namespace ElementHelpers {
template <typename T> bool slider_scalar_field(GUIManager& gui, const char* id, std::string_view name, T* val, T min, T max, TextBoxScalarOptions options = {}) {
gui.push_id(id);
options.onEdit = [&, oE = options.onEdit] {
oE();
gui.set_to_layout();
};
left_to_right_line_layout([&]() {
text_label(gui, name);
input_scalar("textbox", val, min, max, options);
});
gui.element<NumberSlider<T>>("slider", val, min, max, options.onEdit);
gui.pop_id();
}
}}

View file

@ -0,0 +1,23 @@
#include "PopupHelpers.hpp"
#include "../Elements/LayoutElement.hpp"
namespace GUIStuff { namespace ElementHelpers {
void paint_circle_popup_menu(GUIManager& gui, const char* id, const Vector2f& centerPos, const PaintCircleMenu::Data& val) {
constexpr float size = 300.0f;
gui.element<LayoutElement>(id, [&] {
CLAY_AUTO_ID({.layout = {
.sizing = {.width = CLAY_SIZING_FIXED(size), .height = CLAY_SIZING_FIXED(size)}
},
.floating = {
.offset = {centerPos.x() - size * 0.5f, centerPos.y() - size * 0.5f},
.zIndex = gui.zIndex,
.attachTo = CLAY_ATTACH_TO_ROOT
}
}) {
gui.element<PaintCircleMenu>("paint circle menu", val);
}
});
}
}}

View file

@ -0,0 +1,9 @@
#pragma once
#include "../GUIManager.hpp"
#include "../Elements/PaintCircleMenu.hpp"
namespace GUIStuff { namespace ElementHelpers {
void paint_circle_popup_menu(GUIManager& gui, const char* id, const Vector2f& centerPos, const PaintCircleMenu::Data& val);
}}

View file

@ -0,0 +1,15 @@
#include "RadioButtonHelpers.hpp"
#include "../Elements/RadioButton.hpp"
#include "LayoutHelpers.hpp"
#include "TextLabelHelpers.hpp"
namespace GUIStuff { namespace ElementHelpers {
void radio_button_field(GUIManager& gui, const char* id, std::string_view name, const std::function<bool()>& isTicked, const std::function<void()>& onClick) {
left_to_right_line_layout(gui, [&]() {
gui.element<RadioButton>(id, isTicked, onClick);
text_label(gui, name);
});
}
}}

View file

@ -0,0 +1,19 @@
#pragma once
#include "../GUIManager.hpp"
namespace GUIStuff { namespace ElementHelpers {
void radio_button_field(GUIManager& gui, const char* id, std::string_view name, const std::function<bool()>& isTicked, const std::function<void()>& onClick);
template <typename T> void radio_button_selector(GUIManager& gui, const char* id, T* data, const std::vector<std::pair<std::string_view, T>>& options, const std::function<void()>& onClick = nullptr) {
gui.push_id(id);
for(size_t i = 0; i < options.size(); i++) {
gui.push_id(i);
auto& option = options[i];
radio_button_field(gui, "Option", option.first, [data, optionVal = option.second]{ return *data == optionVal; }, [data, optionVal = option.second, onClick]{ *data = optionVal; if(onClick) onClick(); });
gui.pop_id();
}
gui.pop_id();
}
}}

View file

@ -0,0 +1,45 @@
#include "ScrollAreaHelpers.hpp"
#include "../GUIManager.hpp"
namespace GUIStuff { namespace ElementHelpers {
void scroll_area_many_entries(GUIManager& gui, const char* id, const ScrollBarManyEntriesOptions& options) {
gui.element<ScrollArea>(id, ScrollArea::Options{
.scrollVertical = true,
.clipHorizontal = options.clipHorizontal,
.clipVertical = true,
.showScrollbarY = true,
.innerContent = [&, options](const ScrollArea::InnerContentParameters& params) {
options.innerContentExtraCallback(params);
float absScrollAmount = std::fabs(params.scrollOffset.y());
size_t startPoint = absScrollAmount / options.entryHeight;
size_t elementsContainable = (params.containerDimensions.y() / options.entryHeight) + 2; // Displaying 2 more elements ensures that there isn't any empty space in the container
size_t endPoint = std::min(options.entryCount, startPoint + elementsContainable);
if(startPoint != 0) {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED(startPoint * options.entryHeight)},
}
}) { }
}
for(size_t i = startPoint; i < endPoint; i++) {
gui.push_id(i);
options.elementContent(i);
gui.pop_id();
}
if(endPoint != options.entryCount) {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIXED((options.entryCount - endPoint) * options.entryHeight)},
}
}) { }
}
}
});
}
}}

View file

@ -0,0 +1,16 @@
#pragma once
#include "../Elements/ScrollArea.hpp"
namespace GUIStuff { namespace ElementHelpers {
struct ScrollBarManyEntriesOptions {
float entryHeight;
size_t entryCount;
bool clipHorizontal = false;
std::function<void(size_t elementIndex)> elementContent;
std::function<void(const ScrollArea::InnerContentParameters&)> innerContentExtraCallback;
};
void scroll_area_many_entries(GUIManager& gui, const char* id, const ScrollBarManyEntriesOptions& options);
}}

View file

@ -0,0 +1,123 @@
#include "TextBoxHelpers.hpp"
#include "../Elements/TextBox.impl.hpp"
#include "LayoutHelpers.hpp"
namespace GUIStuff { namespace ElementHelpers {
void input_color_component_255(GUIManager& gui, const char* id, float* val, const TextBoxOptions& options) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_NUMBER,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_NUMBER
};
TextBoxData<float> d = textbox_options_to_data<float>(options);
d.textInputProps = textInputProps;
d.fromStr = [](const std::string& str) {
int roundTo255;
std::stringstream ss;
ss << str;
ss >> roundTo255;
return std::clamp<float>(static_cast<float>(roundTo255) / 255.0f, 0.0f, 1.0f);
};
d.toStr = [](const float& a) {
int convertTo255 = a * 255;
std::stringstream ss;
ss << convertTo255;
return ss.str();
};
gui.element<TextBox<std::string>>(id, d);
}
void input_text(GUIManager& gui, const char* id, std::string* val, const TextBoxOptions& options) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
};
TextBoxData<std::string> d = textbox_options_to_data<std::string>(options);
d.textInputProps = textInputProps;
d.fromStr = [](const std::string& a) {
return a;
};
d.toStr = [](const std::string& a) {
return a;
};
gui.element<TextBox<std::string>>(id, d);
}
void input_text_field(GUIManager& gui, const char* id, std::string_view name, std::string* val, const TextBoxOptions& options) {
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
input_text(gui, id, val, options);
});
}
void input_scalar(GUIManager& gui, const char* id, uint8_t* val, uint8_t minVal, uint8_t maxVal, const TextBoxScalarOptions& options) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_NUMBER,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_NUMBER
};
TextBoxData<uint8_t> d = textbox_options_to_data<uint8_t>(options);
d.textInputProps = textInputProps;
d.fromStr = [minVal, maxVal](const std::string& a) {
if(a.empty())
return minVal;
uint8_t toRet;
std::stringstream ss;
ss << a;
ss >> toRet;
if(ss.fail())
return minVal;
return std::clamp(toRet, minVal, maxVal);
};
d.toStr = [options](const uint8_t& a) {
return std::to_string(static_cast<uint32_t>(a));
};
gui.element<TextBox<uint8_t>>(id, d);
}
void input_path(GUIManager& gui, const char* id, std::filesystem::path* val, const TextBoxPathOptions& options) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
};
TextBoxData<std::filesystem::path> d = textbox_options_to_data<std::filesystem::path>(options);
d.textInputProps = textInputProps;
d.fromStr = [fileTypeRestriction = options.fileTypeRestriction](const std::string& a) {
std::filesystem::path toRet = std::filesystem::path(std::u8string_view(reinterpret_cast<const char8_t*>(a.c_str()), a.length()));
if(fileTypeRestriction != std::filesystem::file_type::none && std::filesystem::status(toRet).type() != fileTypeRestriction)
return std::optional<std::filesystem::path>(std::nullopt);
return std::optional<std::filesystem::path>(toRet);
};
d.toStr = [](const std::filesystem::path& a) {
return a.string();
};
gui.element<TextBox<std::filesystem::path>>(id, d);
}
void input_path_field(GUIManager& gui, const char* id, std::string_view name, std::filesystem::path* val, const TextBoxPathOptions& options) {
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
input_path(gui, id, val, options);
});
}
} }

View file

@ -0,0 +1,163 @@
#pragma once
#include "../GUIManager.hpp"
#include "../Elements/TextBox.decl.hpp"
#include "TextLabelHelpers.hpp"
#include "LayoutHelpers.hpp"
namespace GUIStuff { namespace ElementHelpers {
template <typename T, typename OptT> TextBoxData<T> textbox_options_to_data(const OptT& options) {
TextBoxData<T> toRet;
toRet.updateEveryEdit = options.updateEveryEdit;
toRet.onEnter = options.onEnter;
toRet.onEdit = options.onEdit;
toRet.immutable = options.immutable;
return toRet;
}
struct TextBoxOptions {
bool updateEveryEdit = false;
bool immutable = false;
std::function<void()> onEnter;
std::function<void()> onEdit;
};
struct TextBoxScalarOptions {
int decimalPrecision = 0;
bool updateEveryEdit = false;
bool immutable = false;
std::function<void()> onEnter;
std::function<void()> onEdit;
};
struct TextBoxPathOptions {
std::filesystem::file_type fileTypeRestriction;
bool updateEveryEdit = false;
bool immutable = false;
std::function<void()> onEnter;
std::function<void()> onEdit;
};
struct TextBoxHexColorOptions {
bool hasAlpha = true;
bool updateEveryEdit = false;
bool immutable = false;
std::function<void()> onEnter;
std::function<void()> onEdit;
};
void input_color_component_255(GUIManager& gui, const char* id, float* val, const TextBoxOptions& options = {});
void input_text(GUIManager& gui, const char* id, std::string* val, const TextBoxOptions& options = {});
void input_text_field(GUIManager& gui, const char* id, std::string_view name, std::string* val, const TextBoxOptions& options = {});
void input_scalar(GUIManager& gui, const char* id, uint8_t* val, uint8_t minVal, uint8_t maxVal, const TextBoxScalarOptions& options = {});
void input_path(GUIManager& gui, const char* id, std::filesystem::path* val, const TextBoxPathOptions& options = {});
void input_path_field(GUIManager& gui, const char* id, std::string_view name, std::filesystem::path* val, const TextBoxPathOptions& options = {});
template <typename T> void input_scalar(GUIManager& gui, const char* id, T* val, T minVal, T maxVal, const TextBoxScalarOptions& options = {}) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_NUMBER,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_NUMBER
};
if(std::is_signed<T>())
textInputProps.androidInputType |= InputManager::AndroidInputType::ANDROIDTEXT_TYPE_NUMBER_FLAG_SIGNED;
if(std::is_floating_point<T>())
textInputProps.androidInputType |= InputManager::AndroidInputType::ANDROIDTEXT_TYPE_NUMBER_FLAG_DECIMAL;
TextBoxData<T> d = textbox_options_to_data<T>(options);
d.textInputProps = textInputProps;
d.fromStr = [minVal, maxVal](const std::string& a) {
if(a.empty())
return minVal;
T toRet;
std::stringstream ss;
ss << a;
ss >> toRet;
if(ss.fail())
return minVal;
return std::clamp(toRet, minVal, maxVal);
};
d.toStr = [decimalPrecision = options.decimalPrecision, minVal, maxVal](const T& a) {
std::stringstream ss;
if(std::is_floating_point<T>()) {
ss.precision(decimalPrecision);
ss << std::fixed << *a;
}
else
ss << *a;
return ss.str();
};
gui.element<TextBox<T>>(id, d);
}
template <typename TContainer, typename T> void input_scalars_field(GUIManager& gui, const char* id, std::string_view name, TContainer* val, size_t elemCount, T minVal, T maxVal, const TextBoxScalarOptions& options = {}) {
gui.push_id(id);
left_to_right_line_layout([&]() {
text_label(gui, name);
for(size_t i = 0; i < elemCount; i++) {
gui.push_id(i);
input_scalar<T>(gui, "field", &(*val)[i], minVal, maxVal, options);
gui.pop_id();
}
});
gui.pop_id();
}
template <typename T> void input_scalar_field(GUIManager& gui, const char* id, std::string_view name, T* val, T minVal, T maxVal, const TextBoxScalarOptions& options = {}) {
left_to_right_line_layout([&]() {
text_label(gui, name);
input_scalar<T>(gui, id, val, minVal, maxVal, options);
});
}
template <typename T> void input_color_hex(GUIManager& gui, const char* id, T* val, const TextBoxHexColorOptions& options = {}) {
InputManager::TextInputProperties textInputProps {
.inputType = SDL_TextInputType::SDL_TEXTINPUT_TYPE_TEXT,
.capitalization = SDL_Capitalization::SDL_CAPITALIZE_NONE,
.autocorrect = false,
.multiline = false,
.androidInputType = InputManager::AndroidInputType::ANDROIDTEXT_TYPE_CLASS_TEXT
};
TextBoxData<T> d = textbox_options_to_data<T>(options);
d.textInputProps = textInputProps;
d.fromStr = [hasAlpha = options.hasAlpha](const std::string& a) {
T def = {0.0f, 0.0f, 0.0f, 1.0f};
unsigned startIndex = 0;
if(a.empty())
return def;
if(a[0] == '#')
startIndex++;
for(unsigned i = 0; i < (hasAlpha ? 4 : 3); i++) {
int byteColor;
std::stringstream ss;
size_t startingAt = startIndex + i * 2;
if(startingAt + 1 >= a.size())
break;
ss << a.substr(startingAt, 2);
ss >> std::hex >> byteColor;
if(ss.fail())
break;
def[i] = byteColor / 255.0f;
}
return def;
};
d.toStr = [hasAlpha = options.hasAlpha](const T& a) {
std::stringstream ss;
ss << "#" << std::setfill('0') << std::setw(2) << std::hex;
for(size_t i = 0; i < (hasAlpha ? 4 : 3); i++) {
int convertTo255 = static_cast<int>(std::clamp<float>(a[i], 0, 1) * 255);
ss << std::setfill('0') << std::setw(2) << std::hex << convertTo255;
}
return ss.str();
};
gui.element<TextBox<T>>(id, d);
}
} }

View file

@ -0,0 +1,43 @@
#include "TextLabelHelpers.hpp"
namespace GUIStuff { namespace ElementHelpers {
void text_label_size(GUIManager& gui, std::string_view val, float modifier) {
CLAY_TEXT(gui.strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4<Clay_Color>(gui.io->theme->frontColor1), .fontSize = static_cast<uint16_t>(gui.io->fontSize * modifier)}));
}
void text_label_color(GUIManager& gui, std::string_view val, const SkColor4f& color) {
CLAY_TEXT(gui.strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4<Clay_Color>(color), .fontSize = gui.io->fontSize }));
}
void text_label_light(GUIManager& gui, std::string_view val) {
CLAY_TEXT(gui.strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4<Clay_Color>(gui.io->theme->frontColor2), .fontSize = gui.io->fontSize }));
}
void text_label(GUIManager& gui, std::string_view val) {
CLAY_TEXT(gui.strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4<Clay_Color>(gui.io->theme->frontColor1), .fontSize = gui.io->fontSize }));
}
void text_label_centered(GUIManager& gui, std::string_view val) {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) },
.childAlignment = {.x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER},
}
}) {
CLAY_TEXT(gui.strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4<Clay_Color>(gui.io->theme->frontColor1), .fontSize = gui.io->fontSize, .textAlignment = CLAY_TEXT_ALIGN_CENTER}));
}
}
void text_label_light_centered(GUIManager& gui, std::string_view val) {
CLAY_AUTO_ID({
.layout = {
.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_FIT(0) },
.childAlignment = {.x = CLAY_ALIGN_X_CENTER, .y = CLAY_ALIGN_Y_CENTER},
}
}) {
CLAY_TEXT(gui.strArena.std_str_to_clay_str(val), CLAY_TEXT_CONFIG({.textColor = convert_vec4<Clay_Color>(gui.io->theme->frontColor2), .fontSize = gui.io->fontSize, .textAlignment = CLAY_TEXT_ALIGN_CENTER}));
}
}
}}

View file

@ -0,0 +1,13 @@
#pragma once
#include "../GUIManager.hpp"
namespace GUIStuff { namespace ElementHelpers {
void text_label_color(GUIManager& gui, std::string_view val, const SkColor4f& color);
void text_label_size(GUIManager& gui, std::string_view val, float modifier);
void text_label_light(GUIManager& gui, std::string_view val);
void text_label(GUIManager& gui, std::string_view val);
void text_label_light_centered(GUIManager& gui, std::string_view val);
void text_label_centered(GUIManager& gui, std::string_view val);
}}