Better design that has a render/update cycle for components.
This commit is contained in:
parent
48b672eec4
commit
2dbfac27c6
5 changed files with 38 additions and 21 deletions
|
|
@ -37,7 +37,15 @@ struct TestMeters {
|
||||||
$gui.set<guecs::Clickable>(gui_id, {
|
$gui.set<guecs::Clickable>(gui_id, {
|
||||||
[&, gui_id, name](auto) {
|
[&, gui_id, name](auto) {
|
||||||
auto& meter = $gui.get<guecs::Meter>(gui_id);
|
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() {
|
void update() {
|
||||||
|
$gui.update();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -94,9 +103,9 @@ int main() {
|
||||||
|
|
||||||
gui.$gui.query<guecs::Meter>([](auto ent, auto& meter) {
|
gui.$gui.query<guecs::Meter>([](auto ent, auto& meter) {
|
||||||
if(meter.percent <= 0.0f) {
|
if(meter.percent <= 0.0f) {
|
||||||
meter.update_percent(1.0f);
|
meter.percent = 1.0f;
|
||||||
} else {
|
} else {
|
||||||
meter.update_percent(meter.percent - (0.01f * float(ent)));
|
meter.percent -= 0.01f * float(ent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,7 @@ namespace guecs {
|
||||||
|
|
||||||
void init(lel::Cell& cell);
|
void init(lel::Cell& cell);
|
||||||
void render(sf::RenderTarget& window, sf::Shader *shader_ptr);
|
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
|
// BUG: meter needs a backing rectangle of one color for the full bar, then
|
||||||
|
|
@ -72,17 +73,16 @@ namespace guecs {
|
||||||
int padding = THEME.PADDING;
|
int padding = THEME.PADDING;
|
||||||
bool vertical = false;
|
bool vertical = false;
|
||||||
Rectangle bar{padding, color};
|
Rectangle bar{padding, color};
|
||||||
|
|
||||||
// this is set automatically if a Rectangle is configured for the cell
|
// 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_x = 0;
|
||||||
size_t $cell_y = 0;
|
size_t $cell_y = 0;
|
||||||
size_t $cell_w = 0;
|
size_t $cell_w = 0;
|
||||||
size_t $cell_h = 0;
|
size_t $cell_h = 0;
|
||||||
|
|
||||||
void init(lel::Cell& cell, guecs::Rectangle &bg);
|
|
||||||
void init(lel::Cell& cell);
|
void init(lel::Cell& cell);
|
||||||
void render(sf::RenderTarget& window, sf::Shader *shader_ptr);
|
void render(sf::RenderTarget& window, sf::Shader *shader_ptr);
|
||||||
void update_percent(float pct);
|
void update();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Effect {
|
struct Effect {
|
||||||
|
|
|
||||||
|
|
@ -82,6 +82,7 @@ namespace guecs {
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
void render(sf::RenderTarget& window);
|
void render(sf::RenderTarget& window);
|
||||||
|
void update();
|
||||||
bool mouse(float x, float y, Modifiers mods);
|
bool mouse(float x, float y, Modifiers mods);
|
||||||
void click_on(const std::string& name, Modifiers mods=NO_MODS);
|
void click_on(const std::string& name, Modifiers mods=NO_MODS);
|
||||||
void click_on(Entity slot_id, Modifiers mods=NO_MODS);
|
void click_on(Entity slot_id, Modifiers mods=NO_MODS);
|
||||||
|
|
|
||||||
|
|
@ -96,10 +96,7 @@ namespace guecs {
|
||||||
void Rectangle::init(lel::Cell& cell) {
|
void Rectangle::init(lel::Cell& cell) {
|
||||||
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2};
|
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2};
|
||||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
|
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
|
||||||
shape->setFillColor(color);
|
update();
|
||||||
shape->setOutlineColor(border_color);
|
|
||||||
shape->setOutlineThickness(border_px);
|
|
||||||
|
|
||||||
sfml_center_helper(shape, cell, padding);
|
sfml_center_helper(shape, cell, padding);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,9 +104,10 @@ namespace guecs {
|
||||||
window.draw(*shape, shader_ptr);
|
window.draw(*shape, shader_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Meter::init(lel::Cell& cell, Rectangle& bg) {
|
void Rectangle::update() {
|
||||||
backing_rect = bg;
|
shape->setFillColor(color);
|
||||||
init(cell);
|
shape->setOutlineColor(border_color);
|
||||||
|
shape->setOutlineThickness(border_px);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Meter::init(lel::Cell& cell) {
|
void Meter::init(lel::Cell& cell) {
|
||||||
|
|
@ -118,15 +116,18 @@ namespace guecs {
|
||||||
$cell_w = cell.w;
|
$cell_w = cell.w;
|
||||||
$cell_h = cell.h;
|
$cell_h = cell.h;
|
||||||
bar.init(cell);
|
bar.init(cell);
|
||||||
update_percent(percent);
|
update();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Meter::render(sf::RenderTarget& window, sf::Shader *shader_ptr) {
|
void Meter::render(sf::RenderTarget& window, sf::Shader *shader_ptr) {
|
||||||
bar.render(window, shader_ptr);
|
bar.render(window, shader_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Meter::update_percent(float pct) {
|
void Meter::update() {
|
||||||
percent = pct;
|
bar.color = color;
|
||||||
|
bar.padding = padding;
|
||||||
|
bar.update();
|
||||||
|
|
||||||
if(vertical) {
|
if(vertical) {
|
||||||
float level = std::clamp(percent, 0.0f, 1.0f) * float($cell_h);
|
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};
|
sf::Vector2f size{float($cell_w) - padding * 2, std::max(level, 0.0f) - padding};
|
||||||
|
|
|
||||||
|
|
@ -80,11 +80,7 @@ namespace guecs {
|
||||||
});
|
});
|
||||||
|
|
||||||
query<lel::Cell, Meter>([this](auto ent, auto &cell, auto& meter) {
|
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) {
|
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
|
// 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
|
// another validator function I can call in debug modes to confirm they are
|
||||||
void UI::render(sf::RenderTarget& window) {
|
void UI::render(sf::RenderTarget& window) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue