Fixed Icon vs. Sprite and now Icon is just a subclass of Sprite. Closes #12.

This commit is contained in:
Zed A. Shaw 2025-07-22 17:11:02 -04:00
parent a22342cd7e
commit 3752522597
3 changed files with 26 additions and 5 deletions

View file

@ -93,7 +93,7 @@ struct ClickerUI {
if(name != "clicker") { if(name != "clicker") {
$gui.set<guecs::Rectangle>(id, {}); $gui.set<guecs::Rectangle>(id, {});
$gui.set<guecs::Effect>(id, {}); $gui.set<guecs::Effect>(id, {});
$gui.set<guecs::Icon>(id, { "clicker_treat_bone" }); $gui.set<guecs::Sprite>(id, { "clicker_treat_bone" });
$gui.set<guecs::Clickable>(id, { $gui.set<guecs::Clickable>(id, {
[&](auto, auto) { handle_button(Event::A_BUTTON); } [&](auto, auto) { handle_button(Event::A_BUTTON); }
}); });
@ -101,7 +101,7 @@ struct ClickerUI {
} }
$clicker = $gui.entity("clicker"); $clicker = $gui.entity("clicker");
$gui.set<guecs::Sprite>($clicker, {"clicker_the_dog", 0, true}); $gui.set<guecs::Sprite>($clicker, {"clicker_the_dog"});
$gui.set<guecs::Sound>($clicker, {"clicker_bark"}); $gui.set<guecs::Sound>($clicker, {"clicker_bark"});
$gui.set<guecs::Clickable>($clicker, { $gui.set<guecs::Clickable>($clicker, {
[&](auto, auto) { handle_button(Event::CLICKER); } [&](auto, auto) { handle_button(Event::CLICKER); }

View file

@ -40,7 +40,8 @@ namespace guecs {
struct Sprite { struct Sprite {
string name; string name;
int padding = THEME.PADDING; int padding = THEME.PADDING;
bool stretch = false; bool stretch = true;
bool is_icon = false;
std::shared_ptr<sf::Sprite> sprite = nullptr; std::shared_ptr<sf::Sprite> sprite = nullptr;
void init(lel::Cell &cell); void init(lel::Cell &cell);
@ -48,7 +49,19 @@ namespace guecs {
void render(sf::RenderWindow& window, sf::Shader *shader_ptr); void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
}; };
struct Icon : public Sprite { }; struct Icon : public Sprite {
template<typename... Args>
Icon(Args... args) : Sprite(args...)
{
stretch = false;
is_icon = true;
}
Icon() {
// BUG: why do I have to do this again?
stretch = false;
};
};
struct Rectangle { struct Rectangle {
int padding = THEME.PADDING; int padding = THEME.PADDING;

View file

@ -43,8 +43,16 @@ namespace guecs {
} }
} }
inline SpriteTexture load_texture(const string& name, bool is_icon) {
if(is_icon) {
return BACKEND->get_icon(name);
} else {
return BACKEND->get_sprite(name);
}
}
void Sprite::init(lel::Cell &cell) { void Sprite::init(lel::Cell &cell) {
auto sprite_texture = BACKEND->get_sprite(name); auto sprite_texture = load_texture(name, is_icon);
auto bounds = sprite_texture.frame_size; auto bounds = sprite_texture.frame_size;
sf::IntRect rect{{0,0}, bounds}; sf::IntRect rect{{0,0}, bounds};