migrant input: ja compila, ja no peta... falta descomentar mig codi
This commit is contained in:
@@ -27,56 +27,51 @@ Input::Input(std::string game_controller_db_path)
|
||||
// Inicializa el subsistema SDL_INIT_GAMEPAD
|
||||
initSDLGamePad();
|
||||
|
||||
// Inicializa los vectores
|
||||
// key_bindings_.resize(static_cast<int>(Action::SIZE), KeyBindings());
|
||||
// controller_bindings_.resize(gamepads.size(), std::vector<ControllerBindings>(static_cast<int>(Action::SIZE), ControllerBindings()));
|
||||
|
||||
// Listado de los inputs para jugar que utilizan botones, ni palancas ni crucetas
|
||||
button_inputs_ = {Action::FIRE_LEFT, Action::FIRE_CENTER, Action::FIRE_RIGHT, Action::START};
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(Action input, SDL_Scancode code) {
|
||||
key_bindings_.at(static_cast<int>(input)).scancode = code;
|
||||
void Input::bindKey(Action action, SDL_Scancode code) {
|
||||
keyboard_.bindings[action].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input, SDL_GamepadButton button) {
|
||||
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action action, SDL_GamepadButton button) {
|
||||
if (gamepad != nullptr) {
|
||||
gamepad->button_states.at(static_cast<int>(input)).button = button;
|
||||
gamepad->bindings[action].button = button;
|
||||
}
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(std::shared_ptr<Gamepad> gamepad, Action input_target, Action input_source) {
|
||||
if (gamepad != nullptr) {
|
||||
gamepad->button_states.at(static_cast<int>(input_target)).button = gamepad->button_states.at(static_cast<int>(input_source)).button;
|
||||
gamepad->bindings[input_target].button = gamepad->bindings[input_source].button;
|
||||
}
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
auto Input::checkAction(Action input, bool repeat, Device device, std::shared_ptr<Gamepad> gamepad) -> bool {
|
||||
auto Input::checkAction(Action action, bool repeat, Device device, std::shared_ptr<Gamepad> gamepad) -> bool {
|
||||
bool success_keyboard = false;
|
||||
bool success_controller = false;
|
||||
const int INPUT_INDEX = static_cast<int>(input);
|
||||
|
||||
if (device == Device::KEYBOARD || device == Device::ANY) {
|
||||
if (repeat) { // El usuario quiere saber si está pulsada (estado mantenido)
|
||||
success_keyboard = key_bindings_[INPUT_INDEX].is_held;
|
||||
success_keyboard = keyboard_.bindings[action].is_held;
|
||||
} else { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
|
||||
success_keyboard = key_bindings_[INPUT_INDEX].just_pressed;
|
||||
success_keyboard = keyboard_.bindings[action].just_pressed;
|
||||
}
|
||||
}
|
||||
|
||||
if (gamepad != nullptr) {
|
||||
if ((device == Device::CONTROLLER) || (device == Device::ANY)) {
|
||||
success_controller = checkAxisInput(input, gamepad, repeat);
|
||||
success_controller = checkAxisInput(action, gamepad, repeat);
|
||||
|
||||
if (!success_controller) {
|
||||
if (repeat) { // El usuario quiere saber si está pulsada (estado mantenido)
|
||||
success_controller = gamepad->button_states.at(INPUT_INDEX).is_held;
|
||||
success_controller = gamepad->bindings[action].is_held;
|
||||
} else { // El usuario quiere saber si ACABA de ser pulsada (evento de un solo fotograma)
|
||||
success_controller = gamepad->button_states.at(INPUT_INDEX).just_pressed;
|
||||
success_controller = gamepad->bindings[action].just_pressed;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,14 +83,13 @@ auto Input::checkAction(Action input, bool repeat, Device device, std::shared_pt
|
||||
// Comprueba si hay almenos un input activo
|
||||
auto Input::checkAnyInput(Device device, std::shared_ptr<Gamepad> gamepad) -> bool {
|
||||
// Obtenemos el número total de acciones posibles para iterar sobre ellas.
|
||||
const int NUM_ACTIONS = static_cast<int>(Action::SIZE);
|
||||
|
||||
// --- Comprobación del Teclado ---
|
||||
if (device == Device::KEYBOARD || device == Device::ANY) {
|
||||
for (int i = 0; i < NUM_ACTIONS; ++i) {
|
||||
for (const auto &pair : keyboard_.bindings) {
|
||||
// Simplemente leemos el estado pre-calculado por Input::update().
|
||||
// Ya no se llama a SDL_GetKeyboardState ni se modifica el estado '.active'.
|
||||
if (key_bindings_.at(i).just_pressed) {
|
||||
if (pair.second.just_pressed) {
|
||||
return true; // Se encontró una acción recién pulsada.
|
||||
}
|
||||
}
|
||||
@@ -106,9 +100,9 @@ auto Input::checkAnyInput(Device device, std::shared_ptr<Gamepad> gamepad) -> bo
|
||||
if (gamepad != nullptr) {
|
||||
if (device == Device::CONTROLLER || device == Device::ANY) {
|
||||
// Iteramos sobre todas las acciones, no sobre el número de mandos.
|
||||
for (int i = 0; i < NUM_ACTIONS; ++i) {
|
||||
for (const auto &pair : gamepad->bindings) {
|
||||
// Leemos el estado pre-calculado para el mando y la acción específicos.
|
||||
if (gamepad->button_states.at(i).just_pressed) {
|
||||
if (pair.second.just_pressed) {
|
||||
return true; // Se encontró una acción recién pulsada en el mando.
|
||||
}
|
||||
}
|
||||
@@ -150,17 +144,17 @@ auto Input::getNumControllers() const -> int { return gamepads_.size(); }
|
||||
|
||||
// Obtiene el indice del controlador a partir de un event.id
|
||||
auto Input::getJoyIndex(SDL_JoystickID id) const -> int {
|
||||
for (int i = 0; i < num_joysticks_; ++i) {
|
||||
if (SDL_GetJoystickID(joysticks_[i]) == id) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
// for (int i = 0; i < num_joysticks_; ++i) {
|
||||
// if (SDL_GetJoystickID(joysticks_[i]) == id) {
|
||||
// return i;
|
||||
// }
|
||||
// }
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Obtiene el SDL_GamepadButton asignado a un input
|
||||
auto Input::getControllerBinding(std::shared_ptr<Gamepad> gamepad, Action input) const -> SDL_GamepadButton {
|
||||
return gamepad->button_states.at(static_cast<int>(input)).button;
|
||||
return gamepad->bindings[input].button;
|
||||
}
|
||||
|
||||
// Convierte un InputAction a std::string
|
||||
@@ -217,7 +211,7 @@ auto Input::checkAxisInput(Action input, std::shared_ptr<Gamepad> gamepad, bool
|
||||
}
|
||||
|
||||
// Referencia al binding correspondiente
|
||||
auto &binding = gamepad->button_states.at(static_cast<int>(input));
|
||||
auto &binding = gamepad->bindings[input];
|
||||
|
||||
if (repeat) {
|
||||
// Si se permite repetir, simplemente devolvemos el estado actual
|
||||
@@ -252,15 +246,15 @@ void Input::initSDLGamePad() {
|
||||
|
||||
void Input::resetInputStates() {
|
||||
// Resetear todos los KeyBindings.active a false
|
||||
for (auto &key : key_bindings_) {
|
||||
key.is_held = false;
|
||||
key.just_pressed = false;
|
||||
for (auto &key : keyboard_.bindings) {
|
||||
key.second.is_held = false;
|
||||
key.second.just_pressed = false;
|
||||
}
|
||||
// Resetear todos los ControllerBindings.active a false
|
||||
for (auto &gamepad : gamepads_) {
|
||||
for (auto &binding : gamepad->button_states) {
|
||||
binding.is_held = false;
|
||||
binding.just_pressed = false;
|
||||
for (auto &binding : gamepad->bindings) {
|
||||
binding.second.is_held = false;
|
||||
binding.second.just_pressed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -269,22 +263,22 @@ void Input::update() {
|
||||
// --- TECLADO ---
|
||||
const bool *key_states = SDL_GetKeyboardState(nullptr);
|
||||
|
||||
for (auto &key_binding : key_bindings_) {
|
||||
bool key_is_down_now = key_states[key_binding.scancode];
|
||||
for (auto &binding : keyboard_.bindings) {
|
||||
bool key_is_down_now = key_states[binding.second.scancode];
|
||||
|
||||
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo
|
||||
key_binding.just_pressed = key_is_down_now && !key_binding.is_held;
|
||||
key_binding.is_held = key_is_down_now;
|
||||
binding.second.just_pressed = key_is_down_now && !binding.second.is_held;
|
||||
binding.second.is_held = key_is_down_now;
|
||||
}
|
||||
|
||||
// --- MANDOS ---
|
||||
for (auto gamepad : gamepads_) {
|
||||
for (auto &binding : gamepad->button_states) {
|
||||
bool button_is_down_now = static_cast<int>(SDL_GetGamepadButton(gamepad->pad, binding.button)) != 0;
|
||||
for (auto &binding : gamepad->bindings) {
|
||||
bool button_is_down_now = static_cast<int>(SDL_GetGamepadButton(gamepad->pad, binding.second.button)) != 0;
|
||||
|
||||
// El estado .is_held del fotograma anterior nos sirve para saber si es un pulso nuevo
|
||||
binding.just_pressed = button_is_down_now && !binding.is_held;
|
||||
binding.is_held = button_is_down_now;
|
||||
binding.second.just_pressed = button_is_down_now && !binding.second.is_held;
|
||||
binding.second.is_held = button_is_down_now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user