diff --git a/gui/fsm.cpp b/gui/fsm.cpp index d87bb50..4bddb82 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -134,22 +134,17 @@ namespace gui { break; case LOOT_SELECT: { $grab_source = std::any_cast(data); - - if(auto world_entity = $loot_ui.begin_grab(*$grab_source)) { - $status_ui.begin_drop(*world_entity); - } else { - // BUG: need a cancel operation here - $grab_source = std::nullopt; - state(State::LOOTING); - } + auto& source = $loot_ui.get_grab_source(*$grab_source); + source.grab(); } break; case INV_SELECT: { auto gui_id = std::any_cast(data); if($grab_source) { auto& drop = $status_ui.get_drop_target(gui_id); + auto& grab = $loot_ui.get_grab_source(*$grab_source); - if(drop.commit()) { + if(drop.commit(grab.world_entity)) { $loot_ui.commit_grab(*$grab_source); $grab_source = std::nullopt; } @@ -195,8 +190,9 @@ namespace gui { if($grab_source) { auto& drop = $loot_ui.get_drop_target(gui_id); + auto& grab = $status_ui.get_grab_source(*$grab_source); - if(drop.commit()) { + if(drop.commit(grab.world_entity)) { $status_ui.commit_grab(*$grab_source); $grab_source = std::nullopt; } @@ -206,14 +202,8 @@ namespace gui { } break; case INV_SELECT: { $grab_source = std::any_cast(data); - - if(auto world_entity = $status_ui.begin_grab(*$grab_source)) { - $loot_ui.begin_drop(*world_entity); - } else { - // BUG: need a cancel operation here - $grab_source = std::nullopt; - state(State::LOOTING); - } + auto& source = $status_ui.get_grab_source(*$grab_source); + source.grab(); } break; case MOUSE_CLICK: mouse_action(false); diff --git a/gui/guecstra.cpp b/gui/guecstra.cpp index 82e8b74..82cd3ae 100644 --- a/gui/guecstra.cpp +++ b/gui/guecstra.cpp @@ -20,16 +20,12 @@ namespace guecs { }}; } - - void GrabSource::grab() { - sprite->setOrigin({128, 128}); + DinkyECS::Entity GrabSource::grab() { + fmt::println("grab! {}", world_entity); + return world_entity; } void GrabSource::move(sf::Vector2i position) { - sprite->setPosition({ - float(position.x), - float(position.y) - }); + fmt::println("move! {} to {},{}", world_entity, position.x, position.y); } - } diff --git a/gui/guecstra.hpp b/gui/guecstra.hpp index 796eb76..b7fa88c 100644 --- a/gui/guecstra.hpp +++ b/gui/guecstra.hpp @@ -8,12 +8,14 @@ namespace guecs { Clickable make_action(DinkyECS::World& target, Events::GUI event); Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data); - struct GrabSource : public Sprite { - void grab(); + struct GrabSource { + DinkyECS::Entity world_entity = 0; + + DinkyECS::Entity grab(); void move(sf::Vector2i position); }; struct DropTarget { - std::function commit; + std::function commit; }; } diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index 30ffabf..e207a5b 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -46,16 +46,6 @@ namespace gui { update(); } - 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_grab(DinkyECS::Entity slot_id) { contents.erase(slot_id); update(); @@ -76,20 +66,19 @@ namespace gui { dbc::check($level.world->has(item), "item in inventory UI doesn't exist in world. New level?"); auto& sprite = $level.world->get(item); - guecs::GrabSource grabber{sprite.name}; - $gui.set_init(id, grabber); + $gui.set_init(id, {sprite.name}); + $gui.set(id, {item}); } else { $gui.set(id, { - [&, id]() -> bool { return place_slot(id); } + [&, id](DinkyECS::Entity world_entity) -> bool { return place_slot(id, world_entity); } }); } } } - bool LootUI::place_slot(DinkyECS::Entity id) { + bool LootUI::place_slot(DinkyECS::Entity id, DinkyECS::Entity world_entity) { if(contents.size() < INV_SLOTS && !contents.contains(id)) { - contents.try_emplace(id, $selected_entity); - dbc::log(fmt::format("adding entity {}", id)); + contents.try_emplace(id, world_entity); update(); return true; } else { @@ -100,7 +89,7 @@ namespace gui { guecs::GrabSource& LootUI::get_grab_source(DinkyECS::Entity gui_id) { dbc::check($gui.has(gui_id), "invalid GrabSource requested, entity isn't in the GUI."); - return static_cast($gui.get(gui_id)); + return $gui.get(gui_id); } void LootUI::render(sf::RenderWindow& window) { @@ -120,9 +109,4 @@ namespace gui { guecs::DropTarget& LootUI::get_drop_target(DinkyECS::Entity gui_id) { return $gui.get(gui_id); } - - void LootUI::begin_drop(DinkyECS::Entity world_entity) { - dbc::log("begin the loot drop"); - $selected_entity = world_entity; - } } diff --git a/gui/loot_ui.hpp b/gui/loot_ui.hpp index 2e9cbfb..8126fe3 100644 --- a/gui/loot_ui.hpp +++ b/gui/loot_ui.hpp @@ -13,7 +13,6 @@ namespace gui { guecs::UI $gui; GameLevel $level; std::unordered_map contents; - DinkyECS::Entity $selected_entity; LootUI(GameLevel level); void init(); @@ -27,9 +26,7 @@ namespace gui { 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 world_entity); - bool place_slot(DinkyECS::Entity 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 c7530eb..63294ab 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -46,7 +46,9 @@ namespace gui { guecs::make_action(*$level.world, Events::GUI::INV_SELECT, {button}) }); $gui.set(button, { - .commit=[&, button]() -> bool { return place_slot(button); } + .commit=[&, button](DinkyECS::Entity world_target) -> bool { + return place_slot(button, world_target); + } }); } } @@ -83,13 +85,12 @@ namespace gui { init(); } - bool StatusUI::place_slot(DinkyECS::Entity gui_id) { - if($level.world->has($selected_entity)) { - auto& sprite = $level.world->get($selected_entity); - + bool StatusUI::place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity) { + if($level.world->has(world_entity)) { + auto& sprite = $level.world->get(world_entity); $gui.set_init(gui_id, {sprite.name}); - - contents.insert_or_assign(gui_id, $selected_entity); + $gui.set(gui_id, {world_entity}); + contents.insert_or_assign(gui_id, world_entity); return true; } else { return false; @@ -100,26 +101,15 @@ namespace gui { return $gui.get(gui_id); } - void StatusUI::begin_drop(DinkyECS::Entity world_entity) { - $selected_entity = world_entity; - } - guecs::GrabSource& StatusUI::get_grab_source(DinkyECS::Entity gui_id) { - dbc::check($gui.has(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) { - if(!contents.contains(slot_id)) return std::nullopt; + dbc::check($gui.has(gui_id), "invalid GrabSource requested, entity isn't in the GUI."); - auto& source = get_grab_source(slot_id); - source.grab(); - return contents.at(slot_id); + 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); } } diff --git a/gui/status_ui.hpp b/gui/status_ui.hpp index eded60a..4d450b4 100644 --- a/gui/status_ui.hpp +++ b/gui/status_ui.hpp @@ -13,7 +13,6 @@ namespace gui { guecs::UI $gui; GameLevel $level; std::unordered_map contents; - DinkyECS::Entity $selected_entity; ritual::UI $ritual_ui; StatusUI(GameLevel level); @@ -26,12 +25,9 @@ namespace gui { guecs::GrabSource& get_grab_source(DinkyECS::Entity entity); - - 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 world_entity); - bool place_slot(DinkyECS::Entity gui_id); + bool place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity); }; }