Fixed multiple segfaults

This commit is contained in:
Yousef Khadadeh 2026-03-30 18:51:14 +03:00
parent b4c35124c3
commit 2ad5684594
32 changed files with 1035 additions and 1010 deletions

View file

@ -47,20 +47,20 @@ void text_button_sized(GUIManager& gui, const char* id, std::string_view text, C
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.push_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], 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>("button", d);
}
gui.pop_id();
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], 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>("button", d);
}
});
}
}}

View file

@ -11,29 +11,29 @@ struct ColorPickerItemsOptions {
};
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<T>>("c", val, options.hasAlpha, fullOnEdit);
left_to_right_line_layout(gui, [&]() {
text_label(gui, "R");
input_color_component_255(gui, "r", &(*val)[0], { .onEdit = fullOnEdit });
text_label(gui, "G");
input_color_component_255(gui, "g", &(*val)[1], { .onEdit = fullOnEdit });
text_label(gui, "B");
input_color_component_255(gui, "b", &(*val)[2], { .onEdit = fullOnEdit });
if(options.hasAlpha) {
text_label(gui, "A");
input_color_component_255(gui, "a", &(*val)[3], { .onEdit = fullOnEdit });
}
gui.new_id(id, [&] {
auto fullOnEdit = [&gui, oE = options.onEdit, val]() {
oE();
gui.set_to_layout();
};
gui.element<ColorPicker<T>>("c", val, options.hasAlpha, fullOnEdit);
left_to_right_line_layout(gui, [&]() {
text_label(gui, "R");
input_color_component_255(gui, "r", &(*val)[0], { .onEdit = fullOnEdit });
text_label(gui, "G");
input_color_component_255(gui, "g", &(*val)[1], { .onEdit = fullOnEdit });
text_label(gui, "B");
input_color_component_255(gui, "b", &(*val)[2], { .onEdit = fullOnEdit });
if(options.hasAlpha) {
text_label(gui, "A");
input_color_component_255(gui, "a", &(*val)[3], { .onEdit = fullOnEdit });
}
});
left_to_right_line_layout(gui, [&]() {
text_label(gui, "Hex");
input_color_hex(gui, "h", val, { .hasAlpha = options.hasAlpha, .onEdit = fullOnEdit });
});
});
left_to_right_line_layout(gui, [&]() {
text_label(gui, "Hex");
input_color_hex(gui, "h", val, { .hasAlpha = options.hasAlpha, .onEdit = fullOnEdit });
});
gui.pop_id();
}
} }

View file

@ -5,16 +5,16 @@
namespace GUIStuff { namespace ElementHelpers {
template <typename T> void 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(gui, [&]() {
text_label(gui, name);
input_scalar(gui, "textbox", val, min, max, options);
gui.new_id(id, [&] {
options.onEdit = [&, oE = options.onEdit] {
oE();
gui.set_to_layout();
};
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
input_scalar(gui, "textbox", val, min, max, options);
});
gui.element<NumberSlider<T>>("slider", val, min, max, options.onEdit);
});
gui.element<NumberSlider<T>>("slider", val, min, max, options.onEdit);
gui.pop_id();
}
}}

View file

@ -6,14 +6,14 @@ 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();
gui.new_id(id, [&] {
for(size_t i = 0; i < options.size(); i++) {
gui.new_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(); });
});
}
});
}
}}

View file

