319 lines
12 KiB
C++
319 lines
12 KiB
C++
#include "core/input/input.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <iostream>
|
|
#include <memory>
|
|
#include <ranges>
|
|
#include <unordered_map>
|
|
#include <utility>
|
|
|
|
// Singleton
|
|
Input* Input::instance = nullptr;
|
|
|
|
void Input::init(const std::string& game_controller_db_path) {
|
|
Input::instance = new Input(game_controller_db_path);
|
|
}
|
|
|
|
void Input::destroy() { delete Input::instance; }
|
|
auto Input::get() -> Input* { return Input::instance; }
|
|
|
|
// Constructor
|
|
// Los bindings de sistema se mapean a F1-F12.
|
|
// Añade los bindings de tu juego (LEFT, RIGHT, etc.) llamando a bindKey() después de init().
|
|
Input::Input(std::string game_controller_db_path)
|
|
: gamepad_mappings_file_(std::move(game_controller_db_path)) {
|
|
keyboard_.bindings = {
|
|
// Controles de sistema
|
|
{Action::WINDOW_DEC_ZOOM, KeyState{.scancode = SDL_SCANCODE_F1}},
|
|
{Action::WINDOW_INC_ZOOM, KeyState{.scancode = SDL_SCANCODE_F2}},
|
|
{Action::TOGGLE_FULLSCREEN, KeyState{.scancode = SDL_SCANCODE_F3}},
|
|
{Action::TOGGLE_POSTFX, KeyState{.scancode = SDL_SCANCODE_F4}},
|
|
{Action::NEXT_PALETTE, KeyState{.scancode = SDL_SCANCODE_F5}},
|
|
{Action::PREVIOUS_PALETTE, KeyState{.scancode = SDL_SCANCODE_F6}},
|
|
{Action::TOGGLE_INTEGER_SCALE, KeyState{.scancode = SDL_SCANCODE_F7}},
|
|
{Action::TOGGLE_MUSIC, KeyState{.scancode = SDL_SCANCODE_F8}},
|
|
{Action::TOGGLE_BORDER, KeyState{.scancode = SDL_SCANCODE_F9}},
|
|
{Action::TOGGLE_VSYNC, KeyState{.scancode = SDL_SCANCODE_F10}},
|
|
{Action::TOGGLE_DEBUG, KeyState{.scancode = SDL_SCANCODE_F12}}};
|
|
|
|
initSDLGamePad();
|
|
}
|
|
|
|
void Input::bindKey(Action action, SDL_Scancode code) {
|
|
keyboard_.bindings[action].scancode = code;
|
|
}
|
|
|
|
void Input::bindGameControllerButton(const std::shared_ptr<Gamepad>& gamepad, Action action, SDL_GamepadButton button) { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (gamepad != nullptr) {
|
|
gamepad->bindings[action].button = button;
|
|
}
|
|
}
|
|
|
|
void Input::bindGameControllerButton(const std::shared_ptr<Gamepad>& gamepad, Action action_target, Action action_source) { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (gamepad != nullptr) {
|
|
gamepad->bindings[action_target].button = gamepad->bindings[action_source].button;
|
|
}
|
|
}
|
|
|
|
auto Input::checkAction(Action action, bool repeat, bool check_keyboard, const std::shared_ptr<Gamepad>& gamepad) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
|
bool success_keyboard = false;
|
|
bool success_controller = false;
|
|
|
|
if (check_keyboard) {
|
|
if (repeat) {
|
|
success_keyboard = keyboard_.bindings[action].is_held;
|
|
} else {
|
|
success_keyboard = keyboard_.bindings[action].just_pressed;
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<Gamepad> active_gamepad = gamepad;
|
|
if (active_gamepad == nullptr && !gamepads_.empty()) {
|
|
active_gamepad = gamepads_[0];
|
|
}
|
|
|
|
if (active_gamepad != nullptr) {
|
|
success_controller = checkAxisInput(action, active_gamepad, repeat);
|
|
|
|
if (!success_controller) {
|
|
success_controller = checkTriggerInput(action, active_gamepad, repeat);
|
|
}
|
|
|
|
if (!success_controller) {
|
|
if (repeat) {
|
|
success_controller = active_gamepad->bindings[action].is_held;
|
|
} else {
|
|
success_controller = active_gamepad->bindings[action].just_pressed;
|
|
}
|
|
}
|
|
}
|
|
|
|
return (success_keyboard || success_controller);
|
|
}
|
|
|
|
auto Input::checkAnyInput(bool check_keyboard, const std::shared_ptr<Gamepad>& gamepad) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (check_keyboard) {
|
|
for (const auto& pair : keyboard_.bindings) {
|
|
if (pair.second.just_pressed) return true;
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<Gamepad> active_gamepad = gamepad;
|
|
if (active_gamepad == nullptr && !gamepads_.empty()) {
|
|
active_gamepad = gamepads_[0];
|
|
}
|
|
|
|
if (active_gamepad != nullptr) {
|
|
for (const auto& pair : active_gamepad->bindings) {
|
|
if (pair.second.just_pressed) return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
auto Input::checkAnyButton(bool repeat) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
|
for (auto bi : BUTTON_INPUTS) {
|
|
if (checkAction(bi, repeat, CHECK_KEYBOARD)) return true;
|
|
for (const auto& gamepad : gamepads_) {
|
|
if (checkAction(bi, repeat, DO_NOT_CHECK_KEYBOARD, gamepad)) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
auto Input::gameControllerFound() const -> bool { return !gamepads_.empty(); }
|
|
|
|
auto Input::getControllerName(const std::shared_ptr<Gamepad>& gamepad) -> std::string {
|
|
return gamepad == nullptr ? std::string() : gamepad->name;
|
|
}
|
|
|
|
auto Input::getControllerNames() const -> std::vector<std::string> {
|
|
std::vector<std::string> names;
|
|
for (const auto& gamepad : gamepads_) {
|
|
names.push_back(gamepad->name);
|
|
}
|
|
return names;
|
|
}
|
|
|
|
auto Input::getNumGamepads() const -> int { return gamepads_.size(); }
|
|
|
|
auto Input::getGamepad(SDL_JoystickID id) const -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static)
|
|
for (const auto& gamepad : gamepads_) {
|
|
if (gamepad->instance_id == id) return gamepad;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
auto Input::getGamepadByName(const std::string& name) const -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static)
|
|
for (const auto& gamepad : gamepads_) {
|
|
if (gamepad && gamepad->name == name) return gamepad;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
auto Input::getControllerBinding(const std::shared_ptr<Gamepad>& gamepad, Action action) -> SDL_GamepadButton { // NOLINT(readability-convert-member-functions-to-static)
|
|
return static_cast<SDL_GamepadButton>(gamepad->bindings[action].button);
|
|
}
|
|
|
|
auto Input::checkAxisInput(Action action, const std::shared_ptr<Gamepad>& gamepad, bool repeat) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
|
auto& binding = gamepad->bindings[action];
|
|
if (binding.button < 200) return false;
|
|
|
|
bool axis_active_now = false;
|
|
if (binding.button == 200) {
|
|
axis_active_now = SDL_GetGamepadAxis(gamepad->pad, SDL_GAMEPAD_AXIS_LEFTX) < -AXIS_THRESHOLD;
|
|
} else if (binding.button == 201) {
|
|
axis_active_now = SDL_GetGamepadAxis(gamepad->pad, SDL_GAMEPAD_AXIS_LEFTX) > AXIS_THRESHOLD;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
if (repeat) return axis_active_now;
|
|
if (axis_active_now && !binding.axis_active) { binding.axis_active = true; return true; }
|
|
if (!axis_active_now && binding.axis_active) { binding.axis_active = false; }
|
|
return false;
|
|
}
|
|
|
|
auto Input::checkTriggerInput(Action action, const std::shared_ptr<Gamepad>& gamepad, bool repeat) -> bool { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (gamepad->bindings[action].button == static_cast<int>(SDL_GAMEPAD_BUTTON_INVALID)) return false;
|
|
|
|
int button = gamepad->bindings[action].button;
|
|
bool trigger_active_now = false;
|
|
|
|
if (button == TRIGGER_L2_AS_BUTTON) {
|
|
trigger_active_now = SDL_GetGamepadAxis(gamepad->pad, SDL_GAMEPAD_AXIS_LEFT_TRIGGER) > TRIGGER_THRESHOLD;
|
|
} else if (button == TRIGGER_R2_AS_BUTTON) {
|
|
trigger_active_now = SDL_GetGamepadAxis(gamepad->pad, SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) > TRIGGER_THRESHOLD;
|
|
} else {
|
|
return false;
|
|
}
|
|
|
|
auto& binding = gamepad->bindings[action];
|
|
if (repeat) return trigger_active_now;
|
|
if (trigger_active_now && !binding.trigger_active) { binding.trigger_active = true; return true; }
|
|
if (!trigger_active_now && binding.trigger_active) { binding.trigger_active = false; }
|
|
return false;
|
|
}
|
|
|
|
void Input::addGamepadMappingsFromFile() { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (SDL_AddGamepadMappingsFromFile(gamepad_mappings_file_.c_str()) < 0) {
|
|
std::cout << "Error, could not load " << gamepad_mappings_file_.c_str() << ": " << SDL_GetError() << '\n';
|
|
}
|
|
}
|
|
|
|
void Input::discoverGamepads() { // NOLINT(readability-convert-member-functions-to-static)
|
|
SDL_Event event;
|
|
while (SDL_PollEvent(&event)) {
|
|
handleEvent(event);
|
|
}
|
|
}
|
|
|
|
void Input::initSDLGamePad() {
|
|
if (SDL_WasInit(SDL_INIT_GAMEPAD) != 1) {
|
|
if (!SDL_InitSubSystem(SDL_INIT_GAMEPAD)) {
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GAMEPAD could not initialize! SDL Error: %s", SDL_GetError());
|
|
} else {
|
|
addGamepadMappingsFromFile();
|
|
discoverGamepads();
|
|
std::cout << "\n** INPUT SYSTEM **\n";
|
|
std::cout << "Input System initialized successfully\n";
|
|
}
|
|
}
|
|
}
|
|
|
|
void Input::resetInputStates() {
|
|
for (auto& key : keyboard_.bindings) {
|
|
key.second.is_held = false;
|
|
key.second.just_pressed = false;
|
|
}
|
|
for (const auto& gamepad : gamepads_) {
|
|
for (auto& binding : gamepad->bindings) {
|
|
binding.second.is_held = false;
|
|
binding.second.just_pressed = false;
|
|
binding.second.trigger_active = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
void Input::update() { // NOLINT(readability-convert-member-functions-to-static)
|
|
const bool* key_states = SDL_GetKeyboardState(nullptr);
|
|
|
|
for (auto& binding : keyboard_.bindings) {
|
|
bool key_is_down_now = key_states[binding.second.scancode];
|
|
binding.second.just_pressed = key_is_down_now && !binding.second.is_held;
|
|
binding.second.is_held = key_is_down_now;
|
|
}
|
|
|
|
for (const auto& gamepad : gamepads_) {
|
|
for (auto& binding : gamepad->bindings) {
|
|
bool button_is_down_now = static_cast<int>(SDL_GetGamepadButton(gamepad->pad, static_cast<SDL_GamepadButton>(binding.second.button))) != 0;
|
|
binding.second.just_pressed = button_is_down_now && !binding.second.is_held;
|
|
binding.second.is_held = button_is_down_now;
|
|
}
|
|
}
|
|
}
|
|
|
|
auto Input::handleEvent(const SDL_Event& event) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
|
switch (event.type) {
|
|
case SDL_EVENT_GAMEPAD_ADDED:
|
|
return addGamepad(event.gdevice.which);
|
|
case SDL_EVENT_GAMEPAD_REMOVED:
|
|
return removeGamepad(event.gdevice.which);
|
|
}
|
|
return {};
|
|
}
|
|
|
|
auto Input::addGamepad(int device_index) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
|
SDL_Gamepad* pad = SDL_OpenGamepad(device_index);
|
|
if (pad == nullptr) {
|
|
std::cerr << "Error al abrir el gamepad: " << SDL_GetError() << '\n';
|
|
return {};
|
|
}
|
|
|
|
auto gamepad = std::make_shared<Gamepad>(pad);
|
|
auto name = gamepad->name;
|
|
std::cout << "Gamepad connected (" << name << ")\n";
|
|
gamepads_.push_back(std::move(gamepad));
|
|
return name + " CONNECTED";
|
|
}
|
|
|
|
auto Input::removeGamepad(SDL_JoystickID id) -> std::string { // NOLINT(readability-convert-member-functions-to-static)
|
|
auto it = std::ranges::find_if(gamepads_, [id](const std::shared_ptr<Gamepad>& gamepad) -> bool {
|
|
return gamepad->instance_id == id;
|
|
});
|
|
|
|
if (it != gamepads_.end()) {
|
|
std::string name = (*it)->name;
|
|
std::cout << "Gamepad disconnected (" << name << ")\n";
|
|
gamepads_.erase(it);
|
|
return name + " DISCONNECTED";
|
|
}
|
|
std::cerr << "No se encontró el gamepad con ID " << id << '\n';
|
|
return {};
|
|
}
|
|
|
|
void Input::printConnectedGamepads() const { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (gamepads_.empty()) {
|
|
std::cout << "No hay gamepads conectados.\n";
|
|
return;
|
|
}
|
|
std::cout << "Gamepads conectados:\n";
|
|
for (const auto& gamepad : gamepads_) {
|
|
std::string name = gamepad->name.empty() ? "Desconocido" : gamepad->name;
|
|
std::cout << " - ID: " << gamepad->instance_id << ", Nombre: " << name << "\n";
|
|
}
|
|
}
|
|
|
|
auto Input::findAvailableGamepadByName(const std::string& gamepad_name) -> std::shared_ptr<Input::Gamepad> { // NOLINT(readability-convert-member-functions-to-static)
|
|
if (gamepads_.empty()) return nullptr;
|
|
for (const auto& gamepad : gamepads_) {
|
|
if (gamepad && gamepad->name == gamepad_name) return gamepad;
|
|
}
|
|
for (const auto& gamepad : gamepads_) {
|
|
if (gamepad) return gamepad;
|
|
}
|
|
return nullptr;
|
|
}
|