diff --git a/source/common/input.cpp b/source/common/input.cpp index a4210c9..f79be62 100644 --- a/source/common/input.cpp +++ b/source/common/input.cpp @@ -18,35 +18,48 @@ Input::Input(std::string file) gcb.active = false; gameControllerBindings.resize(17, gcb); - // Comprueba si hay un mando conectado - discoverGameController(); + verbose = true; + enabled = true; + + numGamepads = 0; } -// Destructor -Input::~Input() +// Actualiza el estado del objeto +void Input::update() { + if (disabledUntil == d_keyPressed && !checkAnyInput()) + { + enable(); + } } -// Asigna uno de los posibles inputs a una tecla del teclado +// Asigna inputs a teclas void Input::bindKey(Uint8 input, SDL_Scancode code) { - keyBindings.at(input).scancode = code; + keyBindings[input].scancode = code; } -// Asigna uno de los posibles inputs a un botón del mando +// Asigna inputs a botones del mando void Input::bindGameControllerButton(Uint8 input, SDL_GameControllerButton button) { - gameControllerBindings.at(input).button = button; + gameControllerBindings[input].button = button; } // Comprueba si un input esta activo bool Input::checkInput(Uint8 input, bool repeat, int device, int index) { + if (!enabled) + { + return false; + } + bool successKeyboard = false; bool successGameController = false; if (device == INPUT_USE_ANY) + { index = 0; + } if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) { @@ -54,7 +67,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) if (repeat) { - if (keyStates[keyBindings.at(input).scancode] != 0) + if (keyStates[keyBindings[input].scancode] != 0) { successKeyboard = true; } @@ -65,11 +78,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (!keyBindings.at(input).active) + if (!keyBindings[input].active) { - if (keyStates[keyBindings.at(input).scancode] != 0) + if (keyStates[keyBindings[input].scancode] != 0) { - keyBindings.at(input).active = true; + keyBindings[input].active = true; successKeyboard = true; } else @@ -79,9 +92,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (keyStates[keyBindings.at(input).scancode] == 0) + if (keyStates[keyBindings[input].scancode] == 0) { - keyBindings.at(input).active = false; + keyBindings[input].active = false; successKeyboard = false; } else @@ -97,7 +110,7 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) { if (repeat) { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0) { successGameController = true; } @@ -108,11 +121,11 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (!gameControllerBindings.at(input).active) + if (!gameControllerBindings[input].active) { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) != 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) != 0) { - gameControllerBindings.at(input).active = true; + gameControllerBindings[input].active = true; successGameController = true; } else @@ -122,9 +135,9 @@ bool Input::checkInput(Uint8 input, bool repeat, int device, int index) } else { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(input).button) == 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[input].button) == 0) { - gameControllerBindings.at(input).active = false; + gameControllerBindings[input].active = false; successGameController = false; } else @@ -152,7 +165,7 @@ bool Input::checkAnyInput(int device, int index) for (int i = 0; i < (int)keyBindings.size(); ++i) { - if (mKeystates[keyBindings.at(i).scancode] != 0) + if (mKeystates[keyBindings[i].scancode] != 0) { return true; } @@ -165,7 +178,7 @@ bool Input::checkAnyInput(int device, int index) { for (int i = 0; i < (int)gameControllerBindings.size(); ++i) { - if (SDL_GameControllerGetButton(connectedControllers.at(index), gameControllerBindings.at(i).button) != 0) + if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[i].button) != 0) { return true; } @@ -176,7 +189,7 @@ bool Input::checkAnyInput(int device, int index) return false; } -// Comprueba si hay un mando conectado +// Busca si hay un mando conectado bool Input::discoverGameController() { bool found = false; @@ -188,7 +201,10 @@ bool Input::discoverGameController() if (SDL_GameControllerAddMappingsFromFile(dbPath.c_str()) < 0) { - printf("Error, could not load %s file: %s\n", dbPath.c_str(), SDL_GetError()); + if (verbose) + { + std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl; + } } const int nJoysticks = SDL_NumJoysticks(); @@ -203,8 +219,11 @@ bool Input::discoverGameController() } } - printf("\nChecking for game controllers...\n"); - printf("%i joysticks found, %i are gamepads\n", nJoysticks, numGamepads); + if (verbose) + { + std::cout << "\nChecking for game controllers...\n"; + std::cout << nJoysticks << " joysticks found, " << numGamepads << " are gamepads\n"; + } if (numGamepads > 0) { @@ -221,12 +240,18 @@ bool Input::discoverGameController() std::string name = SDL_GameControllerNameForIndex(i); name.resize(25); name = name + separator + std::to_string(i); - std::cout << name << std::endl; + if (verbose) + { + std::cout << name << std::endl; + } controllerNames.push_back(name); } else { - std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl; + if (verbose) + { + std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl; + } } } @@ -254,7 +279,7 @@ std::string Input::getControllerName(int index) { if (numGamepads > 0) { - return controllerNames.at(index); + return controllerNames[index]; } else { @@ -266,4 +291,24 @@ std::string Input::getControllerName(int index) int Input::getNumControllers() { 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; } \ No newline at end of file diff --git a/source/common/input.h b/source/common/input.h index f22b0af..42a0437 100644 --- a/source/common/input.h +++ b/source/common/input.h @@ -32,7 +32,13 @@ #define INPUT_USE_GAMECONTROLLER 1 #define INPUT_USE_ANY 2 -// Clase Input +enum i_disable_e +{ + d_notDisabled, + d_forever, + d_keyPressed +}; + class Input { private: @@ -48,34 +54,40 @@ private: bool active; // Indica si está activo }; + // Objetos y punteros + std::vector connectedControllers; // Vector con todos los mandos conectados + + // Variables std::vector keyBindings; // Vector con las teclas asociadas a los inputs predefinidos std::vector gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos - std::vector connectedControllers; // Vector con todos los mandos conectados std::vector controllerNames; // Vector con los nombres de los mandos int numGamepads; // Numero de mandos conectados std::string dbPath; // Ruta al archivo gamecontrollerdb.txt - - // Comprueba si hay un mando conectado - bool discoverGameController(); + bool verbose; // Indica si ha de mostrar mensajes + i_disable_e disabledUntil; // Tiempo que esta deshabilitado + bool enabled; // Indica si está habilitado public: // Constructor Input(std::string file); - // Destructor - ~Input(); + // Actualiza el estado del objeto + void update(); - // Asigna uno de los posibles inputs a una tecla del teclado + // Asigna inputs a teclas void bindKey(Uint8 input, SDL_Scancode code); - // Asigna uno de los posibles inputs a un botón del mando + // Asigna inputs a botones del mando void bindGameControllerButton(Uint8 input, SDL_GameControllerButton button); // Comprueba si un input esta activo bool checkInput(Uint8 input, bool repeat, int device = INPUT_USE_ANY, int index = 0); // Comprueba si hay almenos un input activo - bool checkAnyInput(int device, int index); + bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0); + + // Busca si hay un mando conectado + bool discoverGameController(); // Comprueba si hay algun mando conectado bool gameControllerFound(); @@ -85,6 +97,15 @@ public: // Obten el nombre de un mando de juego std::string getControllerName(int index); + + // Establece si ha de mostrar mensajes + void setVerbose(bool value); + + // Deshabilita las entradas durante un periodo de tiempo + void disableUntil(i_disable_e value); + + // Hablita las entradas + void enable(); }; #endif diff --git a/source/title.cpp b/source/title.cpp index f4bc589..f7023f8 100644 --- a/source/title.cpp +++ b/source/title.cpp @@ -1102,6 +1102,7 @@ void Title::createTiledBackground() void Title::checkInputDevices() { printf("Filling devices for options menu...\n"); + input->discoverGameController(); const int numControllers = input->getNumControllers(); availableInputDevices.clear(); input_t temp;