Cleanup of GUECS and the textures manager.
This commit is contained in:
parent
438bd8ab8a
commit
4e7f837240
4 changed files with 31 additions and 28 deletions
45
guecs.cpp
45
guecs.cpp
|
@ -68,6 +68,12 @@ namespace guecs {
|
|||
bar.init(cell);
|
||||
}
|
||||
|
||||
void Meter::render(lel::Cell& cell) {
|
||||
float level = std::clamp(percent, 0.0f, 1.0f) * float(cell.w);
|
||||
// ZED: this 6 is a border width, make it a thing
|
||||
bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)});
|
||||
}
|
||||
|
||||
void Sound::play(bool hover) {
|
||||
if(!hover) {
|
||||
sound::play(on_click);
|
||||
|
@ -187,15 +193,15 @@ namespace guecs {
|
|||
});
|
||||
|
||||
$world.query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
|
||||
shader.init(cell);
|
||||
shader.init(cell);
|
||||
});
|
||||
|
||||
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &) {
|
||||
bg.shape->setFillColor(ColorValue::BLACK);
|
||||
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
|
||||
bg.shape->setFillColor(meter.color);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
||||
meter.init(cell);
|
||||
meter.init(cell);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
||||
|
@ -207,18 +213,18 @@ namespace guecs {
|
|||
});
|
||||
|
||||
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
||||
sprite.init(cell);
|
||||
sprite.init(cell);
|
||||
});
|
||||
}
|
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) {
|
||||
$world.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);
|
||||
rect.setOutlineColor(sf::Color::Red);
|
||||
rect.setOutlineThickness(2.0f);
|
||||
window.draw(rect);
|
||||
sf::RectangleShape rect{{float(cell.w), float(cell.h)}};
|
||||
rect.setPosition({float(cell.x), float(cell.y)});
|
||||
rect.setFillColor(sf::Color::Transparent);
|
||||
rect.setOutlineColor(sf::Color::Red);
|
||||
rect.setOutlineThickness(2.0f);
|
||||
window.draw(rect);
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -236,11 +242,9 @@ namespace guecs {
|
|||
render_helper(window, ent, true, rect.shape);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, const auto &meter) {
|
||||
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w);
|
||||
// ZED: this 6 is a border width, make it a thing
|
||||
meter.bar.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)});
|
||||
render_helper(window, ent, true, meter.bar.shape);
|
||||
$world.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) {
|
||||
|
@ -306,9 +310,8 @@ namespace guecs {
|
|||
void UI::show_text(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(has<Textual>(ent)) {
|
||||
auto& text = get<Textual>(ent);
|
||||
text.text->setString(content);
|
||||
if(auto tc = get_if<Textual>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Textual to_set{content, 20};
|
||||
|
@ -341,8 +344,8 @@ namespace guecs {
|
|||
void UI::show_label(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto text = get_if<Label>(ent)) {
|
||||
text->text->setString(content);
|
||||
if(auto tc = get_if<Label>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Label to_set{content, 20};
|
||||
|
|
|
@ -52,7 +52,6 @@ namespace guecs {
|
|||
string name;
|
||||
int padding = GUECS_PADDING;
|
||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||
std::shared_ptr<sf::Texture> texture = nullptr;
|
||||
|
||||
void init(lel::Cell &cell);
|
||||
void update(const string& new_name);
|
||||
|
@ -70,9 +69,11 @@ namespace guecs {
|
|||
|
||||
struct Meter {
|
||||
float percent = 1.0f;
|
||||
sf::Color color = ColorValue::BLACK;
|
||||
Rectangle bar;
|
||||
|
||||
void init(lel::Cell& cell);
|
||||
void render(lel::Cell& cell);
|
||||
};
|
||||
|
||||
struct ActionData {
|
||||
|
|
|
@ -25,7 +25,7 @@ namespace textures {
|
|||
int height = settings["frame_height"];
|
||||
sprite->setTextureRect({{0,0}, {width, height}});
|
||||
|
||||
TMGR.sprite_textures.try_emplace(name, name, sprite, texture);
|
||||
TMGR.sprite_textures.try_emplace(name, sprite, texture);
|
||||
}
|
||||
|
||||
TMGR.floor = load_image(assets["sprites"]["floor"]["path"]);
|
||||
|
@ -53,7 +53,7 @@ namespace textures {
|
|||
}
|
||||
}
|
||||
|
||||
SpriteTexture get(std::string name) {
|
||||
SpriteTexture get(const std::string& name) {
|
||||
dbc::check(initialized, "you forgot to call textures::init()");
|
||||
dbc::check(TMGR.sprite_textures.contains(name),
|
||||
fmt::format("!!!!! texture pack does not contain {} sprite", name));
|
||||
|
@ -68,7 +68,7 @@ namespace textures {
|
|||
return result;
|
||||
}
|
||||
|
||||
sf::Image load_image(std::string filename) {
|
||||
sf::Image load_image(const std::string& filename) {
|
||||
sf::Image texture;
|
||||
bool good = texture.loadFromFile(filename);
|
||||
dbc::check(good, fmt::format("failed to load {}", filename));
|
||||
|
|
|
@ -10,7 +10,6 @@
|
|||
namespace textures {
|
||||
|
||||
struct SpriteTexture {
|
||||
std::string name;
|
||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||
std::shared_ptr<sf::Texture> texture = nullptr;
|
||||
};
|
||||
|
@ -25,9 +24,9 @@ namespace textures {
|
|||
|
||||
void init();
|
||||
|
||||
SpriteTexture get(std::string name);
|
||||
SpriteTexture get(const std::string& name);
|
||||
|
||||
sf::Image load_image(std::string filename);
|
||||
sf::Image load_image(const std::string& filename);
|
||||
|
||||
const uint32_t* get_surface(size_t num);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue