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;