#pragma once #include // Para SDL_Scancode, SDL_GamepadButton, SDL_JoystickID, SDL_CloseGamepad, SDL_Gamepad, etc. #include // Para array #include // Para shared_ptr #include // Para string #include // Para unordered_map #include // Para pair #include // Para vector #include "core/input/input_types.hpp" // Para InputAction // --- Clase Input: gestiona la entrada de teclado y mandos (singleton) --- class Input { public: // --- Constantes --- static constexpr bool ALLOW_REPEAT = true; static constexpr bool DO_NOT_ALLOW_REPEAT = false; static constexpr bool CHECK_KEYBOARD = true; static constexpr bool DO_NOT_CHECK_KEYBOARD = false; static constexpr int TRIGGER_L2_AS_BUTTON = 100; static constexpr int TRIGGER_R2_AS_BUTTON = 101; // --- Tipos --- using Action = InputAction; // --- Estructuras --- struct KeyState { Uint8 scancode{0}; bool is_held{false}; bool just_pressed{false}; }; struct ButtonState { int button{static_cast(SDL_GAMEPAD_BUTTON_INVALID)}; bool is_held{false}; bool just_pressed{false}; bool axis_active{false}; bool trigger_active{false}; }; struct Keyboard { std::unordered_map bindings; }; struct Gamepad { SDL_Gamepad* pad{nullptr}; SDL_JoystickID instance_id{0}; std::string name; std::string path; std::unordered_map bindings; explicit Gamepad(SDL_Gamepad* gamepad) : pad(gamepad), instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))), name(std::string(SDL_GetGamepadName(gamepad))), path(std::string(SDL_GetGamepadPath(pad))), bindings{} {} // Sin bindings por defecto — define los de tu juego en main ~Gamepad() { if (pad != nullptr) { SDL_CloseGamepad(pad); } } void rebindAction(Action action, SDL_GamepadButton new_button) { bindings[action].button = static_cast(new_button); } }; using Gamepads = std::vector>; // --- Singleton --- static void init(const std::string& game_controller_db_path); static void destroy(); static auto get() -> Input*; // --- Actualización --- void update(); // --- Configuración de controles --- void bindKey(Action action, SDL_Scancode code); static void bindGameControllerButton(const std::shared_ptr& gamepad, Action action, SDL_GamepadButton button); static void bindGameControllerButton(const std::shared_ptr& gamepad, Action action_target, Action action_source); // --- Consulta de entrada --- auto checkAction(Action action, bool repeat = true, bool check_keyboard = true, const std::shared_ptr& gamepad = nullptr) -> bool; auto checkAnyInput(bool check_keyboard = true, const std::shared_ptr& gamepad = nullptr) -> bool; auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> bool; void resetInputStates(); // --- Gestión de gamepads --- [[nodiscard]] auto gameControllerFound() const -> bool; [[nodiscard]] auto getNumGamepads() const -> int; [[nodiscard]] auto getGamepad(SDL_JoystickID id) const -> std::shared_ptr; [[nodiscard]] auto getGamepadByName(const std::string& name) const -> std::shared_ptr; [[nodiscard]] auto getGamepads() const -> const Gamepads& { return gamepads_; } auto findAvailableGamepadByName(const std::string& gamepad_name) -> std::shared_ptr; static auto getControllerName(const std::shared_ptr& gamepad) -> std::string; [[nodiscard]] auto getControllerNames() const -> std::vector; [[nodiscard]] static auto getControllerBinding(const std::shared_ptr& gamepad, Action action) -> SDL_GamepadButton; void printConnectedGamepads() const; // --- Eventos --- auto handleEvent(const SDL_Event& event) -> std::string; private: static constexpr Sint16 AXIS_THRESHOLD = 30000; static constexpr Sint16 TRIGGER_THRESHOLD = 16384; // Si tu juego tiene acciones con botones analógicos (triggers/ejes), añádelos aquí: static constexpr std::array BUTTON_INPUTS = {}; explicit Input(std::string game_controller_db_path); ~Input() = default; void initSDLGamePad(); static auto checkAxisInput(Action action, const std::shared_ptr& gamepad, bool repeat) -> bool; static auto checkTriggerInput(Action action, const std::shared_ptr& gamepad, bool repeat) -> bool; auto addGamepad(int device_index) -> std::string; auto removeGamepad(SDL_JoystickID id) -> std::string; void addGamepadMappingsFromFile(); void discoverGamepads(); static Input* instance; Gamepads gamepads_; Keyboard keyboard_{}; std::string gamepad_mappings_file_; };