Major refactoring but now centering text and sprites works. See Issue #16 for why only those and not anything else yet.
This commit is contained in:
parent
2c22da022f
commit
9e9b9620c9
6 changed files with 47 additions and 92 deletions
|
|
@ -7,7 +7,28 @@
|
|||
namespace guecs {
|
||||
using std::make_shared;
|
||||
|
||||
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
|
||||
template<typename T>
|
||||
void sfml_center_helper(T& obj, lel::Cell& cell, int padding) {
|
||||
sf::Vector2f position{float(cell.x + padding), float(cell.y + padding)};
|
||||
|
||||
if(cell.center) {
|
||||
auto bounds = obj->getLocalBounds();
|
||||
position = {float(cell.mid_x), float(cell.mid_y)};
|
||||
obj->setOrigin({bounds.size.x/2, bounds.size.y/2});
|
||||
}
|
||||
|
||||
obj->setPosition(position);
|
||||
}
|
||||
|
||||
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 Text::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
|
||||
assert(font_ptr != nullptr && "you failed to initialize this WideText");
|
||||
if(font == nullptr) font = font_ptr;
|
||||
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size);
|
||||
|
|
@ -25,11 +46,11 @@ namespace guecs {
|
|||
text->setCharacterSize(size);
|
||||
}
|
||||
|
||||
void Textual::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
void Text::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
window.draw(*text, shader_ptr);
|
||||
}
|
||||
|
||||
void Textual::update(const wstring& new_content) {
|
||||
void Text::update(const wstring& new_content) {
|
||||
content = new_content;
|
||||
text->setString(content);
|
||||
}
|
||||
|
|
@ -43,23 +64,14 @@ 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) {
|
||||
if(cell.center) stretch = false;
|
||||
|
||||
auto sprite_texture = load_texture(name, is_icon);
|
||||
auto bounds = sprite_texture.frame_size;
|
||||
|
||||
sf::IntRect rect{{0,0}, bounds};
|
||||
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
|
||||
fmt::println("SPRITE centered: {}", cell.center);
|
||||
|
||||
sprite->setPosition({float(cell.x + padding), float(cell.y + padding)});
|
||||
|
||||
if(stretch) {
|
||||
sprite->setScale({
|
||||
|
|
@ -71,6 +83,8 @@ namespace guecs {
|
|||
float scale = std::min(box_width / float(bounds.x), box_height / float(bounds.y));
|
||||
sprite->setScale({scale, scale});
|
||||
}
|
||||
|
||||
sfml_center_helper(sprite, cell, padding);
|
||||
}
|
||||
|
||||
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) {
|
||||
|
|
@ -80,7 +94,6 @@ namespace guecs {
|
|||
void Rectangle::init(lel::Cell& cell) {
|
||||
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2};
|
||||
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size);
|
||||
shape->setPosition({float(cell.x + padding), float(cell.y + padding)});
|
||||
shape->setFillColor(color);
|
||||
shape->setOutlineColor(border_color);
|
||||
shape->setOutlineThickness(border_px);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue