This commit is contained in:
2024-09-16 22:21:57 +02:00
parent 80425a5ed0
commit 159a75a60e
12 changed files with 384 additions and 307 deletions

View File

@@ -65,17 +65,135 @@ void Input::bindKey(inputs_e input, SDL_Scancode code)
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button)
{
if (index >= (int)connectedControllers.size())
if (index < numGamepads)
{
return;
gameControllerBindings[index][input].button = button;
}
}
// Asigna inputs a botones del mando
void Input::bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource)
{
if (index < numGamepads)
{
gameControllerBindings[index][inputTarget].button = gameControllerBindings[index][inputSource].button;
}
gameControllerBindings[index][input].button = button;
}
// Comprueba si un input esta activo
bool Input::checkInput(inputs_e input, bool repeat, int device, int index)
{
if (!enabled)
if (!enabled || index >= numGamepads)
{
return false;
}
bool successKeyboard = false;
bool successGameController = false;
if (device == INPUT_USE_ANY)
{
index = 0;
}
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY)
{
const Uint8 *keyStates = SDL_GetKeyboardState(nullptr);
if (repeat)
{
if (keyStates[keyBindings[input].scancode] != 0)
{
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (!keyBindings[input].active)
{
if (keyStates[keyBindings[input].scancode] != 0)
{
keyBindings[input].active = true;
successKeyboard = true;
}
else
{
successKeyboard = false;
}
}
else
{
if (keyStates[keyBindings[input].scancode] == 0)
{
keyBindings[input].active = false;
successKeyboard = false;
}
else
{
successKeyboard = false;
}
}
}
}
if (gameControllerFound())
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY))
{
successGameController = checkAxisInput(input, index);
if (!successGameController)
{
if (repeat)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
{
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (!gameControllerBindings[index][input].active)
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) != 0)
{
gameControllerBindings[index][input].active = true;
successGameController = true;
}
else
{
successGameController = false;
}
}
else
{
if (SDL_GameControllerGetButton(connectedControllers[index], gameControllerBindings[index][input].button) == 0)
{
gameControllerBindings[index][input].active = false;
successGameController = false;
}
else
{
successGameController = false;
}
}
}
}
}
return (successKeyboard || successGameController);
}
// Comprueba si un input con modificador esta activo
bool Input::checkModInput(inputs_e inputMod, inputs_e input, bool repeat, int device, int index)
{
if (!enabled || index >= numGamepads || !checkInput(inputMod, INPUT_ALLOW_REPEAT, device, index))
{
return false;
}
@@ -331,27 +449,13 @@ bool Input::discoverGameControllers()
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound()
{
if (numGamepads > 0)
{
return true;
}
else
{
return false;
}
return numGamepads > 0 ? true : false;
}
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int index)
{
if (numGamepads > 0)
{
return controllerNames[index];
}
else
{
return "";
}
return numGamepads > 0 ? controllerNames[index] : "";
}
// Obten el número de mandos conectados
@@ -396,12 +500,7 @@ int Input::getJoyIndex(int id)
// Muestra por consola los controles asignados
void Input::printBindings(int device, int index)
{
if (device == INPUT_USE_ANY)
{
return;
}
if (device == INPUT_USE_KEYBOARD)
if (device == INPUT_USE_ANY || device == INPUT_USE_KEYBOARD)
{
return;
}
@@ -467,6 +566,11 @@ std::string Input::to_string(inputs_e input)
return "input_start";
}
if (input == input_service)
{
return "input_service";
}
return "";
}
@@ -493,6 +597,11 @@ inputs_e Input::to_inputs_e(std::string name)
return input_start;
}
if (name == "input_service")
{
return input_service;
}
return input_null;
}
@@ -509,7 +618,7 @@ void Input::allActive(int index)
bool Input::checkAxisInput(inputs_e input, int index)
{
bool success = false;
switch (input)
{
case input_left:

View File

@@ -38,10 +38,12 @@ enum inputs_e
input_reset,
input_mute,
input_showinfo,
input_config,
input_swap_controllers,
// Input obligatorio
input_null,
input_number_of_inputs
input_number_of_inputs,
};
#define INPUT_ALLOW_REPEAT true
@@ -103,10 +105,14 @@ public:
// Asigna inputs a botones del mando
void bindGameControllerButton(int index, inputs_e input, SDL_GameControllerButton button);
void bindGameControllerButton(int index, inputs_e inputTarget, inputs_e inputSource);
// Comprueba si un input esta activo
bool checkInput(inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
// Comprueba si un input con modificador esta activo
bool checkModInput(inputs_e inputMod, inputs_e input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0);
// Comprueba si hay almenos un input activo
bool checkAnyInput(int device = INPUT_USE_ANY, int index = 0);

View File

@@ -7,7 +7,6 @@
#include "jshader.h"
#endif
#include "dbgtxt.h"
#include "../service.h"
// Constructor
Screen::Screen(SDL_Window *window, SDL_Renderer *renderer, Asset *asset, Input *input, options_t *options)
@@ -320,47 +319,63 @@ void Screen::update()
void Screen::checkInput()
{
#ifndef ARCADE
if (input->checkInput(input_window_fullscreen, INPUT_DO_NOT_ALLOW_REPEAT))
// Comprueba el teclado para cambiar entre pantalla completa y ventana
if (input->checkInput(input_window_fullscreen, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
switchVideoMode();
const std::string mode = options->video.mode == SCREEN_VIDEO_MODE_WINDOW ? "Window" : "Fullscreen";
showNotification(mode + " mode");
return;
}
else if (input->checkInput(input_window_dec_size, INPUT_DO_NOT_ALLOW_REPEAT))
// Comprueba el teclado para decrementar el tamaño de la ventana
if (input->checkInput(input_window_dec_size, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
decWindowSize();
const std::string size = std::to_string(options->video.window.size);
showNotification("Window size x" + size);
return;
}
else if (input->checkInput(input_window_inc_size, INPUT_DO_NOT_ALLOW_REPEAT))
// Comprueba el teclado para incrementar el tamaño de la ventana
if (input->checkInput(input_window_inc_size, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
incWindowSize();
const std::string size = std::to_string(options->video.window.size);
showNotification("Window size x" + size);
return;
}
#endif
if (input->checkInput(input_video_shaders, INPUT_DO_NOT_ALLOW_REPEAT))
// Comprueba el teclado para activar o desactivar los shaders
if (input->checkInput(input_video_shaders, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
switchShaders();
return;
}
else if (input->checkInput(input_showinfo, INPUT_DO_NOT_ALLOW_REPEAT))
// Comprueba el teclado para mostrar la información de debug
if (input->checkInput(input_showinfo, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_KEYBOARD))
{
showInfo = !showInfo;
return;
}
// Comprueba el botón de SERVICE
switch (checkServiceButton(input))
for (int i = 0; i < input->getNumControllers(); ++i)
{
case SERVICE_SHADERS:
switchShaders();
break;
// Comprueba los mandos para activar o desactivar los shaders
if (input->checkModInput(input_service, input_video_shaders, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
switchShaders();
return;
}
default:
break;
// Comprueba los mandos para mostrar la información de debug
if (input->checkModInput(input_service, input_showinfo, INPUT_DO_NOT_ALLOW_REPEAT, INPUT_USE_GAMECONTROLLER, i))
{
showInfo = !showInfo;
return;
}
}
}