Readded grid menu and fixed grid modify tool

This commit is contained in:
Yousef Khadadeh 2026-04-06 12:23:46 +03:00
parent 19db2a57ea
commit b7bd2efc71
10 changed files with 180 additions and 161 deletions

View file

@ -13,6 +13,7 @@ template <typename OptT> SelectableButton::Data selectable_button_options_to_dat
toRet.onClick = options.onClick;
toRet.isSelected = options.isSelected;
toRet.drawType = options.drawType;
toRet.onClickButton = options.onClickButton;
return toRet;
}
@ -23,6 +24,7 @@ struct TextButtonOptions {
bool centered = true;
std::function<void()> onClick;
std::function<void(SelectableButton*)> onClickButton;
};
struct SVGButtonOptions {
@ -31,6 +33,7 @@ struct SVGButtonOptions {
float size = BIG_BUTTON_SIZE;
std::function<void()> onClick;
std::function<void(SelectableButton*)> onClickButton;
};
struct FixedSizeColorButtonOptions {
@ -40,13 +43,15 @@ struct FixedSizeColorButtonOptions {
float size = BIG_BUTTON_SIZE;
std::function<void()> onClick;
std::function<void(SelectableButton*)> onClickButton;
};
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 = {});
SelectableButton* 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 = {}) {
template <typename T> SelectableButton* color_button(GUIManager& gui, const char* id, T* val, const FixedSizeColorButtonOptions& options = {}) {
SelectableButton* toRet;
gui.new_id(id, [&] {
SelectableButton::Data d = selectable_button_options_to_data(options);
d.innerContent = [&] (const SelectableButton::InnerContentCallbackParameters&) {
@ -58,9 +63,10 @@ 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>("button", d);
toRet = gui.element<SelectableButton>("button", d);
}
});
return toRet;
}
}}

View file

@ -32,7 +32,7 @@ void ColorRectangleDisplay::clay_draw(SkCanvas* canvas, UpdateInputData& io, Cla
SkPaint alphaPaint;
alphaPaint.setShader(get_alpha_background_shader());
canvas->drawPaint(alphaPaint);
canvas->drawPaint(SkPaint{color});
canvas->drawPaint(SkPaint{SkColor4f(color.fR, color.fG, color.fB, std::sqrt(color.fA))}); // Square root for alpha to keep color visible
canvas->restore();
}
}

View file

@ -38,7 +38,7 @@ template <typename T> class DropDown : public Element {
.layoutDirection = CLAY_LEFT_TO_RIGHT,
}
}) {
ElementHelpers::text_label(gui, selections[*data]);
ElementHelpers::text_label(gui, selections[static_cast<size_t>(*data)]);
CLAY_AUTO_ID({.layout = {.sizing = {.width = CLAY_SIZING_GROW(0), .height = CLAY_SIZING_GROW(0)}}}) {}
CLAY_AUTO_ID({
.layout = {
@ -121,7 +121,7 @@ template <typename T> class DropDown : public Element {
update_hovering_over(i, l->mouseHovering);
if(l->mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down) {
gui.set_post_callback_func([&, i] {
*d = i;
*d = static_cast<T>(i);
isOpen = false;
if(opts.onClick) opts.onClick();
});

View file

@ -14,7 +14,11 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
SkColor4f backgroundColorHighlight;
SkColor4f backgroundColor;
onClick = d.onClick;
onClick = [&, this, d] {
if(d.onClickButton)
d.onClickButton(this);
else if(d.onClick) d.onClick();
};
if(isHeld || ((d.isSelected || mouseHovering) && d.drawType == DrawType::TRANSPARENT_BORDER))
borderColor = io.theme->fillColor1;
@ -66,8 +70,10 @@ void SelectableButton::layout(const Clay_ElementId& id, const Data& d) {
void SelectableButton::input_mouse_button_callback(const InputManager::MouseButtonCallbackArgs& button) {
bool oldIsHeld = isHeld;
isHeld = mouseHovering && button.button == InputManager::MouseButton::LEFT && button.down;
if(isHeld) {
gui.set_post_callback_func([&]{ if(onClick) onClick(); });
if(isHeld)
gui.set_to_layout();
else if(mouseHovering && oldIsHeld && button.button == InputManager::MouseButton::LEFT && !button.down) {
gui.set_post_callback_func(onClick);
gui.set_to_layout();
}
else if(mouseHovering != oldIsHovering || isHeld != oldIsHeld)

View file

@ -22,6 +22,7 @@ class SelectableButton : public Element {
DrawType drawType = DrawType::TRANSPARENT_ALL;
bool isSelected = false;
std::function<void()> onClick;
std::function<void(SelectableButton*)> onClickButton;
std::function<void(const InnerContentCallbackParameters&)> innerContent;
};

View file

@ -299,11 +299,15 @@ void GUIManager::new_id(int64_t id, const std::function<void()>& f) {
void GUIManager::set_z_index(int16_t z, const std::function<void()>& f) {
auto oldClippingRegion = clippingRegion;
clippingRegion = std::nullopt;
set_z_index_keep_clipping_region(z, f);
clippingRegion = oldClippingRegion;
}
void GUIManager::set_z_index_keep_clipping_region(int16_t z, const std::function<void()>& f) {
int16_t oldZIndex = zIndex;
zIndex = z;
f();
zIndex = oldZIndex;
clippingRegion = oldClippingRegion;
}
int16_t GUIManager::get_z_index() {

View file

@ -29,6 +29,7 @@ class GUIManager {
void new_id(int64_t id, const std::function<void()>& f);
void set_z_index(int16_t z, const std::function<void()>& f);
void set_z_index_keep_clipping_region(int16_t z, const std::function<void()>& f);
int16_t get_z_index();
void set_post_callback_func(const std::function<void()>& f);