From be7b86a913f989b9d881692abc32f10f4821e0be Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 10 Jun 2025 00:52:38 -0400 Subject: [PATCH] Mostly refactored out the common things for drag/drop so now just to refine how it's used and bring back moving the sprite around. --- gui/fsm.cpp | 4 ++-- gui/guecstra.hpp | 3 ++- gui/loot_ui.cpp | 5 +++-- gui/loot_ui.hpp | 2 +- gui/status_ui.cpp | 13 ++++++++----- gui/status_ui.hpp | 3 +-- 6 files changed, 17 insertions(+), 13 deletions(-) diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 4bddb82..7180daa 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -145,7 +145,7 @@ namespace gui { auto& grab = $loot_ui.get_grab_source(*$grab_source); if(drop.commit(grab.world_entity)) { - $loot_ui.commit_grab(*$grab_source); + grab.commit(); $grab_source = std::nullopt; } @@ -193,7 +193,7 @@ namespace gui { auto& grab = $status_ui.get_grab_source(*$grab_source); if(drop.commit(grab.world_entity)) { - $status_ui.commit_grab(*$grab_source); + grab.commit(); $grab_source = std::nullopt; } diff --git a/gui/guecstra.hpp b/gui/guecstra.hpp index b7fa88c..1c33525 100644 --- a/gui/guecstra.hpp +++ b/gui/guecstra.hpp @@ -9,7 +9,8 @@ namespace guecs { Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data); struct GrabSource { - DinkyECS::Entity world_entity = 0; + DinkyECS::Entity world_entity; + std::function commit; DinkyECS::Entity grab(); void move(sf::Vector2i position); diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index e207a5b..2f90e15 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -46,7 +46,7 @@ namespace gui { update(); } - void LootUI::commit_grab(DinkyECS::Entity slot_id) { + void LootUI::remove_slot(DinkyECS::Entity slot_id) { contents.erase(slot_id); update(); } @@ -67,7 +67,8 @@ namespace gui { "item in inventory UI doesn't exist in world. New level?"); auto& sprite = $level.world->get(item); $gui.set_init(id, {sprite.name}); - $gui.set(id, {item}); + $gui.set(id, { + item, [&, id]() { return remove_slot(id); }}); } else { $gui.set(id, { [&, id](DinkyECS::Entity world_entity) -> bool { return place_slot(id, world_entity); } diff --git a/gui/loot_ui.hpp b/gui/loot_ui.hpp index 8126fe3..76f18da 100644 --- a/gui/loot_ui.hpp +++ b/gui/loot_ui.hpp @@ -24,7 +24,7 @@ namespace gui { guecs::GrabSource& get_grab_source(DinkyECS::Entity entity); std::optional begin_grab(DinkyECS::Entity slot); - void commit_grab(DinkyECS::Entity slot_id); + void remove_slot(DinkyECS::Entity slot_id); guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id); bool place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity); diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index 63294ab..bf4e838 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -89,7 +89,8 @@ namespace gui { if($level.world->has(world_entity)) { auto& sprite = $level.world->get(world_entity); $gui.set_init(gui_id, {sprite.name}); - $gui.set(gui_id, {world_entity}); + $gui.set(gui_id, {world_entity, + [&, gui_id]() { return remove_slot(gui_id); }}); contents.insert_or_assign(gui_id, world_entity); return true; } else { @@ -107,9 +108,11 @@ namespace gui { return $gui.get(gui_id); } - void StatusUI::commit_grab(DinkyECS::Entity slot_id) { - contents.erase(slot_id); - $gui.remove(slot_id); - $gui.remove(slot_id); + void StatusUI::remove_slot(DinkyECS::Entity slot_id) { + if(contents.contains(slot_id)) { + contents.erase(slot_id); + $gui.remove(slot_id); + $gui.remove(slot_id); + } } } diff --git a/gui/status_ui.hpp b/gui/status_ui.hpp index 4d450b4..a7db08b 100644 --- a/gui/status_ui.hpp +++ b/gui/status_ui.hpp @@ -23,9 +23,8 @@ namespace gui { void update(); bool mouse(float x, float y, bool hover); - guecs::GrabSource& get_grab_source(DinkyECS::Entity entity); - void commit_grab(DinkyECS::Entity slot_id); + void remove_slot(DinkyECS::Entity slot_id); guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id); bool place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity);