migrant input: ja mapeja be tots els botons desde el fitxer de configuració

This commit is contained in:
2025-08-04 19:58:10 +02:00
parent adf21086e5
commit 4fd78d216a
4 changed files with 22 additions and 12 deletions

View File

@@ -143,7 +143,7 @@ auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
// Obtiene el gamepad a partir de un event.id
std::shared_ptr<Input::Gamepad> Input::getGamepad(SDL_JoystickID id) const {
for (const auto& gamepad : gamepads_) {
for (const auto &gamepad : gamepads_) {
if (gamepad->instance_id == id) {
return gamepad;
}

View File

@@ -49,26 +49,26 @@ class Input {
Keyboard()
: bindings{
// Teclado - Movimiento del jugador
// Movimiento del jugador
{Action::UP, KeyState(SDL_SCANCODE_UP)},
{Action::DOWN, KeyState(SDL_SCANCODE_DOWN)},
{Action::LEFT, KeyState(SDL_SCANCODE_LEFT)},
{Action::RIGHT, KeyState(SDL_SCANCODE_RIGHT)},
// Teclado - Disparo del jugador
// Disparo del jugador
{Action::FIRE_LEFT, KeyState(SDL_SCANCODE_Q)},
{Action::FIRE_CENTER, KeyState(SDL_SCANCODE_W)},
{Action::FIRE_RIGHT, KeyState(SDL_SCANCODE_E)},
// Teclado - Interfaz
// Interfaz
{Action::START, KeyState(SDL_SCANCODE_RETURN)},
// Teclado - Menu de servicio
// Menu de servicio
{Action::SERVICE, KeyState(SDL_SCANCODE_0)},
{Action::SM_SELECT, KeyState(SDL_SCANCODE_RETURN)},
{Action::SM_BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
// Teclado - Control del programa
// Control del programa
{Action::EXIT, KeyState(SDL_SCANCODE_ESCAPE)},
{Action::PAUSE, KeyState(SDL_SCANCODE_P)},
{Action::BACK, KeyState(SDL_SCANCODE_BACKSPACE)},
@@ -99,20 +99,24 @@ class Input {
instance_id(SDL_GetJoystickID(SDL_GetGamepadJoystick(gamepad))),
name(std::string(SDL_GetGamepadName(gamepad))),
bindings{
// Mando - Movimiento del jugador
// Movimiento del jugador
{Action::UP, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_UP)},
{Action::DOWN, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_DOWN)},
{Action::LEFT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_LEFT)},
{Action::RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_DPAD_RIGHT)},
// Mando - Disparo del jugador
// Disparo del jugador
{Action::FIRE_LEFT, ButtonState(SDL_GAMEPAD_BUTTON_WEST)},
{Action::FIRE_CENTER, ButtonState(SDL_GAMEPAD_BUTTON_NORTH)},
{Action::FIRE_RIGHT, ButtonState(SDL_GAMEPAD_BUTTON_EAST)},
// Mando - Interfaz
// Interfaz
{Action::START, ButtonState(SDL_GAMEPAD_BUTTON_START)},
{Action::SERVICE, ButtonState(SDL_GAMEPAD_BUTTON_BACK)}} {}
{Action::SERVICE, ButtonState(SDL_GAMEPAD_BUTTON_BACK)},
// Menu de servicio
{Action::SM_SELECT, ButtonState(SDL_GAMEPAD_BUTTON_WEST)},
{Action::SM_BACK, ButtonState(SDL_GAMEPAD_BUTTON_NORTH)}} {}
~Gamepad() {
if (pad) {
@@ -149,7 +153,7 @@ class Input {
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string;
[[nodiscard]] auto getNumGamepads() const -> int;
std::shared_ptr<Gamepad> getGamepad(SDL_JoystickID id) const;
const Gamepads& getGamepads() const { return gamepads_; }
const Gamepads &getGamepads() const { return gamepads_; }
// --- Métodos de consulta y utilidades ---
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton;

View File

@@ -91,4 +91,9 @@ const std::unordered_map<std::string, SDL_GamepadButton> stringToButton = {
{"DPAD_DOWN", SDL_GAMEPAD_BUTTON_DPAD_DOWN},
{"DPAD_LEFT", SDL_GAMEPAD_BUTTON_DPAD_LEFT},
{"DPAD_RIGHT", SDL_GAMEPAD_BUTTON_DPAD_RIGHT}
};
const std::unordered_map<InputAction, InputAction> actionToAction = {
{InputAction::SM_SELECT, InputAction::FIRE_LEFT},
{InputAction::SM_BACK, InputAction::FIRE_CENTER},
};

View File

@@ -50,4 +50,5 @@ enum class InputAction : int {
extern const std::unordered_map<InputAction, std::string> actionToString;
extern const std::unordered_map<std::string, InputAction> stringToAction;
extern const std::unordered_map<SDL_GamepadButton, std::string> buttonToString;
extern const std::unordered_map<std::string, SDL_GamepadButton> stringToButton;
extern const std::unordered_map<std::string, SDL_GamepadButton> stringToButton;
extern const std::unordered_map<InputAction, InputAction> actionToAction;