From e01e697535490a0bd57a718c68da86fd41de9c8b Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 10 Jun 2025 11:12:04 -0400 Subject: [PATCH] Move now works better, and the API is a lot cleaner. Now just need to make it not crash. --- gui/fsm.cpp | 8 +------- gui/guecstra.cpp | 15 ++++++++++++--- gui/guecstra.hpp | 4 +++- gui/loot_ui.cpp | 7 +++++-- gui/status_ui.cpp | 7 +++++-- 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/gui/fsm.cpp b/gui/fsm.cpp index 7180daa..54e754f 100644 --- a/gui/fsm.cpp +++ b/gui/fsm.cpp @@ -208,6 +208,7 @@ namespace gui { case MOUSE_CLICK: mouse_action(false); break; + case MOUSE_DRAG: case MOUSE_MOVE: { if($grab_source) { auto& source = $status_ui.get_grab_source(*$grab_source); @@ -218,13 +219,6 @@ namespace gui { case MOUSE_DRAG_START: { mouse_action(false); } break; - case MOUSE_DRAG: { - if($grab_source) { - auto& source = $status_ui.get_grab_source(*$grab_source); - source.move($router.position); - } - mouse_action(true); - } break; case MOUSE_DROP: mouse_action(false); break; diff --git a/gui/guecstra.cpp b/gui/guecstra.cpp index 82cd3ae..102507a 100644 --- a/gui/guecstra.cpp +++ b/gui/guecstra.cpp @@ -21,11 +21,20 @@ namespace guecs { } DinkyECS::Entity GrabSource::grab() { - fmt::println("grab! {}", world_entity); return world_entity; } - void GrabSource::move(sf::Vector2i position) { - fmt::println("move! {} to {},{}", world_entity, position.x, position.y); + void GrabSource::setSprite(guecs::UI& gui, DinkyECS::Entity gui_id) { + dbc::check(gui.has(gui_id), "GrabSource given sprite gui_id that doesn't exist"); + + auto& sp = gui.get(gui_id); + sprite = sp.sprite; + } + + void GrabSource::move(sf::Vector2i pos) { + if(sprite) { + sprite->setPosition({ + float(pos.x), float(pos.y)}); + } } } diff --git a/gui/guecstra.hpp b/gui/guecstra.hpp index 1c33525..87e2516 100644 --- a/gui/guecstra.hpp +++ b/gui/guecstra.hpp @@ -11,9 +11,11 @@ namespace guecs { struct GrabSource { DinkyECS::Entity world_entity; std::function commit; + std::shared_ptr sprite = nullptr; DinkyECS::Entity grab(); - void move(sf::Vector2i position); + void setSprite(guecs::UI& gui, DinkyECS::Entity gui_id); + void move(sf::Vector2i pos); }; struct DropTarget { diff --git a/gui/loot_ui.cpp b/gui/loot_ui.cpp index 2f90e15..77b8275 100644 --- a/gui/loot_ui.cpp +++ b/gui/loot_ui.cpp @@ -67,8 +67,11 @@ 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, [&, id]() { return remove_slot(id); }}); + + guecs::GrabSource grabber{ + item, [&, id]() { return remove_slot(id); }}; + grabber.setSprite($gui, id); + $gui.set(id, grabber); } else { $gui.set(id, { [&, id](DinkyECS::Entity world_entity) -> bool { return place_slot(id, world_entity); } diff --git a/gui/status_ui.cpp b/gui/status_ui.cpp index bf4e838..8b848e3 100644 --- a/gui/status_ui.cpp +++ b/gui/status_ui.cpp @@ -89,8 +89,11 @@ 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_id]() { return remove_slot(gui_id); }}); + guecs::GrabSource grabber{ world_entity, + [&, gui_id]() { return remove_slot(gui_id); }}; + grabber.setSprite($gui, gui_id); + $gui.set(gui_id, grabber); + contents.insert_or_assign(gui_id, world_entity); return true; } else {