@ -25,11 +25,8 @@ void scroll_area_many_entries(GUIManager& gui, const char* id, const ScrollBarMa
}) { }
}
for(size_t i = startPoint; i < endPoint; i++) {
gui.push_id(i);
options.elementContent(i);
gui.pop_id();
}
for(size_t i = startPoint; i < endPoint; i++)
gui.new_id(i, [&] { options.elementContent(i); });
if(endPoint != options.entryCount) {
CLAY_AUTO_ID({

View file

@ -13,6 +13,7 @@ void input_color_component_255(GUIManager& gui, const char* id, float* val, cons
};
TextBoxData<float> d = textbox_options_to_data<float>(options);
d.data = val;
d.textInputProps = textInputProps;
d.fromStr = [](const std::string& str) {
int roundTo255;
@ -41,6 +42,7 @@ void input_text(GUIManager& gui, const char* id, std::string* val, const TextBox
};
TextBoxData<std::string> d = textbox_options_to_data<std::string>(options);
d.data = val;
d.textInputProps = textInputProps;
d.fromStr = [](const std::string& a) {
return a;
@ -69,6 +71,7 @@ void input_scalar(GUIManager& gui, const char* id, uint8_t* val, uint8_t minVal,
};
TextBoxData<uint8_t> d = textbox_options_to_data<uint8_t>(options);
d.data = val;
d.textInputProps = textInputProps;
d.fromStr = [minVal, maxVal](const std::string& a) {
if(a.empty())
@ -98,6 +101,7 @@ void input_path(GUIManager& gui, const char* id, std::filesystem::path* val, con
};
TextBoxData<std::filesystem::path> d = textbox_options_to_data<std::filesystem::path>(options);
d.data = val;
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()));

View file

@ -67,6 +67,7 @@ template <typename T> void input_scalar(GUIManager& gui, const char* id, T* val,
textInputProps.androidInputType |= InputManager::AndroidInputType::ANDROIDTEXT_TYPE_NUMBER_FLAG_DECIMAL;
TextBoxData<T> d = textbox_options_to_data<T>(options);
d.data = val;
d.textInputProps = textInputProps;
d.fromStr = [minVal, maxVal](const std::string& a) {
if(a.empty())
@ -94,16 +95,13 @@ template <typename T> void input_scalar(GUIManager& gui, const char* id, T* val,
}
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(gui, [&]() {
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.new_id(id, [&] {
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
for(size_t i = 0; i < elemCount; i++)
gui.new_id(i, [&] { input_scalar<T>(gui, "field", &(*val)[i], minVal, maxVal, options); });
});
});
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 = {}) {
@ -123,6 +121,7 @@ template <typename T> void input_color_hex(GUIManager& gui, const char* id, T* v
};
TextBoxData<T> d = textbox_options_to_data<T>(options);
d.data = val;
d.textInputProps = textInputProps;
d.fromStr = [hasAlpha = options.hasAlpha](const std::string& a) {
T def;

View file

@ -8,14 +8,6 @@ template <typename T> TextBox<T>::TextBox(GUIManager& gui): Element(gui) {}
template <typename T> void TextBox<T>::layout(const TextBoxData<T>& userInfo) {
auto& io = *gui.io;
this->userInfo = userInfo;
textbox->onUserTextEdit = [&] {
if(userInfo.immutable)
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data));
else {
if(update_data() && userInfo.onEdit)
gui.set_post_callback_func(userInfo.onEdit);
}
};
init_textbox(io);
CLAY_AUTO_ID({
.layout = {
@ -118,6 +110,14 @@ template <typename T> void TextBox<T>::init_textbox(UpdateInputData& io) {
rect = std::make_shared<SCollision::AABB<float>>();
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data));
textbox->onUserTextEdit = [&] {
if(userInfo.immutable)
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data));
else {
if(update_data() && userInfo.onEdit)
gui.set_post_callback_func(userInfo.onEdit);
}
};
}
template <typename T> bool TextBox<T>::update_data() {

View file

@ -35,6 +35,7 @@ GUIManager::GUIManager()
clayArena = Clay_CreateArenaWithCapacityAndMemory(Clay_MinMemorySize(), malloc(Clay_MinMemorySize()));
clayInstance = Clay_Initialize(clayArena, Clay_Dimensions(1.0f, 1.0f), (Clay_ErrorHandler)clay_error_handler);
Clay_SetMeasureTextFunction(clay_skia_measure_text, this);
setToLayout = true;
}
Clay_Dimensions GUIManager::clay_skia_measure_text(Clay_StringSlice str, Clay_TextElementConfig* config, void* userData) {
@ -291,6 +292,18 @@ void GUIManager::clay_error_handler(Clay_ErrorData errorData) {
Logger::get().log("INFO", "[Clay Error] " + std::string(errorData.errorText.chars));
}
void GUIManager::new_id(const char* id, const std::function<void()>& f) {
push_id(id);
f();
pop_id();
}
void GUIManager::new_id(int64_t id, const std::function<void()>& f) {
push_id(id);
f();
pop_id();
}
void GUIManager::push_id(int64_t id) {
idStack.emplace_back(id);
}

View file

@ -28,9 +28,8 @@ class GUIManager {
GUIManagerIDStack idStack;
std::unordered_map<GUIManagerIDStack, ElementContainer> elements;
void push_id(int64_t id);
void push_id(const char* id);
void pop_id();
void new_id(const char* id, const std::function<void()>& f);
void new_id(int64_t id, const std::function<void()>& f);
void set_post_callback_func(const std::function<void()>& f);
@ -65,6 +64,10 @@ class GUIManager {
static void clay_error_handler(Clay_ErrorData errorData);
static Clay_Dimensions clay_skia_measure_text(Clay_StringSlice str, Clay_TextElementConfig* config, void* userData);
void push_id(int64_t id);
void push_id(const char* id);
void pop_id();
std::function<void()> postCallbackFunc;
Clay_Context* clayInstance;
Clay_Arena clayArena;