migrant input: ja es poden redefinir els botons. Falta rebindar les accions a accions

This commit is contained in:
2025-08-04 13:55:58 +02:00
parent cde6ad4b71
commit adf21086e5
5 changed files with 41 additions and 47 deletions

View File

@@ -30,18 +30,20 @@ DefineButtons::DefineButtons()
void DefineButtons::render() { void DefineButtons::render() {
static auto text = Resource::get()->getText("8bithud"); static auto text = Resource::get()->getText("8bithud");
if (enabled_) { if (enabled_) {
text->writeCentered(x_, y_ - 10, Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(static_cast<int>(gamepad_->player_id))); text->writeCentered(x_, y_ - 10, Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(static_cast<int>(options_gamepad_->player_id)));
text->writeCentered(x_, y_, gamepad_->name); text->writeCentered(x_, y_, options_gamepad_->name);
text->writeCentered(x_, y_ + 10, buttons_.at(index_button_).label); text->writeCentered(x_, y_ + 10, buttons_.at(index_button_).label);
} }
} }
// Comprueba el botón que se ha pulsado // Comprueba el botón que se ha pulsado
void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) { void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) {
// Solo pilla botones del mando que toca auto gamepad = input_->getGamepad(event.which);
// if (input_->getJoyIndex(event.which) != static_cast<int>(index_controller_)) {
// return; // Asegúrate de que el gamepad sea válido y sea el que corresponde
//} if (!gamepad) {
return;
}
const auto BUTTON = static_cast<SDL_GamepadButton>(event.button); const auto BUTTON = static_cast<SDL_GamepadButton>(event.button);
if (checkButtonNotInUse(BUTTON)) { if (checkButtonNotInUse(BUTTON)) {
@@ -51,14 +53,14 @@ void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event)
} }
// Asigna los botones definidos al input_ // Asigna los botones definidos al input_
void DefineButtons::bindButtons(std::shared_ptr<Input::Gamepad> gamepad) { void DefineButtons::bindButtons(Options::Gamepad *options_gamepad) {
for (const auto &button : buttons_) { for (const auto &button : buttons_) {
input_->bindGameControllerButton(gamepad, button.action, button.button); input_->bindGameControllerButton(options_gamepad->instance, button.action, button.button);
} }
// Remapea los inputs a inputs // Remapea los inputs a inputs
input_->bindGameControllerButton(gamepad, Input::Action::SM_SELECT, Input::Action::FIRE_LEFT); input_->bindGameControllerButton(options_gamepad->instance, Input::Action::SM_SELECT, Input::Action::FIRE_LEFT);
input_->bindGameControllerButton(gamepad, Input::Action::SM_BACK, Input::Action::FIRE_CENTER); input_->bindGameControllerButton(options_gamepad->instance, Input::Action::SM_BACK, Input::Action::FIRE_CENTER);
} }
// Comprueba los eventos // Comprueba los eventos
@@ -78,9 +80,9 @@ void DefineButtons::checkEvents(const SDL_Event &event) {
} }
// Habilita el objeto // Habilita el objeto
auto DefineButtons::enable(Options::Gamepad *gamepad) -> bool { auto DefineButtons::enable(Options::Gamepad *options_gamepad) -> bool {
if (gamepad != nullptr) { if (options_gamepad != nullptr) {
gamepad_ = gamepad; options_gamepad_ = options_gamepad;
enabled_ = true; enabled_ = true;
finished_ = false; finished_ = false;
index_button_ = 0; index_button_ = 0;
@@ -99,15 +101,6 @@ void DefineButtons::incIndexButton() {
} }
} }
// Guarda los cambios en las opciones
void DefineButtons::saveBindingsToOptions() {
// auto &controller = Options::controllers.at(index_controller_);
// controller.name = input_->getControllerName(index_controller_);
// for (size_t j = 0; j < controller.inputs.size(); ++j) {
// controller.buttons.at(j) = input_->getControllerBinding(index_controller_, controller.inputs.at(j));
// }
}
// Comprueba que un botón no esté ya asignado // Comprueba que un botón no esté ya asignado
auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool { auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool {
return std::ranges::all_of(buttons_, [button](const auto &b) { return std::ranges::all_of(buttons_, [button](const auto &b) {
@@ -128,8 +121,8 @@ void DefineButtons::clearButtons() {
// Comprueba si ha finalizado // Comprueba si ha finalizado
void DefineButtons::checkEnd() { void DefineButtons::checkEnd() {
if (finished_) { if (finished_) {
// bindButtons(); // Asigna los botones definidos al input_ bindButtons(options_gamepad_); // Asigna los botones definidos al input_
saveBindingsToOptions(); // Guarda los cambios en las opciones input_->saveGamepadConfigFromGamepad(options_gamepad_->instance); // Guarda los cambios
input_->resetInputStates(); // Reinicia los estados de las pulsaciones de los botones input_->resetInputStates(); // Reinicia los estados de las pulsaciones de los botones
enabled_ = false; // Deshabilita enabled_ = false; // Deshabilita
} }

View File

@@ -29,7 +29,7 @@ class DefineButtons {
void render(); // Dibuja el objeto en pantalla void render(); // Dibuja el objeto en pantalla
void checkEvents(const SDL_Event &event); // Procesa los eventos void checkEvents(const SDL_Event &event); // Procesa los eventos
auto enable(Options::Gamepad *gamepad) -> bool; // Habilita la redefinición de botones auto enable(Options::Gamepad *options_gamepad) -> bool; // Habilita la redefinición de botones
[[nodiscard]] auto isEnabled() const -> bool { return enabled_; }; // Comprueba si está habilitado [[nodiscard]] auto isEnabled() const -> bool { return enabled_; }; // Comprueba si está habilitado
private: private:
@@ -44,13 +44,12 @@ class DefineButtons {
std::vector<Button> buttons_; // Definiciones de botones std::vector<Button> buttons_; // Definiciones de botones
size_t index_button_ = 0; // Índice del botón en proceso size_t index_button_ = 0; // Índice del botón en proceso
std::vector<std::string> controller_names_; // Nombres de los mandos std::vector<std::string> controller_names_; // Nombres de los mandos
Options::Gamepad *gamepad_; Options::Gamepad *options_gamepad_;
// Métodos internos // Métodos internos
void incIndexButton(); // Incrementa el índice de botones void incIndexButton(); // Incrementa el índice de botones
void doControllerButtonDown(const SDL_GamepadButtonEvent &event); // Procesa pulsaciones void doControllerButtonDown(const SDL_GamepadButtonEvent &event); // Procesa pulsaciones
void bindButtons(std::shared_ptr<Input::Gamepad> gamepad); // Asigna botones al sistema de entrada void bindButtons(Options::Gamepad *options_gamepad); // Asigna botones al sistema de entrada
void saveBindingsToOptions(); // Guarda configuraciones
auto checkButtonNotInUse(SDL_GamepadButton button) -> bool; // Verifica uso de botones auto checkButtonNotInUse(SDL_GamepadButton button) -> bool; // Verifica uso de botones
void clearButtons(); // Limpia asignaciones actuales void clearButtons(); // Limpia asignaciones actuales
void checkEnd(); // Comprueba si ha finalizado void checkEnd(); // Comprueba si ha finalizado

View File

@@ -47,7 +47,7 @@ Director::Director(int argc, std::span<char *> argv) {
Section::name = Section::Name::GAME; Section::name = Section::Name::GAME;
Section::options = Section::Options::GAME_PLAY_1P; Section::options = Section::Options::GAME_PLAY_1P;
#elif _DEBUG #elif _DEBUG
Section::name = Section::Name::GAME; Section::name = Section::Name::TITLE;
Section::options = Section::Options::GAME_PLAY_1P; Section::options = Section::Options::GAME_PLAY_1P;
#else // NORMAL GAME #else // NORMAL GAME
Section::name = Section::Name::LOGO; Section::name = Section::Name::LOGO;

View File

@@ -141,14 +141,14 @@ auto Input::getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::st
// Obten el número de mandos conectados // Obten el número de mandos conectados
auto Input::getNumGamepads() const -> int { return gamepads_.size(); } auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
// Obtiene el indice del controlador a partir de un event.id // Obtiene el gamepad a partir de un event.id
auto Input::getJoyIndex(SDL_JoystickID id) const -> int { std::shared_ptr<Input::Gamepad> Input::getGamepad(SDL_JoystickID id) const {
// for (int i = 0; i < num_joysticks_; ++i) { for (const auto& gamepad : gamepads_) {
// if (SDL_GetJoystickID(joysticks_[i]) == id) { if (gamepad->instance_id == id) {
// return i; return gamepad;
// } }
// } }
return -1; return nullptr;
} }
// Obtiene el SDL_GamepadButton asignado a un input // Obtiene el SDL_GamepadButton asignado a un input
@@ -437,27 +437,26 @@ bool Input::removeGamepadConfig(const std::string &gamepadName) {
return false; return false;
} }
std::shared_ptr<Input::Gamepad> Input::findAvailableGamepadByName(const std::string &gamepad_name) std::shared_ptr<Input::Gamepad> Input::findAvailableGamepadByName(const std::string &gamepad_name) {
{
// Si no hay gamepads disponibles, devolver gamepad por defecto // Si no hay gamepads disponibles, devolver gamepad por defecto
if (gamepads_.empty()) { if (gamepads_.empty()) {
return nullptr; return nullptr;
} }
// Buscar por nombre // Buscar por nombre
for (const auto& gamepad : gamepads_) { for (const auto &gamepad : gamepads_) {
if (gamepad && gamepad->name == gamepad_name) { if (gamepad && gamepad->name == gamepad_name) {
return gamepad; return gamepad;
} }
} }
// Si no se encuentra por nombre, devolver el primer gamepad válido // Si no se encuentra por nombre, devolver el primer gamepad válido
for (const auto& gamepad : gamepads_) { for (const auto &gamepad : gamepads_) {
if (gamepad) { if (gamepad) {
return gamepad; return gamepad;
} }
} }
// Si llegamos aquí, no hay gamepads válidos // Si llegamos aquí, no hay gamepads válidos
return nullptr; return nullptr;
} }

View File

@@ -126,6 +126,8 @@ class Input {
} }
}; };
using Gamepads = std::vector<std::shared_ptr<Gamepad>>;
// --- Métodos de singleton --- // --- Métodos de singleton ---
static void init(const std::string &game_controller_db_path, const std::string &gamepad_configs_file); static void init(const std::string &game_controller_db_path, const std::string &gamepad_configs_file);
static void destroy(); static void destroy();
@@ -146,7 +148,8 @@ class Input {
[[nodiscard]] auto gameControllerFound() const -> bool; [[nodiscard]] auto gameControllerFound() const -> bool;
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string; auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string;
[[nodiscard]] auto getNumGamepads() const -> int; [[nodiscard]] auto getNumGamepads() const -> int;
[[nodiscard]] auto getJoyIndex(SDL_JoystickID id) const -> int; std::shared_ptr<Gamepad> getGamepad(SDL_JoystickID id) const;
const Gamepads& getGamepads() const { return gamepads_; }
// --- Métodos de consulta y utilidades --- // --- Métodos de consulta y utilidades ---
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton; [[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton;
@@ -161,15 +164,16 @@ class Input {
void printConnectedGamepads() const; void printConnectedGamepads() const;
[[nodiscard]] auto getGamepads() const -> const std::vector<std::shared_ptr<Gamepad>> & { return gamepads_; } //[[nodiscard]] auto getGamepads() const -> const Gamepads & { return gamepads_; }
std::shared_ptr<Gamepad> findAvailableGamepadByName(const std::string &gamepad_name); std::shared_ptr<Gamepad> findAvailableGamepadByName(const std::string &gamepad_name);
void saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad);
private: private:
// --- Constantes --- // --- Constantes ---
static constexpr Sint16 AXIS_THRESHOLD = 30000; static constexpr Sint16 AXIS_THRESHOLD = 30000;
// --- Variables internas --- // --- Variables internas ---
std::vector<std::shared_ptr<Gamepad>> gamepads_; Gamepads gamepads_;
Keyboard keyboard_; Keyboard keyboard_;
std::vector<Action> button_inputs_; std::vector<Action> button_inputs_;
std::string gamepad_mappings_file_; std::string gamepad_mappings_file_;
@@ -188,7 +192,6 @@ class Input {
void loadGamepadConfigs(); void loadGamepadConfigs();
void saveGamepadConfigs(); void saveGamepadConfigs();
void applyGamepadConfig(std::shared_ptr<Gamepad> gamepad); void applyGamepadConfig(std::shared_ptr<Gamepad> gamepad);
void saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad);
// Métodos auxiliares opcionales // Métodos auxiliares opcionales
void setGamepadConfigsFile(const std::string &filename); void setGamepadConfigsFile(const std::string &filename);