From 49145905e342e7453052e762ec08c354eb2c6f0e Mon Sep 17 00:00:00 2001 From: Sergio Valor Date: Wed, 6 Aug 2025 20:31:47 +0200 Subject: [PATCH] claude: acabat de posar tot lo que deia i reventa. Debuggar --- CMakeLists.txt | 2 + source/define_buttons.h | 2 +- source/global_events.cpp | 11 +++ source/ui/action_list_option.h | 42 ----------- ...action_list_option.cpp => menu_option.cpp} | 23 +----- source/ui/menu_option.h | 34 +++++++++ source/ui/service_menu.cpp | 73 ++++++++++++++++++- source/ui/service_menu.h | 11 ++- 8 files changed, 131 insertions(+), 67 deletions(-) delete mode 100644 source/ui/action_list_option.h rename source/ui/{action_list_option.cpp => menu_option.cpp} (69%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 67ab240..539068d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,10 +33,12 @@ set(APP_SOURCES source/writer.cpp # --- UI (User Interface) --- + source/ui/menu_option.cpp source/ui/menu_renderer.cpp source/ui/notifier.cpp source/ui/service_menu.cpp source/ui/ui_message.cpp + source/ui/window_message.cpp # --- Lógica del Juego --- source/balloon_formations.cpp diff --git a/source/define_buttons.h b/source/define_buttons.h index b962274..2b1989a 100644 --- a/source/define_buttons.h +++ b/source/define_buttons.h @@ -10,7 +10,7 @@ #include "input.h" -class WindowMessage; +#include "ui/window_message.h" namespace Options { struct Gamepad; diff --git a/source/global_events.cpp b/source/global_events.cpp index 2477d84..7005dc4 100644 --- a/source/global_events.cpp +++ b/source/global_events.cpp @@ -6,6 +6,7 @@ #include "mouse.h" // Para handleEvent #include "screen.h" #include "section.hpp" // Para Name, Options, name, options +#include "ui/service_menu.h" // Para ServiceMenu namespace GlobalEvents { // Comprueba los eventos que se pueden producir en cualquier sección del juego @@ -29,6 +30,16 @@ void check(const SDL_Event &event) { break; } + if (ServiceMenu::get()->isEnabled()) { + ServiceMenu::get()->handleEvent(event); // Método que vamos a crear + + // Si DefineButtons está activo, no procesar más eventos + if (ServiceMenu::get()->isDefiningButtons()) { + return; + } + } + + Mouse::handleEvent(event); static auto *input_ = Input::get(); diff --git a/source/ui/action_list_option.h b/source/ui/action_list_option.h deleted file mode 100644 index ccd4dc6..0000000 --- a/source/ui/action_list_option.h +++ /dev/null @@ -1,42 +0,0 @@ -#pragma once - -#include "menu_option.h" - -#include -#include -#include - -class ActionListOption : public MenuOption { - public: - using ValueGetter = std::function; - using ValueSetter = std::function; - using ActionExecutor = std::function; - - ActionListOption( - const std::string& caption, - ServiceMenu::SettingsGroup group, - std::vector options, - ValueGetter getter, - ValueSetter setter, - ActionExecutor action_executor, - bool hidden = false - ); - - // Implementaciones de MenuOption - [[nodiscard]] auto getBehavior() const -> Behavior override; - [[nodiscard]] auto getValueAsString() const -> std::string override; - [[nodiscard]] auto getMaxValueWidth(Text* text) const -> int override; - void adjustValue(bool up) override; - void executeAction() override; - void sync(); //override; - - private: - std::vector options_; - ValueGetter value_getter_; - ValueSetter value_setter_; - ActionExecutor action_executor_; - size_t current_index_; - - void updateCurrentIndex(); - [[nodiscard]] auto findCurrentIndex() const -> size_t; -}; \ No newline at end of file diff --git a/source/ui/action_list_option.cpp b/source/ui/menu_option.cpp similarity index 69% rename from source/ui/action_list_option.cpp rename to source/ui/menu_option.cpp index e1bc437..ec980da 100644 --- a/source/ui/action_list_option.cpp +++ b/source/ui/menu_option.cpp @@ -1,30 +1,9 @@ -#include "action_list_option.h" +#include "menu_option.h" #include #include "text.h" -ActionListOption::ActionListOption( - const std::string& caption, - ServiceMenu::SettingsGroup group, - std::vector options, - ValueGetter getter, - ValueSetter setter, - ActionExecutor action_executor, - bool hidden) : MenuOption(caption, group, hidden), - options_(std::move(options)), - value_getter_(std::move(getter)), - value_setter_(std::move(setter)), - action_executor_(std::move(action_executor)), - current_index_(0) { - updateCurrentIndex(); -} - -auto ActionListOption::getBehavior() const -> Behavior { - // Puede tanto ajustar valor (lista) como ejecutar acción (botón) - return Behavior::BOTH; -} - auto ActionListOption::getValueAsString() const -> std::string { if (value_getter_) { return value_getter_(); diff --git a/source/ui/menu_option.h b/source/ui/menu_option.h index 7d7173d..759471f 100644 --- a/source/ui/menu_option.h +++ b/source/ui/menu_option.h @@ -174,3 +174,37 @@ class ActionOption : public MenuOption { private: std::function action_; }; + +// Opción de lista con acción +class ActionListOption : public MenuOption { + public: + using ValueGetter = std::function; + using ValueSetter = std::function; + using ActionExecutor = std::function; + + ActionListOption(const std::string& caption, ServiceMenu::SettingsGroup group, + std::vector options, ValueGetter getter, ValueSetter setter, + ActionExecutor action_executor, bool hidden = false) + : MenuOption(caption, group, hidden), options_(std::move(options)), + value_getter_(std::move(getter)), value_setter_(std::move(setter)), + action_executor_(std::move(action_executor)), current_index_(0) { + updateCurrentIndex(); + } + + [[nodiscard]] auto getBehavior() const -> Behavior override { return Behavior::BOTH; } + [[nodiscard]] auto getValueAsString() const -> std::string override; + [[nodiscard]] auto getMaxValueWidth(Text* text) const -> int override; + void adjustValue(bool up) override; + void executeAction() override; + void sync(); // Sincroniza con el valor actual + + private: + std::vector options_; + ValueGetter value_getter_; + ValueSetter value_setter_; + ActionExecutor action_executor_; + size_t current_index_; + + void updateCurrentIndex(); + [[nodiscard]] auto findCurrentIndex() const -> size_t; +}; \ No newline at end of file diff --git a/source/ui/service_menu.cpp b/source/ui/service_menu.cpp index 8f56df5..10efd80 100644 --- a/source/ui/service_menu.cpp +++ b/source/ui/service_menu.cpp @@ -16,7 +16,6 @@ #include "section.hpp" // Para Name, name, Options, options #include "ui/ui_message.h" // Para UIMessage #include "utils.h" // Para Zone -#include "action_list_option.h" // Para ActionListOption #include "define_buttons.h" // Para DefineButtons // Singleton @@ -541,4 +540,76 @@ void ServiceMenu::setHiddenOptions() { } updateMenu(); // El menú debe refrescarse si algo se oculta +} + +void ServiceMenu::handleEvent(const SDL_Event &event) { + if (!enabled_) { + return; + } + + // Si DefineButtons está activo, que maneje todos los eventos + if (define_buttons_ && define_buttons_->isEnabled()) { + define_buttons_->checkEvents(event); + return; // No procesar otros eventos mientras DefineButtons está activo + } + + // Procesar eventos normales del ServiceMenu + switch (event.type) { + case SDL_EVENT_KEY_DOWN: + switch (event.key.key) { + case SDLK_ESCAPE: + case SDLK_BACKSPACE: + moveBack(); + break; + case SDLK_RETURN: + case SDLK_KP_ENTER: + selectOption(); + break; + case SDLK_UP: + setSelectorUp(); + break; + case SDLK_DOWN: + setSelectorDown(); + break; + case SDLK_LEFT: + adjustOption(false); + break; + case SDLK_RIGHT: + adjustOption(true); + break; + default: + break; + } + break; + + case SDL_EVENT_GAMEPAD_BUTTON_DOWN: + switch (event.gbutton.button) { + case SDL_GAMEPAD_BUTTON_SOUTH: + case SDL_GAMEPAD_BUTTON_BACK: + moveBack(); + break; + case SDL_GAMEPAD_BUTTON_EAST: + case SDL_GAMEPAD_BUTTON_START: + selectOption(); + break; + case SDL_GAMEPAD_BUTTON_DPAD_UP: + setSelectorUp(); + break; + case SDL_GAMEPAD_BUTTON_DPAD_DOWN: + setSelectorDown(); + break; + case SDL_GAMEPAD_BUTTON_DPAD_LEFT: + adjustOption(false); + break; + case SDL_GAMEPAD_BUTTON_DPAD_RIGHT: + adjustOption(true); + break; + default: + break; + } + break; + + default: + break; + } } \ No newline at end of file diff --git a/source/ui/service_menu.h b/source/ui/service_menu.h index cf89693..068140a 100644 --- a/source/ui/service_menu.h +++ b/source/ui/service_menu.h @@ -7,10 +7,11 @@ #include // Para vector #include "ui_message.h" // Para UIMessage +#include "define_buttons.h" class MenuOption; class MenuRenderer; -class DefineButtons; // Forward declaration +//class DefineButtons; // Forward declaration class ServiceMenu { public: @@ -55,6 +56,14 @@ class ServiceMenu { void moveBack(); void checkEvents(const SDL_Event &event); // Nuevo método para eventos + // NUEVO: Método para manejar eventos (llamado desde GlobalEvents) + void handleEvent(const SDL_Event &event); + + // NUEVO: Getter para saber si DefineButtons está activo + [[nodiscard]] auto isDefiningButtons() const -> bool { + return define_buttons_ && define_buttons_->isEnabled(); + } + // --- Getters para que el Renderer pueda leer el estado --- [[nodiscard]] auto isEnabled() const -> bool { return enabled_; } [[nodiscard]] auto getTitle() const -> const std::string & { return title_; }