From a86912705c531a7b210c21830ef07ee34c20859f Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Thu, 14 Aug 2025 14:10:28 -0400 Subject: [PATCH] Refactored the mouse handling to use the new guecss Modifiers system and improved Clickable. --- gui/boss_fight_ui.cpp | 10 +++++----- gui/boss_fight_ui.hpp | 2 +- gui/combat_ui.cpp | 8 ++++---- gui/combat_ui.hpp | 2 +- gui/debug_ui.cpp | 8 +++++--- gui/debug_ui.hpp | 2 +- gui/fsm.cpp | 34 ++++++++++++++++++---------------- gui/fsm.hpp | 2 +- gui/guecstra.cpp | 11 +++++------ gui/guecstra.hpp | 4 ++-- gui/loot_ui.cpp | 8 ++++---- gui/loot_ui.hpp | 2 +- gui/main_ui.cpp | 4 ++-- gui/main_ui.hpp | 2 +- gui/overlay_ui.cpp | 4 ++-- gui/ritual_ui.cpp | 10 +++++----- gui/ritual_ui.hpp | 2 +- gui/status_ui.cpp | 12 ++++++------ gui/status_ui.hpp | 2 +- wraps/lel-guecs.wrap | 2 +- 20 files changed, 67 insertions(+), 64 deletions(-) diff --git a/gui/boss_fight_ui.cpp b/gui/boss_fight_ui.cpp index f7c8d05..c749651 100644 --- a/gui/boss_fight_ui.cpp +++ b/gui/boss_fight_ui.cpp @@ -68,7 +68,7 @@ namespace gui { auto button = $status.entity(name); $status.set(button, {}); $status.set(button, { - [this, name](auto, auto){ + [this, name](auto){ dbc::log(fmt::format("STATUS: {}", name)); } }); @@ -83,7 +83,7 @@ namespace gui { for(auto& [name, cell] : $overlay.cells()) { auto region = $overlay.entity(name); $overlay.set(region, { - [this, name](auto, auto){ + [this, name](auto){ dbc::log(fmt::format("OVERLAY: {}", name)); } }); @@ -139,12 +139,12 @@ namespace gui { $overlay.render(window); } - bool BossFightUI::mouse(float x, float y, bool hover) { - if($status.mouse(x, y, hover)) { + bool BossFightUI::mouse(float x, float y, guecs::Modifiers mods) { + if($status.mouse(x, y, mods)) { dbc::log("STATUS button pressed"); } - if($overlay.mouse(x, y, hover)) { + if($overlay.mouse(x, y, mods)) { $animation.play(); sound::play("Sword_Hit_1"); $boss_hit = !$boss_hit; diff --git a/gui/boss_fight_ui.hpp b/gui/boss_fight_ui.hpp index 447c719..614f6de 100644 --- a/gui/boss_fight_ui.hpp +++ b/gui/boss_fight_ui.hpp @@ -38,7 +38,7 @@ namespace gui { void init(); void render(sf::RenderWindow& window); - bool mouse(float x, float y, bool hover); + bool mouse(float x, float y, guecs::Modifiers mods); void bounce_boss(sf::RenderWindow& window); bool boss_dead() { return $combat.hp < 0; } void configure_sprite(); diff --git a/gui/combat_ui.cpp b/gui/combat_ui.cpp index 45a0286..0717df2 100644 --- a/gui/combat_ui.cpp +++ b/gui/combat_ui.cpp @@ -31,7 +31,7 @@ namespace gui { $gui.set(button, {sound}); $gui.set(button, {.duration=0.5f, .name=effect_name}); $gui.set(button, - guecs::make_action($level, event, {action})); + guecs::make_action($level, button, event, {action})); return button; } @@ -67,7 +67,7 @@ namespace gui { auto hp_gauge = $gui.entity("hp_gauge"); $gui.set(hp_gauge, {"stone_doll_cursed"}); $gui.set(hp_gauge, - guecs::make_action($level, Events::GUI::HP_STATUS, {})); + guecs::make_action($level, hp_gauge, Events::GUI::HP_STATUS, {})); $gui.init(); } @@ -81,7 +81,7 @@ namespace gui { init(); } - bool CombatUI::mouse(float x, float y, bool hover) { - return $gui.mouse(x, y, hover); + bool CombatUI::mouse(float x, float y, guecs::Modifiers mods) { + return $gui.mouse(x, y, mods); } } diff --git a/gui/combat_ui.hpp b/gui/combat_ui.hpp index e8f4a3d..f52643c 100644 --- a/gui/combat_ui.hpp +++ b/gui/combat_ui.hpp @@ -16,7 +16,7 @@ namespace gui { void init(); void render(sf::RenderWindow& window); void update_level(GameLevel &level); - bool mouse(float x, float y, bool hover); + bool mouse(float x, float y, guecs::Modifiers mods); guecs::Entity make_button(std::string name, Events::GUI event, int action, const std::string &icon_name, const std::string &sound, const std::string &effect_name); diff --git a/gui/debug_ui.cpp b/gui/debug_ui.cpp index 5472f34..6991c53 100644 --- a/gui/debug_ui.cpp +++ b/gui/debug_ui.cpp @@ -35,7 +35,9 @@ namespace gui { void DebugUI::add_spawn_button(std::string enemy_key, std::string sprite_name, std::string region) { auto button = $gui.entity(region); - $gui.set(button, { [this, enemy_key](auto, auto){ spawn(enemy_key); } }); + $gui.set(button, { + [this, enemy_key](auto){ spawn(enemy_key); } + }); $gui.set(button, { sprite_name}); } @@ -91,8 +93,8 @@ namespace gui { } } - bool DebugUI::mouse(float x, float y, bool hover) { - return $gui.mouse(x, y, hover); + bool DebugUI::mouse(float x, float y, guecs::Modifiers mods) { + return $gui.mouse(x, y, mods); } Stats::TimeBullshit DebugUI::time_start() { diff --git a/gui/debug_ui.hpp b/gui/debug_ui.hpp index f8eb029..e7465eb 100644 --- a/gui/debug_ui.hpp +++ b/gui/debug_ui.hpp @@ -17,7 +17,7 @@ namespace gui { void init(lel::Cell cell); void render(sf::RenderWindow& window); - bool mouse(float x, float y, bool hover); + bool mouse(float x, float y, guecs::Modifiers mods); void debug(); void spawn(const std::string& enemy_key); void add_spawn_button(std::string enemy_key, std::string sprite_name, std::string region); diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 4dedd71..dbf9eb9 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -124,7 +124,7 @@ namespace gui { case MOUSE_DRAG_START: case MOUSE_CLICK: case MOUSE_DROP: - mouse_action(false); + mouse_action(guecs::NO_MODS); break; default: if(!$dnd_loot.event(ev, data)) { @@ -202,11 +202,11 @@ namespace gui { } break; case MOUSE_CLICK: fmt::println("CLICK: {}", $router.left_button); - mouse_action(false); - break; - case MOUSE_MOVE: - mouse_action(true); + mouse_action(guecs::NO_MODS); break; + case MOUSE_MOVE: { + mouse_action({1 << guecs::ModBit::hover}); + } break; case AIM_CLICK: System::pickup($level); break; @@ -226,7 +226,7 @@ namespace gui { break; case MOUSE_CLICK: { sf::Vector2f pos = mouse_position(); - $boss_fight_ui->mouse(pos.x, pos.y, false); + $boss_fight_ui->mouse(pos.x, pos.y, guecs::NO_MODS); if($boss_fight_ui->boss_dead()) { event(Event::STAIRS_DOWN); @@ -242,11 +242,11 @@ namespace gui { switch(ev) { case MOUSE_CLICK: - mouse_action(false); - break; - case MOUSE_MOVE: - mouse_action(true); + mouse_action(guecs::NO_MODS); break; + case MOUSE_MOVE: { + mouse_action({1 << guecs::ModBit::hover}); + } break; case ATTACK: $main_ui.dirty(); sound::play("Sword_Hit_1"); @@ -295,16 +295,16 @@ namespace gui { return $window.mapPixelToCoords($router.position); } - void FSM::mouse_action(bool hover) { + void FSM::mouse_action(guecs::Modifiers mods) { sf::Vector2f pos = mouse_position(); - if($debug_ui.active) $debug_ui.mouse(pos.x, pos.y, hover); - $combat_ui.mouse(pos.x, pos.y, hover); - $status_ui.mouse(pos.x, pos.y, hover); + if($debug_ui.active) $debug_ui.mouse(pos.x, pos.y, mods); + $combat_ui.mouse(pos.x, pos.y, mods); + $status_ui.mouse(pos.x, pos.y, mods); if($loot_ui.active) { - $loot_ui.mouse(pos.x, pos.y, hover); + $loot_ui.mouse(pos.x, pos.y, mods); } else { - $main_ui.mouse(pos.x, pos.y, hover); + $main_ui.mouse(pos.x, pos.y, mods); } } @@ -444,6 +444,8 @@ namespace gui { auto [evt, entity, data] = world.recv(); auto player = world.get_the(); + // HERE: this has to go, unify these events and just use them in the state machine directly + switch(evt) { case eGUI::COMBAT: { auto &damage = std::any_cast(data); diff --git a/gui/fsm.hpp b/gui/fsm.hpp index 19da502..2e59d33 100644 --- a/gui/fsm.hpp +++ b/gui/fsm.hpp @@ -70,7 +70,7 @@ namespace gui { void try_move(int dir, bool strafe); sf::Vector2f mouse_position(); - void mouse_action(bool hover); + void mouse_action(guecs::Modifiers mods); void handle_keyboard_mouse(); void draw_gui(); void render(); diff --git a/gui/guecstra.cpp b/gui/guecstra.cpp index d0b0581..89f316a 100644 --- a/gui/guecstra.cpp +++ b/gui/guecstra.cpp @@ -2,15 +2,14 @@ namespace guecs { - Clickable make_action(GameLevel& target, Events::GUI event) { - return {[&, event](auto gui_id, auto data){ - // BUG: either get rid of gui_id or also send a reference the the $gui that is sending the event - target.world->send(event, gui_id, data); + Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event) { + return {[&, gui_id, event](auto){ + target.world->send(event, gui_id, {}); }}; } - Clickable make_action(GameLevel& target, Events::GUI event, std::any data) { - return {[&, event, data](auto gui_id, auto){ + Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event, std::any data) { + return {[&, event, data](auto){ target.world->send(event, gui_id, data); }}; } diff --git a/gui/guecstra.hpp b/gui/guecstra.hpp index 49f0456..f8e7a6d 100644 --- a/gui/guecstra.hpp +++ b/gui/guecstra.hpp @@ -6,8 +6,8 @@ #include "levelmanager.hpp" namespace guecs { - Clickable make_action(GameLevel& target, Events::GUI event); - Clickable make_action(GameLevel& target, Events::GUI event, std::any data); + Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event); + Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event, std::any data); struct GrabSource { DinkyECS::Entity world_entity; diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index e65cd72..2b9573f 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -31,7 +31,7 @@ namespace gui { $gui.set(button, {}); $gui.set(button, {label}); $gui.set(button, - guecs::make_action($level, event)); + guecs::make_action($level, button, event)); } void LootUI::init() { @@ -52,7 +52,7 @@ namespace gui { THEME.TRANSPARENT, THEME.LIGHT_MID }); $gui.set(id, {0.4f, "ui_shader"}); $gui.set(id, { - guecs::make_action($level, Events::GUI::LOOT_SELECT, {id}) + guecs::make_action($level, id, Events::GUI::LOOT_SELECT, {id}) }); } @@ -132,8 +132,8 @@ namespace gui { update(); } - bool LootUI::mouse(float x, float y, bool hover) { - return $gui.mouse(x, y, hover); + bool LootUI::mouse(float x, float y, guecs::Modifiers mods) { + return $gui.mouse(x, y, mods); } bool LootUI::occupied(guecs::Entity slot) { diff --git a/gui/loot_ui.hpp b/gui/loot_ui.hpp index f5481e3..d5513d9 100644 --- a/gui/loot_ui.hpp +++ b/gui/loot_ui.hpp @@ -26,7 +26,7 @@ namespace gui { void update(); void render(sf::RenderWindow& window); void update_level(GameLevel &level); - bool mouse(float x, float y, bool hover); + bool mouse(float x, float y, guecs::Modifiers mods); void make_button(const std::string &name, const std::wstring& label, Events::GUI event); void remove_slot(guecs::Entity slot_id); diff --git a/gui/main_ui.cpp b/gui/main_ui.cpp index 8facfe1..9ddebe6 100644 --- a/gui/main_ui.cpp +++ b/gui/main_ui.cpp @@ -112,7 +112,7 @@ namespace gui { dirty(); } - void MainUI::mouse(int x, int y, bool hover) { - $overlay_ui.$gui.mouse(x, y, hover); + void MainUI::mouse(int x, int y, guecs::Modifiers mods) { + $overlay_ui.$gui.mouse(x, y, mods); } } diff --git a/gui/main_ui.hpp b/gui/main_ui.hpp index 9e89c87..aabe652 100644 --- a/gui/main_ui.hpp +++ b/gui/main_ui.hpp @@ -23,7 +23,7 @@ namespace gui { MainUI(sf::RenderWindow& window); - void mouse(int x, int y, bool hover); + void mouse(int x, int y, guecs::Modifiers mods); void debug(); void render_debug(); diff --git a/gui/overlay_ui.cpp b/gui/overlay_ui.cpp index 89f879e..dd46157 100644 --- a/gui/overlay_ui.cpp +++ b/gui/overlay_ui.cpp @@ -21,8 +21,8 @@ namespace gui { auto area = gui.entity(name); gui.set(area, { - [&](auto ent, auto data) { - level.world->send(Events::GUI::AIM_CLICK, ent, data); + [&](auto) { + level.world->send(Events::GUI::AIM_CLICK, area, {}); } }); } diff --git a/gui/ritual_ui.cpp b/gui/ritual_ui.cpp index 6d08b00..469ccc6 100644 --- a/gui/ritual_ui.cpp +++ b/gui/ritual_ui.cpp @@ -51,7 +51,7 @@ namespace gui { auto open_close_toggle = $gui.entity("ritual_ui"); $gui.set(open_close_toggle, { - [&](auto, auto){ event(Event::TOGGLE); } + [&](auto){ event(Event::TOGGLE); } }); @@ -110,8 +110,8 @@ namespace gui { } } - bool UI::mouse(float x, float y, bool hover) { - return $gui.mouse(x, y, hover); + bool UI::mouse(float x, float y, guecs::Modifiers mods) { + return $gui.mouse(x, y, mods); } bool UI::is_open() { @@ -172,7 +172,7 @@ namespace gui { $gui.set_init(slot_id, {item}); $gui.set(slot_id, { - [&, slot_id, item_id](auto, auto) { + [&, slot_id, item_id](auto) { auto data = std::make_any(slot_id, item_id); event(Event::SELECT, data); } @@ -231,7 +231,7 @@ namespace gui { } $gui.set(combine, { - [&](auto, auto){ event(Event::COMBINE); } + [&](auto){ event(Event::COMBINE); } }); } else { $gui.show_text("result_text", L"That won't work."); diff --git a/gui/ritual_ui.hpp b/gui/ritual_ui.hpp index 1c89eea..9423d23 100644 --- a/gui/ritual_ui.hpp +++ b/gui/ritual_ui.hpp @@ -53,7 +53,7 @@ namespace gui { void OPENING(Event); void CLOSING(Event); - bool mouse(float x, float y, bool hover); + bool mouse(float x, float y, guecs::Modifiers mods); void render(sf::RenderWindow &window); bool is_open(); void load_blanket(); diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index bb98543..56808af 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -35,17 +35,17 @@ namespace gui { $gui.set(gui_id, {"armored_knight"}); } else { $gui.set(gui_id, {}); - $gui.set(gui_id, {make_any(name)}); + dbc::log("!!!!!!!!!!!!!!!!! is this used: $gui.set(gui_id, {make_any(name)});"); if(name == "ritual_ui") { $gui.set(gui_id, { - [this](auto, auto){ select_ritual(); } + [this](auto){ select_ritual(); } }); $gui.set(gui_id, {"pickup"}); } else { $gui.set(gui_id, {guecs::to_wstring(name)}); $gui.set(gui_id, { - guecs::make_action($level, Events::GUI::INV_SELECT, {gui_id}) + guecs::make_action($level, gui_id, Events::GUI::INV_SELECT, {gui_id}) }); $gui.set(gui_id, { .commit=[&, gui_id](DinkyECS::Entity world_target) -> bool { @@ -61,11 +61,11 @@ namespace gui { update(); } - bool StatusUI::mouse(float x, float y, bool hover) { + bool StatusUI::mouse(float x, float y, guecs::Modifiers mods) { if($ritual_ui.is_open()) { - return $ritual_ui.mouse(x, y, hover); + return $ritual_ui.mouse(x, y, mods); } else { - return $gui.mouse(x, y, hover); + return $gui.mouse(x, y, mods); } } diff --git a/gui/status_ui.hpp b/gui/status_ui.hpp index f4a4092..61396ce 100644 --- a/gui/status_ui.hpp +++ b/gui/status_ui.hpp @@ -25,7 +25,7 @@ namespace gui { void init(); void render(sf::RenderWindow &window); void update(); - bool mouse(float x, float y, bool hover); + bool mouse(float x, float y, guecs::Modifiers mods); void remove_slot(guecs::Entity slot_id); bool place_slot(guecs::Entity gui_id, DinkyECS::Entity world_entity); diff --git a/wraps/lel-guecs.wrap b/wraps/lel-guecs.wrap index da50cea..caa2e42 100644 --- a/wraps/lel-guecs.wrap +++ b/wraps/lel-guecs.wrap @@ -1,5 +1,5 @@ [wrap-git] -directory=lel-guecs-0.5.0 +directory=lel-guecs-0.6.0 url=https://git.learnjsthehardway.com/learn-code-the-hard-way/lel-guecs.git revision=HEAD depth=1