I now have a semi-functional GUI system that uses the ECS style to build gui elements rather than inheritance.
This commit is contained in:
parent
615599084a
commit
46de98e6f4
12 changed files with 213 additions and 146 deletions
65
ecs_gui.cpp
Normal file
65
ecs_gui.cpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#include "ecs_gui.hpp"
|
||||
#include "constants.hpp"
|
||||
|
||||
GUECS::GUECS() {
|
||||
$font = make_shared<sf::Font>(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<CellName>(entity, {name});
|
||||
return entity;
|
||||
}
|
||||
|
||||
void GUECS::init() {
|
||||
$world.query<lel::Cell, Rectangle>([](const auto &, auto& cell, auto& rect) {
|
||||
rect.init(cell);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Textual>([this](const auto &, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
}
|
||||
|
||||
void GUECS::render(sf::RenderWindow& window) {
|
||||
$world.query<lel::Cell, Meter>([&](const auto &ent, const auto& cell, const auto &meter) {
|
||||
if($world.has<Rectangle>(ent)) {
|
||||
float level = meter.percent * float(cell.w);
|
||||
auto& target = $world.get<Rectangle>(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<Rectangle>([&](const auto &, const auto& rect) {
|
||||
window.draw(*rect.shape);
|
||||
});
|
||||
|
||||
$world.query<Textual>([&](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<lel::Cell, Clickable>([&](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<CellName>(ent);
|
||||
fmt::println("clicked on entity {} with name {} and event {}",
|
||||
ent, cn.name, clicked.event);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue