migrant input: ja guarda i llig configuracions de gamepad en fitxer
This commit is contained in:
235
source/input.h
235
source/input.h
@@ -1,17 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
#include "gamepad_config_manager.h"
|
||||
#include "input_types.h"
|
||||
|
||||
// Forward declaration para evitar dependencia circular
|
||||
struct GamepadConfig;
|
||||
class GamepadConfigManager;
|
||||
// struct GamepadConfig;
|
||||
// class GamepadConfigManager;
|
||||
|
||||
// Clase Input: gestiona la entrada de teclado y mandos (singleton)
|
||||
class Input {
|
||||
@@ -28,162 +30,177 @@ class Input {
|
||||
|
||||
// --- Estructuras ---
|
||||
struct KeyState {
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
|
||||
KeyState(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false)
|
||||
: scancode(scancode), is_held(is_held), just_pressed(just_pressed) {}
|
||||
KeyState(Uint8 scancode = 0, bool is_held = false, bool just_pressed = false)
|
||||
: scancode(scancode), is_held(is_held), just_pressed(just_pressed) {}
|
||||
};
|
||||
|
||||
struct ButtonState {
|
||||
SDL_GamepadButton button; // GameControllerButton asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
bool axis_active; // Estado del eje
|
||||
SDL_GamepadButton button; // GameControllerButton asociado
|
||||
bool is_held; // Está pulsada ahora mismo
|
||||
bool just_pressed; // Se acaba de pulsar en este fotograma
|
||||
bool axis_active; // Estado del eje
|
||||
|
||||
ButtonState(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false)
|
||||
: button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
|
||||
ButtonState(SDL_GamepadButton btn = SDL_GAMEPAD_BUTTON_INVALID, bool is_held = false, bool just_pressed = false, bool axis_act = false)
|
||||
: button(btn), is_held(is_held), just_pressed(just_pressed), axis_active(axis_act) {}
|
||||
};
|
||||
|
||||
struct Keyboard {
|
||||
std::unordered_map<InputAction, KeyState> bindings;
|
||||
std::unordered_map<InputAction, KeyState> bindings;
|
||||
|
||||
Keyboard()
|
||||
: bindings{
|
||||
// Teclado - Movimiento del jugador
|
||||
{InputAction::UP, KeyState(SDL_SCANCODE_UP)},
|
||||
{InputAction::DOWN, KeyState(SDL_SCANCODE_DOWN)},
|
||||
{InputAction::LEFT, KeyState(SDL_SCANCODE_LEFT)},
|
||||
{InputAction::RIGHT, KeyState(SDL_SCANCODE_RIGHT)},
|
||||
Keyboard()
|
||||
: bindings{
|
||||
// Teclado - Movimiento del jugador
|
||||
{InputAction::UP, KeyState(SDL_SCANCODE_UP)},
|
||||
{InputAction::DOWN, KeyState(SDL_SCANCODE_DOWN)},
|
||||
{InputAction::LEFT, KeyState(SDL_SCANCODE_LEFT)},
|
||||
{InputAction::RIGHT, KeyState(SDL_SCANCODE_RIGHT)},
|
||||
|
||||
// Teclado - Disparo del jugador
|
||||
{InputAction::FIRE_LEFT, KeyState(SDL_SCANCODE_Q)},
|
||||
{InputAction::FIRE_CENTER, KeyState(SDL_SCANCODE_W)},
|
||||
{InputAction::FIRE_RIGHT, KeyState(SDL_SCANCODE_E)},
|
||||
// Teclado - Disparo del jugador
|
||||
{InputAction::FIRE_LEFT, KeyState(SDL_SCANCODE_Q)},
|
||||
{InputAction::FIRE_CENTER, KeyState(SDL_SCANCODE_W)},
|
||||
{InputAction::FIRE_RIGHT, KeyState(SDL_SCANCODE_E)},
|
||||
|
||||
// Teclado - Interfaz
|
||||
{InputAction::START, KeyState(SDL_SCANCODE_RETURN)},
|
||||
// Teclado - Interfaz
|
||||
{InputAction::START, KeyState(SDL_SCANCODE_RETURN)},
|
||||
|
||||
// Teclado - Menu de servicio
|
||||
{InputAction::SERVICE, KeyState(SDL_SCANCODE_0)},
|
||||
{InputAction::SM_SELECT, KeyState(SDL_SCANCODE_RETURN)},
|
||||
{InputAction::SM_BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
// Teclado - Menu de servicio
|
||||
{InputAction::SERVICE, KeyState(SDL_SCANCODE_0)},
|
||||
{InputAction::SM_SELECT, KeyState(SDL_SCANCODE_RETURN)},
|
||||
{InputAction::SM_BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
|
||||
// Teclado - Control del programa
|
||||
{InputAction::EXIT, KeyState(SDL_SCANCODE_ESCAPE)},
|
||||
{InputAction::PAUSE, KeyState(SDL_SCANCODE_P)},
|
||||
{InputAction::BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
// Teclado - Control del programa
|
||||
{InputAction::EXIT, KeyState(SDL_SCANCODE_ESCAPE)},
|
||||
{InputAction::PAUSE, KeyState(SDL_SCANCODE_P)},
|
||||
{InputAction::BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
|
||||
|
||||
{InputAction::WINDOW_DEC_SIZE, KeyState(SDL_SCANCODE_F1)},
|
||||
{InputAction::WINDOW_INC_SIZE, KeyState(SDL_SCANCODE_F2)},
|
||||
{InputAction::WINDOW_FULLSCREEN, KeyState(SDL_SCANCODE_F3)},
|
||||
{InputAction::TOGGLE_VIDEO_SHADERS, KeyState(SDL_SCANCODE_F4)},
|
||||
{InputAction::TOGGLE_VIDEO_INTEGER_SCALE, KeyState(SDL_SCANCODE_F5)},
|
||||
{InputAction::TOGGLE_VIDEO_VSYNC, KeyState(SDL_SCANCODE_F6)},
|
||||
{InputAction::WINDOW_DEC_SIZE, KeyState(SDL_SCANCODE_F1)},
|
||||
{InputAction::WINDOW_INC_SIZE, KeyState(SDL_SCANCODE_F2)},
|
||||
{InputAction::WINDOW_FULLSCREEN, KeyState(SDL_SCANCODE_F3)},
|
||||
{InputAction::TOGGLE_VIDEO_SHADERS, KeyState(SDL_SCANCODE_F4)},
|
||||
{InputAction::TOGGLE_VIDEO_INTEGER_SCALE, KeyState(SDL_SCANCODE_F5)},
|
||||
{InputAction::TOGGLE_VIDEO_VSYNC, KeyState(SDL_SCANCODE_F6)},
|
||||
|
||||
{InputAction::TOGGLE_AUDIO, KeyState(SDL_SCANCODE_F7)},
|
||||
{InputAction::TOGGLE_AUTO_FIRE, KeyState(SDL_SCANCODE_F8)},
|
||||
{InputAction::CHANGE_LANG, KeyState(SDL_SCANCODE_F9)},
|
||||
{InputAction::TOGGLE_AUDIO, KeyState(SDL_SCANCODE_F7)},
|
||||
{InputAction::TOGGLE_AUTO_FIRE, KeyState(SDL_SCANCODE_F8)},
|
||||
{InputAction::CHANGE_LANG, KeyState(SDL_SCANCODE_F9)},
|
||||
|
||||
{InputAction::RESET, KeyState(SDL_SCANCODE_F10)},
|
||||
{InputAction::SHOW_INFO, KeyState(SDL_SCANCODE_F12)}} {}
|
||||
{InputAction::RESET, KeyState(SDL_SCANCODE_F10)},
|
||||
{InputAction::SHOW_INFO, KeyState(SDL_SCANCODE_F12)}} {}
|
||||
};
|
||||
|
||||
struct Gamepad {
|
||||
SDL_Gamepad *pad;
|
||||
SDL_JoystickID instance_id;
|
||||
std::string name;
|
||||
std::unordered_map<InputAction, ButtonState> bindings;
|
||||
SDL_Gamepad *pad;
|
||||
SDL_JoystickID instance_id;
|
||||
std::string name;
|
||||
std::unordered_map<InputAction, ButtonState> bindings;
|
||||
|
||||
Gamepad(SDL_Gamepad *gamepad)
|
||||
: pad(gamepad),
|
||||
instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))),
|
||||
name(std::string(SDL_GetGamepadName(gamepad))),
|
||||
bindings{
|
||||
// Mando - Movimiento del jugador
|
||||
{InputAction::UP, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_UP)},
|
||||
{InputAction::DOWN, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_DOWN)},
|
||||
{InputAction::LEFT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_LEFT)},
|
||||
{InputAction::RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)},
|
||||
Gamepad(SDL_Gamepad *gamepad)
|
||||
: pad(gamepad),
|
||||
instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))),
|
||||
name(std::string(SDL_GetGamepadName(gamepad))),
|
||||
bindings{
|
||||
// Mando - Movimiento del jugador
|
||||
{InputAction::UP, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_UP)},
|
||||
{InputAction::DOWN, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_DOWN)},
|
||||
{InputAction::LEFT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_LEFT)},
|
||||
{InputAction::RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)},
|
||||
|
||||
// Mando - Disparo del jugador
|
||||
{InputAction::FIRE_LEFT, ButtonState(SDL_GAMEPAD_BUTTON_WEST)},
|
||||
{InputAction::FIRE_CENTER, ButtonState(SDL_GAMEPAD_BUTTON_NORTH)},
|
||||
{InputAction::FIRE_RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_EAST)},
|
||||
// Mando - Disparo del jugador
|
||||
{InputAction::FIRE_LEFT, ButtonState(SDL_GAMEPAD_BUTTON_WEST)},
|
||||
{InputAction::FIRE_CENTER, ButtonState(SDL_GAMEPAD_BUTTON_NORTH)},
|
||||
{InputAction::FIRE_RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_EAST)},
|
||||
|
||||
// Mando - Interfaz
|
||||
{InputAction::START, ButtonState(SDL_GAMEPAD_BUTTON_START)},
|
||||
{InputAction::SERVICE, ButtonState(SDL_GAMEPAD_BUTTON_BACK)}} {}
|
||||
// Mando - Interfaz
|
||||
{InputAction::START, ButtonState(SDL_GAMEPAD_BUTTON_START)},
|
||||
{InputAction::SERVICE, ButtonState(SDL_GAMEPAD_BUTTON_BACK)}} {}
|
||||
|
||||
~Gamepad() {
|
||||
if (pad) {
|
||||
SDL_CloseGamepad(pad);
|
||||
~Gamepad() {
|
||||
if (pad) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
|
||||
// Reasigna un botón a una acción
|
||||
void rebindAction(InputAction action, SDL_GamepadButton new_button) {
|
||||
bindings[action] = new_button;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// --- Métodos de singleton ---
|
||||
static void init(const std::string &game_controller_db_path);
|
||||
static void destroy();
|
||||
static auto get() -> Input *;
|
||||
static void init(const std::string &game_controller_db_path, const std::string &gamepad_configs_file);
|
||||
static void destroy();
|
||||
static auto get() -> Input *;
|
||||
|
||||
// --- Métodos de configuración de controles ---
|
||||
void bindKey(Action input, SDL_Scancode code);
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button);
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source);
|
||||
void bindKey(Action input, SDL_Scancode code);
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button);
|
||||
void bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source);
|
||||
|
||||
// --- Métodos de consulta de entrada ---
|
||||
void update();
|
||||
auto checkAction(Action input, bool repeat = true, bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool;
|
||||
auto checkAnyInput(bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool;
|
||||
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> bool;
|
||||
void update();
|
||||
auto checkAction(Action input, bool repeat = true, bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool;
|
||||
auto checkAnyInput(bool check_keyboard = true, std::shared_ptr<Gamepad> gamepad = nullptr) -> bool;
|
||||
auto checkAnyButton(bool repeat = DO_NOT_ALLOW_REPEAT) -> bool;
|
||||
|
||||
// --- Métodos de gestión de mandos ---
|
||||
[[nodiscard]] auto gameControllerFound() const -> bool;
|
||||
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string;
|
||||
[[nodiscard]] auto getNumControllers() const -> int;
|
||||
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int;
|
||||
[[nodiscard]] auto gameControllerFound() const -> bool;
|
||||
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string;
|
||||
[[nodiscard]] auto getNumControllers() const -> int;
|
||||
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int;
|
||||
|
||||
// --- Métodos de consulta y utilidades ---
|
||||
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton;
|
||||
[[nodiscard]] static auto inputToString(Action input) -> std::string;
|
||||
[[nodiscard]] static auto stringToInput(const std::string &name) -> Action;
|
||||
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton;
|
||||
[[nodiscard]] static auto inputToString(Action input) -> std::string;
|
||||
[[nodiscard]] static auto stringToInput(const std::string &name) -> Action;
|
||||
|
||||
// --- Métodos de reseteo de estado de entrada ---
|
||||
void resetInputStates();
|
||||
void resetInputStates();
|
||||
|
||||
// --- Eventos ---
|
||||
void handleEvent(const SDL_Event &event);
|
||||
void handleEvent(const SDL_Event &event);
|
||||
|
||||
void printConnectedGamepads() const;
|
||||
|
||||
[[nodiscard]] auto getGamepads() const -> const std::vector<std::shared_ptr<Gamepad>> & { return gamepads_; }
|
||||
|
||||
// --- Métodos para integración con GamepadConfigManager ---
|
||||
void loadGamepadConfigs(const std::string& filename);
|
||||
void saveGamepadConfigs(const std::string& filename) const;
|
||||
void applyConfigToGamepad(std::shared_ptr<Gamepad> gamepad, const GamepadConfig& config);
|
||||
|
||||
private:
|
||||
// --- Constantes ---
|
||||
static constexpr Sint16 AXIS_THRESHOLD = 30000;
|
||||
|
||||
// --- Variables internas ---
|
||||
std::vector<std::shared_ptr<Gamepad>> gamepads_;
|
||||
Keyboard keyboard_;
|
||||
std::vector<Action> button_inputs_;
|
||||
std::string game_controller_db_path_;
|
||||
std::vector<std::shared_ptr<Gamepad>> gamepads_;
|
||||
Keyboard keyboard_;
|
||||
std::vector<Action> button_inputs_;
|
||||
std::string game_controller_db_path_;
|
||||
std::string gamepad_configs_file_;
|
||||
GamepadConfigs gamepad_configs_;
|
||||
|
||||
// --- Métodos internos ---
|
||||
void initSDLGamePad();
|
||||
auto checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool repeat) -> bool;
|
||||
void initSDLGamePad();
|
||||
auto checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool repeat) -> bool;
|
||||
void add_gamepad(int device_index);
|
||||
void remove_gamepad(SDL_JoystickID id);
|
||||
void addGamepadMappingsFromFile();
|
||||
void discoverGamepads();
|
||||
|
||||
// --- Métodos para integración con GamepadConfigManager ---
|
||||
void loadGamepadConfigs();
|
||||
void saveGamepadConfigs();
|
||||
void applyGamepadConfig(std::shared_ptr<Gamepad> gamepad);
|
||||
void saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad);
|
||||
|
||||
// Métodos auxiliares opcionales
|
||||
void setGamepadConfigsFile(const std::string& filename);
|
||||
GamepadConfig* getGamepadConfig(const std::string& gamepadName);
|
||||
bool removeGamepadConfig(const std::string& gamepadName);
|
||||
|
||||
// --- Constructor y destructor ---
|
||||
explicit Input(std::string game_controller_db_path);
|
||||
~Input() = default;
|
||||
explicit Input(std::string game_controller_db_path, std::string gamepad_configs_file);
|
||||
~Input() = default;
|
||||
|
||||
// --- Singleton ---
|
||||
static Input *instance;
|
||||
|
||||
Reference in New Issue
Block a user