#include "status_ui.hpp" #include "components.hpp" #include "inventory.hpp" #include "color.hpp" #include "guecs.hpp" #include "rand.hpp" namespace gui { using namespace guecs; StatusUI::StatusUI(GameLevel level) : $level(level) { $gui.position(STATUS_UI_X, STATUS_UI_Y, STATUS_UI_WIDTH, STATUS_UI_HEIGHT); $gui.layout( "[slot1 | slot2 | slot3]" "[slot4 | slot5 | slot6]" "[slot7 | slot8 | slot9]" "[*%(100,300)log_view]" "[_]" "[_]"); } void StatusUI::render() { for(auto& [name, cell] : $gui.cells()) { if(name == "log_view") { $log_to = $gui.entity("log_view"); $gui.set($log_to, {}); $gui.set($log_to, {"Welcome to the Game!", 20}); } else { auto button = $gui.entity(name); $gui.set(button, {}); $gui.set(button, {""}); $gui.set(button, guecs::make_action(*$level.world, Events::GUI::NOOP)); } } $gui.init(); } /* WARNING: This is really not the greatest way to do this. */ void StatusUI::update() { if($gui.has($log_to)) { auto& text = $gui.get($log_to); std::string log; for(auto msg : $messages) { log += msg + "\n"; } text.update(log); } auto world = $level.world; if(world->has($level.player)) { auto& inventory = world->get($level.player); if(inventory.count() > 0) { size_t limit = std::min(inventory.count(), $slots.size()); for(size_t i = 0; i < limit; i++) { auto slot = $gui.entity($slots[i]); auto& item = inventory.get(i); auto comp_sprite = components::get(item.data); $gui.set_init(slot, {comp_sprite.name}); std::string count_label = item.count > 1 ? fmt::format("{}", item.count): ""; auto& label = $gui.get(slot); label.text->setString(count_label); } } } } void StatusUI::draw(sf::RenderWindow &window) { $gui.render(window); } void StatusUI::log(std::string msg) { $messages.push_front(msg); if($messages.size() > MAX_LOG_MESSAGES) { $messages.pop_back(); } } }