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.
This commit is contained in:
parent
4c019048d0
commit
5d0d8b16fc
5 changed files with 29 additions and 41 deletions
|
@ -169,8 +169,8 @@ struct CalculatorUI {
|
|||
$gui.set<guecs::Text>(id, { label });
|
||||
wchar_t op = label[0];
|
||||
$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);
|
||||
}
|
||||
|
||||
void mouse(float x, float y, bool hover) {
|
||||
$gui.mouse(x, y, hover);
|
||||
void mouse(float x, float y, guecs::Modifiers mods) {
|
||||
$gui.mouse(x, y, mods);
|
||||
}
|
||||
|
||||
void handle_button(wchar_t op) {
|
||||
|
|
|
@ -94,7 +94,7 @@ struct ClickerUI {
|
|||
$gui.set<guecs::Effect>(id, {});
|
||||
$gui.set<guecs::Icon>(id, { "clicker_treat_bone" });
|
||||
$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::Sound>($clicker, {"clicker_bark"});
|
||||
$gui.set<guecs::Clickable>($clicker, {
|
||||
[&](auto, auto) { handle_button(Event::CLICKER); }
|
||||
[&](auto) { handle_button(Event::CLICKER); }
|
||||
});
|
||||
|
||||
// custom components need to be initialized manually
|
||||
|
@ -126,8 +126,8 @@ struct ClickerUI {
|
|||
// $gui.debug_layout(window);
|
||||
}
|
||||
|
||||
void mouse(float x, float y, bool hover) {
|
||||
$gui.mouse(x, y, hover);
|
||||
void mouse(float x, float y, guecs::Modifiers mods) {
|
||||
$gui.mouse(x, y, mods);
|
||||
}
|
||||
|
||||
void handle_button(Event ev) {
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include "guecs/theme.hpp"
|
||||
#include "guecs/sfml/components.hpp"
|
||||
#include <cassert>
|
||||
#include <bitset>
|
||||
|
||||
namespace guecs {
|
||||
using std::shared_ptr, std::wstring, std::string;
|
||||
|
@ -24,22 +25,25 @@ namespace guecs {
|
|||
std::queue<size_t> free_indices;
|
||||
};
|
||||
|
||||
enum class ModBit {
|
||||
hover=0,
|
||||
left=1,
|
||||
right=2
|
||||
};
|
||||
|
||||
using Modifiers = std::bitset<16>;
|
||||
|
||||
struct Clickable {
|
||||
/* 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.
|
||||
*/
|
||||
std::function<void(Entity ent, std::any data)> action;
|
||||
};
|
||||
|
||||
struct ActionData {
|
||||
std::any data;
|
||||
std::function<void(Modifiers mods)> action;
|
||||
};
|
||||
|
||||
struct CellName {
|
||||
string name;
|
||||
};
|
||||
|
||||
|
||||
class UI {
|
||||
public:
|
||||
Entity MAIN = 0;
|
||||
|
@ -69,9 +73,8 @@ namespace guecs {
|
|||
|
||||
void init();
|
||||
void render(sf::RenderWindow& window);
|
||||
bool mouse(float x, float y, bool hover);
|
||||
void click_on(const string& name, bool required=false);
|
||||
void click_on(Entity slot_id);
|
||||
bool mouse(float x, float y, Modifiers mods);
|
||||
void click_on(Entity slot_id, Modifiers mods);
|
||||
void debug_layout(sf::RenderWindow& window);
|
||||
|
||||
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
|
||||
|
||||
project('lel-guecs', 'cpp',
|
||||
version: '0.5.0',
|
||||
version: '0.6.0',
|
||||
default_options: [
|
||||
'cpp_std=c++20',
|
||||
'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;
|
||||
// BUG: Is Parser::hit useful?
|
||||
bool hover = mods.test(size_t(ModBit::hover));
|
||||
|
||||
query<lel::Cell>([&](auto ent, auto& cell) {
|
||||
if((x >= cell.x && x <= cell.x + cell.w) &&
|
||||
|
@ -169,11 +169,11 @@ namespace guecs {
|
|||
|
||||
if(hover) return;
|
||||
|
||||
click_on(ent);
|
||||
click_on(ent, mods);
|
||||
|
||||
action_count++;
|
||||
} else {
|
||||
do_if<Effect>(ent, [hover](auto& effect) {
|
||||
do_if<Effect>(ent, [](auto& effect) {
|
||||
effect.stop();
|
||||
});
|
||||
|
||||
|
@ -220,25 +220,10 @@ namespace guecs {
|
|||
}
|
||||
}
|
||||
|
||||
void UI::click_on(const string& name, bool required) {
|
||||
auto ent = entity(name);
|
||||
|
||||
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, {});
|
||||
}
|
||||
}
|
||||
void UI::click_on(Entity gui_id, Modifiers mods) {
|
||||
if(auto to_click = get_if<Clickable>(gui_id)) {
|
||||
to_click->action(mods);
|
||||
};
|
||||
}
|
||||
|
||||
wstring to_wstring(const string& str) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue