#include "gui/loot_ui.hpp" #include "constants.hpp" #include namespace gui { using namespace guecs; LootUI::LootUI(GameLevel level) : $level(level) { $gui.position(RAY_VIEW_X+RAY_VIEW_WIDTH/2-200, RAY_VIEW_Y+RAY_VIEW_HEIGHT/2-200, 400, 400); $gui.layout( "[item_0 | item_1 |item_2 | item_3 ]" "[item_4 | item_5 |item_6 | item_7 ]" "[item_8 | item_9 |item_10| item_11]" "[item_12| item_13|item_14|item_15 ]" "[_ | %(100,50)close| _]" ); } void LootUI::init() { using guecs::THEME; auto bg_color = THEME.DARK_LIGHT; bg_color.a = 140; $gui.set($gui.MAIN, {$gui.$parser, bg_color}); auto close = $gui.entity("close"); $gui.set(close, {}); $gui.set(close, {L"CLOSE"}); $gui.set(close, guecs::make_action(*$level.world, Events::GUI::LOOT_CLOSE)); for(int i = 0; i < INV_SLOTS; i++) { auto id = $gui.entity("item_", i); $gui.set(id, {THEME.PADDING, THEME.TRANSPARENT, THEME.LIGHT_MID }); $gui.set(id, {0.4f, "ui_shader"}); $gui.set(id, { guecs::make_action(*$level.world, Events::GUI::LOOT_SELECT, {id}) }); } $gui.init(); update(); } std::optional 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(); } void LootUI::update() { dbc::check(contents.size() < INV_SLOTS, "too many items in loot contents, must be < 16"); for(size_t i = 0; i < INV_SLOTS; i++) { auto id = $gui.entity("item_", int(i)); if($gui.has(id)) { $gui.remove(id); } if(contents.contains(id)) { auto item = contents.at(id); dbc::check($level.world->has(item), "item in inventory UI doesn't exist in world. New level?"); auto& sprite = $level.world->get(item); guecs::GrabSource grabber{sprite.name}; $gui.set_init(id, grabber); } else { $gui.set(id, { [&, id]() -> bool { return place_slot(id); } }); } } } bool LootUI::place_slot(DinkyECS::Entity id) { if(contents.size() < INV_SLOTS && !contents.contains(id)) { contents.try_emplace(id, $selected_entity); dbc::log(fmt::format("adding entity {}", id)); update(); return true; } else { return false; } } guecs::GrabSource& LootUI::get_grab_source(DinkyECS::Entity gui_id) { dbc::check($gui.has(gui_id), "invalid GrabSource requested, entity isn't in the GUI."); return static_cast($gui.get(gui_id)); } void LootUI::render(sf::RenderWindow& window) { $gui.render(window); } void LootUI::update_level(GameLevel &level) { $level = level; contents.clear(); init(); } bool LootUI::mouse(float x, float y, bool hover) { return $gui.mouse(x, y, hover); } guecs::DropTarget& LootUI::get_drop_target(DinkyECS::Entity gui_id) { return $gui.get(gui_id); } void LootUI::begin_drop(DinkyECS::Entity world_entity) { dbc::log("begin the loot drop"); $selected_entity = world_entity; } }