Compare commits

...

9 Commits
v0.4.0 ... main

  1. 18
      demos/calc.cpp
  2. 4
      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. 82
      src/guecs/sfml/components.cpp
  8. 43
      src/guecs/ui.cpp

@ -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); }

@ -73,7 +73,7 @@ struct ClickerUI {
ClickerUI() {
$gui.position(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
$gui.layout(
"[_|*%(300,400)clicker|_|_|_]"
"[_|=*%(300,400)clicker|_|_|_]"
"[_|_ |_|_|_]"
"[_|_ |_|_|_]"
"[_|_ |_|_|_]"
@ -91,7 +91,6 @@ struct ClickerUI {
for(auto& [name, cell] : $gui.cells()) {
auto id = $gui.entity(name);
if(name != "clicker") {
$gui.set<guecs::Rectangle>(id, {});
$gui.set<guecs::Effect>(id, {});
$gui.set<guecs::Icon>(id, { "clicker_treat_bone" });
$gui.set<guecs::Clickable>(id, {
@ -143,7 +142,6 @@ struct ClickerUI {
case A_BUTTON:
fmt::println("a button clicked");
break;
default:
assert(false && "invalid event");
}

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

@ -218,6 +218,7 @@ namespace guecs {
sf::Shader* find_shader(Entity ent, bool is_shape);
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_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
project('lel-guecs', 'cpp',
version: '0.4.0',
version: '0.5.0',
default_options: [
'cpp_std=c++20',
'cpp_args=-D_GLIBCXX_DEBUG=1 -D_GLIBCXX_DEBUG_PEDANTIC=1',

@ -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,13 +7,34 @@
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);
text->setFillColor(color);
if(centered) {
if(centered || cell.center) {
auto bounds = text->getLocalBounds();
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
@ -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);
}
@ -44,59 +65,40 @@ namespace guecs {
}
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};
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
auto sprite_texture = load_texture(name, is_icon);
auto bounds = sprite_texture.frame_size;
sprite->setPosition({
float(cell.x + padding),
float(cell.y + padding)});
auto bounds = sprite->getLocalBounds();
sf::IntRect rect{{0,0}, bounds};
sprite = make_shared<sf::Sprite>(*sprite_texture.texture, rect);
if(stretch) {
sprite->setScale({
float(cell.w - padding * 2) / bounds.size.x,
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(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});
}
float(cell.w - padding * 2) / float(bounds.x),
float(cell.h - padding * 2) / float(bounds.y)});
} else {
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);
}
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);
sfml_center_helper(shape, cell, padding);
}
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) {
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);
});
@ -147,12 +143,7 @@ namespace guecs {
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);
});
@ -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) {
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 +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) {
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;
return $converter.from_bytes(str);

Loading…
Cancel
Save