#include "guecs/ui.hpp" #include #include #include namespace guecs { using std::make_shared; UI::UI() { $font = make_shared(THEME.FONT_FILE_NAME); } void UI::position(int x, int y, int width, int height) { $parser.position(x, y, width, height); } sf::Vector2f UI::get_position() { return {float($parser.grid_x), float($parser.grid_y)}; } sf::Vector2f UI::get_size() { return {float($parser.grid_w), float($parser.grid_h)}; } void UI::layout(const string& grid) { $grid = grid; bool good = $parser.parse($grid); assert(good && "LEL parsing failed."); for(auto& [name, cell] : $parser.cells) { auto ent = init_entity(name); set(ent, cell); } } Entity UI::init_entity(const string& name) { auto ent = entity(); // this lets you look up an entity by name $name_ents.insert_or_assign(name, ent); // this makes it easier to get the name during querying set(ent, {name}); return ent; } Entity UI::entity(const string& name) { assert($name_ents.contains(name) && "GUECS entity does not exist. Mispelled cell name?"); return $name_ents.at(name); } Entity UI::entity(const string& name, int id) { return entity(fmt::format("{}{}", name, id)); } void UI::init() { query([](auto, auto& bg) { bg.init(); }); query([](auto, auto& cell, auto& rect) { rect.init(cell); }); query([](auto, auto& cell, auto& shader) { shader.init(cell); }); query([](auto, auto& bg, auto &meter) { bg.shape->setFillColor(meter.color); }); query([](auto, auto &cell, auto& meter) { meter.init(cell); }); query([this](auto, auto& cell, auto& text) { text.init(cell, $font); }); query([this](auto, auto& cell, auto& text) { text.init(cell, $font); }); query([&](auto, auto &cell, auto &sprite) { sprite.init(cell); }); } void UI::debug_layout(sf::RenderWindow& window) { query([&](const auto, auto &cell) { sf::RectangleShape rect{{float(cell.w), float(cell.h)}}; rect.setPosition({float(cell.x), float(cell.y)}); rect.setFillColor(sf::Color::Transparent); rect.setOutlineColor(sf::Color::Red); rect.setOutlineThickness(2.0f); window.draw(rect); }); } void UI::render(sf::RenderWindow& window) { if(auto bg = get_if(MAIN)) { window.draw(*(bg->shape)); } query([&](auto, auto& shader) { if(shader.$active) shader.step(); }); query([&](auto ent, auto& rect) { render_helper(window, ent, true, rect.shape); }); query([&](auto ent, auto& cell, auto &meter) { meter.render(cell); render_helper(window, ent, true, meter.bar.shape); }); query([&](auto ent, auto& sprite) { render_helper(window, ent, false, sprite.sprite); }); query