More of the drag/drop is handled by the GrabSource/DropTarget components.
This commit is contained in:
parent
7a551cf83a
commit
570b70ab0c
7 changed files with 36 additions and 81 deletions
26
gui/fsm.cpp
26
gui/fsm.cpp
|
@ -134,22 +134,17 @@ namespace gui {
|
|||
break;
|
||||
case LOOT_SELECT: {
|
||||
$grab_source = std::any_cast<DinkyECS::Entity>(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<DinkyECS::Entity>(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<DinkyECS::Entity>(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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<bool()> commit;
|
||||
std::function<bool(DinkyECS::Entity entity)> commit;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -46,16 +46,6 @@ namespace gui {
|
|||
update();
|
||||
}
|
||||
|
||||
std::optional<DinkyECS::Entity> 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<components::Sprite>(item),
|
||||
"item in inventory UI doesn't exist in world. New level?");
|
||||
auto& sprite = $level.world->get<components::Sprite>(item);
|
||||
guecs::GrabSource grabber{sprite.name};
|
||||
$gui.set_init<guecs::Sprite>(id, grabber);
|
||||
$gui.set_init<guecs::Sprite>(id, {sprite.name});
|
||||
$gui.set<guecs::GrabSource>(id, {item});
|
||||
} else {
|
||||
$gui.set<guecs::DropTarget>(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<guecs::Sprite>(gui_id), "invalid GrabSource requested, entity isn't in the GUI.");
|
||||
|
||||
return static_cast<guecs::GrabSource&>($gui.get<guecs::Sprite>(gui_id));
|
||||
return $gui.get<guecs::GrabSource>(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<guecs::DropTarget>(gui_id);
|
||||
}
|
||||
|
||||
void LootUI::begin_drop(DinkyECS::Entity world_entity) {
|
||||
dbc::log("begin the loot drop");
|
||||
$selected_entity = world_entity;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ namespace gui {
|
|||
guecs::UI $gui;
|
||||
GameLevel $level;
|
||||
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> contents;
|
||||
DinkyECS::Entity $selected_entity;
|
||||
|
||||
LootUI(GameLevel level);
|
||||
void init();
|
||||
|
@ -27,9 +26,7 @@ namespace gui {
|
|||
std::optional<DinkyECS::Entity> 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);
|
||||
};
|
||||
}
|
||||
|
|
|
@ -46,7 +46,9 @@ namespace gui {
|
|||
guecs::make_action(*$level.world, Events::GUI::INV_SELECT, {button})
|
||||
});
|
||||
$gui.set<DropTarget>(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<components::Sprite>($selected_entity)) {
|
||||
auto& sprite = $level.world->get<components::Sprite>($selected_entity);
|
||||
|
||||
bool StatusUI::place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity) {
|
||||
if($level.world->has<components::Sprite>(world_entity)) {
|
||||
auto& sprite = $level.world->get<components::Sprite>(world_entity);
|
||||
$gui.set_init<guecs::Sprite>(gui_id, {sprite.name});
|
||||
|
||||
contents.insert_or_assign(gui_id, $selected_entity);
|
||||
$gui.set<guecs::GrabSource>(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<guecs::DropTarget>(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<guecs::Sprite>(gui_id), "invalid GrabSource requested, entity isn't in the GUI.");
|
||||
dbc::check($gui.has<guecs::GrabSource>(gui_id), "invalid GrabSource requested, entity isn't in the GUI.");
|
||||
|
||||
return static_cast<guecs::GrabSource&>($gui.get<guecs::Sprite>(gui_id));
|
||||
}
|
||||
|
||||
std::optional<DinkyECS::Entity> StatusUI::begin_grab(DinkyECS::Entity slot_id) {
|
||||
if(!contents.contains(slot_id)) return std::nullopt;
|
||||
|
||||
auto& source = get_grab_source(slot_id);
|
||||
source.grab();
|
||||
return contents.at(slot_id);
|
||||
return $gui.get<guecs::GrabSource>(gui_id);
|
||||
}
|
||||
|
||||
void StatusUI::commit_grab(DinkyECS::Entity slot_id) {
|
||||
contents.erase(slot_id);
|
||||
$gui.remove<guecs::GrabSource>(slot_id);
|
||||
$gui.remove<guecs::Sprite>(slot_id);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ namespace gui {
|
|||
guecs::UI $gui;
|
||||
GameLevel $level;
|
||||
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> 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<DinkyECS::Entity> 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);
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue