More GUI code moved to events, code now compiles but application doesn't run

This commit is contained in:
Yousef Khadadeh 2026-03-30 17:05:24 +03:00
parent 3566b7e563
commit b4c35124c3
37 changed files with 913 additions and 879 deletions

View file

@ -15,7 +15,7 @@ void text_button(GUIManager& gui, const char* id, std::string_view text, const T
.childAlignment = {.x = options.centered ? CLAY_ALIGN_X_CENTER : CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}
}
}) {
gui.element<SelectableButton>(id, options);
gui.element<SelectableButton>(id, d);
}
}
@ -30,7 +30,7 @@ void text_button_sized(GUIManager& gui, const char* id, std::string_view text, C
.childAlignment = {.x = options.centered ? CLAY_ALIGN_X_CENTER : CLAY_ALIGN_X_LEFT, .y = CLAY_ALIGN_Y_CENTER}
}
}) {
gui.element<SelectableButton>(id, options);
gui.element<SelectableButton>(id, d);
}
}
@ -40,7 +40,7 @@ void svg_icon_button(GUIManager& gui, const char* id, const std::string& svgPath
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);
gui.element<SelectableButton>(id, d);
}
}

View file

@ -47,9 +47,10 @@ 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>([val, hasAlpha = options.hasAlpha] {
gui.element<ColorRectangleDisplay>("color display", [val, hasAlpha = options.hasAlpha] {
if(hasAlpha)
return SkColor4f{(*val)[0], (*val)[1], (*val)[2], 1.0f};
else
@ -57,8 +58,9 @@ template <typename T> void color_button(GUIManager& gui, const char* id, T* val,
});
};
CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_FIXED(options.size), .height = CLAY_SIZING_FIXED(options.size) } } }) {
gui.element<SelectableButton>(id, options);
gui.element<SelectableButton>("button", d);
}
gui.pop_id();
}
}}

View file

@ -1,44 +1,11 @@
#pragma once
#include "../GUIManager.hpp"
#include "TextBoxHelpers.hpp"
#include "../Elements/ColorPicker.hpp"
#include "../Elements/ColorPickerButton.decl.hpp"
#include "ColorPickerItemsHelpers.hpp"
#include "../Elements/ColorPickerButton.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([&]() {
left_to_right_line_layout(gui, [&]() {
gui.element<ColorPickerButton<T>>(id, val, options);
text_label(gui, name);
});

View file

@ -0,0 +1,39 @@
#pragma once
#include "../GUIManager.hpp"
#include "TextBoxHelpers.hpp"
#include "../Elements/ColorPicker.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<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 });
});
gui.pop_id();
}
} }

View file

@ -4,15 +4,15 @@
#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 = {}) {
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([&]() {
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
input_scalar("textbox", val, min, max, options);
input_scalar(gui, "textbox", val, min, max, options);
});
gui.element<NumberSlider<T>>("slider", val, min, max, options.onEdit);
gui.pop_id();

View file

