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
|
@ -141,12 +141,12 @@ struct CalculatorUI {
|
|||
$gui.layout(
|
||||
"[*%(400)stack |_|_|_]"
|
||||
"[*%(400)readout|_|_|_]"
|
||||
"[push|pop|clear|eq]"
|
||||
"[add |sub|mul |div]"
|
||||
"[btn7|btn8|btn9]"
|
||||
"[btn4|btn5|btn6]"
|
||||
"[btn1|btn2|btn3]"
|
||||
"[neg |btn0|del]");
|
||||
"[=push|=pop|=clear|=eq]"
|
||||
"[=add |=sub|=mul |=div]"
|
||||
"[=btn7|=btn8|=btn9]"
|
||||
"[=btn4|=btn5|=btn6]"
|
||||
"[=btn1|=btn2|=btn3]"
|
||||
"[=neg |=btn0|=del]");
|
||||
}
|
||||
|
||||
void init() {
|
||||
|
@ -161,12 +161,12 @@ struct CalculatorUI {
|
|||
$gui.set<guecs::Rectangle>(id, {});
|
||||
|
||||
if(name == "readout") {
|
||||
$gui.set<guecs::Textual>(id, {L"", 40});
|
||||
$gui.set<guecs::Text>(id, {L"", 40});
|
||||
} else if(name == "stack") {
|
||||
$gui.set<guecs::Textual>(id, {L"", 20});
|
||||
$gui.set<guecs::Text>(id, {L"", 20});
|
||||
} else {
|
||||
$gui.set<guecs::Effect>(id, {});
|
||||
$gui.set<guecs::Label>(id, { label });
|
||||
$gui.set<guecs::Text>(id, { label });
|
||||
wchar_t op = label[0];
|
||||
$gui.set<guecs::Clickable>(id, {
|
||||
[&, op](auto, auto) { handle_button(op); }
|
||||
|
|
|
@ -77,7 +77,7 @@ struct ClickerUI {
|
|||
"[_|_ |_|_|_]"
|
||||
"[_|_ |_|_|_]"
|
||||
"[_|_ |_|_|_]"
|
||||
"[a1|a2|a3|a4|a5]");
|
||||
"[(30,30)a1|=(30,30)a2|a3|=a4|a5]");
|
||||
}
|
||||
|
||||
void init() {
|
||||
|
@ -91,9 +91,9 @@ struct ClickerUI {
|
|||
for(auto& [name, cell] : $gui.cells()) {
|
||||
auto id = $gui.entity(name);
|
||||
if(name != "clicker") {
|
||||
$gui.set<guecs::Rectangle>(id, {});
|
||||
$gui.set<guecs::Rectangle>(id, {0, {255, 0, 0, 255}});
|
||||
$gui.set<guecs::Effect>(id, {});
|
||||
$gui.set<guecs::Sprite>(id, { "clicker_treat_bone" });
|
||||
// $gui.set<guecs::Sprite>(id, { "clicker_treat_bone" });
|
||||
$gui.set<guecs::Clickable>(id, {
|
||||
[&](auto, auto) { handle_button(Event::A_BUTTON); }
|
||||
});
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
namespace guecs {
|
||||
using std::shared_ptr, std::wstring, std::string;
|
||||
|
||||
struct Textual {
|
||||
struct Text {
|
||||
std::wstring content;
|
||||
unsigned int size = THEME.TEXT_SIZE;
|
||||
sf::Color color = THEME.TEXT_COLOR;
|
||||
|
@ -24,19 +24,6 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||
};
|
||||
|
||||
struct Label : public Textual {
|
||||
template<typename... Args>
|
||||
Label(Args... args) : Textual(args...)
|
||||
{
|
||||
centered = true;
|
||||
size = THEME.LABEL_SIZE;
|
||||
}
|
||||
|
||||
Label() {
|
||||
centered = true;
|
||||
};
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
string name;
|
||||
int padding = THEME.PADDING;
|
||||
|
@ -49,20 +36,6 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window, sf::Shader *shader_ptr);
|
||||
};
|
||||
|
||||
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 {
|
||||
int padding = THEME.PADDING;
|
||||
sf::Color color = THEME.FILL_COLOR;
|
||||
|
|
|
@ -50,6 +50,7 @@ namespace lel {
|
|||
int cell_height = grid_h / rows;
|
||||
|
||||
for(auto& row : grid) {
|
||||
// BUG: see issue #16
|
||||
size_t columns = row.size();
|
||||
int cell_width = grid_w / columns;
|
||||
assert(cell_width > 0 && "invalid cell width calc");
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -82,21 +82,13 @@ namespace guecs {
|
|||
}
|
||||
});
|
||||
|
||||
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
|
||||
query<lel::Cell, Text>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
||||
sprite.init(cell);
|
||||
});
|
||||
|
||||
query<lel::Cell, Icon>([&](auto, auto &cell, auto &icon) {
|
||||
icon.init(cell);
|
||||
});
|
||||
}
|
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) {
|
||||
|
@ -142,17 +134,7 @@ namespace guecs {
|
|||
sprite.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
query<Icon>([&](auto ent, auto& icon) {
|
||||
auto shader_ptr = find_shader(ent, false);
|
||||
icon.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
query<Label>([&](auto ent, auto& text) {
|
||||
auto shader_ptr = find_shader(ent, false);
|
||||
text.render(window, shader_ptr);
|
||||
});
|
||||
|
||||
query<Textual>([&](auto ent, auto& text) {
|
||||
query<Text>([&](auto ent, auto& text) {
|
||||
auto shader_ptr = find_shader(ent, false);
|
||||
text.render(window, shader_ptr);
|
||||
});
|
||||
|
@ -209,13 +191,13 @@ namespace guecs {
|
|||
void UI::show_text(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto tc = get_if<Textual>(ent)) {
|
||||
if(auto tc = get_if<Text>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Textual to_set{content, THEME.TEXT_SIZE};
|
||||
Text to_set{content, THEME.TEXT_SIZE};
|
||||
to_set.init(cell, $font);
|
||||
set<Textual>(ent, to_set);
|
||||
set<Text>(ent, to_set);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -240,20 +222,6 @@ namespace guecs {
|
|||
}
|
||||
}
|
||||
|
||||
void UI::show_label(const string& region, const wstring& content) {
|
||||
auto ent = entity(region);
|
||||
|
||||
if(auto tc = get_if<Label>(ent)) {
|
||||
tc->text->setString(content);
|
||||
} else {
|
||||
auto &cell = cell_for(ent);
|
||||
Label to_set{content, THEME.LABEL_SIZE};
|
||||
to_set.init(cell, $font);
|
||||
to_set.text->setFillColor(THEME.TEXT_COLOR);
|
||||
set<Label>(ent, to_set);
|
||||
}
|
||||
}
|
||||
|
||||
wstring to_wstring(const string& str) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
|
||||
return $converter.from_bytes(str);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue