Variables renombrades en input.cpp
This commit is contained in:
384
source/input.cpp
384
source/input.cpp
@@ -6,122 +6,108 @@
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basi...
|
||||
|
||||
// [SINGLETON] Hay que definir las variables estáticas, desde el .h sólo la hemos declarado
|
||||
Input *Input::input = nullptr;
|
||||
Input *Input::input_ = nullptr;
|
||||
|
||||
// [SINGLETON] Crearemos el objeto input con esta función estática
|
||||
void Input::init(std::string dbPath)
|
||||
void Input::init(std::string game_controller_db_path)
|
||||
{
|
||||
Input::input = new Input(dbPath);
|
||||
Input::input_ = new Input(game_controller_db_path);
|
||||
}
|
||||
|
||||
// [SINGLETON] Destruiremos el objeto input con esta función estática
|
||||
void Input::destroy()
|
||||
{
|
||||
delete Input::input;
|
||||
delete Input::input_;
|
||||
}
|
||||
|
||||
// [SINGLETON] Con este método obtenemos el objeto input y podemos trabajar con él
|
||||
Input *Input::get()
|
||||
{
|
||||
return Input::input;
|
||||
return Input::input_;
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string dbPath)
|
||||
: dbPath(dbPath)
|
||||
Input::Input(std::string game_controller_db_path)
|
||||
: game_controller_db_path_(game_controller_db_path)
|
||||
{
|
||||
// Inicializa variables
|
||||
verbose = false;
|
||||
enabled = true;
|
||||
enabled_ = true;
|
||||
|
||||
// Busca si hay mandos conectados
|
||||
discoverGameControllers();
|
||||
|
||||
// Inicializa las vectores
|
||||
keyBindings_t kb;
|
||||
KeyBindings kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
keyBindings.resize(input_number_of_inputs, kb);
|
||||
key_bindings_.resize(static_cast<int>(InputType::NUMBER_OF_INPUTS), kb);
|
||||
|
||||
GameControllerBindings_t gcb;
|
||||
ControllerBindings gcb;
|
||||
gcb.button = SDL_CONTROLLER_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(numGamepads);
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
controller_bindings_.resize(num_gamepads_);
|
||||
for (int i = 0; i < num_gamepads_; ++i)
|
||||
{
|
||||
gameControllerBindings[i].resize(input_number_of_inputs, gcb);
|
||||
controller_bindings_[i].resize(static_cast<int>(InputType::NUMBER_OF_INPUTS), gcb);
|
||||
}
|
||||
|
||||
// Listado de los inputs usados para jugar, excluyendo botones para la interfaz
|
||||
gameInputs.clear();
|
||||
gameInputs.push_back(input_fire_left);
|
||||
gameInputs.push_back(input_fire_center);
|
||||
gameInputs.push_back(input_fire_right);
|
||||
gameInputs.push_back(input_up);
|
||||
gameInputs.push_back(input_down);
|
||||
gameInputs.push_back(input_left);
|
||||
gameInputs.push_back(input_right);
|
||||
game_inputs_.clear();
|
||||
game_inputs_.push_back(InputType::FIRE_LEFT);
|
||||
game_inputs_.push_back(InputType::FIRE_CENTER);
|
||||
game_inputs_.push_back(InputType::FIRE_RIGHT);
|
||||
game_inputs_.push_back(InputType::UP);
|
||||
game_inputs_.push_back(InputType::DOWN);
|
||||
game_inputs_.push_back(InputType::LEFT);
|
||||
game_inputs_.push_back(InputType::RIGHT);
|
||||
|
||||
// Listado de los inputs para jugar que utilizan botones, ni palancas ni crucetas
|
||||
buttonInputs.clear();
|
||||
buttonInputs.push_back(input_fire_left);
|
||||
buttonInputs.push_back(input_fire_center);
|
||||
buttonInputs.push_back(input_fire_right);
|
||||
buttonInputs.push_back(input_start);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Input::~Input()
|
||||
{
|
||||
}
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void Input::update()
|
||||
{
|
||||
if (disabledUntil == d_keyPressed && !checkAnyInput())
|
||||
{
|
||||
enable();
|
||||
}
|
||||
button_inputs_.clear();
|
||||
button_inputs_.push_back(InputType::FIRE_LEFT);
|
||||
button_inputs_.push_back(InputType::FIRE_CENTER);
|
||||
button_inputs_.push_back(InputType::FIRE_RIGHT);
|
||||
button_inputs_.push_back(InputType::START);
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(inputs_e input, SDL_Scancode code)
|
||||
void Input::bindKey(InputType input, SDL_Scancode code)
|
||||
{
|
||||
keyBindings[input].scancode = code;
|
||||
key_bindings_[static_cast<int>(input)].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button)
|
||||
void Input::bindGameControllerButton(int controller_index, InputType input, SDL_GameControllerButton button)
|
||||
{
|
||||
if (index < numGamepads)
|
||||
if (controller_index < num_gamepads_)
|
||||
{
|
||||
gameControllerBindings[index][input].button = button;
|
||||
controller_bindings_[controller_index][static_cast<int>(input)].button = button;
|
||||
}
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource)
|
||||
void Input::bindGameControllerButton(int controller_index, InputType input_target, InputType input_source)
|
||||
{
|
||||
if (index < numGamepads)
|
||||
if (controller_index < num_gamepads_)
|
||||
{
|
||||
gameControllerBindings[index][inputTarget].button = gameControllerBindings[index][inputSource].button;
|
||||
controller_bindings_[controller_index][static_cast<int>(input_target)].button = controller_bindings_[controller_index][static_cast<int>(input_source)].button;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
|
||||
bool Input::checkInput(InputType input, bool repeat, int device, int controller_index)
|
||||
{
|
||||
if (!enabled)
|
||||
if (!enabled_)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool successKeyboard = false;
|
||||
bool successGameController = false;
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
const int input_index = static_cast<int>(input);
|
||||
|
||||
if (device == INPUT_USE_ANY)
|
||||
{
|
||||
index = 0;
|
||||
controller_index = 0;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||
@@ -130,108 +116,109 @@ bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
successKeyboard = true;
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!keyBindings[input].active)
|
||||
if (!key_bindings_[input_index].active)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
keyBindings[input].active = true;
|
||||
successKeyboard = true;
|
||||
key_bindings_[input_index].active = true;
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] == 0)
|
||||
if (keyStates[key_bindings_[input_index].scancode] == 0)
|
||||
{
|
||||
keyBindings[input].active = false;
|
||||
successKeyboard = false;
|
||||
key_bindings_[input_index].active = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index < numGamepads)
|
||||
if (gameControllerFound() && controller_index < num_gamepads_)
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
successGameController = checkAxisInput(input, index);
|
||||
if (!successGameController)
|
||||
success_controller = checkAxisInput(input, controller_index);
|
||||
if (!success_controller)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0)
|
||||
{
|
||||
successGameController = true;
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!gameControllerBindings[index][input].active)
|
||||
if (!controller_bindings_[controller_index][input_index].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0)
|
||||
{
|
||||
gameControllerBindings[index][input].active = true;
|
||||
successGameController = true;
|
||||
controller_bindings_[controller_index][input_index].active = true;
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) == 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) == 0)
|
||||
{
|
||||
gameControllerBindings[index][input].active = false;
|
||||
successGameController = false;
|
||||
controller_bindings_[controller_index][input_index].active = false;
|
||||
success_controller = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
return (success_keyboard || success_controller);
|
||||
}
|
||||
|
||||
// Comprueba si un input con modificador esta activo
|
||||
bool Input::checkModInput(inputs_e inputMod, inputs_e input, bool repeat, int device, int index)
|
||||
bool Input::checkModInput(InputType input_mod, InputType input, bool repeat, int device, int controller_index)
|
||||
{
|
||||
if (!enabled || index >= numGamepads || !checkInput(inputMod, INPUT_ALLOW_REPEAT, device, index))
|
||||
if (!enabled_ || controller_index >= num_gamepads_ || !checkInput(input_mod, INPUT_ALLOW_REPEAT, device, controller_index))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
bool successKeyboard = false;
|
||||
bool successGameController = false;
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
const int input_index = static_cast<int>(input);
|
||||
|
||||
if (device == INPUT_USE_ANY)
|
||||
{
|
||||
index = 0;
|
||||
controller_index = 0;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||
@@ -240,111 +227,111 @@ bool Input::checkModInput(inputs_e inputMod, inputs_e input, bool repeat, int de
|
||||
|
||||
if (repeat)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
successKeyboard = true;
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!keyBindings[input].active)
|
||||
if (!key_bindings_[input_index].active)
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] != 0)
|
||||
if (keyStates[key_bindings_[input_index].scancode] != 0)
|
||||
{
|
||||
keyBindings[input].active = true;
|
||||
successKeyboard = true;
|
||||
key_bindings_[input_index].active = true;
|
||||
success_keyboard = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyStates[keyBindings[input].scancode] == 0)
|
||||
if (keyStates[key_bindings_[input_index].scancode] == 0)
|
||||
{
|
||||
keyBindings[input].active = false;
|
||||
successKeyboard = false;
|
||||
key_bindings_[input_index].active = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successKeyboard = false;
|
||||
success_keyboard = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index < numGamepads)
|
||||
if (gameControllerFound() && controller_index < num_gamepads_)
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
|
||||
{
|
||||
successGameController = checkAxisInput(input, index);
|
||||
if (!successGameController)
|
||||
success_controller = checkAxisInput(input, controller_index);
|
||||
if (!success_controller)
|
||||
{
|
||||
if (repeat)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0)
|
||||
{
|
||||
successGameController = true;
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!gameControllerBindings[index][input].active)
|
||||
if (!controller_bindings_[controller_index][input_index].active)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) != 0)
|
||||
{
|
||||
gameControllerBindings[index][input].active = true;
|
||||
successGameController = true;
|
||||
controller_bindings_[controller_index][input_index].active = true;
|
||||
success_controller = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) == 0)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][input_index].button) == 0)
|
||||
{
|
||||
gameControllerBindings[index][input].active = false;
|
||||
successGameController = false;
|
||||
controller_bindings_[controller_index][input_index].active = false;
|
||||
success_controller = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
successGameController = false;
|
||||
success_controller = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
return (success_keyboard || success_controller);
|
||||
}
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool Input::checkAnyInput(int device, int index)
|
||||
bool Input::checkAnyInput(int device, int controller_index)
|
||||
{
|
||||
if (device == INPUT_USE_ANY)
|
||||
{
|
||||
index = 0;
|
||||
controller_index = 0;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
|
||||
{
|
||||
const Uint8 *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < (int)keyBindings.size(); ++i)
|
||||
for (int i = 0; i < (int)key_bindings_.size(); ++i)
|
||||
{
|
||||
if (mKeystates[keyBindings[i].scancode] != 0 && !keyBindings[i].active)
|
||||
if (mKeystates[key_bindings_[i].scancode] != 0 && !key_bindings_[i].active)
|
||||
{
|
||||
keyBindings[i].active = true;
|
||||
key_bindings_[i].active = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -354,11 +341,11 @@ bool Input::checkAnyInput(int device, int index)
|
||||
{
|
||||
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY)
|
||||
{
|
||||
for (int i = 0; i < (int)gameControllerBindings.size(); ++i)
|
||||
for (int i = 0; i < (int)controller_bindings_.size(); ++i)
|
||||
{
|
||||
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][i].button) != 0 && !gameControllerBindings[index][i].active)
|
||||
if (SDL_GameControllerGetButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button) != 0 && !controller_bindings_[controller_index][i].active)
|
||||
{
|
||||
gameControllerBindings[index][i].active = true;
|
||||
controller_bindings_[controller_index][i].active = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -372,13 +359,13 @@ bool Input::checkAnyInput(int device, int index)
|
||||
int Input::checkAnyButtonPressed(bool repeat)
|
||||
{
|
||||
// Si está pulsado el botón de servicio, ningún botón se puede considerar pulsado
|
||||
if (checkInput(input_service, INPUT_ALLOW_REPEAT, INPUT_USE_ANY))
|
||||
if (checkInput(InputType::SERVICE, INPUT_ALLOW_REPEAT, INPUT_USE_ANY))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Solo comprueba los botones definidos previamente
|
||||
for (auto bi : buttonInputs)
|
||||
for (auto bi : button_inputs_)
|
||||
{
|
||||
// Comprueba el teclado
|
||||
if (checkInput(bi, repeat, INPUT_USE_KEYBOARD))
|
||||
@@ -387,7 +374,7 @@ int Input::checkAnyButtonPressed(bool repeat)
|
||||
}
|
||||
|
||||
// Comprueba los mandos
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
for (int i = 0; i < num_gamepads_; ++i)
|
||||
{
|
||||
if (checkInput(bi, repeat, INPUT_USE_GAMECONTROLLER, i))
|
||||
{
|
||||
@@ -409,60 +396,64 @@ bool Input::discoverGameControllers()
|
||||
SDL_InitSubSystem(SDL_INIT_GAMECONTROLLER);
|
||||
}
|
||||
|
||||
if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0)
|
||||
if (SDL_GameControllerAddMappingsFromFile(game_controller_db_path_.c_str()) < 0)
|
||||
{
|
||||
if (verbose)
|
||||
#ifdef VERBOSE
|
||||
{
|
||||
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
std::cout << "Error, could not load " << game_controller_db_path_.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
numJoysticks = SDL_NumJoysticks();
|
||||
numGamepads = 0;
|
||||
num_joysticks_ = SDL_NumJoysticks();
|
||||
num_gamepads_ = 0;
|
||||
|
||||
// Cuenta el número de mandos
|
||||
joysticks.clear();
|
||||
for (int i = 0; i < numJoysticks; ++i)
|
||||
joysticks_.clear();
|
||||
for (int i = 0; i < num_joysticks_; ++i)
|
||||
{
|
||||
SDL_Joystick *joy = SDL_JoystickOpen(i);
|
||||
joysticks.push_back(joy);
|
||||
joysticks_.push_back(joy);
|
||||
if (SDL_IsGameController(i))
|
||||
{
|
||||
numGamepads++;
|
||||
num_gamepads_++;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose)
|
||||
#ifdef VERBOSE
|
||||
{
|
||||
std::cout << "\nChecking for game controllers...\n";
|
||||
std::cout << numJoysticks << " joysticks found, " << numGamepads << " are gamepads\n";
|
||||
std::cout << num_joysticks_ << " joysticks found, " << num_gamepads_ << " are gamepads\n";
|
||||
}
|
||||
#endif
|
||||
|
||||
if (numGamepads > 0)
|
||||
if (num_gamepads_ > 0)
|
||||
{
|
||||
found = true;
|
||||
|
||||
for (int i = 0; i < numGamepads; i++)
|
||||
for (int i = 0; i < num_gamepads_; i++)
|
||||
{
|
||||
// Abre el mando y lo añade a la lista
|
||||
SDL_GameController *pad = SDL_GameControllerOpen(i);
|
||||
if (SDL_GameControllerGetAttached(pad) == 1)
|
||||
{
|
||||
connectedControllers.push_back(pad);
|
||||
connected_controllers_.push_back(pad);
|
||||
const std::string separator(" #");
|
||||
std::string name = SDL_GameControllerNameForIndex(i);
|
||||
if (verbose)
|
||||
#ifdef VERBOSE
|
||||
{
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
controllerNames.push_back(name);
|
||||
#endif
|
||||
controller_names_.push_back(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (verbose)
|
||||
#ifdef VERBOSE
|
||||
{
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,47 +466,27 @@ bool Input::discoverGameControllers()
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound()
|
||||
{
|
||||
return numGamepads > 0 ? true : false;
|
||||
return num_gamepads_ > 0 ? true : false;
|
||||
}
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int index) const
|
||||
std::string Input::getControllerName(int controller_index) const
|
||||
{
|
||||
return numGamepads > 0 ? controllerNames[index] : "";
|
||||
return num_gamepads_ > 0 ? controller_names_[controller_index] : "";
|
||||
}
|
||||
|
||||
// Obten el número de mandos conectados
|
||||
int Input::getNumControllers() const
|
||||
{
|
||||
return numGamepads;
|
||||
}
|
||||
|
||||
// Establece si ha de mostrar mensajes
|
||||
void Input::setVerbose(bool value)
|
||||
{
|
||||
verbose = value;
|
||||
}
|
||||
|
||||
// Deshabilita las entradas durante un periodo de tiempo
|
||||
void Input::disableUntil(i_disable_e value)
|
||||
{
|
||||
disabledUntil = value;
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
// Hablita las entradas
|
||||
void Input::enable()
|
||||
{
|
||||
enabled = true;
|
||||
disabledUntil = d_notDisabled;
|
||||
return num_gamepads_;
|
||||
}
|
||||
|
||||
// Obtiene el indice del controlador a partir de un event.id
|
||||
int Input::getJoyIndex(int id) const
|
||||
{
|
||||
for (int i = 0; i < numJoysticks; ++i)
|
||||
for (int i = 0; i < num_joysticks_; ++i)
|
||||
{
|
||||
if (SDL_JoystickInstanceID(joysticks[i]) == id)
|
||||
if (SDL_JoystickInstanceID(joysticks_[i]) == id)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
@@ -524,7 +495,7 @@ int Input::getJoyIndex(int id) const
|
||||
}
|
||||
|
||||
// Muestra por consola los controles asignados
|
||||
void Input::printBindings(int device, int index) const
|
||||
void Input::printBindings(int device, int controller_index) const
|
||||
{
|
||||
if (device == INPUT_USE_ANY || device == INPUT_USE_KEYBOARD)
|
||||
{
|
||||
@@ -533,35 +504,35 @@ void Input::printBindings(int device, int index) const
|
||||
|
||||
if (device == INPUT_USE_GAMECONTROLLER)
|
||||
{
|
||||
if (index >= numGamepads)
|
||||
if (controller_index >= num_gamepads_)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Muestra el nombre del mando
|
||||
std::cout << "\n"
|
||||
<< controllerNames[index] << std::endl;
|
||||
<< controller_names_[controller_index] << std::endl;
|
||||
|
||||
// Muestra los botones asignados
|
||||
for (auto bi : buttonInputs)
|
||||
for (auto bi : button_inputs_)
|
||||
{
|
||||
std::cout << to_string(bi) << " : " << gameControllerBindings[index][bi].button << std::endl;
|
||||
std::cout << to_string(bi) << " : " << controller_bindings_[controller_index][static_cast<int>(bi)].button << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Obtiene el SDL_GameControllerButton asignado a un input
|
||||
SDL_GameControllerButton Input::getControllerBinding(int index, inputs_e input) const
|
||||
SDL_GameControllerButton Input::getControllerBinding(int controller_index, InputType input) const
|
||||
{
|
||||
return gameControllerBindings[index][input].button;
|
||||
return controller_bindings_[controller_index][static_cast<int>(input)].button;
|
||||
}
|
||||
|
||||
// Obtiene el indice a partir del nombre del mando
|
||||
int Input::getIndexByName(std::string name) const
|
||||
{
|
||||
for (int i = 0; i < numGamepads; ++i)
|
||||
for (int i = 0; i < num_gamepads_; ++i)
|
||||
{
|
||||
if (controllerNames[i] == name)
|
||||
if (controller_names_[i] == name)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
@@ -569,30 +540,30 @@ int Input::getIndexByName(std::string name) const
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convierte un inputs_e a std::string
|
||||
std::string Input::to_string(inputs_e input) const
|
||||
// Convierte un InputType a std::string
|
||||
std::string Input::to_string(InputType input) const
|
||||
{
|
||||
if (input == input_fire_left)
|
||||
if (input == InputType::FIRE_LEFT)
|
||||
{
|
||||
return "input_fire_left";
|
||||
}
|
||||
|
||||
if (input == input_fire_center)
|
||||
if (input == InputType::FIRE_CENTER)
|
||||
{
|
||||
return "input_fire_center";
|
||||
}
|
||||
|
||||
if (input == input_fire_right)
|
||||
if (input == InputType::FIRE_RIGHT)
|
||||
{
|
||||
return "input_fire_right";
|
||||
}
|
||||
|
||||
if (input == input_start)
|
||||
if (input == InputType::START)
|
||||
{
|
||||
return "input_start";
|
||||
}
|
||||
|
||||
if (input == input_service)
|
||||
if (input == InputType::SERVICE)
|
||||
{
|
||||
return "input_service";
|
||||
}
|
||||
@@ -600,76 +571,77 @@ std::string Input::to_string(inputs_e input) const
|
||||
return "";
|
||||
}
|
||||
|
||||
// Convierte un std::string a inputs_e
|
||||
inputs_e Input::to_inputs_e(std::string name) const
|
||||
// Convierte un std::string a InputType
|
||||
InputType Input::to_inputs_e(std::string name) const
|
||||
{
|
||||
if (name == "input_fire_left")
|
||||
{
|
||||
return input_fire_left;
|
||||
return InputType::FIRE_LEFT;
|
||||
}
|
||||
|
||||
if (name == "input_fire_center")
|
||||
{
|
||||
return input_fire_center;
|
||||
return InputType::FIRE_CENTER;
|
||||
}
|
||||
|
||||
if (name == "input_fire_right")
|
||||
{
|
||||
return input_fire_right;
|
||||
return InputType::FIRE_RIGHT;
|
||||
}
|
||||
|
||||
if (name == "input_start")
|
||||
{
|
||||
return input_start;
|
||||
return InputType::START;
|
||||
}
|
||||
|
||||
if (name == "input_service")
|
||||
{
|
||||
return input_service;
|
||||
return InputType::SERVICE;
|
||||
}
|
||||
|
||||
return input_null;
|
||||
return InputType::NONE;
|
||||
}
|
||||
|
||||
// Activa todos los inputs. Sirve para evitar inputs sin repeticiones pero que ya vienen pulsados cuando checkInput no estaba monitorizando
|
||||
void Input::allActive(int index)
|
||||
/*void Input::allActive(int controller_index)
|
||||
{
|
||||
for (int i = 0; i < (int)buttonInputs.size(); ++i)
|
||||
for (int i = 0; i < (int)button_inputs_.size(); ++i)
|
||||
{
|
||||
gameControllerBindings[index][i].active = true;
|
||||
controller_bindings_[controller_index][i].active = true;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Comprueba el eje del mando
|
||||
bool Input::checkAxisInput(inputs_e input, int index) const
|
||||
bool Input::checkAxisInput(InputType input, int controller_index) const
|
||||
{
|
||||
bool success = false;
|
||||
|
||||
switch (input)
|
||||
{
|
||||
case input_left:
|
||||
if (SDL_GameControllerGetAxis(connectedControllers[index], SDL_CONTROLLER_AXIS_LEFTX) < -30000)
|
||||
case InputType::LEFT:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) < -30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case input_right:
|
||||
if (SDL_GameControllerGetAxis(connectedControllers[index], SDL_CONTROLLER_AXIS_LEFTX) > 30000)
|
||||
case InputType::RIGHT:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTX) > 30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case input_up:
|
||||
if (SDL_GameControllerGetAxis(connectedControllers[index], SDL_CONTROLLER_AXIS_LEFTY) < -30000)
|
||||
case InputType::UP:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) < -30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case input_down:
|
||||
if (SDL_GameControllerGetAxis(connectedControllers[index], SDL_CONTROLLER_AXIS_LEFTY) > 30000)
|
||||
case InputType::DOWN:
|
||||
if (SDL_GameControllerGetAxis(connected_controllers_[controller_index], SDL_CONTROLLER_AXIS_LEFTY) > 30000)
|
||||
{
|
||||
success = true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user