From 4b0d76bbcc1522bfddc585a0b91e7f4fc6e3b85e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 8 Jun 2025 23:55:59 -0400 Subject: [PATCH] Even better API, but still not the best organization. This will let me implement both sides, then I can pull it out and try to generalize it into a few guecs components. --- gui/fsm.cpp | 16 ++++++---------- gui/guecstra.cpp | 3 +-- gui/guecstra.hpp | 2 +- gui/loot_ui.cpp | 10 ++++++++-- gui/loot_ui.hpp | 11 +++++++++-- gui/status_ui.cpp | 33 +++++++++++++++++++++++++++++---- gui/status_ui.hpp | 10 +++++++++- 7 files changed, 63 insertions(+), 22 deletions(-) diff --git a/gui/fsm.cpp b/gui/fsm.cpp index e8ce90a..9573f9e 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -135,12 +135,9 @@ namespace gui { case LOOT_SELECT: { $grab_source = std::any_cast(data); - if(auto world_entity = $loot_ui.start_grab(*$grab_source)) { - auto& source = $loot_ui.get_grab_source(*$grab_source); - source.grab($window); - source.move($router.position); - - $status_ui.start_drop(*world_entity); + if(auto world_entity = $loot_ui.begin_grab(*$grab_source)) { + $window.setMouseCursorVisible(false); + $status_ui.begin_drop(*world_entity); } else { // BUG: need a cancel operation here $window.setMouseCursorVisible(true); @@ -149,11 +146,10 @@ namespace gui { } break; case INV_SELECT: { auto gui_id = std::any_cast(data); - if($grab_source) { - auto& drop = $status_ui.get_drop_target(gui_id); - if(drop.commit()) { - $loot_ui.commit_drop(*$grab_source); + if($grab_source) { + if($status_ui.commit_drop(gui_id)) { + $loot_ui.commit_grab(*$grab_source); $grab_source = std::nullopt; } diff --git a/gui/guecstra.cpp b/gui/guecstra.cpp index 553c83d..82e8b74 100644 --- a/gui/guecstra.cpp +++ b/gui/guecstra.cpp @@ -21,8 +21,7 @@ namespace guecs { } - void GrabSource::grab(sf::RenderWindow& window) { - window.setMouseCursorVisible(false); + void GrabSource::grab() { sprite->setOrigin({128, 128}); } diff --git a/gui/guecstra.hpp b/gui/guecstra.hpp index 88fd3d1..796eb76 100644 --- a/gui/guecstra.hpp +++ b/gui/guecstra.hpp @@ -9,7 +9,7 @@ namespace guecs { Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data); struct GrabSource : public Sprite { - void grab(sf::RenderWindow& window); + void grab(); void move(sf::Vector2i position); }; diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index fd5bcb4..45ea096 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -46,15 +46,17 @@ namespace gui { update(); } - std::optional LootUI::start_grab(DinkyECS::Entity slot_id) { + std::optional LootUI::begin_grab(DinkyECS::Entity slot_id) { if(contents.contains(slot_id)) { + auto& source = get_grab_source(slot_id); + source.grab(); return contents.at(slot_id); } else { return std::nullopt; } } - void LootUI::commit_drop(DinkyECS::Entity slot_id) { + void LootUI::commit_grab(DinkyECS::Entity slot_id) { contents.erase(slot_id); update(); } @@ -103,4 +105,8 @@ namespace gui { bool LootUI::mouse(float x, float y, bool hover) { return $gui.mouse(x, y, hover); } + + guecs::DropTarget& LootUI::get_drop_target(DinkyECS::Entity gui_id) { + return $gui.get(gui_id); + } } diff --git a/gui/loot_ui.hpp b/gui/loot_ui.hpp index 44abdcd..d45ac2e 100644 --- a/gui/loot_ui.hpp +++ b/gui/loot_ui.hpp @@ -21,9 +21,16 @@ namespace gui { void render(sf::RenderWindow& window); void update_level(GameLevel &level); bool mouse(float x, float y, bool hover); - std::optional start_grab(DinkyECS::Entity slot); + guecs::GrabSource& get_grab_source(DinkyECS::Entity entity); bool has_grab_source(DinkyECS::Entity gui_id); - void commit_drop(DinkyECS::Entity slot_id); + + std::optional begin_grab(DinkyECS::Entity slot); + void commit_grab(DinkyECS::Entity slot_id); + + + guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id); + void begin_drop(DinkyECS::Entity entity); + void commit_drop(DinkyECS::Entity entity); }; } diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index f12d02a..40baf7e 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -83,10 +83,6 @@ namespace gui { init(); } - void StatusUI::start_drop(DinkyECS::Entity entity) { - $selected_entity = entity; - } - bool StatusUI::place_slot(DinkyECS::Entity gui_id) { if($level.world->has($selected_entity)) { auto& sprite = $level.world->get($selected_entity); @@ -103,4 +99,33 @@ namespace gui { guecs::DropTarget& StatusUI::get_drop_target(DinkyECS::Entity gui_id) { return $gui.get(gui_id); } + + void StatusUI::begin_drop(DinkyECS::Entity entity) { + $selected_entity = entity; + } + + bool StatusUI::commit_drop(DinkyECS::Entity gui_id) { + auto& drop = get_drop_target(gui_id); + + return drop.commit(); + } + + bool StatusUI::has_grab_source(DinkyECS::Entity gui_id) { + return $gui.has(gui_id); + } + + guecs::GrabSource& StatusUI::get_grab_source(DinkyECS::Entity gui_id) { + dbc::check(has_grab_source(gui_id), "invalid GrabSource requested, entity isn't in the GUI."); + + return static_cast($gui.get(gui_id)); + } + + std::optional StatusUI::begin_grab(DinkyECS::Entity slot_id) { + (void)slot_id; + return std::nullopt; + } + + void StatusUI::commit_grab(DinkyECS::Entity slot_id) { + (void)slot_id; + } } diff --git a/gui/status_ui.hpp b/gui/status_ui.hpp index 5adde4f..929a2e0 100644 --- a/gui/status_ui.hpp +++ b/gui/status_ui.hpp @@ -23,8 +23,16 @@ namespace gui { void init(); void render(sf::RenderWindow &window); void update(); - void start_drop(DinkyECS::Entity entity); bool place_slot(DinkyECS::Entity gui_id); + + guecs::GrabSource& get_grab_source(DinkyECS::Entity entity); + bool has_grab_source(DinkyECS::Entity gui_id); + + std::optional begin_grab(DinkyECS::Entity slot); + void commit_grab(DinkyECS::Entity slot_id); + guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id); + void begin_drop(DinkyECS::Entity entity); + bool commit_drop(DinkyECS::Entity entity); }; }