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() {
static auto text = Resource::get()->getText("8bithud");
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_, gamepad_->name);
text->writeCentered(x_, y_ - 10, Lang::getText("[DEFINE_BUTTONS] PLAYER") + std::to_string(static_cast<int>(options_gamepad_->player_id)));
text->writeCentered(x_, y_, options_gamepad_->name);
text->writeCentered(x_, y_ + 10, buttons_.at(index_button_).label);
}
}
// Comprueba el botón que se ha pulsado
void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event) {
// Solo pilla botones del mando que toca
// if (input_->getJoyIndex(event.which) != static_cast<int>(index_controller_)) {
// return;
//}
auto gamepad = input_->getGamepad(event.which);
// 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);
if (checkButtonNotInUse(BUTTON)) {
@@ -51,14 +53,14 @@ void DefineButtons::doControllerButtonDown(const SDL_GamepadButtonEvent &event)
}
// 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_) {
input_->bindGameControllerButton(gamepad, button.action, button.button);
input_->bindGameControllerButton(options_gamepad->instance, button.action, button.button);
}
// Remapea los inputs a inputs
input_->bindGameControllerButton(gamepad, 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_SELECT, Input::Action::FIRE_LEFT);
input_->bindGameControllerButton(options_gamepad->instance, Input::Action::SM_BACK, Input::Action::FIRE_CENTER);
}
// Comprueba los eventos
@@ -78,9 +80,9 @@ void DefineButtons::checkEvents(const SDL_Event &event) {
}
// Habilita el objeto
auto DefineButtons::enable(Options::Gamepad *gamepad) -> bool {
if (gamepad != nullptr) {
gamepad_ = gamepad;
auto DefineButtons::enable(Options::Gamepad *options_gamepad) -> bool {
if (options_gamepad != nullptr) {
options_gamepad_ = options_gamepad;
enabled_ = true;
finished_ = false;
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
auto DefineButtons::checkButtonNotInUse(SDL_GamepadButton button) -> bool {
return std::ranges::all_of(buttons_, [button](const auto &b) {
@@ -128,8 +121,8 @@ void DefineButtons::clearButtons() {
// Comprueba si ha finalizado
void DefineButtons::checkEnd() {
if (finished_) {
// bindButtons(); // Asigna los botones definidos al input_
saveBindingsToOptions(); // Guarda los cambios en las opciones
bindButtons(options_gamepad_); // Asigna los botones definidos al input_
input_->saveGamepadConfigFromGamepad(options_gamepad_->instance); // Guarda los cambios
input_->resetInputStates(); // Reinicia los estados de las pulsaciones de los botones
enabled_ = false; // Deshabilita
}

View File

@@ -29,7 +29,7 @@ class DefineButtons {
void render(); // Dibuja el objeto en pantalla
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
private:
@@ -44,13 +44,12 @@ class DefineButtons {
std::vector<Button> buttons_; // Definiciones de botones
size_t index_button_ = 0; // Índice del botón en proceso
std::vector<std::string> controller_names_; // Nombres de los mandos
Options::Gamepad *gamepad_;
Options::Gamepad *options_gamepad_;
// Métodos internos
void incIndexButton(); // Incrementa el índice de botones
void doControllerButtonDown(const SDL_GamepadButtonEvent &event); // Procesa pulsaciones
void bindButtons(std::shared_ptr<Input::Gamepad> gamepad); // Asigna botones al sistema de entrada
void saveBindingsToOptions(); // Guarda configuraciones
void bindButtons(Options::Gamepad *options_gamepad); // Asigna botones al sistema de entrada
auto checkButtonNotInUse(SDL_GamepadButton button) -> bool; // Verifica uso de botones
void clearButtons(); // Limpia asignaciones actuales
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::options = Section::Options::GAME_PLAY_1P;
#elif _DEBUG
Section::name = Section::Name::GAME;
Section::name = Section::Name::TITLE;
Section::options = Section::Options::GAME_PLAY_1P;
#else // NORMAL GAME
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
auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
// Obtiene el indice del controlador a partir de un event.id
auto Input::getJoyIndex(SDL_JoystickID id) const -> int {
// for (int i = 0; i < num_joysticks_; ++i) {
// if (SDL_GetJoystickID(joysticks_[i]) == id) {
// return i;
// }
// }
return -1;
// 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_) {
if (gamepad->instance_id == id) {
return gamepad;
}
}
return nullptr;
}
// Obtiene el SDL_GamepadButton asignado a un input
@@ -437,22 +437,21 @@ bool Input::removeGamepadConfig(const std::string &gamepadName) {
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
if (gamepads_.empty()) {
return nullptr;
}
// Buscar por nombre
for (const auto& gamepad : gamepads_) {
for (const auto &gamepad : gamepads_) {
if (gamepad && gamepad->name == gamepad_name) {
return gamepad;
}
}
// Si no se encuentra por nombre, devolver el primer gamepad válido
for (const auto& gamepad : gamepads_) {
for (const auto &gamepad : gamepads_) {
if (gamepad) {
return gamepad;
}

View File

@@ -126,6 +126,8 @@ class Input {
}
};
using Gamepads = std::vector<std::shared_ptr<Gamepad>>;
// --- Métodos de singleton ---
static void init(const std::string &game_controller_db_path, const std::string &gamepad_configs_file);
static void destroy();
@@ -146,7 +148,8 @@ class Input {
[[nodiscard]] auto gameControllerFound() const -> bool;
auto getControllerName(std::shared_ptr<Gamepad> gamepad) const -> std::string;
[[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 ---
[[nodiscard]] auto getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton;
@@ -161,15 +164,16 @@ class Input {
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);
void saveGamepadConfigFromGamepad(std::shared_ptr<Gamepad> gamepad);
private:
// --- Constantes ---
static constexpr Sint16 AXIS_THRESHOLD = 30000;
// --- Variables internas ---
std::vector<std::shared_ptr<Gamepad>> gamepads_;
Gamepads gamepads_;
Keyboard keyboard_;
std::vector<Action> button_inputs_;
std::string gamepad_mappings_file_;
@@ -188,7 +192,6 @@ class Input {
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);