This commit is contained in:
2025-10-27 11:53:12 +01:00
parent 231dcd4b3b
commit 5d8811026d
69 changed files with 899 additions and 888 deletions

View File

@@ -13,21 +13,21 @@
#include "game/ui/notifier.hpp" // Para Notifier, NotificationText
#include "utils/utils.hpp" // Para stringInVector
namespace globalInputs {
namespace GlobalInputs {
void quit() {
const std::string code = SceneManager::current == SceneManager::Scene::GAME ? "PRESS AGAIN TO RETURN TO MENU" : "PRESS AGAIN TO EXIT";
auto code_found = stringInVector(Notifier::get()->getCodes(), code);
const std::string CODE = SceneManager::current == SceneManager::Scene::GAME ? "PRESS AGAIN TO RETURN TO MENU" : "PRESS AGAIN TO EXIT";
auto code_found = stringInVector(Notifier::get()->getCodes(), CODE);
if (code_found) {
// Si la notificación de salir está activa, cambia de sección
SceneManager::current = SceneManager::current == SceneManager::Scene::GAME ? SceneManager::Scene::TITLE : SceneManager::Scene::QUIT;
} else {
// Si la notificación de salir no está activa, muestra la notificación
Notifier::get()->show({code}, NotificationText::CENTER, 2000, -1, true, code);
Notifier::get()->show({CODE}, NotificationText::CENTER, 2000, -1, true, CODE);
}
}
// Cambia de seccion
void skip_section() {
void skipSection() {
switch (SceneManager::current) {
case SceneManager::Scene::LOGO:
case SceneManager::Scene::LOADING_SCREEN:
@@ -52,7 +52,7 @@ void check() {
}
else if (Input::get()->checkInput(InputAction::ACCEPT, INPUT_DO_NOT_ALLOW_REPEAT)) {
skip_section();
skipSection();
}
else if (Input::get()->checkInput(InputAction::TOGGLE_BORDER, INPUT_DO_NOT_ALLOW_REPEAT)) {
@@ -62,7 +62,7 @@ void check() {
else if (Input::get()->checkInput(InputAction::TOGGLE_VIDEOMODE, INPUT_DO_NOT_ALLOW_REPEAT)) {
Screen::get()->toggleVideoMode();
Notifier::get()->show({"FULLSCREEN " + std::string(Options::video.fullscreen == 0 ? "DISABLED" : "ENABLED")}, NotificationText::CENTER);
Notifier::get()->show({"FULLSCREEN " + std::string(static_cast<int>(Options::video.fullscreen) == 0 ? "DISABLED" : "ENABLED")}, NotificationText::CENTER);
}
else if (Input::get()->checkInput(InputAction::WINDOW_DEC_ZOOM, INPUT_DO_NOT_ALLOW_REPEAT)) {
@@ -102,4 +102,4 @@ void check() {
Screen::get()->toggleDebugInfo();
}
}
} // namespace globalInputs
} // namespace GlobalInputs

View File

@@ -1,6 +1,6 @@
#pragma once
namespace globalInputs {
namespace GlobalInputs {
// Comprueba los inputs que se pueden introducir en cualquier sección del juego
void check();
} // namespace globalInputs
} // namespace GlobalInputs

View File

@@ -9,21 +9,21 @@
#include <utility> // Para pair
// [SINGLETON]
Input* Input::input_ = nullptr;
Input* Input::input = nullptr;
// [SINGLETON] Crearemos el objeto con esta función estática
void Input::init(const std::string& game_controller_db_path) {
Input::input_ = new Input(game_controller_db_path);
Input::input = new Input(game_controller_db_path);
}
// [SINGLETON] Destruiremos el objeto con esta función estática
void Input::destroy() {
delete Input::input_;
delete Input::input;
}
// [SINGLETON] Con este método obtenemos el objeto y podemos trabajar con él
Input* Input::get() {
return Input::input_;
return Input::input;
}
// Constructor
@@ -63,24 +63,24 @@ void Input::bindGameControllerButton(int controller_index, InputAction input_tar
bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device, int controller_index) {
bool success_keyboard = false;
bool success_controller = false;
const int input_index = static_cast<int>(input);
const int INPUT_INDEX = static_cast<int>(input);
if (device == InputDeviceToUse::KEYBOARD || device == InputDeviceToUse::ANY) {
auto key_states = SDL_GetKeyboardState(nullptr);
const auto* key_states = SDL_GetKeyboardState(nullptr);
if (repeat) {
success_keyboard = key_states[key_bindings_[input_index].scancode] != 0;
success_keyboard = static_cast<int>(key_states[key_bindings_[INPUT_INDEX].scancode]) != 0;
} else {
if (!key_bindings_[input_index].active) {
if (key_states[key_bindings_[input_index].scancode] != 0) {
key_bindings_[input_index].active = true;
if (!key_bindings_[INPUT_INDEX].active) {
if (static_cast<int>(key_states[key_bindings_[INPUT_INDEX].scancode]) != 0) {
key_bindings_[INPUT_INDEX].active = true;
success_keyboard = true;
} else {
success_keyboard = false;
}
} else {
if (key_states[key_bindings_[input_index].scancode] == 0) {
key_bindings_[input_index].active = false;
if (static_cast<int>(key_states[key_bindings_[INPUT_INDEX].scancode]) == 0) {
key_bindings_[INPUT_INDEX].active = false;
}
success_keyboard = false;
}
@@ -93,18 +93,18 @@ bool Input::checkInput(InputAction input, bool repeat, InputDeviceToUse device,
if (!success_controller) {
if (repeat) {
success_controller = SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0;
success_controller = static_cast<int>(SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(INPUT_INDEX).button)) != 0;
} else {
if (!controller_bindings_.at(controller_index).at(input_index).active) {
if (SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) != 0) {
controller_bindings_.at(controller_index).at(input_index).active = true;
if (!controller_bindings_.at(controller_index).at(INPUT_INDEX).active) {
if (static_cast<int>(SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(INPUT_INDEX).button)) != 0) {
controller_bindings_.at(controller_index).at(INPUT_INDEX).active = true;
success_controller = true;
} else {
success_controller = false;
}
} else {
if (SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(input_index).button) == 0) {
controller_bindings_.at(controller_index).at(input_index).active = false;
if (static_cast<int>(SDL_GetGamepadButton(connected_controllers_.at(controller_index), controller_bindings_.at(controller_index).at(INPUT_INDEX).button)) == 0) {
controller_bindings_.at(controller_index).at(INPUT_INDEX).active = false;
}
success_controller = false;
}
@@ -122,7 +122,7 @@ bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) {
const bool* key_states = SDL_GetKeyboardState(nullptr);
for (int i = 0; i < (int)key_bindings_.size(); ++i) {
if (key_states[key_bindings_[i].scancode] != 0 && !key_bindings_[i].active) {
if (static_cast<int>(key_states[key_bindings_[i].scancode]) != 0 && !key_bindings_[i].active) {
key_bindings_[i].active = true;
return true;
}
@@ -132,7 +132,7 @@ bool Input::checkAnyInput(InputDeviceToUse device, int controller_index) {
if (gameControllerFound()) {
if (device == InputDeviceToUse::CONTROLLER || device == InputDeviceToUse::ANY) {
for (int i = 0; i < (int)controller_bindings_.size(); ++i) {
if (SDL_GetGamepadButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button) != 0 && !controller_bindings_[controller_index][i].active) {
if (static_cast<int>(SDL_GetGamepadButton(connected_controllers_[controller_index], controller_bindings_[controller_index][i].button)) != 0 && !controller_bindings_[controller_index][i].active) {
controller_bindings_[controller_index][i].active = true;
return true;
}
@@ -191,12 +191,12 @@ bool Input::discoverGameControllers() {
for (int i = 0; i < num_joysticks_; i++) {
if (SDL_IsGamepad(joystick_ids[i])) {
SDL_Gamepad* pad = SDL_OpenGamepad(joystick_ids[i]);
if (pad && SDL_GamepadConnected(pad)) {
if ((pad != nullptr) && SDL_GamepadConnected(pad)) {
connected_controllers_.push_back(pad);
const char* name = SDL_GetGamepadName(pad);
std::cout << "#" << i << ": " << (name ? name : "Unknown") << std::endl;
controller_names_.push_back(name ? name : "Unknown");
std::cout << "#" << i << ": " << ((name != nullptr) ? name : "Unknown") << std::endl;
controller_names_.push_back((name != nullptr) ? name : "Unknown");
} else {
std::cout << "SDL_GetError() = " << SDL_GetError() << std::endl;
}
@@ -214,7 +214,7 @@ bool Input::discoverGameControllers() {
}
// Comprueba si hay algun mando conectado
bool Input::gameControllerFound() { return num_gamepads_ > 0 ? true : false; }
bool Input::gameControllerFound() const { return num_gamepads_ > 0; }
// Obten el nombre de un mando de juego
std::string Input::getControllerName(int controller_index) const { return num_gamepads_ > 0 ? controller_names_.at(controller_index) : std::string(); }
@@ -246,21 +246,21 @@ int Input::getIndexByName(const std::string& name) const {
// Comprueba el eje del mando
bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat) {
// Umbral para considerar el eje como activo
const Sint16 threshold = 30000;
const Sint16 THRESHOLD = 30000;
bool axis_active_now = false;
switch (input) {
case InputAction::LEFT:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) < -THRESHOLD;
break;
case InputAction::RIGHT:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTX) > THRESHOLD;
break;
case InputAction::UP:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) < -THRESHOLD;
break;
case InputAction::DOWN:
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > threshold;
axis_active_now = SDL_GetGamepadAxis(connected_controllers_[controller_index], SDL_GAMEPAD_AXIS_LEFTY) > THRESHOLD;
break;
default:
return false;
@@ -272,17 +272,15 @@ bool Input::checkAxisInput(InputAction input, int controller_index, bool repeat)
if (repeat) {
// Si se permite repetir, simplemente devolvemos el estado actual
return axis_active_now;
} else {
// Si no se permite repetir, aplicamos la lógica de transición
if (axis_active_now && !binding.axis_active) {
// Transición de inactivo a activo
binding.axis_active = true;
return true;
} else if (!axis_active_now && binding.axis_active) {
// Transición de activo a inactivo
binding.axis_active = false;
}
// Mantener el estado actual
return false;
} // Si no se permite repetir, aplicamos la lógica de transición
if (axis_active_now && !binding.axis_active) {
// Transición de inactivo a activo
binding.axis_active = true;
return true;
} else if (!axis_active_now && binding.axis_active) {
// Transición de activo a inactivo
binding.axis_active = false;
}
// Mantener el estado actual
return false;
}

View File

@@ -48,7 +48,7 @@ enum class InputAction {
class Input {
private:
// [SINGLETON] Objeto privado
static Input* input_;
static Input* input;
struct KeyBindings {
Uint8 scancode; // Scancode asociado
@@ -107,7 +107,7 @@ class Input {
// Asigna inputs a botones del mando
void bindGameControllerButton(int controller_index, InputAction input, SDL_GamepadButton button);
void bindGameControllerButton(int controller_index, InputAction inputTarget, InputAction inputSource);
void bindGameControllerButton(int controller_index, InputAction input_target, InputAction input_source);
// Comprueba si un input esta activo
bool checkInput(InputAction input, bool repeat = true, InputDeviceToUse device = InputDeviceToUse::ANY, int controller_index = 0);
@@ -119,7 +119,7 @@ class Input {
bool discoverGameControllers();
// Comprueba si hay algun mando conectado
bool gameControllerFound();
bool gameControllerFound() const;
// Obten el número de mandos conectados
int getNumControllers() const;