From 1f7214fcd4a717e7397db54fcacde6f8ab65ee0e Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 7 Jan 2025 15:04:28 -0500 Subject: [PATCH] GUI now handles modals better and there's now a death screen that makes you exit. More to come. --- gui.cpp | 114 ++++++++++++++++++++++++++++++++++++++------------------ gui.hpp | 3 ++ 2 files changed, 80 insertions(+), 37 deletions(-) diff --git a/gui.cpp b/gui.cpp index eabd269..ae9d621 100644 --- a/gui.cpp +++ b/gui.cpp @@ -243,15 +243,18 @@ void GUI::handle_world_events() { $status_ui.log("You MISSED the enemy."); } } + break; case eGUI::DEATH: { // auto &dead_data = std::any_cast(data); - println("PLAYER DEAD!"); + auto player = $world.get_the(); + dbc::check(player.entity == entity, "received death event for something not the player"); + auto player_combat = $world.get(entity); if(player_combat.dead) { toggle_modal(&$death_ui, $player_died); - println("PLAYER DEAD show UI, after is {}", $player_died); } - } break; + } + break; case eGUI::LOOT: { auto &item = std::any_cast(data); $sounds.play("loot_gold"); @@ -269,50 +272,85 @@ void GUI::shutdown() { $renderer.close(); } -bool GUI::handle_ui_events() { +bool GUI::game_ui_events() { using KB = sf::Keyboard; - using MOUSE = sf::Mouse; - bool event_happened = false; - sf::Event event; auto player = $world.get_the(); int map_font_size = $renderer.font_size(); auto& player_motion = $world.get(player.entity); + bool event_happened = false; + + if(KB::isKeyPressed(KB::Left)) { + player_motion.dx = -1; + event_happened = true; + } else if(KB::isKeyPressed(KB::Right)) { + player_motion.dx = 1; + event_happened = true; + } else if(KB::isKeyPressed(KB::Up)) { + player_motion.dy = -1; + event_happened = true; + } else if(KB::isKeyPressed(KB::Down)) { + player_motion.dy = 1; + event_happened = true; + } else if(KB::isKeyPressed(KB::Equal)) { + resize_map(map_font_size + 10); + } else if(KB::isKeyPressed(KB::Hyphen)) { + resize_map(map_font_size - 10); + } else if(KB::isKeyPressed(KB::L)) { + auto &debug = $world.get_the(); + debug.LIGHT = !debug.LIGHT; + } else if(KB::isKeyPressed(KB::I)) { + toggle_modal(&$inventory_ui, $inventory_open); + } else if(KB::isKeyPressed(KB::P)) { + auto &debug = $world.get_the(); + debug.PATHS = !debug.PATHS; + } else if(KB::isKeyPressed(KB::S)) { + save_world(); + } else if(KB::isKeyPressed(KB::Tab)) { + $status_ui.key_press(Event::Tab); + } else if(KB::isKeyPressed(KB::Enter)) { + $status_ui.key_press(Event::Return); + } + + return event_happened; +} + + +bool GUI::modal_ui_events() { + using KB = sf::Keyboard; + bool event_happened = false; + + for(Panel *panel : $active_panels) { + if(KB::isKeyPressed(KB::Tab)) { + event_happened = true; + panel->key_press(Event::Tab); + } else if(KB::isKeyPressed(KB::Enter)) { + event_happened = true; + panel->key_press(Event::Return); + } else if(KB::isKeyPressed(KB::Escape)) { + // BUG: this is dogshit, rewerite it + if($inventory_open) { + toggle_modal(panel, $inventory_open); + } + } + } + + return event_happened; +} + +bool GUI::handle_ui_events() { + using MOUSE = sf::Mouse; + bool event_happened = false; + sf::Event event; Point pos; while($renderer.poll_event(event)) { if(event.type == sf::Event::Closed) { shutdown(); } else if(event.type == sf::Event::KeyPressed) { - if(KB::isKeyPressed(KB::Left)) { - player_motion.dx = -1; - event_happened = true; - } else if(KB::isKeyPressed(KB::Right)) { - player_motion.dx = 1; - event_happened = true; - } else if(KB::isKeyPressed(KB::Up)) { - player_motion.dy = -1; - event_happened = true; - } else if(KB::isKeyPressed(KB::Down)) { - player_motion.dy = 1; - event_happened = true; - } else if(KB::isKeyPressed(KB::Equal)) { - resize_map(map_font_size + 10); - } else if(KB::isKeyPressed(KB::Hyphen)) { - resize_map(map_font_size - 10); - } else if(KB::isKeyPressed(KB::L)) { - auto &debug = $world.get_the(); - debug.LIGHT = !debug.LIGHT; - } else if(KB::isKeyPressed(KB::I)) { - toggle_modal(&$inventory_ui, $inventory_open); - } else if(KB::isKeyPressed(KB::P)) { - auto &debug = $world.get_the(); - debug.PATHS = !debug.PATHS; - } else if(KB::isKeyPressed(KB::S)) { - save_world(); - } else if(KB::isKeyPressed(KB::Tab)) { - $status_ui.key_press(Event::Tab); - } else if(KB::isKeyPressed(KB::Enter)) { - $status_ui.key_press(Event::Return); + if($modal_shown) { + event_happened = modal_ui_events(); + } else { + event_happened = game_ui_events(); } } else { for(Panel *panel : $active_panels) { @@ -380,6 +418,8 @@ void GUI::toggle_modal(Panel *panel, bool &is_open_out) { $active_panels = {panel}; is_open_out = true; } + + $modal_shown = is_open_out; } void GUI::render_scene() { diff --git a/gui.hpp b/gui.hpp index da161b8..109de45 100644 --- a/gui.hpp +++ b/gui.hpp @@ -120,6 +120,7 @@ class GUI { Canvas $canvas; bool $inventory_open = false; bool $player_died = false; + bool $modal_shown = false; Component $test_button; SoundManager $sounds; SFMLRender $renderer; @@ -134,6 +135,8 @@ public: void resize_map(int new_size); void create_renderer(); void render_scene(); + bool modal_ui_events(); + bool game_ui_events(); bool handle_ui_events(); void handle_world_events(); void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);