forked from jaildesigner-jailgames/jaildoctors_dilemma
linter
This commit is contained in:
@@ -9,21 +9,21 @@
|
||||
#include <utility> // Para pair
|
||||
|
||||
// [SINGLETON]
|
||||
Input* Input::input_ = nullptr;
|
||||
Input* Input::input = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto con esta función estática
|
||||
void Input::init(const std::string& game_controller_db_path) {
|
||||
Input::input_ = new Input(game_controller_db_path);
|
||||
Input::input = new Input(game_controller_db_path);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto con esta función estática
|
||||
void Input::destroy() {
|
||||
delete Input::input_;
|
||||
delete Input::input;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
|
||||
Input* Input::get() {
|
||||
return Input::input_;
|
||||
return Input::input;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
@@ -63,24 +63,24 @@ void Input::bindGameControllerButton(int controller_index, InputAction input_tar
|
||||
bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device, int controller_index) {
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
const int input_index = static_cast<int>(input);
|
||||
const int INPUT_INDEX = static_cast<int>(input);
|
||||
|
||||
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY) {
|
||||
auto key_states = SDL_GetKeyboardState(nullptr);
|
||||
const auto* key_states = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat) {
|
||||
success_keyboard = key_states[key_bindings_[input_index].scancode] != 0;
|
||||
success_keyboard = static_cast<int>(key_states[key_bindings_[INPUT_INDEX].scancode]) != 0;
|
||||
} else {
|
||||
if (!key_bindings_[input_index].active) {
|
||||
if (key_states[key_bindings_[input_index].scancode] != 0) {
|
||||
key_bindings_[input_index].active = true;
|
||||
if (!key_bindings_[INPUT_INDEX].active) {
|
||||
if (static_cast<int>(key_states[key_bindings_[INPUT_INDEX].scancode]) != 0) {
|
||||
key_bindings_[INPUT_INDEX].active = true;
|
||||
success_keyboard = true;
|
||||
} else {
|
||||
success_keyboard = false;
|
||||
}
|
||||
} else {
|
||||
if (key_states[key_bindings_[input_index].scancode] == 0) {
|
||||
key_bindings_[input_index].active = false;
|
||||
if (static_cast<int>(key_states[key_bindings_[INPUT_INDEX].scancode]) == 0) {
|
||||
key_bindings_[INPUT_INDEX].active = false;
|
||||
}
|
||||
success_keyboard = false;
|
||||
}
|
||||
@@ -93,18 +93,18 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
|
||||
|
||||
if (!success_controller) {
|
||||
if (repeat) {
|
||||
success_controller = SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0;
|
||||
success_controller = static_cast<int>(SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(INPUT_INDEX).button)) != 0;
|
||||
} else {
|
||||
if (!controller_bindings_.at(controller_index).at(input_index).active) {
|
||||
if (SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0) {
|
||||
controller_bindings_.at(controller_index).at(input_index).active = true;
|
||||
if (!controller_bindings_.at(controller_index).at(INPUT_INDEX).active) {
|
||||
if (static_cast<int>(SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(INPUT_INDEX).button)) != 0) {
|
||||
controller_bindings_.at(controller_index).at(INPUT_INDEX).active = true;
|
||||
success_controller = true;
|
||||
} else {
|
||||
success_controller = false;
|
||||
}
|
||||
} else {
|
||||
if (SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) == 0) {
|
||||
controller_bindings_.at(controller_index).at(input_index).active = false;
|
||||
if (static_cast<int>(SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(INPUT_INDEX).button)) == 0) {
|
||||
controller_bindings_.at(controller_index).at(INPUT_INDEX).active = false;
|
||||
}
|
||||
success_controller = false;
|
||||
}
|
||||
@@ -122,7 +122,7 @@ bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) {
|
||||
const bool* key_states = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < (int)key_bindings_.size(); ++i) {
|
||||
if (key_states[key_bindings_[i].scancode] != 0 && !key_bindings_[i].active) {
|
||||
if (static_cast<int>(key_states[key_bindings_[i].scancode]) != 0 && !key_bindings_[i].active) {
|
||||
key_bindings_[i].active = true;
|
||||
return true;
|
||||
}
|
||||
@@ -132,7 +132,7 @@ bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) {
|
||||
if (gameControllerFound()) {
|
||||
if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY) {
|
||||
for (int i = 0; i < (int)controller_bindings_.size(); ++i) {
|
||||
if (SDL_GetGamepadButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button) != 0 && !controller_bindings_[controller_index][i].active) {
|
||||
if (static_cast<int>(SDL_GetGamepadButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button)) != 0 && !controller_bindings_[controller_index][i].active) {
|
||||
controller_bindings_[controller_index][i].active = true;
|
||||
return true;
|
||||
}
|
||||
@@ -191,12 +191,12 @@ bool Input::discoverGameControllers() {
|
||||
for (int i = 0; i < num_joysticks_; i++) {
|
||||
if (SDL_IsGamepad(joystick_ids[i])) {
|
||||
SDL_Gamepad* pad = SDL_OpenGamepad(joystick_ids[i]);
|
||||
if (pad && SDL_GamepadConnected(pad)) {
|
||||
if ((pad != nullptr) && SDL_GamepadConnected(pad)) {
|
||||
connected_controllers_.push_back(pad);
|
||||
|
||||
const char* name = SDL_GetGamepadName(pad);
|
||||
std::cout << "#" << i << ": " << (name ? name : "Unknown") << std::endl;
|
||||
controller_names_.push_back(name ? name : "Unknown");
|
||||
std::cout << "#" << i << ": " << ((name != nullptr) ? name : "Unknown") << std::endl;
|
||||
controller_names_.push_back((name != nullptr) ? name : "Unknown");
|
||||
} else {
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
@@ -214,7 +214,7 @@ bool Input::discoverGameControllers() {
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound() { return num_gamepads_ > 0 ? true : false; }
|
||||
bool Input::gameControllerFound() const { return num_gamepads_ > 0; }
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int controller_index) const { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
|
||||
@@ -246,21 +246,21 @@ int Input::getIndexByName(const std::string& name) const {
|
||||
// Comprueba el eje del mando
|
||||
bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat) {
|
||||
// Umbral para considerar el eje como activo
|
||||
const Sint16 threshold = 30000;
|
||||
const Sint16 THRESHOLD = 30000;
|
||||
bool axis_active_now = false;
|
||||
|
||||
switch (input) {
|
||||
case InputAction::LEFT:
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -threshold;
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -THRESHOLD;
|
||||
break;
|
||||
case InputAction::RIGHT:
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > threshold;
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > THRESHOLD;
|
||||
break;
|
||||
case InputAction::UP:
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -threshold;
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -THRESHOLD;
|
||||
break;
|
||||
case InputAction::DOWN:
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > threshold;
|
||||
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > THRESHOLD;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
@@ -272,17 +272,15 @@ bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat)
|
||||
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;
|
||||
} // 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;
|
||||
}
|
||||
Reference in New Issue
Block a user