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;
|
break;
|
||||||
case LOOT_SELECT: {
|
case LOOT_SELECT: {
|
||||||
$grab_source = std::any_cast<DinkyECS::Entity>(data);
|
$grab_source = std::any_cast<DinkyECS::Entity>(data);
|
||||||
|
auto& source = $loot_ui.get_grab_source(*$grab_source);
|
||||||
if(auto world_entity = $loot_ui.begin_grab(*$grab_source)) {
|
source.grab();
|
||||||
$status_ui.begin_drop(*world_entity);
|
|
||||||
} else {
|
|
||||||
// BUG: need a cancel operation here
|
|
||||||
$grab_source = std::nullopt;
|
|
||||||
state(State::LOOTING);
|
|
||||||
}
|
|
||||||
} 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);
|
||||||
|
|
||||||
if($grab_source) {
|
if($grab_source) {
|
||||||
auto& drop = $status_ui.get_drop_target(gui_id);
|
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);
|
$loot_ui.commit_grab(*$grab_source);
|
||||||
$grab_source = std::nullopt;
|
$grab_source = std::nullopt;
|
||||||
}
|
}
|
||||||
|
@ -195,8 +190,9 @@ namespace gui {
|
||||||
|
|
||||||
if($grab_source) {
|
if($grab_source) {
|
||||||
auto& drop = $loot_ui.get_drop_target(gui_id);
|
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);
|
$status_ui.commit_grab(*$grab_source);
|
||||||
$grab_source = std::nullopt;
|
$grab_source = std::nullopt;
|
||||||
}
|
}
|
||||||
|
@ -206,14 +202,8 @@ namespace gui {
|
||||||
} break;
|
} break;
|
||||||
case INV_SELECT: {
|
case INV_SELECT: {
|
||||||
$grab_source = std::any_cast<DinkyECS::Entity>(data);
|
$grab_source = std::any_cast<DinkyECS::Entity>(data);
|
||||||
|
auto& source = $status_ui.get_grab_source(*$grab_source);
|
||||||
if(auto world_entity = $status_ui.begin_grab(*$grab_source)) {
|
source.grab();
|
||||||
$loot_ui.begin_drop(*world_entity);
|
|
||||||
} else {
|
|
||||||
// BUG: need a cancel operation here
|
|
||||||
$grab_source = std::nullopt;
|
|
||||||
state(State::LOOTING);
|
|
||||||
}
|
|
||||||
} break;
|
} break;
|
||||||
case MOUSE_CLICK:
|
case MOUSE_CLICK:
|
||||||
mouse_action(false);
|
mouse_action(false);
|
||||||
|
|
|
@ -20,16 +20,12 @@ namespace guecs {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DinkyECS::Entity GrabSource::grab() {
|
||||||
void GrabSource::grab() {
|
fmt::println("grab! {}", world_entity);
|
||||||
sprite->setOrigin({128, 128});
|
return world_entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GrabSource::move(sf::Vector2i position) {
|
void GrabSource::move(sf::Vector2i position) {
|
||||||
sprite->setPosition({
|
fmt::println("move! {} to {},{}", world_entity, position.x, position.y);
|
||||||
float(position.x),
|
|
||||||
float(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);
|
||||||
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data);
|
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data);
|
||||||
|
|
||||||
struct GrabSource : public Sprite {
|
struct GrabSource {
|
||||||
void grab();
|
DinkyECS::Entity world_entity = 0;
|
||||||
|
|
||||||
|
DinkyECS::Entity grab();
|
||||||
void move(sf::Vector2i position);
|
void move(sf::Vector2i position);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DropTarget {
|
struct DropTarget {
|
||||||
std::function<bool()> commit;
|
std::function<bool(DinkyECS::Entity entity)> commit;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,16 +46,6 @@ namespace gui {
|
||||||
update();
|
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) {
|
void LootUI::commit_grab(DinkyECS::Entity slot_id) {
|
||||||
contents.erase(slot_id);
|
contents.erase(slot_id);
|
||||||
update();
|
update();
|
||||||
|
@ -76,20 +66,19 @@ namespace gui {
|
||||||
dbc::check($level.world->has<components::Sprite>(item),
|
dbc::check($level.world->has<components::Sprite>(item),
|
||||||
"item in inventory UI doesn't exist in world. New level?");
|
"item in inventory UI doesn't exist in world. New level?");
|
||||||
auto& sprite = $level.world->get<components::Sprite>(item);
|
auto& sprite = $level.world->get<components::Sprite>(item);
|
||||||
guecs::GrabSource grabber{sprite.name};
|
$gui.set_init<guecs::Sprite>(id, {sprite.name});
|
||||||
$gui.set_init<guecs::Sprite>(id, grabber);
|
$gui.set<guecs::GrabSource>(id, {item});
|
||||||
} else {
|
} else {
|
||||||
$gui.set<guecs::DropTarget>(id, {
|
$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)) {
|
if(contents.size() < INV_SLOTS && !contents.contains(id)) {
|
||||||
contents.try_emplace(id, $selected_entity);
|
contents.try_emplace(id, world_entity);
|
||||||
dbc::log(fmt::format("adding entity {}", id));
|
|
||||||
update();
|
update();
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,7 +89,7 @@ namespace gui {
|
||||||
guecs::GrabSource& LootUI::get_grab_source(DinkyECS::Entity gui_id) {
|
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.");
|
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) {
|
void LootUI::render(sf::RenderWindow& window) {
|
||||||
|
@ -120,9 +109,4 @@ namespace gui {
|
||||||
guecs::DropTarget& LootUI::get_drop_target(DinkyECS::Entity gui_id) {
|
guecs::DropTarget& LootUI::get_drop_target(DinkyECS::Entity gui_id) {
|
||||||
return $gui.get<guecs::DropTarget>(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;
|
guecs::UI $gui;
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> contents;
|
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> contents;
|
||||||
DinkyECS::Entity $selected_entity;
|
|
||||||
|
|
||||||
LootUI(GameLevel level);
|
LootUI(GameLevel level);
|
||||||
void init();
|
void init();
|
||||||
|
@ -27,9 +26,7 @@ namespace gui {
|
||||||
std::optional<DinkyECS::Entity> begin_grab(DinkyECS::Entity slot);
|
std::optional<DinkyECS::Entity> begin_grab(DinkyECS::Entity slot);
|
||||||
void commit_grab(DinkyECS::Entity slot_id);
|
void commit_grab(DinkyECS::Entity slot_id);
|
||||||
|
|
||||||
|
|
||||||
guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id);
|
guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id);
|
||||||
void begin_drop(DinkyECS::Entity world_entity);
|
bool place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity);
|
||||||
bool place_slot(DinkyECS::Entity id);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,7 +46,9 @@ 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, {
|
||||||
.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();
|
init();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool StatusUI::place_slot(DinkyECS::Entity gui_id) {
|
bool StatusUI::place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity) {
|
||||||
if($level.world->has<components::Sprite>($selected_entity)) {
|
if($level.world->has<components::Sprite>(world_entity)) {
|
||||||
auto& sprite = $level.world->get<components::Sprite>($selected_entity);
|
auto& sprite = $level.world->get<components::Sprite>(world_entity);
|
||||||
|
|
||||||
$gui.set_init<guecs::Sprite>(gui_id, {sprite.name});
|
$gui.set_init<guecs::Sprite>(gui_id, {sprite.name});
|
||||||
|
$gui.set<guecs::GrabSource>(gui_id, {world_entity});
|
||||||
contents.insert_or_assign(gui_id, $selected_entity);
|
contents.insert_or_assign(gui_id, world_entity);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
@ -100,26 +101,15 @@ namespace gui {
|
||||||
return $gui.get<guecs::DropTarget>(gui_id);
|
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) {
|
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));
|
return $gui.get<guecs::GrabSource>(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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void StatusUI::commit_grab(DinkyECS::Entity slot_id) {
|
void StatusUI::commit_grab(DinkyECS::Entity slot_id) {
|
||||||
contents.erase(slot_id);
|
contents.erase(slot_id);
|
||||||
|
$gui.remove<guecs::GrabSource>(slot_id);
|
||||||
$gui.remove<guecs::Sprite>(slot_id);
|
$gui.remove<guecs::Sprite>(slot_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ namespace gui {
|
||||||
guecs::UI $gui;
|
guecs::UI $gui;
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> contents;
|
std::unordered_map<DinkyECS::Entity, DinkyECS::Entity> contents;
|
||||||
DinkyECS::Entity $selected_entity;
|
|
||||||
ritual::UI $ritual_ui;
|
ritual::UI $ritual_ui;
|
||||||
|
|
||||||
StatusUI(GameLevel level);
|
StatusUI(GameLevel level);
|
||||||
|
@ -26,12 +25,9 @@ namespace gui {
|
||||||
|
|
||||||
|
|
||||||
guecs::GrabSource& get_grab_source(DinkyECS::Entity entity);
|
guecs::GrabSource& get_grab_source(DinkyECS::Entity entity);
|
||||||
|
|
||||||
std::optional<DinkyECS::Entity> begin_grab(DinkyECS::Entity slot);
|
|
||||||
void commit_grab(DinkyECS::Entity slot_id);
|
void commit_grab(DinkyECS::Entity slot_id);
|
||||||
|
|
||||||
guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id);
|
guecs::DropTarget& get_drop_target(DinkyECS::Entity gui_id);
|
||||||
void begin_drop(DinkyECS::Entity world_entity);
|
bool place_slot(DinkyECS::Entity gui_id, DinkyECS::Entity world_entity);
|
||||||
bool place_slot(DinkyECS::Entity gui_id);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue