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
25
guecs.cpp
25
guecs.cpp
|
@ -68,6 +68,12 @@ namespace guecs {
|
||||||
bar.init(cell);
|
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) {
|
void Sound::play(bool hover) {
|
||||||
if(!hover) {
|
if(!hover) {
|
||||||
sound::play(on_click);
|
sound::play(on_click);
|
||||||
|
@ -190,8 +196,8 @@ namespace guecs {
|
||||||
shader.init(cell);
|
shader.init(cell);
|
||||||
});
|
});
|
||||||
|
|
||||||
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &) {
|
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
|
||||||
bg.shape->setFillColor(ColorValue::BLACK);
|
bg.shape->setFillColor(meter.color);
|
||||||
});
|
});
|
||||||
|
|
||||||
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
||||||
|
@ -236,10 +242,8 @@ namespace guecs {
|
||||||
render_helper(window, ent, true, rect.shape);
|
render_helper(window, ent, true, rect.shape);
|
||||||
});
|
});
|
||||||
|
|
||||||
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, const auto &meter) {
|
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
|
||||||
float level = std::clamp(meter.percent, 0.0f, 1.0f) * float(cell.w);
|
meter.render(cell);
|
||||||
// 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);
|
render_helper(window, ent, true, meter.bar.shape);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -306,9 +310,8 @@ namespace guecs {
|
||||||
void UI::show_text(const string& region, const wstring& content) {
|
void UI::show_text(const string& region, const wstring& content) {
|
||||||
auto ent = entity(region);
|
auto ent = entity(region);
|
||||||
|
|
||||||
if(has<Textual>(ent)) {
|
if(auto tc = get_if<Textual>(ent)) {
|
||||||
auto& text = get<Textual>(ent);
|
tc->text->setString(content);
|
||||||
text.text->setString(content);
|
|
||||||
} else {
|
} else {
|
||||||
auto &cell = cell_for(ent);
|
auto &cell = cell_for(ent);
|
||||||
Textual to_set{content, 20};
|
Textual to_set{content, 20};
|
||||||
|
@ -341,8 +344,8 @@ namespace guecs {
|
||||||
void UI::show_label(const string& region, const wstring& content) {
|
void UI::show_label(const string& region, const wstring& content) {
|
||||||
auto ent = entity(region);
|
auto ent = entity(region);
|
||||||
|
|
||||||
if(auto text = get_if<Label>(ent)) {
|
if(auto tc = get_if<Label>(ent)) {
|
||||||
text->text->setString(content);
|
tc->text->setString(content);
|
||||||
} else {
|
} else {
|
||||||
auto &cell = cell_for(ent);
|
auto &cell = cell_for(ent);
|
||||||
Label to_set{content, 20};
|
Label to_set{content, 20};
|
||||||
|
|
|
@ -52,7 +52,6 @@ namespace guecs {
|
||||||
string name;
|
string name;
|
||||||
int padding = GUECS_PADDING;
|
int padding = GUECS_PADDING;
|
||||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||||
std::shared_ptr<sf::Texture> texture = nullptr;
|
|
||||||
|
|
||||||
void init(lel::Cell &cell);
|
void init(lel::Cell &cell);
|
||||||
void update(const string& new_name);
|
void update(const string& new_name);
|
||||||
|
@ -70,9 +69,11 @@ namespace guecs {
|
||||||
|
|
||||||
struct Meter {
|
struct Meter {
|
||||||
float percent = 1.0f;
|
float percent = 1.0f;
|
||||||
|
sf::Color color = ColorValue::BLACK;
|
||||||
Rectangle bar;
|
Rectangle bar;
|
||||||
|
|
||||||
void init(lel::Cell& cell);
|
void init(lel::Cell& cell);
|
||||||
|
void render(lel::Cell& cell);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ActionData {
|
struct ActionData {
|
||||||
|
|
|
@ -25,7 +25,7 @@ namespace textures {
|
||||||
int height = settings["frame_height"];
|
int height = settings["frame_height"];
|
||||||
sprite->setTextureRect({{0,0}, {width, 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"]);
|
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(initialized, "you forgot to call textures::init()");
|
||||||
dbc::check(TMGR.sprite_textures.contains(name),
|
dbc::check(TMGR.sprite_textures.contains(name),
|
||||||
fmt::format("!!!!! texture pack does not contain {} sprite", name));
|
fmt::format("!!!!! texture pack does not contain {} sprite", name));
|
||||||
|
@ -68,7 +68,7 @@ namespace textures {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Image load_image(std::string filename) {
|
sf::Image load_image(const std::string& filename) {
|
||||||
sf::Image texture;
|
sf::Image texture;
|
||||||
bool good = texture.loadFromFile(filename);
|
bool good = texture.loadFromFile(filename);
|
||||||
dbc::check(good, fmt::format("failed to load {}", filename));
|
dbc::check(good, fmt::format("failed to load {}", filename));
|
||||||
|
|
|
@ -10,7 +10,6 @@
|
||||||
namespace textures {
|
namespace textures {
|
||||||
|
|
||||||
struct SpriteTexture {
|
struct SpriteTexture {
|
||||||
std::string name;
|
|
||||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
||||||
std::shared_ptr<sf::Texture> texture = nullptr;
|
std::shared_ptr<sf::Texture> texture = nullptr;
|
||||||
};
|
};
|
||||||
|
@ -25,9 +24,9 @@ namespace textures {
|
||||||
|
|
||||||
void init();
|
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);
|
const uint32_t* get_surface(size_t num);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue