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

@ -37,7 +37,15 @@ struct TestMeters {
$gui.set<guecs::Clickable>(gui_id, {
[&, gui_id, name](auto) {
auto& meter = $gui.get<guecs::Meter>(gui_id);
meter.update_percent(1.0f);
auto& bg = $gui.get<guecs::Rectangle>(gui_id);
meter.percent = 1.0f;
if(meter.color.r == 255) {
meter.color = guecs::THEME.DARK_LIGHT;
bg.color = {120, 25, 25, 255};
} else {
meter.color = {255, 0, 0, 255};
bg.color = {0,255,0,255};
}
}
});
}
@ -54,6 +62,7 @@ struct TestMeters {
}
void update() {
$gui.update();
}
};
@ -94,9 +103,9 @@ int main() {
gui.$gui.query<guecs::Meter>([](auto ent, auto& meter) {
if(meter.percent <= 0.0f) {
meter.update_percent(1.0f);
meter.percent = 1.0f;
} else {
meter.update_percent(meter.percent - (0.01f * float(ent)));
meter.percent -= 0.01f * float(ent);
}
});

View file

@ -61,6 +61,7 @@ namespace guecs {
void init(lel::Cell& cell);
void render(sf::RenderTarget& window, sf::Shader *shader_ptr);
void update();
};
// BUG: meter needs a backing rectangle of one color for the full bar, then
@ -72,17 +73,16 @@ namespace guecs {
int padding = THEME.PADDING;
bool vertical = false;
Rectangle bar{padding, color};
// this is set automatically if a Rectangle is configured for the cell
Rectangle backing_rect{padding, color};
size_t $cell_x = 0;
size_t $cell_y = 0;
size_t $cell_w = 0;
size_t $cell_h = 0;
void init(lel::Cell& cell, guecs::Rectangle &bg);
void init(lel::Cell& cell);
void render(sf::RenderTarget& window, sf::Shader *shader_ptr);
void update_percent(float pct);
void update();
};
struct Effect {

View file

@ -82,6 +82,7 @@ namespace guecs {
void init();
void render(sf::RenderTarget& window);
void update();
bool mouse(float x, float y, Modifiers mods);
void click_on(const std::string& name, Modifiers mods=NO_MODS);
void click_on(Entity slot_id, Modifiers mods=NO_MODS);

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) {