Refactored the mouse handling to use the new guecss Modifiers system and improved Clickable.

master
Zed A. Shaw 20 hours ago
parent ad0069e899
commit a86912705c
  1. 10
      gui/boss_fight_ui.cpp
  2. 2
      gui/boss_fight_ui.hpp
  3. 8
      gui/combat_ui.cpp
  4. 2
      gui/combat_ui.hpp
  5. 8
      gui/debug_ui.cpp
  6. 2
      gui/debug_ui.hpp
  7. 34
      gui/fsm.cpp
  8. 2
      gui/fsm.hpp
  9. 11
      gui/guecstra.cpp
  10. 4
      gui/guecstra.hpp
  11. 8
      gui/loot_ui.cpp
  12. 2
      gui/loot_ui.hpp
  13. 4
      gui/main_ui.cpp
  14. 2
      gui/main_ui.hpp
  15. 4
      gui/overlay_ui.cpp
  16. 10
      gui/ritual_ui.cpp
  17. 2
      gui/ritual_ui.hpp
  18. 12
      gui/status_ui.cpp
  19. 2
      gui/status_ui.hpp
  20. 2
      wraps/lel-guecs.wrap

@ -68,7 +68,7 @@ namespace gui {
auto button = $status.entity(name); auto button = $status.entity(name);
$status.set<Rectangle>(button, {}); $status.set<Rectangle>(button, {});
$status.set<Clickable>(button, { $status.set<Clickable>(button, {
[this, name](auto, auto){ [this, name](auto){
dbc::log(fmt::format("STATUS: {}", name)); dbc::log(fmt::format("STATUS: {}", name));
} }
}); });
@ -83,7 +83,7 @@ namespace gui {
for(auto& [name, cell] : $overlay.cells()) { for(auto& [name, cell] : $overlay.cells()) {
auto region = $overlay.entity(name); auto region = $overlay.entity(name);
$overlay.set<Clickable>(region, { $overlay.set<Clickable>(region, {
[this, name](auto, auto){ [this, name](auto){
dbc::log(fmt::format("OVERLAY: {}", name)); dbc::log(fmt::format("OVERLAY: {}", name));
} }
}); });
@ -139,12 +139,12 @@ namespace gui {
$overlay.render(window); $overlay.render(window);
} }
bool BossFightUI::mouse(float x, float y, bool hover) { bool BossFightUI::mouse(float x, float y, guecs::Modifiers mods) {
if($status.mouse(x, y, hover)) { if($status.mouse(x, y, mods)) {
dbc::log("STATUS button pressed"); dbc::log("STATUS button pressed");
} }
if($overlay.mouse(x, y, hover)) { if($overlay.mouse(x, y, mods)) {
$animation.play(); $animation.play();
sound::play("Sword_Hit_1"); sound::play("Sword_Hit_1");
$boss_hit = !$boss_hit; $boss_hit = !$boss_hit;

@ -38,7 +38,7 @@ namespace gui {
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, guecs::Modifiers mods);
void bounce_boss(sf::RenderWindow& window); void bounce_boss(sf::RenderWindow& window);
bool boss_dead() { return $combat.hp < 0; } bool boss_dead() { return $combat.hp < 0; }
void configure_sprite(); void configure_sprite();

@ -31,7 +31,7 @@ namespace gui {
$gui.set<Sound>(button, {sound}); $gui.set<Sound>(button, {sound});
$gui.set<Effect>(button, {.duration=0.5f, .name=effect_name}); $gui.set<Effect>(button, {.duration=0.5f, .name=effect_name});
$gui.set<Clickable>(button, $gui.set<Clickable>(button,
guecs::make_action($level, event, {action})); guecs::make_action($level, button, event, {action}));
return button; return button;
} }
@ -67,7 +67,7 @@ namespace gui {
auto hp_gauge = $gui.entity("hp_gauge"); auto hp_gauge = $gui.entity("hp_gauge");
$gui.set<Sprite>(hp_gauge, {"stone_doll_cursed"}); $gui.set<Sprite>(hp_gauge, {"stone_doll_cursed"});
$gui.set<Clickable>(hp_gauge, $gui.set<Clickable>(hp_gauge,
guecs::make_action($level, Events::GUI::HP_STATUS, {})); guecs::make_action($level, hp_gauge, Events::GUI::HP_STATUS, {}));
$gui.init(); $gui.init();
} }
@ -81,7 +81,7 @@ namespace gui {
init(); init();
} }
bool CombatUI::mouse(float x, float y, bool hover) { bool CombatUI::mouse(float x, float y, guecs::Modifiers mods) {
return $gui.mouse(x, y, hover); return $gui.mouse(x, y, mods);
} }
} }

@ -16,7 +16,7 @@ namespace gui {
void init(); void init();
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
void update_level(GameLevel &level); void update_level(GameLevel &level);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, guecs::Modifiers mods);
guecs::Entity make_button(std::string name, Events::GUI event, guecs::Entity make_button(std::string name, Events::GUI event,
int action, const std::string &icon_name, int action, const std::string &icon_name,
const std::string &sound, const std::string &effect_name); const std::string &sound, const std::string &effect_name);

@ -35,7 +35,9 @@ namespace gui {
void DebugUI::add_spawn_button(std::string enemy_key, std::string sprite_name, std::string region) { void DebugUI::add_spawn_button(std::string enemy_key, std::string sprite_name, std::string region) {
auto button = $gui.entity(region); auto button = $gui.entity(region);
$gui.set<guecs::Clickable>(button, { [this, enemy_key](auto, auto){ spawn(enemy_key); } }); $gui.set<guecs::Clickable>(button, {
[this, enemy_key](auto){ spawn(enemy_key); }
});
$gui.set<guecs::Sprite>(button, { sprite_name}); $gui.set<guecs::Sprite>(button, { sprite_name});
} }
@ -91,8 +93,8 @@ namespace gui {
} }
} }
bool DebugUI::mouse(float x, float y, bool hover) { bool DebugUI::mouse(float x, float y, guecs::Modifiers mods) {
return $gui.mouse(x, y, hover); return $gui.mouse(x, y, mods);
} }
Stats::TimeBullshit DebugUI::time_start() { Stats::TimeBullshit DebugUI::time_start() {

@ -17,7 +17,7 @@ namespace gui {
void init(lel::Cell cell); void init(lel::Cell cell);
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, guecs::Modifiers mods);
void debug(); void debug();
void spawn(const std::string& enemy_key); void spawn(const std::string& enemy_key);
void add_spawn_button(std::string enemy_key, std::string sprite_name, std::string region); void add_spawn_button(std::string enemy_key, std::string sprite_name, std::string region);

@ -124,7 +124,7 @@ namespace gui {
case MOUSE_DRAG_START: case MOUSE_DRAG_START:
case MOUSE_CLICK: case MOUSE_CLICK:
case MOUSE_DROP: case MOUSE_DROP:
mouse_action(false); mouse_action(guecs::NO_MODS);
break; break;
default: default:
if(!$dnd_loot.event(ev, data)) { if(!$dnd_loot.event(ev, data)) {
@ -202,11 +202,11 @@ namespace gui {
} break; } break;
case MOUSE_CLICK: case MOUSE_CLICK:
fmt::println("CLICK: {}", $router.left_button); fmt::println("CLICK: {}", $router.left_button);
mouse_action(false); mouse_action(guecs::NO_MODS);
break;
case MOUSE_MOVE:
mouse_action(true);
break; break;
case MOUSE_MOVE: {
mouse_action({1 << guecs::ModBit::hover});
} break;
case AIM_CLICK: case AIM_CLICK:
System::pickup($level); System::pickup($level);
break; break;
@ -226,7 +226,7 @@ namespace gui {
break; break;
case MOUSE_CLICK: { case MOUSE_CLICK: {
sf::Vector2f pos = mouse_position(); sf::Vector2f pos = mouse_position();
$boss_fight_ui->mouse(pos.x, pos.y, false); $boss_fight_ui->mouse(pos.x, pos.y, guecs::NO_MODS);
if($boss_fight_ui->boss_dead()) { if($boss_fight_ui->boss_dead()) {
event(Event::STAIRS_DOWN); event(Event::STAIRS_DOWN);
@ -242,11 +242,11 @@ namespace gui {
switch(ev) { switch(ev) {
case MOUSE_CLICK: case MOUSE_CLICK:
mouse_action(false); mouse_action(guecs::NO_MODS);
break;
case MOUSE_MOVE:
mouse_action(true);
break; break;
case MOUSE_MOVE: {
mouse_action({1 << guecs::ModBit::hover});
} break;
case ATTACK: case ATTACK:
$main_ui.dirty(); $main_ui.dirty();
sound::play("Sword_Hit_1"); sound::play("Sword_Hit_1");
@ -295,16 +295,16 @@ namespace gui {
return $window.mapPixelToCoords($router.position); return $window.mapPixelToCoords($router.position);
} }
void FSM::mouse_action(bool hover) { void FSM::mouse_action(guecs::Modifiers mods) {
sf::Vector2f pos = mouse_position(); sf::Vector2f pos = mouse_position();
if($debug_ui.active) $debug_ui.mouse(pos.x, pos.y, hover); if($debug_ui.active) $debug_ui.mouse(pos.x, pos.y, mods);
$combat_ui.mouse(pos.x, pos.y, hover); $combat_ui.mouse(pos.x, pos.y, mods);
$status_ui.mouse(pos.x, pos.y, hover); $status_ui.mouse(pos.x, pos.y, mods);
if($loot_ui.active) { if($loot_ui.active) {
$loot_ui.mouse(pos.x, pos.y, hover); $loot_ui.mouse(pos.x, pos.y, mods);
} else { } else {
$main_ui.mouse(pos.x, pos.y, hover); $main_ui.mouse(pos.x, pos.y, mods);
} }
} }
@ -444,6 +444,8 @@ namespace gui {
auto [evt, entity, data] = world.recv<eGUI>(); auto [evt, entity, data] = world.recv<eGUI>();
auto player = world.get_the<Player>(); auto player = world.get_the<Player>();
// HERE: this has to go, unify these events and just use them in the state machine directly
switch(evt) { switch(evt) {
case eGUI::COMBAT: { case eGUI::COMBAT: {
auto &damage = std::any_cast<Events::Combat&>(data); auto &damage = std::any_cast<Events::Combat&>(data);

@ -70,7 +70,7 @@ namespace gui {
void try_move(int dir, bool strafe); void try_move(int dir, bool strafe);
sf::Vector2f mouse_position(); sf::Vector2f mouse_position();
void mouse_action(bool hover); void mouse_action(guecs::Modifiers mods);
void handle_keyboard_mouse(); void handle_keyboard_mouse();
void draw_gui(); void draw_gui();
void render(); void render();

@ -2,15 +2,14 @@
namespace guecs { namespace guecs {
Clickable make_action(GameLevel& target, Events::GUI event) { Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event) {
return {[&, event](auto gui_id, auto data){ return {[&, gui_id, event](auto){
// BUG: either get rid of gui_id or also send a reference the the $gui that is sending the event target.world->send<Events::GUI>(event, gui_id, {});
target.world->send<Events::GUI>(event, gui_id, data);
}}; }};
} }
Clickable make_action(GameLevel& target, Events::GUI event, std::any data) { Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event, std::any data) {
return {[&, event, data](auto gui_id, auto){ return {[&, event, data](auto){
target.world->send<Events::GUI>(event, gui_id, data); target.world->send<Events::GUI>(event, gui_id, data);
}}; }};
} }

@ -6,8 +6,8 @@
#include "levelmanager.hpp" #include "levelmanager.hpp"
namespace guecs { namespace guecs {
Clickable make_action(GameLevel& target, Events::GUI event); Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event);
Clickable make_action(GameLevel& target, Events::GUI event, std::any data); Clickable make_action(GameLevel& target, guecs::Entity gui_id, Events::GUI event, std::any data);
struct GrabSource { struct GrabSource {
DinkyECS::Entity world_entity; DinkyECS::Entity world_entity;

@ -31,7 +31,7 @@ namespace gui {
$gui.set<guecs::Rectangle>(button, {}); $gui.set<guecs::Rectangle>(button, {});
$gui.set<guecs::Text>(button, {label}); $gui.set<guecs::Text>(button, {label});
$gui.set<guecs::Clickable>(button, $gui.set<guecs::Clickable>(button,
guecs::make_action($level, event)); guecs::make_action($level, button, event));
} }
void LootUI::init() { void LootUI::init() {
@ -52,7 +52,7 @@ namespace gui {
THEME.TRANSPARENT, THEME.LIGHT_MID }); THEME.TRANSPARENT, THEME.LIGHT_MID });
$gui.set<guecs::Effect>(id, {0.4f, "ui_shader"}); $gui.set<guecs::Effect>(id, {0.4f, "ui_shader"});
$gui.set<guecs::Clickable>(id, { $gui.set<guecs::Clickable>(id, {
guecs::make_action($level, Events::GUI::LOOT_SELECT, {id}) guecs::make_action($level, id, Events::GUI::LOOT_SELECT, {id})
}); });
} }
@ -132,8 +132,8 @@ namespace gui {
update(); update();
} }
bool LootUI::mouse(float x, float y, bool hover) { bool LootUI::mouse(float x, float y, guecs::Modifiers mods) {
return $gui.mouse(x, y, hover); return $gui.mouse(x, y, mods);
} }
bool LootUI::occupied(guecs::Entity slot) { bool LootUI::occupied(guecs::Entity slot) {

@ -26,7 +26,7 @@ namespace gui {
void update(); void update();
void render(sf::RenderWindow& window); void render(sf::RenderWindow& window);
void update_level(GameLevel &level); void update_level(GameLevel &level);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, guecs::Modifiers mods);
void make_button(const std::string &name, const std::wstring& label, Events::GUI event); void make_button(const std::string &name, const std::wstring& label, Events::GUI event);
void remove_slot(guecs::Entity slot_id); void remove_slot(guecs::Entity slot_id);

@ -112,7 +112,7 @@ namespace gui {
dirty(); dirty();
} }
void MainUI::mouse(int x, int y, bool hover) { void MainUI::mouse(int x, int y, guecs::Modifiers mods) {
$overlay_ui.$gui.mouse(x, y, hover); $overlay_ui.$gui.mouse(x, y, mods);
} }
} }

@ -23,7 +23,7 @@ namespace gui {
MainUI(sf::RenderWindow& window); MainUI(sf::RenderWindow& window);
void mouse(int x, int y, bool hover); void mouse(int x, int y, guecs::Modifiers mods);
void debug(); void debug();
void render_debug(); void render_debug();

@ -21,8 +21,8 @@ namespace gui {
auto area = gui.entity(name); auto area = gui.entity(name);
gui.set<Clickable>(area, { gui.set<Clickable>(area, {
[&](auto ent, auto data) { [&](auto) {
level.world->send<Events::GUI>(Events::GUI::AIM_CLICK, ent, data); level.world->send<Events::GUI>(Events::GUI::AIM_CLICK, area, {});
} }
}); });
} }

@ -51,7 +51,7 @@ namespace gui {
auto open_close_toggle = $gui.entity("ritual_ui"); auto open_close_toggle = $gui.entity("ritual_ui");
$gui.set<Clickable>(open_close_toggle, { $gui.set<Clickable>(open_close_toggle, {
[&](auto, auto){ event(Event::TOGGLE); } [&](auto){ event(Event::TOGGLE); }
}); });
@ -110,8 +110,8 @@ namespace gui {
} }
} }
bool UI::mouse(float x, float y, bool hover) { bool UI::mouse(float x, float y, guecs::Modifiers mods) {
return $gui.mouse(x, y, hover); return $gui.mouse(x, y, mods);
} }
bool UI::is_open() { bool UI::is_open() {
@ -172,7 +172,7 @@ namespace gui {
$gui.set_init<Sprite>(slot_id, {item}); $gui.set_init<Sprite>(slot_id, {item});
$gui.set<Clickable>(slot_id, { $gui.set<Clickable>(slot_id, {
[&, slot_id, item_id](auto, auto) { [&, slot_id, item_id](auto) {
auto data = std::make_any<SelectedItem>(slot_id, item_id); auto data = std::make_any<SelectedItem>(slot_id, item_id);
event(Event::SELECT, data); event(Event::SELECT, data);
} }
@ -231,7 +231,7 @@ namespace gui {
} }
$gui.set<Clickable>(combine, { $gui.set<Clickable>(combine, {
[&](auto, auto){ event(Event::COMBINE); } [&](auto){ event(Event::COMBINE); }
}); });
} else { } else {
$gui.show_text("result_text", L"That won't work."); $gui.show_text("result_text", L"That won't work.");

@ -53,7 +53,7 @@ namespace gui {
void OPENING(Event); void OPENING(Event);
void CLOSING(Event); void CLOSING(Event);
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, guecs::Modifiers mods);
void render(sf::RenderWindow &window); void render(sf::RenderWindow &window);
bool is_open(); bool is_open();
void load_blanket(); void load_blanket();

@ -35,17 +35,17 @@ namespace gui {
$gui.set<Sprite>(gui_id, {"armored_knight"}); $gui.set<Sprite>(gui_id, {"armored_knight"});
} else { } else {
$gui.set<Rectangle>(gui_id, {}); $gui.set<Rectangle>(gui_id, {});
$gui.set<ActionData>(gui_id, {make_any<string>(name)}); dbc::log("!!!!!!!!!!!!!!!!! is this used: $gui.set<ActionData>(gui_id, {make_any<string>(name)});");
if(name == "ritual_ui") { if(name == "ritual_ui") {
$gui.set<Clickable>(gui_id, { $gui.set<Clickable>(gui_id, {
[this](auto, auto){ select_ritual(); } [this](auto){ select_ritual(); }
}); });
$gui.set<Sound>(gui_id, {"pickup"}); $gui.set<Sound>(gui_id, {"pickup"});
} else { } else {
$gui.set<Text>(gui_id, {guecs::to_wstring(name)}); $gui.set<Text>(gui_id, {guecs::to_wstring(name)});
$gui.set<Clickable>(gui_id, { $gui.set<Clickable>(gui_id, {
guecs::make_action($level, Events::GUI::INV_SELECT, {gui_id}) guecs::make_action($level, gui_id, Events::GUI::INV_SELECT, {gui_id})
}); });
$gui.set<DropTarget>(gui_id, { $gui.set<DropTarget>(gui_id, {
.commit=[&, gui_id](DinkyECS::Entity world_target) -> bool { .commit=[&, gui_id](DinkyECS::Entity world_target) -> bool {
@ -61,11 +61,11 @@ namespace gui {
update(); update();
} }
bool StatusUI::mouse(float x, float y, bool hover) { bool StatusUI::mouse(float x, float y, guecs::Modifiers mods) {
if($ritual_ui.is_open()) { if($ritual_ui.is_open()) {
return $ritual_ui.mouse(x, y, hover); return $ritual_ui.mouse(x, y, mods);
} else { } else {
return $gui.mouse(x, y, hover); return $gui.mouse(x, y, mods);
} }
} }

@ -25,7 +25,7 @@ namespace gui {
void init(); void init();
void render(sf::RenderWindow &window); void render(sf::RenderWindow &window);
void update(); void update();
bool mouse(float x, float y, bool hover); bool mouse(float x, float y, guecs::Modifiers mods);
void remove_slot(guecs::Entity slot_id); void remove_slot(guecs::Entity slot_id);
bool place_slot(guecs::Entity gui_id, DinkyECS::Entity world_entity); bool place_slot(guecs::Entity gui_id, DinkyECS::Entity world_entity);

@ -1,5 +1,5 @@
[wrap-git] [wrap-git]
directory=lel-guecs-0.5.0 directory=lel-guecs-0.6.0
url=https://git.learnjsthehardway.com/learn-code-the-hard-way/lel-guecs.git url=https://git.learnjsthehardway.com/learn-code-the-hard-way/lel-guecs.git
revision=HEAD revision=HEAD
depth=1 depth=1

Loading…
Cancel
Save