treball en curs: correccions de tidy
This commit is contained in:
@@ -8,29 +8,29 @@ namespace GlobalInputs {
|
||||
auto handle() -> bool {
|
||||
if (Screen::get() == nullptr || Input::get() == nullptr) { return false; }
|
||||
|
||||
if (Input::get()->checkInput(input_window_fullscreen, REPEAT_FALSE)) {
|
||||
if (Input::get()->checkInput(WINDOW_FULLSCREEN, REPEAT_FALSE)) {
|
||||
Screen::get()->toggleVideoMode();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(input_window_dec_size, REPEAT_FALSE)) {
|
||||
if (Input::get()->checkInput(WINDOW_DEC_ZOOM, REPEAT_FALSE)) {
|
||||
Screen::get()->decWindowZoom();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(input_window_inc_size, REPEAT_FALSE)) {
|
||||
if (Input::get()->checkInput(WINDOW_INC_ZOOM, REPEAT_FALSE)) {
|
||||
Screen::get()->incWindowZoom();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(input_toggle_shader, REPEAT_FALSE)) {
|
||||
if (Input::get()->checkInput(TOGGLE_SHADER, REPEAT_FALSE)) {
|
||||
Screen::get()->toggleShaderEnabled();
|
||||
return true;
|
||||
}
|
||||
// F5/F6 només actuen quan el post-procesado està actiu.
|
||||
if (Screen::get()->isShaderEnabled()) {
|
||||
if (Input::get()->checkInput(input_toggle_shader_type, REPEAT_FALSE)) {
|
||||
if (Input::get()->checkInput(TOGGLE_SHADER_TYPE, REPEAT_FALSE)) {
|
||||
Screen::get()->toggleActiveShader();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(input_next_preset, REPEAT_FALSE)) {
|
||||
if (Input::get()->checkInput(NEXT_SHADER_PRESET, REPEAT_FALSE)) {
|
||||
Screen::get()->nextPreset();
|
||||
return true;
|
||||
}
|
||||
|
||||
+166
-169
@@ -45,8 +45,8 @@ static void installWebStandardMapping(SDL_JoystickID jid) {
|
||||
Input *Input::instance = nullptr;
|
||||
|
||||
// Singleton API
|
||||
void Input::init(const std::string &gameControllerDbPath) {
|
||||
Input::instance = new Input(gameControllerDbPath);
|
||||
void Input::init(const std::string &game_controller_db_path) {
|
||||
Input::instance = new Input(game_controller_db_path);
|
||||
}
|
||||
|
||||
void Input::destroy() {
|
||||
@@ -60,111 +60,99 @@ auto Input::get() -> Input * {
|
||||
|
||||
// Constructor
|
||||
Input::Input(std::string file)
|
||||
: dbPath(std::move(file)) {
|
||||
: db_path_(std::move(file)) {
|
||||
// Inicializa las variables
|
||||
KeyBindings kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
keyBindings.resize(input_number_of_inputs, kb);
|
||||
key_bindings_.resize(NUMBER_OF_INPUTS, kb);
|
||||
|
||||
GameControllerBindings gcb;
|
||||
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
gameControllerBindings.resize(input_number_of_inputs, gcb);
|
||||
game_controller_bindings_.resize(NUMBER_OF_INPUTS, gcb);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
Input::~Input() {
|
||||
for (auto *pad : connectedControllers) {
|
||||
for (auto *pad : connected_controllers_) {
|
||||
if (pad != nullptr) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
connectedControllers.clear();
|
||||
connectedControllerIds.clear();
|
||||
controllerNames.clear();
|
||||
numGamepads = 0;
|
||||
connected_controllers_.clear();
|
||||
connected_controller_ids_.clear();
|
||||
controller_names_.clear();
|
||||
num_gamepads_ = 0;
|
||||
}
|
||||
|
||||
// Actualiza el estado del objeto
|
||||
void Input::update() {
|
||||
if (disabledUntil == d_keyPressed && !checkAnyInput()) {
|
||||
if (disabled_until_ == KEY_PRESSED && !checkAnyInput()) {
|
||||
enable();
|
||||
}
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(Uint8 input, SDL_Scancode code) {
|
||||
keyBindings[input].scancode = code;
|
||||
key_bindings_[input].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button) {
|
||||
gameControllerBindings[input].button = button;
|
||||
game_controller_bindings_[input].button = button;
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
auto Input::checkInput(Uint8 input, bool repeat, int device, int index) -> bool {
|
||||
if (!enabled) {
|
||||
if (!enabled_) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool successKeyboard = false;
|
||||
bool successGameController = false;
|
||||
|
||||
if (device == INPUT_USE_ANY) {
|
||||
index = 0;
|
||||
}
|
||||
|
||||
bool success_keyboard = false;
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
|
||||
const bool *keyStates = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
if (repeat) {
|
||||
successKeyboard = keyStates[keyBindings[input].scancode];
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
success_keyboard = checkKeyboardInput(input, repeat);
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size()) {
|
||||
if ((device == INPUT_USE_GAMECONTROLLER) || (device == INPUT_USE_ANY)) {
|
||||
if (repeat) {
|
||||
successGameController = SDL_GetGamepadButton(connectedControllers[index], gameControllerBindings[input].button);
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
bool success_game_controller = false;
|
||||
if ((device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) && gameControllerFound() && index >= 0 && index < (int)connected_controllers_.size()) {
|
||||
success_game_controller = checkGameControllerInput(input, repeat, index);
|
||||
}
|
||||
|
||||
return (successKeyboard || successGameController);
|
||||
return success_keyboard || success_game_controller;
|
||||
}
|
||||
|
||||
// Helper de checkInput: comprueba el estado de una tecla
|
||||
auto Input::checkKeyboardInput(Uint8 input, bool repeat) -> bool {
|
||||
const bool *key_states = SDL_GetKeyboardState(nullptr);
|
||||
const bool IS_DOWN = key_states[key_bindings_[input].scancode];
|
||||
|
||||
if (repeat) {
|
||||
return IS_DOWN;
|
||||
}
|
||||
|
||||
// Modo edge-trigger: éxito sólo en el frame en que la tecla pasa de up a down
|
||||
const bool PRESS_EDGE = IS_DOWN && !key_bindings_[input].active;
|
||||
key_bindings_[input].active = IS_DOWN;
|
||||
return PRESS_EDGE;
|
||||
}
|
||||
|
||||
// Helper de checkInput: comprueba el estado de un botón de mando
|
||||
auto Input::checkGameControllerInput(Uint8 input, bool repeat, int index) -> bool {
|
||||
const bool IS_DOWN = SDL_GetGamepadButton(connected_controllers_[index], game_controller_bindings_[input].button);
|
||||
|
||||
if (repeat) {
|
||||
return IS_DOWN;
|
||||
}
|
||||
|
||||
// Modo edge-trigger: éxito sólo en el frame en que el botón pasa de up a down
|
||||
const bool PRESS_EDGE = IS_DOWN && !game_controller_bindings_[input].active;
|
||||
game_controller_bindings_[input].active = IS_DOWN;
|
||||
return PRESS_EDGE;
|
||||
}
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
@@ -174,19 +162,19 @@ auto Input::checkAnyInput(int device, int index) -> bool {
|
||||
}
|
||||
|
||||
if (device == INPUT_USE_KEYBOARD || device == INPUT_USE_ANY) {
|
||||
const bool *mKeystates = SDL_GetKeyboardState(nullptr);
|
||||
const bool *key_states = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (auto &keyBinding : keyBindings) {
|
||||
if (mKeystates[keyBinding.scancode]) {
|
||||
for (auto &key_binding : key_bindings_) {
|
||||
if (key_states[key_binding.scancode]) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connectedControllers.size()) {
|
||||
if (gameControllerFound() && index >= 0 && index < (int)connected_controllers_.size()) {
|
||||
if (device == INPUT_USE_GAMECONTROLLER || device == INPUT_USE_ANY) {
|
||||
for (auto &gameControllerBinding : gameControllerBindings) {
|
||||
if (SDL_GetGamepadButton(connectedControllers[index], gameControllerBinding.button)) {
|
||||
for (auto &game_controller_binding : game_controller_bindings_) {
|
||||
if (SDL_GetGamepadButton(connected_controllers_[index], game_controller_binding.button)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -199,13 +187,13 @@ auto Input::checkAnyInput(int device, int index) -> bool {
|
||||
// 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.
|
||||
auto Input::buildControllerName(SDL_Gamepad *pad, int padIndex) -> std::string {
|
||||
(void)padIndex;
|
||||
const char *padName = SDL_GetGamepadName(pad);
|
||||
std::string name = (padName != nullptr) ? padName : "Unknown";
|
||||
const auto pos = name.find_first_of("([");
|
||||
if (pos != std::string::npos) {
|
||||
name.erase(pos);
|
||||
auto Input::buildControllerName(SDL_Gamepad *pad, int pad_index) -> std::string {
|
||||
(void)pad_index;
|
||||
const char *pad_name = SDL_GetGamepadName(pad);
|
||||
std::string name = (pad_name != nullptr) ? pad_name : "Unknown";
|
||||
const auto POS = name.find_first_of("([");
|
||||
if (POS != std::string::npos) {
|
||||
name.erase(POS);
|
||||
}
|
||||
while (!name.empty() && name.back() == ' ') {
|
||||
name.pop_back();
|
||||
@@ -219,136 +207,145 @@ auto Input::buildControllerName(SDL_Gamepad *pad, int padIndex) -> std::string {
|
||||
// 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.
|
||||
auto Input::discoverGameController() -> bool {
|
||||
// Cierra los mandos ya abiertos y limpia los vectores paralelos
|
||||
for (auto *pad : connectedControllers) {
|
||||
resetGameControllerState();
|
||||
ensureGamepadSubsystem();
|
||||
|
||||
int num_joysticks = 0;
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(&num_joysticks);
|
||||
if (joysticks == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int gamepad_count = 0;
|
||||
for (int i = 0; i < num_joysticks; ++i) {
|
||||
if (SDL_IsGamepad(joysticks[i])) {
|
||||
gamepad_count++;
|
||||
}
|
||||
}
|
||||
|
||||
if (verbose_) {
|
||||
std::cout << "\nChecking for game controllers...\n";
|
||||
std::cout << num_joysticks << " joysticks found, " << gamepad_count << " are gamepads\n";
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
if (gamepad_count > 0) {
|
||||
found = true;
|
||||
int pad_index = 0;
|
||||
for (int i = 0; i < num_joysticks; i++) {
|
||||
if (!SDL_IsGamepad(joysticks[i])) {
|
||||
continue;
|
||||
}
|
||||
if (openGamepad(joysticks[i], pad_index)) {
|
||||
pad_index++;
|
||||
}
|
||||
}
|
||||
SDL_SetGamepadEventsEnabled(true);
|
||||
}
|
||||
|
||||
SDL_free(joysticks);
|
||||
return found;
|
||||
}
|
||||
|
||||
// Helper de discoverGameController: cierra mandos previos y limpia vectores paralelos
|
||||
void Input::resetGameControllerState() {
|
||||
for (auto *pad : connected_controllers_) {
|
||||
if (pad != nullptr) {
|
||||
SDL_CloseGamepad(pad);
|
||||
}
|
||||
}
|
||||
connectedControllers.clear();
|
||||
connectedControllerIds.clear();
|
||||
controllerNames.clear();
|
||||
numGamepads = 0;
|
||||
|
||||
bool found = false;
|
||||
connected_controllers_.clear();
|
||||
connected_controller_ids_.clear();
|
||||
controller_names_.clear();
|
||||
num_gamepads_ = 0;
|
||||
}
|
||||
|
||||
// Helper de discoverGameController: inicializa el subsystem de gamepad y carga el mapping
|
||||
void Input::ensureGamepadSubsystem() {
|
||||
if (SDL_WasInit(SDL_INIT_GAMEPAD) != SDL_INIT_GAMEPAD) {
|
||||
SDL_InitSubSystem(SDL_INIT_GAMEPAD);
|
||||
}
|
||||
if (SDL_AddGamepadMappingsFromFile(db_path_.c_str()) < 0 && verbose_) {
|
||||
std::cout << "Error, could not load " << db_path_.c_str() << " file: " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_AddGamepadMappingsFromFile(dbPath.c_str()) < 0) {
|
||||
if (verbose) {
|
||||
std::cout << "Error, could not load " << dbPath.c_str() << " file: " << SDL_GetError() << '\n';
|
||||
// Helper de discoverGameController: abre y registra un mando. Devuelve true si tuvo éxito.
|
||||
auto Input::openGamepad(SDL_JoystickID joystick_id, int pad_index) -> bool {
|
||||
installWebStandardMapping(joystick_id);
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(joystick_id);
|
||||
if (pad == nullptr) {
|
||||
if (verbose_) {
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << '\n';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
int nJoysticks = 0;
|
||||
SDL_JoystickID *joysticks = SDL_GetJoysticks(&nJoysticks);
|
||||
|
||||
if (joysticks != nullptr) {
|
||||
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 << '\n';
|
||||
}
|
||||
} else {
|
||||
if (verbose) {
|
||||
std::cout << "SDL_GetError() = " << SDL_GetError() << '\n';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_SetGamepadEventsEnabled(true);
|
||||
}
|
||||
|
||||
SDL_free(joysticks);
|
||||
const std::string NAME = buildControllerName(pad, pad_index);
|
||||
connected_controllers_.push_back(pad);
|
||||
connected_controller_ids_.push_back(joystick_id);
|
||||
controller_names_.push_back(NAME);
|
||||
num_gamepads_++;
|
||||
if (verbose_) {
|
||||
std::cout << NAME << '\n';
|
||||
}
|
||||
|
||||
return found;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_ADDED
|
||||
auto Input::handleGamepadAdded(SDL_JoystickID jid, std::string &outName) -> bool {
|
||||
auto Input::handleGamepadAdded(SDL_JoystickID jid, std::string &out_name) -> bool {
|
||||
if (!SDL_IsGamepad(jid)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Si el mando ya está registrado no hace nada (ej. evento retroactivo tras el scan inicial)
|
||||
if (std::ranges::any_of(connectedControllerIds, [jid](SDL_JoystickID existing) { return existing == jid; })) {
|
||||
if (std::ranges::any_of(connected_controller_ids_, [jid](SDL_JoystickID existing) { return existing == jid; })) {
|
||||
return false;
|
||||
}
|
||||
|
||||
installWebStandardMapping(jid);
|
||||
SDL_Gamepad *pad = SDL_OpenGamepad(jid);
|
||||
if (pad == nullptr) {
|
||||
if (verbose) {
|
||||
if (verbose_) {
|
||||
std::cout << "Failed to open gamepad " << jid << ": " << SDL_GetError() << '\n';
|
||||
}
|
||||
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++;
|
||||
const int PAD_INDEX = (int)connected_controllers_.size();
|
||||
const std::string NAME = buildControllerName(pad, PAD_INDEX);
|
||||
connected_controllers_.push_back(pad);
|
||||
connected_controller_ids_.push_back(jid);
|
||||
controller_names_.push_back(NAME);
|
||||
num_gamepads_++;
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Gamepad connected: " << name << '\n';
|
||||
if (verbose_) {
|
||||
std::cout << "Gamepad connected: " << NAME << '\n';
|
||||
}
|
||||
|
||||
outName = name;
|
||||
out_name = NAME;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED
|
||||
auto Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) -> bool {
|
||||
for (size_t i = 0; i < connectedControllerIds.size(); ++i) {
|
||||
if (connectedControllerIds[i] != jid) {
|
||||
auto Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &out_name) -> bool {
|
||||
for (size_t i = 0; i < connected_controller_ids_.size(); ++i) {
|
||||
if (connected_controller_ids_[i] != jid) {
|
||||
continue;
|
||||
}
|
||||
|
||||
outName = controllerNames[i];
|
||||
if (connectedControllers[i] != nullptr) {
|
||||
SDL_CloseGamepad(connectedControllers[i]);
|
||||
out_name = controller_names_[i];
|
||||
if (connected_controllers_[i] != nullptr) {
|
||||
SDL_CloseGamepad(connected_controllers_[i]);
|
||||
}
|
||||
connectedControllers.erase(connectedControllers.begin() + i);
|
||||
connectedControllerIds.erase(connectedControllerIds.begin() + i);
|
||||
controllerNames.erase(controllerNames.begin() + i);
|
||||
numGamepads--;
|
||||
numGamepads = std::max(numGamepads, 0);
|
||||
connected_controllers_.erase(connected_controllers_.begin() + i);
|
||||
connected_controller_ids_.erase(connected_controller_ids_.begin() + i);
|
||||
controller_names_.erase(controller_names_.begin() + i);
|
||||
num_gamepads_--;
|
||||
num_gamepads_ = std::max(num_gamepads_, 0);
|
||||
|
||||
if (verbose) {
|
||||
std::cout << "Gamepad disconnected: " << outName << '\n';
|
||||
if (verbose_) {
|
||||
std::cout << "Gamepad disconnected: " << out_name << '\n';
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -358,35 +355,35 @@ auto Input::handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) -> bo
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
auto Input::gameControllerFound() const -> bool {
|
||||
return numGamepads > 0;
|
||||
return num_gamepads_ > 0;
|
||||
}
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
auto Input::getControllerName(int index) -> std::string {
|
||||
if (numGamepads > 0) {
|
||||
return controllerNames[index];
|
||||
if (num_gamepads_ > 0) {
|
||||
return controller_names_[index];
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
auto Input::getNumControllers() const -> int {
|
||||
return numGamepads;
|
||||
return num_gamepads_;
|
||||
}
|
||||
|
||||
// Establece si ha de mostrar mensajes
|
||||
void Input::setVerbose(bool value) {
|
||||
verbose = value;
|
||||
verbose_ = value;
|
||||
}
|
||||
|
||||
// Deshabilita las entradas durante un periodo de tiempo
|
||||
void Input::disableUntil(InputDisable value) {
|
||||
disabledUntil = value;
|
||||
enabled = false;
|
||||
disabled_until_ = value;
|
||||
enabled_ = false;
|
||||
}
|
||||
|
||||
// Hablita las entradas
|
||||
void Input::enable() {
|
||||
enabled = true;
|
||||
disabledUntil = d_notDisabled;
|
||||
enabled_ = true;
|
||||
disabled_until_ = NOT_DISABLED;
|
||||
}
|
||||
|
||||
+78
-91
@@ -17,40 +17,73 @@ constexpr int INPUT_USE_ANY = 2;
|
||||
|
||||
enum InputAction : std::uint8_t {
|
||||
// Inputs obligatorios
|
||||
input_null,
|
||||
input_up,
|
||||
input_down,
|
||||
input_left,
|
||||
input_right,
|
||||
input_pause,
|
||||
input_exit,
|
||||
input_accept,
|
||||
input_cancel,
|
||||
INVALID,
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
PAUSE,
|
||||
EXIT,
|
||||
ACCEPT,
|
||||
CANCEL,
|
||||
|
||||
// Inputs personalizados
|
||||
input_fire_left,
|
||||
input_fire_center,
|
||||
input_fire_right,
|
||||
input_window_fullscreen,
|
||||
input_window_inc_size,
|
||||
input_window_dec_size,
|
||||
FIRE_LEFT,
|
||||
FIRE_CENTER,
|
||||
FIRE_RIGHT,
|
||||
WINDOW_FULLSCREEN,
|
||||
WINDOW_INC_ZOOM,
|
||||
WINDOW_DEC_ZOOM,
|
||||
|
||||
// GPU / shaders (hotkeys provisionales hasta que haya menú de opciones)
|
||||
input_next_preset,
|
||||
input_toggle_shader,
|
||||
input_toggle_shader_type,
|
||||
NEXT_SHADER_PRESET,
|
||||
TOGGLE_SHADER,
|
||||
TOGGLE_SHADER_TYPE,
|
||||
|
||||
// Input obligatorio
|
||||
input_number_of_inputs
|
||||
NUMBER_OF_INPUTS
|
||||
};
|
||||
|
||||
enum InputDisable : std::uint8_t {
|
||||
d_notDisabled,
|
||||
d_forever,
|
||||
d_keyPressed
|
||||
NOT_DISABLED,
|
||||
FOREVER,
|
||||
KEY_PRESSED
|
||||
};
|
||||
|
||||
class Input {
|
||||
public:
|
||||
// Singleton API
|
||||
static void init(const std::string &game_controller_db_path); // Crea la instancia
|
||||
static void destroy(); // Libera la instancia
|
||||
static auto get() -> Input *; // Obtiene el puntero a la instancia
|
||||
|
||||
~Input(); // Destructor
|
||||
|
||||
void update(); // Actualiza el estado del objeto
|
||||
void bindKey(Uint8 input, SDL_Scancode code); // Asigna inputs a teclas
|
||||
void bindGameControllerButton(Uint8 input, SDL_GamepadButton button); // Asigna inputs a botones del mando
|
||||
|
||||
auto checkInput(Uint8 input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0) -> bool; // Comprueba si un input esta activo
|
||||
auto checkAnyInput(int device = INPUT_USE_ANY, int index = 0) -> bool; // Comprueba si hay almenos un input activo
|
||||
|
||||
auto discoverGameController() -> bool; // Busca si hay un mando conectado
|
||||
|
||||
// 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.
|
||||
auto handleGamepadAdded(SDL_JoystickID jid, std::string &out_name) -> bool;
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED. Devuelve true si se ha encontrado y
|
||||
// eliminado, y escribe el nombre visible en outName.
|
||||
auto handleGamepadRemoved(SDL_JoystickID jid, std::string &out_name) -> bool;
|
||||
|
||||
[[nodiscard]] auto gameControllerFound() const -> bool; // Comprueba si hay algun mando conectado
|
||||
[[nodiscard]] auto getNumControllers() const -> int; // Obten el numero de mandos conectados
|
||||
auto getControllerName(int index) -> std::string; // Obten el nombre de un mando de juego
|
||||
|
||||
void setVerbose(bool value); // Establece si ha de mostrar mensajes
|
||||
void disableUntil(InputDisable value); // Deshabilita las entradas durante un periodo de tiempo
|
||||
void enable(); // Hablita las entradas
|
||||
|
||||
private:
|
||||
struct KeyBindings {
|
||||
Uint8 scancode; // Scancode asociado
|
||||
@@ -63,78 +96,32 @@ class Input {
|
||||
};
|
||||
|
||||
// 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
|
||||
std::vector<SDL_Gamepad *> connected_controllers_; // Vector con todos los mandos conectados
|
||||
std::vector<SDL_JoystickID> connected_controller_ids_; // Instance IDs paralelos para mapear eventos
|
||||
|
||||
// Variables
|
||||
std::vector<KeyBindings> keyBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<GameControllerBindings> gameControllerBindings; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::string> controllerNames; // Vector con los nombres de los mandos
|
||||
int numGamepads{0}; // Numero de mandos conectados
|
||||
std::string dbPath; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose{true}; // Indica si ha de mostrar mensajes
|
||||
InputDisable disabledUntil{d_notDisabled}; // Tiempo que esta deshabilitado
|
||||
bool enabled{true}; // Indica si está habilitado
|
||||
std::vector<KeyBindings> key_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<GameControllerBindings> game_controller_bindings_; // Vector con las teclas asociadas a los inputs predefinidos
|
||||
std::vector<std::string> controller_names_; // Vector con los nombres de los mandos
|
||||
int num_gamepads_{0}; // Numero de mandos conectados
|
||||
std::string db_path_; // Ruta al archivo gamecontrollerdb.txt
|
||||
bool verbose_{true}; // Indica si ha de mostrar mensajes
|
||||
InputDisable disabled_until_{NOT_DISABLED}; // Tiempo que esta deshabilitado
|
||||
bool enabled_{true}; // Indica si está habilitado
|
||||
|
||||
static Input *instance; // Instancia única
|
||||
|
||||
explicit Input(std::string file); // Constructor privado (usar Input::init)
|
||||
|
||||
// Construye el nombre visible de un mando (name truncado + sufijo #N)
|
||||
static auto buildControllerName(SDL_Gamepad *pad, int padIndex) -> std::string;
|
||||
static auto buildControllerName(SDL_Gamepad *pad, int pad_index) -> std::string;
|
||||
|
||||
// Constructor privado (usar Input::init)
|
||||
explicit Input(std::string file);
|
||||
// Helpers de checkInput
|
||||
auto checkKeyboardInput(Uint8 input, bool repeat) -> bool;
|
||||
auto checkGameControllerInput(Uint8 input, bool repeat, int index) -> bool;
|
||||
|
||||
// Instancia única
|
||||
static Input *instance;
|
||||
|
||||
public:
|
||||
// Singleton API
|
||||
static void init(const std::string &gameControllerDbPath); // Crea la instancia
|
||||
static void destroy(); // Libera la instancia
|
||||
static auto get() -> Input *; // Obtiene el puntero a la instancia
|
||||
|
||||
// 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
|
||||
auto checkInput(Uint8 input, bool repeat = true, int device = INPUT_USE_ANY, int index = 0) -> bool;
|
||||
|
||||
// Comprueba si hay almenos un input activo
|
||||
auto checkAnyInput(int device = INPUT_USE_ANY, int index = 0) -> bool;
|
||||
|
||||
// Busca si hay un mando conectado
|
||||
auto discoverGameController() -> bool;
|
||||
|
||||
// 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.
|
||||
auto handleGamepadAdded(SDL_JoystickID jid, std::string &outName) -> bool;
|
||||
|
||||
// Procesa un evento SDL_EVENT_GAMEPAD_REMOVED. Devuelve true si se ha encontrado y
|
||||
// eliminado, y escribe el nombre visible en outName.
|
||||
auto handleGamepadRemoved(SDL_JoystickID jid, std::string &outName) -> bool;
|
||||
|
||||
// Comprueba si hay algun mando conectado
|
||||
[[nodiscard]] auto gameControllerFound() const -> bool;
|
||||
|
||||
// Obten el numero de mandos conectados
|
||||
[[nodiscard]] auto getNumControllers() const -> int;
|
||||
|
||||
// Obten el nombre de un mando de juego
|
||||
auto getControllerName(int index) -> std::string;
|
||||
|
||||
// Establece si ha de mostrar mensajes
|
||||
void setVerbose(bool value);
|
||||
|
||||
// Deshabilita las entradas durante un periodo de tiempo
|
||||
void disableUntil(InputDisable value);
|
||||
|
||||
// Hablita las entradas
|
||||
void enable();
|
||||
};
|
||||
// Helpers de discoverGameController
|
||||
void resetGameControllerState();
|
||||
void ensureGamepadSubsystem();
|
||||
auto openGamepad(SDL_JoystickID joystick_id, int pad_index) -> bool;
|
||||
};
|
||||
|
||||
+11
-11
@@ -1,16 +1,16 @@
|
||||
#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
|
||||
Uint32 cursor_hide_time = 3000; // Tiempo en milisegundos para ocultar el cursor por inactividad
|
||||
Uint32 last_mouse_move_time = 0; // Última vez que el ratón se movió
|
||||
bool cursor_visible = 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) {
|
||||
last_mouse_move_time = SDL_GetTicks();
|
||||
if (!cursor_visible && !fullscreen) {
|
||||
SDL_ShowCursor();
|
||||
cursorVisible = true;
|
||||
cursor_visible = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,18 +18,18 @@ namespace Mouse {
|
||||
void updateCursorVisibility(bool fullscreen) {
|
||||
// En pantalla completa el cursor siempre está oculto
|
||||
if (fullscreen) {
|
||||
if (cursorVisible) {
|
||||
if (cursor_visible) {
|
||||
SDL_HideCursor();
|
||||
cursorVisible = false;
|
||||
cursor_visible = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// En modo ventana, lo oculta tras el periodo de inactividad
|
||||
const Uint32 currentTime = SDL_GetTicks();
|
||||
if (cursorVisible && (currentTime - lastMouseMoveTime > cursorHideTime)) {
|
||||
const Uint32 CURRENT_TIME = SDL_GetTicks();
|
||||
if (cursor_visible && (CURRENT_TIME - last_mouse_move_time > cursor_hide_time)) {
|
||||
SDL_HideCursor();
|
||||
cursorVisible = false;
|
||||
cursor_visible = false;
|
||||
}
|
||||
}
|
||||
} // namespace Mouse
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#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
|
||||
extern Uint32 cursor_hide_time; // Tiempo en milisegundos para ocultar el cursor por inactividad
|
||||
extern Uint32 last_mouse_move_time; // Última vez que el ratón se movió
|
||||
extern bool cursor_visible; // Estado del cursor
|
||||
|
||||
// Procesa un evento de ratón. En pantalla completa ignora el movimiento
|
||||
// para no volver a mostrar el cursor.
|
||||
|
||||
Reference in New Issue
Block a user