Better meaning API on both sides for the drag-n-drop operations, but I _swear_ there's a way to do this in the GrabSource/DropTarget instead.

master
Zed A. Shaw 1 week ago
parent 47c219b86e
commit 3e0adf0c22
  1. 17
      gui/fsm.cpp
  2. 2
      gui/guecstra.hpp
  3. 10
      gui/loot_ui.cpp
  4. 8
      gui/loot_ui.hpp
  5. 8
      gui/status_ui.cpp
  6. 4
      gui/status_ui.hpp

@ -135,12 +135,12 @@ namespace gui {
case LOOT_SELECT: { case LOOT_SELECT: {
$grab_source = std::any_cast<DinkyECS::Entity>(data); $grab_source = std::any_cast<DinkyECS::Entity>(data);
if(auto world_entity = $loot_ui.select_slot(*$grab_source)) { if(auto world_entity = $loot_ui.start_grab(*$grab_source)) {
auto& source = $loot_ui.get_grabber(*$grab_source); auto& source = $loot_ui.get_grab_source(*$grab_source);
source.grab($window); source.grab($window);
source.move($router.position); source.move($router.position);
$status_ui.select_slot(*world_entity); $status_ui.start_drop(*world_entity);
} else { } else {
// BUG: need a cancel operation here // BUG: need a cancel operation here
$window.setMouseCursorVisible(true); $window.setMouseCursorVisible(true);
@ -149,14 +149,11 @@ namespace gui {
} break; } break;
case INV_SELECT: { case INV_SELECT: {
auto gui_id = std::any_cast<DinkyECS::Entity>(data); auto gui_id = std::any_cast<DinkyECS::Entity>(data);
dbc::log(fmt::format("INV_SELECT $grab_source null? {} gui_id {}",
$grab_source == std::nullopt, gui_id));
if($grab_source) { if($grab_source) {
auto& drop = $status_ui.$gui.get<guecs::DropTarget>(gui_id); auto& drop = $status_ui.get_drop_target(gui_id);
if(drop.action()) { if(drop.commit()) {
$loot_ui.remove_slot(*$grab_source); $loot_ui.commit_drop(*$grab_source);
$grab_source = std::nullopt; $grab_source = std::nullopt;
} }
@ -171,7 +168,7 @@ namespace gui {
case MOUSE_DRAG: case MOUSE_DRAG:
case MOUSE_MOVE: { case MOUSE_MOVE: {
if($grab_source) { if($grab_source) {
auto& source = $loot_ui.get_grabber(*$grab_source); auto& source = $loot_ui.get_grab_source(*$grab_source);
source.move($router.position); source.move($router.position);
} }
mouse_action(true); mouse_action(true);

@ -14,6 +14,6 @@ namespace guecs {
}; };
struct DropTarget { struct DropTarget {
std::function<bool()> action; std::function<bool()> commit;
}; };
} }

@ -46,7 +46,7 @@ namespace gui {
update(); update();
} }
std::optional<DinkyECS::Entity> LootUI::select_slot(DinkyECS::Entity slot_id) { std::optional<DinkyECS::Entity> LootUI::start_grab(DinkyECS::Entity slot_id) {
if(contents.contains(slot_id)) { if(contents.contains(slot_id)) {
return contents.at(slot_id); return contents.at(slot_id);
} else { } else {
@ -54,7 +54,7 @@ namespace gui {
} }
} }
void LootUI::remove_slot(DinkyECS::Entity slot_id) { void LootUI::commit_drop(DinkyECS::Entity slot_id) {
contents.erase(slot_id); contents.erase(slot_id);
update(); update();
} }
@ -80,12 +80,12 @@ namespace gui {
} }
} }
bool LootUI::has_grabber(DinkyECS::Entity gui_id) { bool LootUI::has_grab_source(DinkyECS::Entity gui_id) {
return $gui.has<guecs::Sprite>(gui_id); return $gui.has<guecs::Sprite>(gui_id);
} }
guecs::GrabSource& LootUI::get_grabber(DinkyECS::Entity gui_id) { guecs::GrabSource& LootUI::get_grab_source(DinkyECS::Entity gui_id) {
dbc::check(has_grabber(gui_id), "invalid GrabSource requested, entity isn't in the GUI."); dbc::check(has_grab_source(gui_id), "invalid GrabSource requested, entity isn't in the GUI.");
return static_cast<guecs::GrabSource&>($gui.get<guecs::Sprite>(gui_id)); return static_cast<guecs::GrabSource&>($gui.get<guecs::Sprite>(gui_id));
} }

@ -21,9 +21,9 @@ namespace gui {
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
void update_level(GameLevel &level); void update_level(GameLevel &level);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, bool hover);
std::optional<DinkyECS::Entity> select_slot(DinkyECS::Entity slot); std::optional<DinkyECS::Entity> start_grab(DinkyECS::Entity slot);
void remove_slot(DinkyECS::Entity slot_id); guecs::GrabSource& get_grab_source(DinkyECS::Entity entity);
guecs::GrabSource& get_grabber(DinkyECS::Entity entity); bool has_grab_source(DinkyECS::Entity gui_id);
bool has_grabber(DinkyECS::Entity gui_id); void commit_drop(DinkyECS::Entity slot_id);
}; };
} }

@ -46,7 +46,7 @@ namespace gui {
guecs::make_action(*$level.world, Events::GUI::INV_SELECT, {button}) guecs::make_action(*$level.world, Events::GUI::INV_SELECT, {button})
}); });
$gui.set<DropTarget>(button, { $gui.set<DropTarget>(button, {
[&, button]() -> bool { return place_slot(button); } .commit=[&, button]() -> bool { return place_slot(button); }
}); });
} }
} }
@ -83,7 +83,7 @@ namespace gui {
init(); init();
} }
void StatusUI::select_slot(DinkyECS::Entity entity) { void StatusUI::start_drop(DinkyECS::Entity entity) {
$selected_entity = entity; $selected_entity = entity;
} }
@ -99,4 +99,8 @@ namespace gui {
return false; return false;
} }
} }
guecs::DropTarget& StatusUI::get_drop_target(DinkyECS::Entity gui_id) {
return $gui.get<guecs::DropTarget>(gui_id);
}
} }

@ -5,6 +5,7 @@
#include "textures.hpp" #include "textures.hpp"
#include <guecs/ui.hpp> #include <guecs/ui.hpp>
#include "gui/ritual_ui.hpp" #include "gui/ritual_ui.hpp"
#include "gui/guecstra.hpp"
namespace gui { namespace gui {
class StatusUI { class StatusUI {
@ -22,7 +23,8 @@ namespace gui {
void init(); void init();
void render(sf::RenderWindow &window); void render(sf::RenderWindow &window);
void update(); void update();
void select_slot(DinkyECS::Entity entity); void start_drop(DinkyECS::Entity entity);
bool place_slot(DinkyECS::Entity gui_id); bool place_slot(DinkyECS::Entity gui_id);
guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id);
}; };
} }

Loading…
Cancel
Save