|
|
@ -7,13 +7,34 @@ |
|
|
|
namespace guecs { |
|
|
|
namespace guecs { |
|
|
|
using std::make_shared; |
|
|
|
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"); |
|
|
|
assert(font_ptr != nullptr && "you failed to initialize this WideText"); |
|
|
|
if(font == nullptr) font = font_ptr; |
|
|
|
if(font == nullptr) font = font_ptr; |
|
|
|
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size); |
|
|
|
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size); |
|
|
|
text->setFillColor(color); |
|
|
|
text->setFillColor(color); |
|
|
|
|
|
|
|
|
|
|
|
if(centered) { |
|
|
|
if(centered || cell.center) { |
|
|
|
auto bounds = text->getLocalBounds(); |
|
|
|
auto bounds = text->getLocalBounds(); |
|
|
|
auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell); |
|
|
|
auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell); |
|
|
|
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
|
|
|
|
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
|
|
|
@ -25,11 +46,11 @@ namespace guecs { |
|
|
|
text->setCharacterSize(size); |
|
|
|
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); |
|
|
|
window.draw(*text, shader_ptr); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Textual::update(const wstring& new_content) { |
|
|
|
void Text::update(const wstring& new_content) { |
|
|
|
content = new_content; |
|
|
|
content = new_content; |
|
|
|
text->setString(content); |
|
|
|
text->setString(content); |
|
|
|
} |
|
|
|
} |
|
|
@ -44,59 +65,40 @@ namespace guecs { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Sprite::init(lel::Cell &cell) { |
|
|
|
void Sprite::init(lel::Cell &cell) { |
|
|
|
auto sprite_texture = BACKEND->get_sprite(name); |
|
|
|
if(cell.center) stretch = false; |
|
|
|
|
|
|
|
|
|
|
|
sf::IntRect rect{{0,0},sprite_texture.frame_size}; |
|
|
|
auto sprite_texture = load_texture(name, is_icon); |
|
|
|
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect); |
|
|
|
auto bounds = sprite_texture.frame_size; |
|
|
|
|
|
|
|
|
|
|
|
sprite->setPosition({ |
|
|
|
|
|
|
|
float(cell.x + padding), |
|
|
|
|
|
|
|
float(cell.y + padding)}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
auto bounds = sprite->getLocalBounds(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sprite->setScale({ |
|
|
|
sf::IntRect rect{{0,0}, bounds}; |
|
|
|
float(cell.w - padding * 2) / bounds.size.x, |
|
|
|
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect); |
|
|
|
float(cell.h - padding * 2) / bounds.size.y}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { |
|
|
|
|
|
|
|
window.draw(*sprite, shader_ptr); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Icon::update(const string& new_name) { |
|
|
|
if(stretch) { |
|
|
|
if(new_name != name) { |
|
|
|
sprite->setScale({ |
|
|
|
name = new_name; |
|
|
|
float(cell.w - padding * 2) / float(bounds.x), |
|
|
|
auto sprite_texture = BACKEND->get_icon(name); |
|
|
|
float(cell.h - padding * 2) / float(bounds.y)}); |
|
|
|
sprite->setTexture(*sprite_texture.texture); |
|
|
|
} else { |
|
|
|
sprite->setTextureRect({{0,0},sprite_texture.frame_size}); |
|
|
|
float box_width = float(cell.w - padding * 2); |
|
|
|
|
|
|
|
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::init(lel::Cell &cell) { |
|
|
|
|
|
|
|
auto sprite_texture = BACKEND->get_icon(name); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sf::IntRect rect{{0,0},sprite_texture.frame_size}; |
|
|
|
|
|
|
|
fmt::println("ICON SIZE: {},{}; {},{}", |
|
|
|
|
|
|
|
rect.position.x, rect.position.y, |
|
|
|
|
|
|
|
rect.size.x, rect.size.y); |
|
|
|
|
|
|
|
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sprite->setPosition({ float(cell.x + padding), float(cell.y + padding)}); |
|
|
|
sfml_center_helper(sprite, cell, padding); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Icon::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { |
|
|
|
void Sprite::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { |
|
|
|
window.draw(*sprite, shader_ptr); |
|
|
|
window.draw(*sprite, shader_ptr); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Rectangle::init(lel::Cell& cell) { |
|
|
|
void Rectangle::init(lel::Cell& cell) { |
|
|
|
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; |
|
|
|
sf::Vector2f size{float(cell.w) - padding * 2, float(cell.h) - padding * 2}; |
|
|
|
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); |
|
|
|
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); |
|
|
|
shape->setPosition({float(cell.x + padding), float(cell.y + padding)}); |
|
|
|
|
|
|
|
shape->setFillColor(color); |
|
|
|
shape->setFillColor(color); |
|
|
|
shape->setOutlineColor(border_color); |
|
|
|
shape->setOutlineColor(border_color); |
|
|
|
shape->setOutlineThickness(border_px); |
|
|
|
shape->setOutlineThickness(border_px); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sfml_center_helper(shape, cell, padding); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Rectangle::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { |
|
|
|
void Rectangle::render(sf::RenderWindow& window, sf::Shader *shader_ptr) { |
|
|
|