Compare commits

...

6 commits
v0.5.0 ... main

7 changed files with 74 additions and 40 deletions

View file

@ -169,8 +169,8 @@ struct CalculatorUI {
$gui.set<guecs::Text>(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) { handle_button(op); }
}); });
} }
} }
@ -182,8 +182,8 @@ struct CalculatorUI {
// $gui.debug_layout(window); // $gui.debug_layout(window);
} }
void mouse(float x, float y, bool hover) { void mouse(float x, float y, guecs::Modifiers mods) {
$gui.mouse(x, y, hover); $gui.mouse(x, y, mods);
} }
void handle_button(wchar_t op) { void handle_button(wchar_t op) {

View file

@ -92,9 +92,9 @@ struct ClickerUI {
auto id = $gui.entity(name); auto id = $gui.entity(name);
if(name != "clicker") { if(name != "clicker") {
$gui.set<guecs::Effect>(id, {}); $gui.set<guecs::Effect>(id, {});
$gui.set<guecs::Sprite>(id, { "clicker_treat_bone" }); $gui.set<guecs::Icon>(id, { "clicker_treat_bone" });
$gui.set<guecs::Clickable>(id, { $gui.set<guecs::Clickable>(id, {
[&](auto, auto) { handle_button(Event::A_BUTTON); } [&](auto) { handle_button(Event::A_BUTTON); }
}); });
} }
} }
@ -103,7 +103,7 @@ struct ClickerUI {
$gui.set<guecs::Sprite>($clicker, {"clicker_the_dog"}); $gui.set<guecs::Sprite>($clicker, {"clicker_the_dog"});
$gui.set<guecs::Sound>($clicker, {"clicker_bark"}); $gui.set<guecs::Sound>($clicker, {"clicker_bark"});
$gui.set<guecs::Clickable>($clicker, { $gui.set<guecs::Clickable>($clicker, {
[&](auto, auto) { handle_button(Event::CLICKER); } [&](auto) { handle_button(Event::CLICKER); }
}); });
// custom components need to be initialized manually // custom components need to be initialized manually
@ -126,8 +126,8 @@ struct ClickerUI {
// $gui.debug_layout(window); // $gui.debug_layout(window);
} }
void mouse(float x, float y, bool hover) { void mouse(float x, float y, guecs::Modifiers mods) {
$gui.mouse(x, y, hover); $gui.mouse(x, y, mods);
} }
void handle_button(Event ev) { void handle_button(Event ev) {

View file

@ -36,6 +36,20 @@ namespace guecs {
void render(sf::RenderWindow& window, sf::Shader *shader_ptr); 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() {
stretch = false;
is_icon = true;
};
};
struct Rectangle { struct Rectangle {
int padding = THEME.PADDING; int padding = THEME.PADDING;
sf::Color color = THEME.FILL_COLOR; sf::Color color = THEME.FILL_COLOR;

View file

@ -10,6 +10,7 @@
#include "guecs/theme.hpp" #include "guecs/theme.hpp"
#include "guecs/sfml/components.hpp" #include "guecs/sfml/components.hpp"
#include <cassert> #include <cassert>
#include <bitset>
namespace guecs { namespace guecs {
using std::shared_ptr, std::wstring, std::string; using std::shared_ptr, std::wstring, std::string;
@ -24,22 +25,30 @@ namespace guecs {
std::queue<size_t> free_indices; std::queue<size_t> free_indices;
}; };
namespace ModBit {
enum {
NONE=0,
hover=1,
left=2,
right=3
};
}
using Modifiers = std::bitset<16>;
constexpr const Modifiers NO_MODS{0};
struct Clickable { struct Clickable {
/* This is actually called by UI::mouse and passed the entity ID of the /* This is actually called by UI::mouse and passed the entity ID of the
* button pressed so you can interact with it in the event handler. * button pressed so you can interact with it in the event handler.
*/ */
std::function<void(Entity ent, std::any data)> action; std::function<void(Modifiers mods)> action;
};
struct ActionData {
std::any data;
}; };
struct CellName { struct CellName {
string name; string name;
}; };
class UI { class UI {
public: public:
Entity MAIN = 0; Entity MAIN = 0;
@ -69,9 +78,9 @@ namespace guecs {
void init(); void init();
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, Modifiers mods);
void click_on(const string& name, bool required=false); void click_on(const std::string& name, Modifiers mods=NO_MODS);
void click_on(Entity slot_id); void click_on(Entity slot_id, Modifiers mods=NO_MODS);
void debug_layout(sf::RenderWindow& window); void debug_layout(sf::RenderWindow& window);
Entity entity() { return ++entity_count; } Entity entity() { return ++entity_count; }
@ -218,6 +227,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);
}; };

View file

@ -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.5.0', version: '0.6.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',

View file

@ -97,6 +97,8 @@ namespace guecs {
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) {

View file

@ -89,6 +89,10 @@ namespace guecs {
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) { query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
sprite.init(cell); sprite.init(cell);
}); });
query<lel::Cell, Icon>([&](auto, auto &cell, auto &icon) {
icon.init(cell);
});
} }
void UI::debug_layout(sf::RenderWindow& window) { void UI::debug_layout(sf::RenderWindow& window) {
@ -134,15 +138,20 @@ namespace guecs {
sprite.render(window, shader_ptr); sprite.render(window, shader_ptr);
}); });
query<Icon>([&](auto ent, auto& icon) {
auto shader_ptr = find_shader(ent, false);
icon.render(window, shader_ptr);
});
query<Text>([&](auto ent, auto& text) { query<Text>([&](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);
}); });
} }
bool UI::mouse(float x, float y, bool hover) { bool UI::mouse(float x, float y, Modifiers mods) {
int action_count = 0; int action_count = 0;
// BUG: Is Parser::hit useful? bool hover = mods.test(size_t(ModBit::hover));
query<lel::Cell>([&](auto ent, auto& cell) { query<lel::Cell>([&](auto ent, auto& cell) {
if((x >= cell.x && x <= cell.x + cell.w) && if((x >= cell.x && x <= cell.x + cell.w) &&
@ -160,11 +169,11 @@ namespace guecs {
if(hover) return; if(hover) return;
click_on(ent); click_on(ent, mods);
action_count++; action_count++;
} else { } else {
do_if<Effect>(ent, [hover](auto& effect) { do_if<Effect>(ent, [](auto& effect) {
effect.stop(); effect.stop();
}); });
@ -188,6 +197,16 @@ 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);
@ -201,25 +220,14 @@ namespace guecs {
} }
} }
void UI::click_on(const string& name, bool required) { void UI::click_on(const std::string& name, Modifiers mods) {
auto ent = entity(name); click_on(entity(name), mods);
if(required) {
assert(has<Clickable>(ent) &&
"click_on required '{}' to exist but it doesn't");
}
click_on(ent);
} }
void UI::click_on(Entity ent) { void UI::click_on(Entity gui_id, Modifiers mods) {
if(auto clicked = get_if<Clickable>(ent)) { if(auto to_click = get_if<Clickable>(gui_id)) {
if(auto action_data = get_if<ActionData>(ent)) { to_click->action(mods);
clicked->action(ent, action_data->data); };
} else {
clicked->action(ent, {});
}
}
} }
wstring to_wstring(const string& str) { wstring to_wstring(const string& str) {