|
|
|
#include "guecs.hpp"
|
|
|
|
|
|
|
|
namespace guecs {
|
|
|
|
UI::UI() {
|
|
|
|
$font = make_shared<sf::Font>(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;
|
|
|
|
$parser.parse($grid);
|
|
|
|
}
|
|
|
|
|
|
|
|
DinkyECS::Entity UI::entity(std::string name) {
|
|
|
|
auto entity = $world.entity();
|
|
|
|
$world.set<CellName>(entity, {name});
|
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UI::init(TexturePack& textures) {
|
|
|
|
if($world.has_the<Background>()) {
|
|
|
|
auto& bg = $world.get_the<Background>();
|
|
|
|
bg.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
$world.query<Background>([](auto, auto& bg) {
|
|
|
|
bg.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
|
|
|
|
rect.init(cell);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &) {
|
|
|
|
bg.shape->setFillColor(ColorValue::BLACK);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
|
|
|
meter.init(cell);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
|
|
|
text.init(cell, $font);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
|
|
|
|
text.init(cell, $font);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
|
|
|
auto sprite_texture = textures.get(sprite.name);
|
|
|
|
sprite.texture = sprite_texture.texture;
|
|
|
|
sprite.sprite = make_shared<sf::Sprite>(*sprite.texture);
|
|
|
|
sprite.sprite->setPosition({
|
|
|
|
float(cell.x + GUECS_PADDING),
|
|
|
|
float(cell.y + GUECS_PADDING)});
|
|
|
|
|
|
|
|
auto size = sprite.texture->getSize();
|
|
|
|
sprite.sprite->setScale({
|
|
|
|
float(cell.w - GUECS_PADDING * 2) / size.x,
|
|
|
|
float(cell.h - GUECS_PADDING * 2) / size.y});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void UI::render(sf::RenderWindow& window) {
|
|
|
|
if($world.has_the<Background>()) {
|
|
|
|
auto& bg = $world.get_the<Background>();
|
|
|
|
window.draw(*bg.shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
$world.query<Rectangle>([&](auto, auto& rect) {
|
|
|
|
window.draw(*rect.shape);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<lel::Cell, Meter>([&](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<Sprite>([&](auto, auto& sprite) {
|
|
|
|
window.draw(*sprite.sprite);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<Label>([&](auto, auto& text) {
|
|
|
|
window.draw(*text.text);
|
|
|
|
});
|
|
|
|
|
|
|
|
$world.query<Textual>([&](auto, auto& text) {
|
|
|
|
window.draw(*text.text);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void UI::mouse(float x, float y) {
|
|
|
|
$world.query<lel::Cell, Clickable>([&](auto ent, auto& cell, auto &clicked) {
|
|
|
|
if((x >= cell.x && x <= cell.x + cell.w) &&
|
|
|
|
(y >= cell.y && y <= cell.y + cell.h))
|
|
|
|
{
|
|
|
|
auto& cn = $world.get<CellName>(ent);
|
|
|
|
clicked.action(ent, cn.name);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Clickable make_action(DinkyECS::World& target, Events::GUI event) {
|
|
|
|
return {[&, event](auto ent, auto&){
|
|
|
|
target.send<Events::GUI>(event, ent, {});
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
}
|