60 lines
2.9 KiB
C++
60 lines
2.9 KiB
C++
#include "game/scenes/start_input.hpp"
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include "core/input/gamepad_bindings.hpp"
|
|
#include "core/input/gamepad_manager.hpp"
|
|
#include "core/system/event_buffer.hpp"
|
|
#include "game/options.hpp"
|
|
#include "utils/defaults.hpp"
|
|
|
|
namespace Input::Detect {
|
|
|
|
auto pressedStart(int player_id) -> bool {
|
|
const SDL_Scancode KB = (player_id == 0) ? Defaults::KeysGame::START : Defaults::KeysGameP2::START;
|
|
SDL_Gamepad* pad = GamepadManager::get().getForPlayer(player_id);
|
|
const SDL_JoystickID PAD_ID = (pad != nullptr) ? SDL_GetGamepadID(pad) : 0;
|
|
const auto& slot = (player_id == 0) ? Options::controls().gamepad_p1
|
|
: Options::controls().gamepad_p2;
|
|
const int START_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::START);
|
|
|
|
return std::ranges::any_of(EventBuffer::events, [&](const SDL_Event& ev) {
|
|
if (ev.type == SDL_EVENT_KEY_DOWN && ev.key.scancode == KB) { return true; }
|
|
if (ev.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN && pad != nullptr && ev.gbutton.which == PAD_ID && static_cast<int>(ev.gbutton.button) == START_BTN) {
|
|
return true;
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
auto pressedActionButton(int player_id) -> bool {
|
|
// Teclat: les bindings actives venen d'`Options::controls()` (les
|
|
// que el menú de servei pot reescriure), no de `Defaults` cru —
|
|
// així respectem el remapping de l'usuari igual que fa el Player.
|
|
const auto& kb = (player_id == 0) ? Options::controls().keyboard_p1
|
|
: Options::controls().keyboard_p2;
|
|
const SDL_Scancode KB_JUMP = kb.jump;
|
|
const SDL_Scancode KB_ATTACK = kb.attack;
|
|
SDL_Gamepad* pad = GamepadManager::get().getForPlayer(player_id);
|
|
const SDL_JoystickID PAD_ID = (pad != nullptr) ? SDL_GetGamepadID(pad) : 0;
|
|
const auto& slot = (player_id == 0) ? Options::controls().gamepad_p1
|
|
: Options::controls().gamepad_p2;
|
|
const int JUMP_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::JUMP);
|
|
const int ATTACK_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::ATTACK);
|
|
|
|
return std::ranges::any_of(EventBuffer::events, [&](const SDL_Event& ev) {
|
|
if (ev.type == SDL_EVENT_KEY_DOWN && (ev.key.scancode == KB_JUMP || ev.key.scancode == KB_ATTACK)) {
|
|
return true;
|
|
}
|
|
if (ev.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN && pad != nullptr && ev.gbutton.which == PAD_ID) {
|
|
const int BTN = static_cast<int>(ev.gbutton.button);
|
|
if (BTN == JUMP_BTN || BTN == ATTACK_BTN) { return true; }
|
|
}
|
|
return false;
|
|
});
|
|
}
|
|
|
|
} // namespace Input::Detect
|