#include "guecs.hpp" namespace guecs { void Textual::init(lel::Cell &cell, shared_ptr font_ptr) { dbc::check(font_ptr != nullptr, "you failed to initialize this WideText"); if(font == nullptr) font = font_ptr; if(text == nullptr) text = make_shared(*font, content, size); text->setFillColor(color); if(centered) { auto bounds = text->getLocalBounds(); auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell); // this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box text->setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2}); } else { text->setPosition({float(cell.x + padding * 2), float(cell.y + padding * 2)}); } text->setCharacterSize(size); } void Textual::update(std::wstring& new_content) { content = new_content; text->setString(content); } void Sprite::init(lel::Cell &cell) { auto sprite_texture = textures::get(name); texture = sprite_texture.texture; sprite = make_shared(*texture); sprite->setPosition({ float(cell.x + padding), float(cell.y + padding)}); auto bounds = sprite->getGlobalBounds(); sprite->setScale({ float(cell.w - padding * 2) / bounds.size.x, float(cell.h - padding * 2) / bounds.size.y}); } void Rectangle::init(lel::Cell& cell) { sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; if(shape == nullptr) shape = make_shared(size); shape->setPosition({float(cell.x + padding), float(cell.y + padding)}); shape->setFillColor(color); shape->setOutlineColor(border_color); shape->setOutlineThickness(border_px); } void Meter::init(lel::Cell& cell) { bar.init(cell); } void Background::init() { sf::Vector2f size{float(w), float(h)}; if(shape == nullptr) shape = make_shared(size); shape->setPosition({float(x), float(y)}); shape->setFillColor(color); } UI::UI() { $font = make_shared(FONT_FILE_NAME); } void UI::position(int x, int y, int width, int height) { $parser.position(x, y, width, height); } void UI::layout(std::string grid) { $grid = grid; bool good = $parser.parse($grid); dbc::check(good, "LEL parsing failed."); for(auto& [name, cell] : $parser.cells) { auto ent = entity(name); $world.set(ent, cell); } } DinkyECS::Entity UI::entity(std::string name) { if($name_ents.contains(name)) { // already exists so just return it return $name_ents.at(name); } else { auto entity = $world.entity(); // this lets you look up an entity by name $name_ents.insert_or_assign(name, entity); // this makes it easier to get the name during querying $world.set(entity, {name}); return entity; } } void UI::init() { if($world.has_the()) { auto& bg = $world.get_the(); bg.init(); } $world.query([](auto, auto& bg) { bg.init(); }); $world.query([](auto, auto& cell, auto& rect) { rect.init(cell); }); $world.query([](auto, auto& bg, auto &) { bg.shape->setFillColor(ColorValue::BLACK); }); $world.query([](auto, auto &cell, auto& meter) { meter.init(cell); }); $world.query([this](auto, auto& cell, auto& text) { text.init(cell, $font); }); $world.query([this](auto, auto& cell, auto& text) { text.init(cell, $font); }); $world.query([&](auto, auto &cell, auto &sprite) { sprite.init(cell); }); } void UI::debug_layout(sf::RenderWindow& window) { $world.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($world.has_the()) { auto& bg = $world.get_the(); window.draw(*bg.shape); } $world.query([&](auto, auto& rect) { window.draw(*rect.shape); }); $world.query([&](auto, auto& cell, const auto &meter) { float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w); // ZED: this 6 is a border width, make it a thing meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); window.draw(*meter.bar.shape); }); $world.query([&](auto, auto& sprite) { window.draw(*sprite.sprite); }); $world.query