GUECS refactor part 1.

This commit is contained in:
Zed A. Shaw 2025-05-04 11:06:53 -04:00
parent 4e7f837240
commit 20176cf54a
2 changed files with 108 additions and 60 deletions

View file

@ -155,70 +155,70 @@ namespace guecs {
for(auto& [name, cell] : $parser.cells) {
auto ent = init_entity(name);
$world.set<lel::Cell>(ent, cell);
set<lel::Cell>(ent, cell);
}
}
DinkyECS::Entity UI::init_entity(const string& name) {
auto entity = $world.entity();
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, entity);
$name_ents.insert_or_assign(name, ent);
// this makes it easier to get the name during querying
$world.set<CellName>(entity, {name});
return entity;
set<CellName>(ent, {name});
return ent;
}
DinkyECS::Entity UI::entity(const string& name) {
Entity UI::entity(const string& name) {
dbc::check($name_ents.contains(name),
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
return $name_ents.at(name);
}
DinkyECS::Entity UI::entity(const string& name, int id) {
Entity UI::entity(const string& name, int id) {
return entity(fmt::format("{}{}", name, id));
}
void UI::init() {
if($world.has_the<Background>()) {
auto& bg = $world.get_the<Background>();
if(has_the<Background>()) {
auto& bg = get_the<Background>();
bg.init();
}
$world.query<Background>([](auto, auto& bg) {
query<Background>([](auto, auto& bg) {
bg.init();
});
$world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
rect.init(cell);
});
$world.query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
shader.init(cell);
});
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
bg.shape->setFillColor(meter.color);
});
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
meter.init(cell);
});
$world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
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) {
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
text.init(cell, $font);
});
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
sprite.init(cell);
});
}
void UI::debug_layout(sf::RenderWindow& window) {
$world.query<lel::Cell>([&](const auto, auto &cell) {
query<lel::Cell>([&](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);
@ -229,33 +229,33 @@ namespace guecs {
}
void UI::render(sf::RenderWindow& window) {
if($world.has_the<Background>()) {
auto& bg = $world.get_the<Background>();
if(has_the<Background>()) {
auto& bg = get_the<Background>();
window.draw(*bg.shape);
}
$world.query<Effect>([&](auto, auto& shader) {
query<Effect>([&](auto, auto& shader) {
if(shader.$active) shader.step();
});
$world.query<Rectangle>([&](auto ent, auto& rect) {
query<Rectangle>([&](auto ent, auto& rect) {
render_helper(window, ent, true, rect.shape);
});
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
meter.render(cell);
render_helper(window, ent, true, meter.bar.shape);
});
$world.query<Sprite>([&](auto ent, auto& sprite) {
query<Sprite>([&](auto ent, auto& sprite) {
render_helper(window, ent, false, sprite.sprite);
});
$world.query<Label>([&](auto ent, auto& text) {
query<Label>([&](auto ent, auto& text) {
render_helper(window, ent, false, text.text);
});
$world.query<Textual>([&](auto ent, auto& text) {
query<Textual>([&](auto ent, auto& text) {
render_helper(window, ent, true, text.text);
});
}
@ -263,7 +263,7 @@ namespace guecs {
bool UI::mouse(float x, float y, bool hover) {
int action_count = 0;
$world.query<lel::Cell>([&](auto ent, auto& cell) {
query<lel::Cell>([&](auto ent, auto& cell) {
if((x >= cell.x && x <= cell.x + cell.w) &&
(y >= cell.y && y <= cell.y + cell.h))
{
@ -331,7 +331,7 @@ namespace guecs {
click_on(ent);
}
void UI::click_on(DinkyECS::Entity ent) {
void UI::click_on(Entity ent) {
if(auto clicked = get_if<Clickable>(ent)) {
if(auto action_data = get_if<ActionData>(ent)) {
clicked->action(ent, action_data->data);