Now Sprite can do either aspect_ratio scaling or stretching and Icon is just a subclass.
This commit is contained in:
parent
b7cfa4db2d
commit
a22342cd7e
3 changed files with 13 additions and 43 deletions
|
@ -101,7 +101,7 @@ struct ClickerUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
$clicker = $gui.entity("clicker");
|
$clicker = $gui.entity("clicker");
|
||||||
$gui.set<guecs::Sprite>($clicker, {"clicker_the_dog"});
|
$gui.set<guecs::Sprite>($clicker, {"clicker_the_dog", 0, true});
|
||||||
$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); }
|
||||||
|
|
|
@ -40,6 +40,7 @@ namespace guecs {
|
||||||
struct Sprite {
|
struct Sprite {
|
||||||
string name;
|
string name;
|
||||||
int padding = THEME.PADDING;
|
int padding = THEME.PADDING;
|
||||||
|
bool stretch = 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);
|
||||||
|
@ -47,15 +48,7 @@ namespace guecs {
|
||||||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Icon {
|
struct Icon : public Sprite { };
|
||||||
string name;
|
|
||||||
int padding = THEME.PADDING;
|
|
||||||
std::shared_ptr<sf::Sprite> sprite = nullptr;
|
|
||||||
|
|
||||||
void init(lel::Cell &cell);
|
|
||||||
void update(const string& new_name);
|
|
||||||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
int padding = THEME.PADDING;
|
int padding = THEME.PADDING;
|
||||||
|
|
|
@ -52,42 +52,19 @@ namespace guecs {
|
||||||
|
|
||||||
sprite->setPosition({float(cell.x + padding), float(cell.y + padding)});
|
sprite->setPosition({float(cell.x + padding), float(cell.y + padding)});
|
||||||
|
|
||||||
sprite->setScale({
|
if(stretch) {
|
||||||
float(cell.w - padding * 2) / float(bounds.x),
|
sprite->setScale({
|
||||||
float(cell.h - padding * 2) / float(bounds.y)});
|
float(cell.w - padding * 2) / float(bounds.x),
|
||||||
}
|
float(cell.h - padding * 2) / float(bounds.y)});
|
||||||
|
} else {
|
||||||
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
float box_width = float(cell.w - padding * 2);
|
||||||
window.draw(*sprite, shader_ptr);
|
float box_height = float(cell.h - padding * 2);
|
||||||
}
|
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y));
|
||||||
|
sprite->setScale({scale, scale});
|
||||||
void Icon::update(const string& new_name) {
|
|
||||||
if(new_name != name) {
|
|
||||||
name = new_name;
|
|
||||||
auto sprite_texture = BACKEND->get_icon(name);
|
|
||||||
sprite->setTexture(*sprite_texture.texture);
|
|
||||||
sprite->setTextureRect({{0,0},sprite_texture.frame_size});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Icon::init(lel::Cell &cell) {
|
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||||
auto sprite_texture = BACKEND->get_icon(name);
|
|
||||||
auto bounds = sprite_texture.frame_size;
|
|
||||||
|
|
||||||
sf::IntRect rect{{0,0}, bounds};
|
|
||||||
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
|
|
||||||
|
|
||||||
sprite->setPosition({ float(cell.x + padding), float(cell.y + padding)});
|
|
||||||
|
|
||||||
float a_ratio = float(bounds.x) / float(bounds.y);
|
|
||||||
float x_scale = float(cell.w - padding * 2) / float(bounds.x);
|
|
||||||
float y_new_size = (float(bounds.x) * x_scale) / a_ratio;
|
|
||||||
float y_scale = float(cell.h - padding * 2) / y_new_size;
|
|
||||||
|
|
||||||
sprite->setScale({x_scale, y_scale});
|
|
||||||
}
|
|
||||||
|
|
||||||
void Icon::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
|
||||||
window.draw(*sprite, shader_ptr);
|
window.draw(*sprite, shader_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue