Big BIG refactor to make inventory use a model that's placed into the world, following a more sane MVC style.

This commit is contained in:
Zed A. Shaw 2025-06-20 13:17:12 -04:00
parent 119b3ed11d
commit a0eff927b6
21 changed files with 270 additions and 123 deletions

View file

@ -5,6 +5,7 @@
#include <fmt/xchar.h>
#include "gui/guecstra.hpp"
#include "systems.hpp"
#include "inventory.hpp"
namespace gui {
using namespace guecs;
@ -27,28 +28,29 @@ namespace gui {
$gui.set<Background>($gui.MAIN, {$gui.$parser});
for(auto& [name, cell] : $gui.cells()) {
auto gui_id = $gui.entity(name);
$slot_to_name.insert_or_assign(gui_id, name);
if(name == "character_view") {
auto char_view = $gui.entity(name);
$gui.set<Rectangle>(char_view, {});
$gui.set<Sprite>(char_view, {"armored_knight"});
$gui.set<Rectangle>(gui_id, {});
$gui.set<Sprite>(gui_id, {"armored_knight"});
} else {
auto button = $gui.entity(name);
$gui.set<Rectangle>(button, {});
$gui.set<ActionData>(button, {make_any<string>(name)});
$gui.set<Rectangle>(gui_id, {});
$gui.set<ActionData>(gui_id, {make_any<string>(name)});
if(name == "ritual_ui") {
$gui.set<Clickable>(button, {
$gui.set<Clickable>(gui_id, {
[this](auto, auto){ select_ritual(); }
});
$gui.set<Sound>(button, {"pickup"});
$gui.set<Sound>(gui_id, {"pickup"});
} else {
$gui.set<Textual>(button, {guecs::to_wstring(name)});
$gui.set<Clickable>(button, {
guecs::make_action($level, Events::GUI::INV_SELECT, {button})
$gui.set<Textual>(gui_id, {guecs::to_wstring(name)});
$gui.set<Clickable>(gui_id, {
guecs::make_action($level, Events::GUI::INV_SELECT, {gui_id})
});
$gui.set<DropTarget>(button, {
.commit=[&, button](DinkyECS::Entity world_target) -> bool {
return place_slot(button, world_target);
$gui.set<DropTarget>(gui_id, {
.commit=[&, gui_id](DinkyECS::Entity world_target) -> bool {
return place_slot(gui_id, world_target);
}
});
}
@ -57,6 +59,7 @@ namespace gui {
$ritual_ui.event(ritual::Event::STARTED);
$gui.init();
update();
}
bool StatusUI::mouse(float x, float y, bool hover) {
@ -72,7 +75,17 @@ namespace gui {
}
void StatusUI::update() {
dbc::log("REWRITE ME!");
auto& inventory = $level.world->get_the<inventory::Model>();
for(auto& [slot, world_entity] : inventory.by_slot) {
auto gui_id = $gui.entity(slot);
auto& sprite = $level.world->get<components::Sprite>(world_entity);
$gui.set_init<guecs::Sprite>(gui_id, {sprite.name});
guecs::GrabSource grabber{ world_entity,
[&, gui_id]() { return remove_slot(gui_id); }};
grabber.setSprite($gui, gui_id);
$gui.set<guecs::GrabSource>(gui_id, grabber);
}
}
void StatusUI::render(sf::RenderWindow &window) {
@ -87,30 +100,30 @@ namespace gui {
}
bool StatusUI::place_slot(guecs::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});
guecs::GrabSource grabber{ world_entity,
[&, gui_id]() { return remove_slot(gui_id); }};
grabber.setSprite($gui, gui_id);
$gui.set<guecs::GrabSource>(gui_id, grabber);
contents.insert_or_assign(gui_id, world_entity);
return true;
} else {
return false;
}
auto& slot_name = $slot_to_name.at(gui_id);
auto& inventory = $level.world->get_the<inventory::Model>();
inventory.add(slot_name, world_entity);
update();
return true;
}
bool StatusUI::drop_item(DinkyECS::Entity item_id) {
return System::drop_item($level, item_id);
bool dropped = System::drop_item($level, item_id);
if(dropped) update();
return dropped;
}
// NOTE: do I need this or how does it relate to drop_item?
void StatusUI::remove_slot(guecs::Entity slot_id) {
if(contents.contains(slot_id)) {
contents.erase(slot_id);
$gui.remove<guecs::GrabSource>(slot_id);
$gui.remove<guecs::Sprite>(slot_id);
}
// NOTE: really the System should coordinate either dropping on the
// ground or moving from one container or another, so when loot_ui
// moves to use an ECS id to a container I can have the System
// do it.
auto& slot_name = $slot_to_name.at(slot_id);
auto& inventory = $level.world->get_the<inventory::Model>();
inventory.remove(slot_name);
$gui.remove<guecs::GrabSource>(slot_id);
$gui.remove<guecs::Sprite>(slot_id);
}
}