From 5d0d8b16fc0716bdf96a96238c98a18559e97696 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 14 Aug 2025 12:35:25 -0400 Subject: [PATCH] Removed hover on guecs::UI::mouse and now use a generic 16 bit modifier bitset. Also finally fixed Clickable so it just a simple callback with only the modifiers. --- demos/calc.cpp | 8 ++++---- demos/clicker_game.cpp | 8 ++++---- include/guecs/ui.hpp | 21 ++++++++++++--------- meson.build | 2 +- src/guecs/ui.cpp | 31 ++++++++----------------------- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/demos/calc.cpp b/demos/calc.cpp index c96ffea..8f38517 100644 --- a/demos/calc.cpp +++ b/demos/calc.cpp @@ -169,8 +169,8 @@ struct CalculatorUI { $gui.set(id, { label }); wchar_t op = label[0]; $gui.set(id, { - [&, op](auto, auto) { handle_button(op); } - }); + [&, op](auto) { handle_button(op); } + }); } } @@ -182,8 +182,8 @@ struct CalculatorUI { // $gui.debug_layout(window); } - void mouse(float x, float y, bool hover) { - $gui.mouse(x, y, hover); + void mouse(float x, float y, guecs::Modifiers mods) { + $gui.mouse(x, y, mods); } void handle_button(wchar_t op) { diff --git a/demos/clicker_game.cpp b/demos/clicker_game.cpp index c76080a..c5e68b6 100644 --- a/demos/clicker_game.cpp +++ b/demos/clicker_game.cpp @@ -94,7 +94,7 @@ struct ClickerUI { $gui.set(id, {}); $gui.set(id, { "clicker_treat_bone" }); $gui.set(id, { - [&](auto, auto) { handle_button(Event::A_BUTTON); } + [&](auto) { handle_button(Event::A_BUTTON); } }); } } @@ -103,7 +103,7 @@ struct ClickerUI { $gui.set($clicker, {"clicker_the_dog"}); $gui.set($clicker, {"clicker_bark"}); $gui.set($clicker, { - [&](auto, auto) { handle_button(Event::CLICKER); } + [&](auto) { handle_button(Event::CLICKER); } }); // custom components need to be initialized manually @@ -126,8 +126,8 @@ struct ClickerUI { // $gui.debug_layout(window); } - void mouse(float x, float y, bool hover) { - $gui.mouse(x, y, hover); + void mouse(float x, float y, guecs::Modifiers mods) { + $gui.mouse(x, y, mods); } void handle_button(Event ev) { diff --git a/include/guecs/ui.hpp b/include/guecs/ui.hpp index 1b309b5..2d30731 100644 --- a/include/guecs/ui.hpp +++ b/include/guecs/ui.hpp @@ -10,6 +10,7 @@ #include "guecs/theme.hpp" #include "guecs/sfml/components.hpp" #include +#include namespace guecs { using std::shared_ptr, std::wstring, std::string; @@ -24,22 +25,25 @@ namespace guecs { std::queue free_indices; }; + enum class ModBit { + hover=0, + left=1, + right=2 + }; + + using Modifiers = std::bitset<16>; + struct Clickable { /* This is actually called by UI::mouse and passed the entity ID of the * button pressed so you can interact with it in the event handler. */ - std::function action; - }; - - struct ActionData { - std::any data; + std::function action; }; struct CellName { string name; }; - class UI { public: Entity MAIN = 0; @@ -69,9 +73,8 @@ namespace guecs { void init(); void render(sf::RenderWindow& window); - bool mouse(float x, float y, bool hover); - void click_on(const string& name, bool required=false); - void click_on(Entity slot_id); + bool mouse(float x, float y, Modifiers mods); + void click_on(Entity slot_id, Modifiers mods); void debug_layout(sf::RenderWindow& window); Entity entity() { return ++entity_count; } diff --git a/meson.build b/meson.build index a9f4b00..57f07ec 100644 --- a/meson.build +++ b/meson.build @@ -3,7 +3,7 @@ # HEY BUG: when you have a . spec in a LEL it doesn't work on text project('lel-guecs', 'cpp', - version: '0.5.0', + version: '0.6.0', default_options: [ 'cpp_std=c++20', 'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1', diff --git a/src/guecs/ui.cpp b/src/guecs/ui.cpp index 396a277..2d6c4d3 100644 --- a/src/guecs/ui.cpp +++ b/src/guecs/ui.cpp @@ -149,9 +149,9 @@ namespace guecs { }); } - bool UI::mouse(float x, float y, bool hover) { + bool UI::mouse(float x, float y, Modifiers mods) { int action_count = 0; - // BUG: Is Parser::hit useful? + bool hover = mods.test(size_t(ModBit::hover)); query([&](auto ent, auto& cell) { if((x >= cell.x && x <= cell.x + cell.w) && @@ -169,11 +169,11 @@ namespace guecs { if(hover) return; - click_on(ent); + click_on(ent, mods); action_count++; } else { - do_if(ent, [hover](auto& effect) { + do_if(ent, [](auto& effect) { effect.stop(); }); @@ -220,25 +220,10 @@ namespace guecs { } } - void UI::click_on(const string& name, bool required) { - auto ent = entity(name); - - if(required) { - assert(has(ent) && - "click_on required '{}' to exist but it doesn't"); - } - - click_on(ent); - } - - void UI::click_on(Entity ent) { - if(auto clicked = get_if(ent)) { - if(auto action_data = get_if(ent)) { - clicked->action(ent, action_data->data); - } else { - clicked->action(ent, {}); - } - } + void UI::click_on(Entity gui_id, Modifiers mods) { + if(auto to_click = get_if(gui_id)) { + to_click->action(mods); + }; } wstring to_wstring(const string& str) {