@ -1,5 +1,4 @@
#include "TextBoxHelpers.hpp"
#include "../Elements/TextBox.impl.hpp"
#include "LayoutHelpers.hpp"
namespace GUIStuff { namespace ElementHelpers {
@ -29,7 +28,7 @@ void input_color_component_255(GUIManager& gui, const char* id, float* val, cons
return ss.str();
};
gui.element<TextBox<std::string>>(id, d);
gui.element<TextBox<float>>(id, d);
}
void input_text(GUIManager& gui, const char* id, std::string* val, const TextBoxOptions& options) {

View file

@ -1,6 +1,6 @@
#pragma once
#include "../GUIManager.hpp"
#include "../Elements/TextBox.decl.hpp"
#include "../Elements/TextBox.hpp"
#include "TextLabelHelpers.hpp"
#include "LayoutHelpers.hpp"
@ -83,10 +83,10 @@ template <typename T> void input_scalar(GUIManager& gui, const char* id, T* val,
std::stringstream ss;
if(std::is_floating_point<T>()) {
ss.precision(decimalPrecision);
ss << std::fixed << *a;
ss << std::fixed << a;
}
else
ss << *a;
ss << a;
return ss.str();
};
@ -95,7 +95,7 @@ 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([&]() {
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
for(size_t i = 0; i < elemCount; i++) {
gui.push_id(i);
@ -107,7 +107,7 @@ template <typename TContainer, typename T> void input_scalars_field(GUIManager&
}
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([&]() {
left_to_right_line_layout(gui, [&]() {
text_label(gui, name);
input_scalar<T>(gui, id, val, minVal, maxVal, options);
});
@ -125,7 +125,12 @@ template <typename T> void input_color_hex(GUIManager& gui, const char* id, T* v
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};
T def;
def[0] = 0.0f;
def[1] = 0.0f;
def[2] = 0.0f;
if(hasAlpha)
def[3] = 1.0f;
unsigned startIndex = 0;
if(a.empty())
return def;

View file

@ -77,15 +77,6 @@ template <typename T> class ColorPicker : public Element {
static constexpr float BAR_WIDTH = 30.0;
static constexpr float BAR_GAP = 5.0;
static sk_sp<SkShader> hueBarShader;
static sk_sp<SkRuntimeEffect> svSelectionAreaEffect;
static sk_sp<SkRuntimeEffect> alphaBarEffect;
static sk_sp<SkShader> get_hue_shader();
static sk_sp<SkShader> get_sv_selection_shader(float hue);
static sk_sp<SkShader> get_alpha_bar_shader(const Vector3f& mainColor, float horizontalResolution);
T* data;
Vector3f savedHsv; // We save the HSV so that conversion doesnt ruin the UI
std::function<void()> onChange;

View file

@ -4,9 +4,11 @@
namespace GUIStuff {
template <typename T> ColorPicker<T>::ColorPicker(GUIManager& gui): Element(gui) {}
template <typename T> void ColorPicker<T>::layout(T* data, bool selectAlpha, const std::function<void()>& onChange) {
this->data = data;
savedHsv = rgb_to_hsv<T>(*data);
savedHsv = rgb_to_hsv<Vector3f, T>(*data);
this->selectAlpha = selectAlpha;
this->onChange = onChange;
@ -29,7 +31,7 @@ template <typename T> void ColorPicker<T>::clay_draw(SkCanvas* canvas, UpdateInp
canvas->clipRect(SkRect::MakeXYWH(0.0f, 0.0f, 1.0f, 1.0f), skiaAA);
SkPaint svSelectionAreaPaint;
svSelectionAreaPaint.setShader(get_sv_selection_shader(savedHsv.x() / 360.0f));
svSelectionAreaPaint.setShader(ColorPickerShaders::get_sv_selection_shader(savedHsv.x() / 360.0f));
canvas->drawPaint(svSelectionAreaPaint);
SkPaint selectionLinePaint({1.0f, 1.0f, 1.0f, 1.0f});
@ -50,7 +52,7 @@ template <typename T> void ColorPicker<T>::clay_draw(SkCanvas* canvas, UpdateInp
canvas->clipRect(SkRect::MakeXYWH(0.0f, 0.0f, 1.0f, 1.0f), skiaAA);
SkPaint hueBarPaint;
hueBarPaint.setShader(get_hue_shader());
hueBarPaint.setShader(ColorPickerShaders::get_hue_shader());
canvas->drawPaint(hueBarPaint);
canvas->drawLine(0.0f, 1.0f - normalizedHue, 1.0f, 1.0f - normalizedHue, selectionLinePaint);
@ -64,7 +66,7 @@ template <typename T> void ColorPicker<T>::clay_draw(SkCanvas* canvas, UpdateInp
SkPaint alphaBarPaint;
if(selectAlpha)
alphaBarPaint.setShader(get_alpha_bar_shader({(*data)[0], (*data)[1], (*data)[2]}, alphaBarDim.x()));
alphaBarPaint.setShader(ColorPickerShaders::get_alpha_bar_shader({(*data)[0], (*data)[1], (*data)[2]}, alphaBarDim.x()));
else
alphaBarPaint.setColor4f(SkColor4f{(*data)[0], (*data)[1], (*data)[2], 1.0f});
canvas->drawPaint(alphaBarPaint);
@ -103,7 +105,7 @@ template <typename T> bool ColorPicker<T>::input_mouse_button_callback(const Inp
template <typename T> bool ColorPicker<T>::input_mouse_motion_callback(const InputManager::MouseMotionCallbackArgs& motion, bool mouseHovering) {
update_color_picker_pos(motion.pos);
return Element::input_mouse_button_callback(motion, mouseHovering);
return Element::input_mouse_motion_callback(motion, mouseHovering);
}
template <typename T> void ColorPicker<T>::update_color_picker_pos(const Vector2f& p) {
@ -111,6 +113,8 @@ template <typename T> void ColorPicker<T>::update_color_picker_pos(const Vector2
gui.set_post_callback_func([&, p]() {
auto& bb = boundingBox.value();
switch(held) {
case HeldBar::NONE:
break;
case HeldBar::SV_HELD: {
float svSelectionAreaSize = bb.width() - (BAR_GAP + BAR_WIDTH);
Vector2f newSv = cwise_vec_clamp<Vector2f>((p - bb.min) / svSelectionAreaSize, Vector2f{0.0f, 0.0f}, Vector2f{1.0f, 1.0f});

View file

@ -4,14 +4,14 @@
namespace GUIStuff {
struct ColorPickerButtonData {
bool hasAlpha = false;
bool hasAlpha = true;
std::function<void()> onEdit;
};
template <typename T> class ColorPickerButton : public Element {
public:
ColorPickerButton(GUIManager& gui);
void layout(T* val, ColorPickerButtonData& d);
void layout(T* val, const ColorPickerButtonData& d);
private:
ColorPickerButtonData data;
bool isOpen = false;

View file

@ -1,12 +1,13 @@
#include "ColorPickerButton.decl.hpp"
#include "../ElementHelpers/ColorPickerHelpers.hpp"
#include "../ElementHelpers/ColorPickerItemsHelpers.hpp"
#include "../ElementHelpers/ButtonHelpers.hpp"
#include "../ElementHelpers/LayoutHelpers.hpp"
namespace GUIStuff {
template <typename T> ColorPickerButton<T>::ColorPickerButton(GUIManager& gui): Element(gui) {}
template <typename T> void ColorPickerButton<T>::layout(T* val, ColorPickerButtonData& d) {
template <typename T> void ColorPickerButton<T>::layout(T* val, const ColorPickerButtonData& d) {
this->data = d;
GUIStuff::ElementHelpers::color_button(gui, "Color Picker Button", val, {
@ -17,7 +18,7 @@ template <typename T> void ColorPickerButton<T>::layout(T* val, ColorPickerButto
}
});
if(isOpen) {
top_to_bottom_window_popup_layout("Color Picker", CLAY_SIZING_FIT(300), CLAY_SIZING_FIT(0), [&, val, data = data]() {
ElementHelpers::top_to_bottom_window_popup_layout(gui, "Color Picker", CLAY_SIZING_FIT(300), CLAY_SIZING_FIT(0), [&, val, data = data]() {
ElementHelpers::color_picker_items(gui, "c", val, {
.hasAlpha = data.hasAlpha,
.onEdit = data.onEdit

View file

@ -2,6 +2,8 @@
namespace GUIStuff {
sk_sp<SkRuntimeEffect> ColorRectangleDisplay::alphaBackgroundEffect;
ColorRectangleDisplay::ColorRectangleDisplay(GUIManager& gui): Element(gui) {}
void ColorRectangleDisplay::layout(const std::function<SkColor4f()>& colorFunc) {

View file

@ -14,9 +14,8 @@ constexpr double ROTATE_BAR_SNAP_DISTRIBUTION = std::numbers::pi * 0.25;
PaintCircleMenu::PaintCircleMenu(GUIManager& gui):
Element(gui) {}
void PaintCircleMenu::layout(const Data& data, const std::function<void()>& onChange) {
void PaintCircleMenu::layout(const Data& data) {
d = data;
this->onChange = onChange;
CLAY_AUTO_ID({
.layout = {

View file

@ -13,7 +13,7 @@ class PaintCircleMenu : public Element {
std::function<void()> onRotate;
std::function<void()> onPaletteClick;
};
void layout(const Data& data, const std::function<void()>& onChange);
void layout(const Data& data);
virtual bool collides_with_point(const Vector2f& p) const override;
virtual void clay_draw(SkCanvas* canvas, UpdateInputData& io, Clay_RenderCommand* command, bool skiaAA) override;
virtual bool input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button, bool mouseHovering) override;
@ -22,7 +22,6 @@ class PaintCircleMenu : public Element {
void draw_rotate_bar(SkCanvas* canvas, UpdateInputData& io, bool skiaAA);
void draw_palette_bar(SkCanvas* canvas, UpdateInputData& io, bool skiaAA);
Data d;
std::function<void()> onChange;
unsigned colorSelectionIndex = 0;
bool isHovering = false;
bool isRotateBarHeld = false;

View file

@ -3,12 +3,14 @@
namespace GUIStuff {
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}, toStr(*userInfo.data));
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);
@ -79,7 +81,7 @@ template <typename T> bool TextBox<T>::input_mouse_button_callback(const InputMa
gui.io->input->remove_rich_text_box_input(textbox);
if(update_data())
gui.set_post_callback_func(userInfo.onEdit);
update_textbox(io);
init_textbox(*gui.io);
isSelected = false;
}
}
@ -115,7 +117,7 @@ template <typename T> void TextBox<T>::init_textbox(UpdateInputData& io) {
cur = std::make_shared<RichText::TextBox::Cursor>();
rect = std::make_shared<SCollision::AABB<float>>();
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, toStr(*userInfo.data));
cur->pos = cur->selectionBeginPos = cur->selectionEndPos = textbox->insert({0, 0}, userInfo.toStr(*userInfo.data));
}
template <typename T> bool TextBox<T>::update_data() {