Compare commits

...

9 Commits
v0.4.0 ... main

  1. 18
      demos/calc.cpp
  2. 18
      demos/clicker_game.cpp
  3. 35
      include/guecs/sfml/components.hpp
  4. 1
      include/guecs/ui.hpp
  5. 2
      meson.build
  6. 1
      src/guecs/lel.cpp
  7. 84
      src/guecs/sfml/components.cpp
  8. 43
      src/guecs/ui.cpp

@ -141,12 +141,12 @@ struct CalculatorUI {
$gui.layout( $gui.layout(
"[*%(400)stack |_|_|_]" "[*%(400)stack |_|_|_]"
"[*%(400)readout|_|_|_]" "[*%(400)readout|_|_|_]"
"[push|pop|clear|eq]" "[=push|=pop|=clear|=eq]"
"[add |sub|mul |div]" "[=add |=sub|=mul |=div]"
"[btn7|btn8|btn9]" "[=btn7|=btn8|=btn9]"
"[btn4|btn5|btn6]" "[=btn4|=btn5|=btn6]"
"[btn1|btn2|btn3]" "[=btn1|=btn2|=btn3]"
"[neg |btn0|del]"); "[=neg |=btn0|=del]");
} }
void init() { void init() {
@ -161,12 +161,12 @@ struct CalculatorUI {
$gui.set<guecs::Rectangle>(id, {}); $gui.set<guecs::Rectangle>(id, {});
if(name == "readout") { if(name == "readout") {
$gui.set<guecs::Textual>(id, {L"", 40}); $gui.set<guecs::Text>(id, {L"", 40});
} else if(name == "stack") { } else if(name == "stack") {
$gui.set<guecs::Textual>(id, {L"", 20}); $gui.set<guecs::Text>(id, {L"", 20});
} else { } else {
$gui.set<guecs::Effect>(id, {}); $gui.set<guecs::Effect>(id, {});
$gui.set<guecs::Label>(id, { label }); $gui.set<guecs::Text>(id, { label });
wchar_t op = label[0]; wchar_t op = label[0];
$gui.set<guecs::Clickable>(id, { $gui.set<guecs::Clickable>(id, {
[&, op](auto, auto) { handle_button(op); } [&, op](auto, auto) { handle_button(op); }

@ -73,7 +73,7 @@ struct ClickerUI {
ClickerUI() { ClickerUI() {
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); $gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
$gui.layout( $gui.layout(
"[_|*%(300,400)clicker|_|_|_]" "[_|=*%(300,400)clicker|_|_|_]"
"[_|_ |_|_|_]" "[_|_ |_|_|_]"
"[_|_ |_|_|_]" "[_|_ |_|_|_]"
"[_|_ |_|_|_]" "[_|_ |_|_|_]"
@ -91,7 +91,6 @@ struct ClickerUI {
for(auto& [name, cell] : $gui.cells()) { for(auto& [name, cell] : $gui.cells()) {
auto id = $gui.entity(name); auto id = $gui.entity(name);
if(name != "clicker") { if(name != "clicker") {
$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::Icon>(id, { "clicker_treat_bone" });
$gui.set<guecs::Clickable>(id, { $gui.set<guecs::Clickable>(id, {
@ -135,15 +134,14 @@ struct ClickerUI {
using enum Event; using enum Event;
switch(ev) { switch(ev) {
case CLICKER: { case CLICKER: {
auto& shaker = $gui.get<Shake>($clicker); auto& shaker = $gui.get<Shake>($clicker);
auto& sprite = $gui.get<guecs::Sprite>($clicker); auto& sprite = $gui.get<guecs::Sprite>($clicker);
shaker.play(sprite); shaker.play(sprite);
fmt::println("CLICKER LOVES YOU!"); fmt::println("CLICKER LOVES YOU!");
} break; } break;
case A_BUTTON: case A_BUTTON:
fmt::println("a button clicked"); fmt::println("a button clicked");
break; break;
default: default:
assert(false && "invalid event"); assert(false && "invalid event");
} }

@ -10,7 +10,7 @@
namespace guecs { namespace guecs {
using std::shared_ptr, std::wstring, std::string; using std::shared_ptr, std::wstring, std::string;
struct Textual { struct Text {
std::wstring content; std::wstring content;
unsigned int size = THEME.TEXT_SIZE; unsigned int size = THEME.TEXT_SIZE;
sf::Color color = THEME.TEXT_COLOR; sf::Color color = THEME.TEXT_COLOR;
@ -24,22 +24,11 @@ namespace guecs {
void render(sf::RenderWindow& window, sf::Shader *shader_ptr); 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 { struct Sprite {
string name; string name;
int padding = THEME.PADDING; int padding = THEME.PADDING;
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);
@ -47,14 +36,18 @@ 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; template<typename... Args>
int padding = THEME.PADDING; Icon(Args... args) : Sprite(args...)
std::shared_ptr<sf::Sprite> sprite = nullptr; {
stretch = false;
is_icon = true;
}
void init(lel::Cell &cell); Icon() {
void update(const string& new_name); stretch = false;
void render(sf::RenderWindow& window, sf::Shader *shader_ptr); is_icon = true;
};
}; };
struct Rectangle { struct Rectangle {

@ -218,6 +218,7 @@ namespace guecs {
sf::Shader* find_shader(Entity ent, bool is_shape); sf::Shader* find_shader(Entity ent, bool is_shape);
void show_sprite(const string& region, const string& sprite_name); void show_sprite(const string& region, const string& sprite_name);
void show_icon(const string& region, const string& sprite_name);
void show_text(const string& region, const wstring& content); void show_text(const string& region, const wstring& content);
void show_label(const string& region, const wstring& content); void show_label(const string& region, const wstring& content);
}; };

@ -3,7 +3,7 @@
# HEY BUG: when you have a . spec in a LEL it doesn't work on text # HEY BUG: when you have a . spec in a LEL it doesn't work on text
project('lel-guecs', 'cpp', project('lel-guecs', 'cpp',
version: '0.4.0', version: '0.5.0',
default_options: [ default_options: [
'cpp_std=c++20', 'cpp_std=c++20',
'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1', 'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1',

@ -50,6 +50,7 @@ namespace lel {
int cell_height = grid_h / rows; int cell_height = grid_h / rows;
for(auto& row : grid) { for(auto& row : grid) {
// BUG: see issue #16
size_t columns = row.size(); size_t columns = row.size();
int cell_width = grid_w / columns; int cell_width = grid_w / columns;
assert(cell_width > 0 && "invalid cell width calc"); assert(cell_width > 0 && "invalid cell width calc");

@ -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) {

@ -82,11 +82,7 @@ namespace guecs {
} }
}); });
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) { query<lel::Cell, Text>([this](auto, auto& cell, auto& text) {
text.init(cell, $font);
});
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
text.init(cell, $font); text.init(cell, $font);
}); });
@ -147,12 +143,7 @@ namespace guecs {
icon.render(window, shader_ptr); icon.render(window, shader_ptr);
}); });
query<Label>([&](auto ent, auto& text) { query<Text>([&](auto ent, auto& text) {
auto shader_ptr = find_shader(ent, false);
text.render(window, shader_ptr);
});
query<Textual>([&](auto ent, auto& text) {
auto shader_ptr = find_shader(ent, false); auto shader_ptr = find_shader(ent, false);
text.render(window, shader_ptr); text.render(window, shader_ptr);
}); });
@ -206,16 +197,26 @@ namespace guecs {
} }
} }
void UI::show_icon(const string& region, const string& sprite_name) {
auto ent = entity(region);
if(auto sprite = get_if<Icon>(ent)) {
sprite->update(sprite_name);
} else {
set_init<Icon>(ent, {sprite_name});
}
}
void UI::show_text(const string& region, const wstring& content) { void UI::show_text(const string& region, const wstring& content) {
auto ent = entity(region); auto ent = entity(region);
if(auto tc = get_if<Textual>(ent)) { if(auto tc = get_if<Text>(ent)) {
tc->text->setString(content); tc->text->setString(content);
} else { } else {
auto &cell = cell_for(ent); auto &cell = cell_for(ent);
Textual to_set{content, THEME.TEXT_SIZE}; Text to_set{content, THEME.TEXT_SIZE};
to_set.init(cell, $font); to_set.init(cell, $font);
set<Textual>(ent, to_set); set<Text>(ent, to_set);
} }
} }
@ -240,20 +241,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) { wstring to_wstring(const string& str) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter; std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
return $converter.from_bytes(str); return $converter.from_bytes(str);

Loading…
Cancel
Save