Removed hover on guecs::UI::mouse and now use a generic 16 bit modifier bitset. Also finally fixed Clickable so it just a simple callback with only the modifiers.

main
Zed A. Shaw 3 days ago
parent 4c019048d0
commit 5d0d8b16fc
  1. 6
      demos/calc.cpp
  2. 8
      demos/clicker_game.cpp
  3. 21
      include/guecs/ui.hpp
  4. 2
      meson.build
  5. 31
      src/guecs/ui.cpp

@ -169,7 +169,7 @@ 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) {

@ -94,7 +94,7 @@ struct ClickerUI {
$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, {
[&](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) {

@ -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,25 @@ namespace guecs {
std::queue<size_t> free_indices; std::queue<size_t> free_indices;
}; };
enum class ModBit {
hover=0,
left=1,
right=2
};
using Modifiers = std::bitset<16>;
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 +73,8 @@ 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(Entity slot_id, Modifiers mods);
void click_on(Entity slot_id);
void debug_layout(sf::RenderWindow& window); void debug_layout(sf::RenderWindow& window);
Entity entity() { return ++entity_count; } Entity entity() { return ++entity_count; }

@ -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',

@ -149,9 +149,9 @@ namespace guecs {
}); });
} }
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) &&
@ -169,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();
}); });
@ -220,25 +220,10 @@ namespace guecs {
} }
} }
void UI::click_on(const string& name, bool required) { void UI::click_on(Entity gui_id, Modifiers mods) {
auto ent = entity(name); if(auto to_click = get_if<Clickable>(gui_id)) {
to_click->action(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) {
if(auto clicked = get_if<Clickable>(ent)) {
if(auto action_data = get_if<ActionData>(ent)) {
clicked->action(ent, action_data->data);
} else {
clicked->action(ent, {});
}
}
} }
wstring to_wstring(const string& str) { wstring to_wstring(const string& str) {

Loading…
Cancel
Save