Implementat control de repetició per als eixos del joystick
This commit is contained in:
@@ -110,9 +110,11 @@ bool Input::checkInput(InputType input, bool repeat, InputDeviceToUse device, in
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
|
if (gameControllerFound() && controller_index >= 0 && controller_index < num_gamepads_)
|
||||||
|
{
|
||||||
if ((device == InputDeviceToUse::CONTROLLER) || (device == InputDeviceToUse::ANY))
|
if ((device == InputDeviceToUse::CONTROLLER) || (device == InputDeviceToUse::ANY))
|
||||||
{
|
{
|
||||||
success_controller = checkAxisInput(input, controller_index);
|
success_controller = checkAxisInput(input, controller_index, repeat);
|
||||||
|
|
||||||
if (!success_controller)
|
if (!success_controller)
|
||||||
{
|
{
|
||||||
if (repeat)
|
if (repeat)
|
||||||
@@ -144,6 +146,7 @@ bool Input::checkInput(InputType input, bool repeat, InputDeviceToUse device, in
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (success_keyboard || success_controller);
|
return (success_keyboard || success_controller);
|
||||||
}
|
}
|
||||||
@@ -379,19 +382,53 @@ InputType Input::to_inputs_e(const std::string &name) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Comprueba el eje del mando
|
// Comprueba el eje del mando
|
||||||
bool Input::checkAxisInput(InputType input, int controller_index) const
|
bool Input::checkAxisInput(InputType input, int controller_index, bool repeat)
|
||||||
{
|
{
|
||||||
|
// Umbral para considerar el eje como activo
|
||||||
|
const Sint16 threshold = 30000;
|
||||||
|
bool axis_active_now = false;
|
||||||
|
|
||||||
switch (input)
|
switch (input)
|
||||||
{
|
{
|
||||||
case InputType::LEFT:
|
case InputType::LEFT:
|
||||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) < -30000;
|
axis_active_now = SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) < -threshold;
|
||||||
|
break;
|
||||||
case InputType::RIGHT:
|
case InputType::RIGHT:
|
||||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) > 30000;
|
axis_active_now = SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) > threshold;
|
||||||
|
break;
|
||||||
case InputType::UP:
|
case InputType::UP:
|
||||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) < -30000;
|
axis_active_now = SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) < -threshold;
|
||||||
|
break;
|
||||||
case InputType::DOWN:
|
case InputType::DOWN:
|
||||||
return SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) > 30000;
|
axis_active_now = SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) > threshold;
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Referencia al binding correspondiente
|
||||||
|
auto &binding = controller_bindings_.at(controller_index).at(static_cast<int>(input));
|
||||||
|
|
||||||
|
if (repeat)
|
||||||
|
{
|
||||||
|
// Si se permite repetir, simplemente devolvemos el estado actual
|
||||||
|
return axis_active_now;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Si no se permite repetir, aplicamos la lógica de transición
|
||||||
|
if (axis_active_now && !binding.axis_active)
|
||||||
|
{
|
||||||
|
// Transición de inactivo a activo
|
||||||
|
binding.axis_active = true;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else if (!axis_active_now && binding.axis_active)
|
||||||
|
{
|
||||||
|
// Transición de activo a inactivo
|
||||||
|
binding.axis_active = false;
|
||||||
|
}
|
||||||
|
// Mantener el estado actual
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -81,10 +81,11 @@ private:
|
|||||||
{
|
{
|
||||||
SDL_GameControllerButton button; // GameControllerButton asociado
|
SDL_GameControllerButton button; // GameControllerButton asociado
|
||||||
bool active; // Indica si está activo
|
bool active; // Indica si está activo
|
||||||
|
bool axis_active; // Estado del eje
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
explicit ControllerBindings(SDL_GameControllerButton btn = SDL_CONTROLLER_BUTTON_INVALID, bool act = false)
|
explicit ControllerBindings(SDL_GameControllerButton btn = SDL_CONTROLLER_BUTTON_INVALID, bool act = false, bool axis_act = false)
|
||||||
: button(btn), active(act) {}
|
: button(btn), active(act), axis_active(axis_act) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
@@ -99,7 +100,7 @@ private:
|
|||||||
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
|
std::string game_controller_db_path_; // Ruta al archivo gamecontrollerdb.txt
|
||||||
|
|
||||||
// Comprueba el eje del mando
|
// Comprueba el eje del mando
|
||||||
bool checkAxisInput(InputType input, int controller_index = 0) const;
|
bool checkAxisInput(InputType input, int controller_index, bool repeat);
|
||||||
|
|
||||||
// Constructor
|
// Constructor
|
||||||
explicit Input(const std::string &game_controller_db_path);
|
explicit Input(const std::string &game_controller_db_path);
|
||||||
|
|||||||
Reference in New Issue
Block a user