Better design that has a render/update cycle for components.

This commit is contained in:
Zed A. Shaw 2026-04-20 16:02:13 -04:00
parent 48b672eec4
commit 2dbfac27c6
5 changed files with 38 additions and 21 deletions

View file

@ -96,10 +96,7 @@ namespace guecs {
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<sf::RectangleShape>(size);
shape->setFillColor(color);
shape->setOutlineColor(border_color);
shape->setOutlineThickness(border_px);
update();
sfml_center_helper(shape, cell, padding);
}
@ -107,9 +104,10 @@ namespace guecs {
window.draw(*shape, shader_ptr);
}
void Meter::init(lel::Cell& cell, Rectangle& bg) {
backing_rect = bg;
init(cell);
void Rectangle::update() {
shape->setFillColor(color);
shape->setOutlineColor(border_color);
shape->setOutlineThickness(border_px);
}
void Meter::init(lel::Cell& cell) {
@ -118,15 +116,18 @@ namespace guecs {
$cell_w = cell.w;
$cell_h = cell.h;
bar.init(cell);
update_percent(percent);
update();
}
void Meter::render(sf::RenderTarget& window, sf::Shader *shader_ptr) {
bar.render(window, shader_ptr);
}
void Meter::update_percent(float pct) {
percent = pct;
void Meter::update() {
bar.color = color;
bar.padding = padding;
bar.update();
if(vertical) {
float level = std::clamp(percent, 0.0f, 1.0f) * float($cell_h);
sf::Vector2f size{float($cell_w) - padding * 2, std::max(level, 0.0f) - padding};

View file

@ -80,11 +80,7 @@ namespace guecs {
});
query<lel::Cell, Meter>([this](auto ent, auto &cell, auto& meter) {
if(auto bg = get_if<Rectangle>(ent)) {
meter.init(cell, *bg);
} else {
meter.init(cell);
}
meter.init(cell);
});
query<lel::Cell, Text>([this](auto, auto& cell, auto& text) {
@ -119,6 +115,16 @@ namespace guecs {
}
}
void UI::update() {
query<Rectangle>([&](auto ent, auto& rect) {
rect.update();
});
query<Meter>([&](auto ent, auto &meter) {
meter.update();
});
}
// BUG: either render detects that the things are initialized or there's
// another validator function I can call in debug modes to confirm they are
void UI::render(sf::RenderTarget& window) {