reestructuració
This commit is contained in:
389
source/core/input/input.cpp
Normal file
389
source/core/input/input.cpp
Normal file
@@ -0,0 +1,389 @@
|
||||
#include "core/input/input.h"
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <iostream> // for basic_ostream, operator<<, cout, basi...
|
||||
|
||||
// Emscripten-only: SDL 3.4+ ja no casa el GUID dels mandos de Chrome Android
|
||||
// amb gamecontrollerdb (el gamepad.id d'Android no porta Vendor/Product, el
|
||||
// parser extreu valors escombraries, el GUID resultant no està a la db i el
|
||||
// gamepad queda obert amb un mapping incorrecte). Com el W3C Gamepad API
|
||||
// garanteix el layout estàndard quan el navegador reporta mapping=="standard",
|
||||
// injectem un mapping SDL amb eixe layout per al GUID del joystick abans
|
||||
// d'obrir-lo com gamepad. Fora d'Emscripten és un no-op.
|
||||
static void installWebStandardMapping(SDL_JoystickID jid) {
|
||||
#ifdef __EMSCRIPTEN__
|
||||
SDL_GUID guid = SDL_GetJoystickGUIDForID(jid);
|
||||
char guidStr[33];
|
||||
SDL_GUIDToString(guid, guidStr, sizeof(guidStr));
|
||||
const char *name = SDL_GetJoystickNameForID(jid);
|
||||
if (!name || !*name) name = "Standard Gamepad";
|
||||
|
||||
char mapping[512];
|
||||
SDL_snprintf(mapping, sizeof(mapping),
|
||||
"%s,%s,"
|
||||
"a:b0,b:b1,x:b2,y:b3,"
|
||||
"leftshoulder:b4,rightshoulder:b5,"
|
||||
"lefttrigger:b6,righttrigger:b7,"
|
||||
"back:b8,start:b9,"
|
||||
"leftstick:b10,rightstick:b11,"
|
||||
"dpup:b12,dpdown:b13,dpleft:b14,dpright:b15,"
|
||||
"guide:b16,"
|
||||
"leftx:a0,lefty:a1,rightx:a2,righty:a3,"
|
||||
"platform:Emscripten",
|
||||
guidStr,
|
||||
name);
|
||||
SDL_AddGamepadMapping(mapping);
|
||||
#else
|
||||
(void)jid;
|
||||
#endif
|
||||
}
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string file) {
|
||||
// Fichero gamecontrollerdb.txt
|
||||
dbPath = file;
|
||||
|
||||
// Inicializa las variables
|
||||
keyBindings_t kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
keyBindings.resize(input_number_of_inputs, kb);
|
||||
|
||||
GameControllerBindings_t gcb;
|
||||
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||
|
||||
numGamepads = 0;
|
||||
verbose = true;
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Input::~Input() {
|
||||
for (auto *pad : connectedControllers) {
|
||||
if (pad != nullptr) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
connectedControllers.clear();
|
||||
connectedControllerIds.clear();
|
||||
controllerNames.clear();
|
||||
numGamepads = 0;
|
||||
}
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void Input::update() {
|
||||
if (disabledUntil == d_keyPressed && !checkAnyInput()) {
|
||||
enable();
|
||||
}
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(Uint8 input, SDL_Scancode code) {
|
||||
keyBindings[input].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton 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) {
|
||||
const bool *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat) {
|
||||
if (keyStates[keyBindings[input].scancode]) {
|
||||
successKeyboard = true;
|
||||
} else {
|
||||
successKeyboard = false;
|
||||
}
|
||||
} else {
|
||||
if (!keyBindings[input].active) {
|
||||
if (keyStates[keyBindings[input].scancode]) {
|
||||
keyBindings[input].active = true;
|
||||
successKeyboard = true;
|
||||
} else {
|
||||
successKeyboard = false;
|
||||
}
|
||||
} else {
|
||||
if (!keyStates[keyBindings[input].scancode]) {
|
||||
keyBindings[input].active = false;
|
||||
successKeyboard = false;
|
||||
} else {
|
||||
successKeyboard = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size())
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) {
|
||||
if (repeat) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
|
||||
successGameController = true;
|
||||
} else {
|
||||
successGameController = false;
|
||||
}
|
||||
} else {
|
||||
if (!gameControllerBindings[input].active) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
|
||||
gameControllerBindings[input].active = true;
|
||||
successGameController = true;
|
||||
} else {
|
||||
successGameController = false;
|
||||
}
|
||||
} else {
|
||||
if (!SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button)) {
|
||||
gameControllerBindings[input].active = false;
|
||||
successGameController = false;
|
||||
} else {
|
||||
successGameController = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
}
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
bool Input::checkAnyInput(int device, int index) {
|
||||
if (device == INPUT_USE_ANY) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
|
||||
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (int i = 0; i < (int)keyBindings.size(); ++i) {
|
||||
if (mKeystates[keyBindings[i].scancode]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size()) {
|
||||
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) {
|
||||
for (int i = 0; i < (int)gameControllerBindings.size(); ++i) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[i].button)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// Construye el nombre visible de un mando.
|
||||
// Recorta des del primer '(' o '[' (per a evitar coses tipus
|
||||
// "Retroid Controller (vendor: 1001) ...") i talla a 25 caràcters.
|
||||
std::string Input::buildControllerName(SDL_Gamepad *pad, int padIndex) {
|
||||
(void)padIndex;
|
||||
const char *padName = SDL_GetGamepadName(pad);
|
||||
std::string name = padName ? padName : "Unknown";
|
||||
const auto pos = name.find_first_of("([");
|
||||
if (pos != std::string::npos) {
|
||||
name.erase(pos);
|
||||
}
|
||||
while (!name.empty() && name.back() == ' ') {
|
||||
name.pop_back();
|
||||
}
|
||||
if (name.size() > 25) {
|
||||
name.resize(25);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
// Busca si hay un mando conectado. Cierra y limpia el estado previo para
|
||||
// que la función sea idempotente si se invoca más de una vez.
|
||||
bool Input::discoverGameController() {
|
||||
// Cierra los mandos ya abiertos y limpia los vectores paralelos
|
||||
for (auto *pad : connectedControllers) {
|
||||
if (pad != nullptr) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
connectedControllers.clear();
|
||||
connectedControllerIds.clear();
|
||||
controllerNames.clear();
|
||||
numGamepads = 0;
|
||||
|
||||
bool found = false;
|
||||
|
||||
if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD) {
|
||||
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
|
||||
}
|
||||
|
||||
if (SDL_AddGamepadMappingsFromFile(dbPath.c_str()) < 0) {
|
||||
if (verbose) {
|
||||
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
int nJoysticks = 0;
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
|
||||
|
||||
if (joysticks) {
|
||||
int gamepadCount = 0;
|
||||
for (int i = 0; i < nJoysticks; ++i) {
|
||||
if (SDL_IsGamepad(joysticks[i])) {
|
||||
gamepadCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "\nChecking for game controllers...\n";
|
||||
std::cout << nJoysticks << " joysticks found, " << gamepadCount << " are gamepads\n";
|
||||
}
|
||||
|
||||
if (gamepadCount > 0) {
|
||||
found = true;
|
||||
int padIndex = 0;
|
||||
|
||||
for (int i = 0; i < nJoysticks; i++) {
|
||||
if (!SDL_IsGamepad(joysticks[i])) continue;
|
||||
|
||||
installWebStandardMapping(joysticks[i]);
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(joysticks[i]);
|
||||
if (pad != nullptr) {
|
||||
const std::string name = buildControllerName(pad, padIndex);
|
||||
connectedControllers.push_back(pad);
|
||||
connectedControllerIds.push_back(joysticks[i]);
|
||||
controllerNames.push_back(name);
|
||||
numGamepads++;
|
||||
padIndex++;
|
||||
if (verbose) {
|
||||
std::cout << name << std::endl;
|
||||
}
|
||||
} else {
|
||||
if (verbose) {
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetGamepadEventsEnabled(true);
|
||||
}
|
||||
|
||||
SDL_free(joysticks);
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_ADDED
|
||||
bool Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) {
|
||||
if (!SDL_IsGamepad(jid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Si el mando ya está registrado no hace nada (ej. evento retroactivo tras el scan inicial)
|
||||
for (SDL_JoystickID existing : connectedControllerIds) {
|
||||
if (existing == jid) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
installWebStandardMapping(jid);
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(jid);
|
||||
if (pad == nullptr) {
|
||||
if (verbose) {
|
||||
std::cout << "Failed to open gamepad " << jid << ": " << SDL_GetError() << std::endl;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
const int padIndex = (int)connectedControllers.size();
|
||||
const std::string name = buildControllerName(pad, padIndex);
|
||||
connectedControllers.push_back(pad);
|
||||
connectedControllerIds.push_back(jid);
|
||||
controllerNames.push_back(name);
|
||||
numGamepads++;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Gamepad connected: " << name << std::endl;
|
||||
}
|
||||
|
||||
outName = name;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED
|
||||
bool Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) {
|
||||
for (size_t i = 0; i < connectedControllerIds.size(); ++i) {
|
||||
if (connectedControllerIds[i] != jid) continue;
|
||||
|
||||
outName = controllerNames[i];
|
||||
if (connectedControllers[i] != nullptr) {
|
||||
SDL_CloseGamepad(connectedControllers[i]);
|
||||
}
|
||||
connectedControllers.erase(connectedControllers.begin() + i);
|
||||
connectedControllerIds.erase(connectedControllerIds.begin() + i);
|
||||
controllerNames.erase(controllerNames.begin() + i);
|
||||
numGamepads--;
|
||||
if (numGamepads < 0) numGamepads = 0;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Gamepad disconnected: " << outName << std::endl;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool Input::gameControllerFound() {
|
||||
if (numGamepads > 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
std::string Input::getControllerName(int index) {
|
||||
if (numGamepads > 0) {
|
||||
return controllerNames[index];
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
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;
|
||||
}
|
||||
126
source/core/input/input.h
Normal file
126
source/core/input/input.h
Normal file
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
// Valores de repetición
|
||||
constexpr bool REPEAT_TRUE = true;
|
||||
constexpr bool REPEAT_FALSE = false;
|
||||
|
||||
// Métodos de entrada
|
||||
constexpr int INPUT_USE_KEYBOARD = 0;
|
||||
constexpr int INPUT_USE_GAMECONTROLLER = 1;
|
||||
constexpr int INPUT_USE_ANY = 2;
|
||||
|
||||
enum inputs_e {
|
||||
// Inputs obligatorios
|
||||
input_null,
|
||||
input_up,
|
||||
input_down,
|
||||
input_left,
|
||||
input_right,
|
||||
input_pause,
|
||||
input_exit,
|
||||
input_accept,
|
||||
input_cancel,
|
||||
|
||||
// Inputs personalizados
|
||||
input_fire_left,
|
||||
input_fire_center,
|
||||
input_fire_right,
|
||||
input_window_fullscreen,
|
||||
input_window_inc_size,
|
||||
input_window_dec_size,
|
||||
|
||||
// Input obligatorio
|
||||
input_number_of_inputs
|
||||
};
|
||||
|
||||
enum i_disable_e {
|
||||
d_notDisabled,
|
||||
d_forever,
|
||||
d_keyPressed
|
||||
};
|
||||
|
||||
class Input {
|
||||
private:
|
||||
struct keyBindings_t {
|
||||
Uint8 scancode; // Scancode asociado
|
||||
bool active; // Indica si está activo
|
||||
};
|
||||
|
||||
struct GameControllerBindings_t {
|
||||
SDL_GamepadButton button; // GameControllerButton asociado
|
||||
bool active; // Indica si está activo
|
||||
};
|
||||
|
||||
// Objetos y punteros
|
||||
std::vector<SDL_Gamepad *> connectedControllers; // Vector con todos los mandos conectados
|
||||
std::vector<SDL_JoystickID> connectedControllerIds; // Instance IDs paralelos para mapear eventos
|
||||
|
||||
// Variables
|
||||
std::vector<keyBindings_t> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<GameControllerBindings_t> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
|
||||
int numGamepads; // Numero de mandos conectados
|
||||
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose; // Indica si ha de mostrar mensajes
|
||||
i_disable_e disabledUntil; // Tiempo que esta deshabilitado
|
||||
bool enabled; // Indica si está habilitado
|
||||
|
||||
// Construye el nombre visible de un mando (name truncado + sufijo #N)
|
||||
std::string buildControllerName(SDL_Gamepad *pad, int padIndex);
|
||||
|
||||
public:
|
||||
// Constructor
|
||||
Input(std::string file);
|
||||
|
||||
// Destructor
|
||||
~Input();
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void update();
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void bindKey(Uint8 input, SDL_Scancode code);
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void bindGameControllerButton(Uint8 input, SDL_GamepadButton button);
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
bool checkInput(Uint8 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);
|
||||
|
||||
// Busca si hay un mando conectado
|
||||
bool discoverGameController();
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_ADDED. Devuelve true si el mando se ha añadido
|
||||
// (no estaba ya registrado) y escribe el nombre visible en outName.
|
||||
bool handleGamepadAdded(SDL_JoystickID jid, std::string &outName);
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED. Devuelve true si se ha encontrado y
|
||||
// eliminado, y escribe el nombre visible en outName.
|
||||
bool handleGamepadRemoved(SDL_JoystickID jid, std::string &outName);
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
bool gameControllerFound();
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
int getNumControllers();
|
||||
|
||||
// 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();
|
||||
};
|
||||
35
source/core/input/mouse.cpp
Normal file
35
source/core/input/mouse.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "core/input/mouse.hpp"
|
||||
|
||||
namespace Mouse {
|
||||
Uint32 cursorHideTime = 3000; // Tiempo en milisegundos para ocultar el cursor por inactividad
|
||||
Uint32 lastMouseMoveTime = 0; // Última vez que el ratón se movió
|
||||
bool cursorVisible = true; // Estado del cursor
|
||||
|
||||
void handleEvent(const SDL_Event &event, bool fullscreen) {
|
||||
if (event.type == SDL_EVENT_MOUSE_MOTION) {
|
||||
lastMouseMoveTime = SDL_GetTicks();
|
||||
if (!cursorVisible && !fullscreen) {
|
||||
SDL_ShowCursor();
|
||||
cursorVisible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void updateCursorVisibility(bool fullscreen) {
|
||||
// En pantalla completa el cursor siempre está oculto
|
||||
if (fullscreen) {
|
||||
if (cursorVisible) {
|
||||
SDL_HideCursor();
|
||||
cursorVisible = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// En modo ventana, lo oculta tras el periodo de inactividad
|
||||
const Uint32 currentTime = SDL_GetTicks();
|
||||
if (cursorVisible && (currentTime - lastMouseMoveTime > cursorHideTime)) {
|
||||
SDL_HideCursor();
|
||||
cursorVisible = false;
|
||||
}
|
||||
}
|
||||
} // namespace Mouse
|
||||
18
source/core/input/mouse.hpp
Normal file
18
source/core/input/mouse.hpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
namespace Mouse {
|
||||
extern Uint32 cursorHideTime; // Tiempo en milisegundos para ocultar el cursor por inactividad
|
||||
extern Uint32 lastMouseMoveTime; // Última vez que el ratón se movió
|
||||
extern bool cursorVisible; // Estado del cursor
|
||||
|
||||
// Procesa un evento de ratón. En pantalla completa ignora el movimiento
|
||||
// para no volver a mostrar el cursor.
|
||||
void handleEvent(const SDL_Event &event, bool fullscreen);
|
||||
|
||||
// Actualiza la visibilidad del cursor. En modo ventana lo oculta
|
||||
// después de cursorHideTime ms sin movimiento. En pantalla completa
|
||||
// lo mantiene siempre oculto.
|
||||
void updateCursorVisibility(bool fullscreen);
|
||||
} // namespace Mouse
|
||||
Reference in New Issue
Block a user