#include "ecs_gui.hpp" #include "constants.hpp" GUECS::GUECS() { $font = make_shared(FONT_FILE_NAME); } void GUECS::position(int x, int y, int width, int height) { $parser.position(x, y, width, height); } void GUECS::layout(std::string grid) { $grid = grid; $parser.parse($grid); } DinkyECS::Entity GUECS::entity(std::string name) { auto entity = $world.entity(); $world.set(entity, {name}); return entity; } void GUECS::init(TexturePack& textures) { $world.query([](const auto &, auto& cell, auto& rect) { rect.init(cell); }); $world.query([this](const auto &, auto& cell, auto& text) { text.init(cell, $font); }); $world.query([&](const auto &, auto &cell, auto &sprite) { auto sprite_texture = textures.get(sprite.name); sprite.texture = sprite_texture.texture; sprite.sprite = make_shared(*sprite.texture); sprite.sprite->setPosition({float(cell.x + 5), float(cell.y + 5)}); auto size = sprite.texture->getSize(); sprite.sprite->setScale({float(cell.w - 10) / size.x, float(cell.h - 10) / size.y}); }); } void GUECS::render(sf::RenderWindow& window) { $world.query([&](const auto &ent, const auto& cell, const auto &meter) { if($world.has(ent)) { float level = meter.percent * float(cell.w); auto& target = $world.get(ent); // ZED: this 6 is a border width, make it a thing target.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); } }); $world.query([&](const auto &, const auto& rect) { window.draw(*rect.shape); }); $world.query([&](const auto &, const auto& sprite) { window.draw(*sprite.sprite); }); $world.query([&](const auto &, const auto& text) { window.draw(*text.text); }); } void GUECS::mouse(sf::RenderWindow &window) { if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { sf::Vector2f pos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); $world.query([&](const auto &ent, auto& cell, auto &clicked) { if((pos.x >= cell.x && pos.x <= cell.x + cell.w) && (pos.y >= cell.y && pos.y <= cell.y + cell.h)) { auto& cn = $world.get(ent); fmt::println("clicked on entity {} with name {} and event {}", ent, cn.name, clicked.event); } }); } }