refactor(input): elimina InputState vestigial
This commit is contained in:
1 parent
fd3da8cb99
commit
80140434e7
6 files changed
+14
-119
No files matched your search
@@ -74,7 +74,6 @@ set(APP_SOURCES
|
|||||||
source/core/ui/ui_config.cpp
|
source/core/ui/ui_config.cpp
|
||||||
source/core/input/key_config.cpp
|
source/core/input/key_config.cpp
|
||||||
source/core/input/global_inputs.cpp
|
source/core/input/global_inputs.cpp
|
||||||
source/core/input/input_state.cpp
|
|
||||||
source/core/input/gamepad_manager.cpp
|
source/core/input/gamepad_manager.cpp
|
||||||
source/core/input/gamepad_bindings.cpp
|
source/core/input/gamepad_bindings.cpp
|
||||||
source/core/input/define_buttons.cpp
|
source/core/input/define_buttons.cpp
|
||||||
|
|||||||
@@ -14,9 +14,8 @@
|
|||||||
//
|
//
|
||||||
// El polling real de botons i eixos NO es fa a través d'aquesta classe:
|
// El polling real de botons i eixos NO es fa a través d'aquesta classe:
|
||||||
// els consumidors obtenen el `SDL_Gamepad*` primari (o l'enèsim per a
|
// els consumidors obtenen el `SDL_Gamepad*` primari (o l'enèsim per a
|
||||||
// multi-jugador futur) i el passen a `InputState::getGamepadButton/Axis`,
|
// multi-jugador futur) i el passen directament a
|
||||||
// que ho filtra pel flag de bloqueig de la UI (mateixa estratègia que
|
// `SDL_GetGamepadButton`/`SDL_GetGamepadAxis`.
|
||||||
// amb el teclat).
|
|
||||||
class GamepadManager {
|
class GamepadManager {
|
||||||
public:
|
public:
|
||||||
static auto get() -> GamepadManager&;
|
static auto get() -> GamepadManager&;
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
#include "core/input/input_state.hpp"
|
|
||||||
|
|
||||||
#include <array>
|
|
||||||
|
|
||||||
namespace InputState {
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
bool blocked = false;
|
|
||||||
|
|
||||||
// Buffer retornat quan el bloqueig és actiu. `SDL_GetKeyboardState`
|
|
||||||
// retorna un pointer d'una mida igual a `SDL_SCANCODE_COUNT`; el
|
|
||||||
// nostre buffer ha de ser com a mínim igual de gran perquè els
|
|
||||||
// consumidors puguin indexar-lo amb qualsevol scancode sense
|
|
||||||
// llegir fora de rang.
|
|
||||||
auto zeroKeyboard() -> const bool* {
|
|
||||||
static const std::array<bool, SDL_SCANCODE_COUNT> ZEROS{};
|
|
||||||
return ZEROS.data();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
void setBlocked(bool value) { blocked = value; }
|
|
||||||
|
|
||||||
auto isBlocked() -> bool { return blocked; }
|
|
||||||
|
|
||||||
auto getKeyboardState() -> const bool* {
|
|
||||||
return blocked ? zeroKeyboard() : SDL_GetKeyboardState(nullptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto getGamepadButton(SDL_Gamepad* gp, SDL_GamepadButton b) -> bool {
|
|
||||||
return (blocked || gp == nullptr) ? false : SDL_GetGamepadButton(gp, b);
|
|
||||||
}
|
|
||||||
|
|
||||||
auto getGamepadAxis(SDL_Gamepad* gp, SDL_GamepadAxis ax) -> Sint16 {
|
|
||||||
return (blocked || gp == nullptr) ? static_cast<Sint16>(0) : SDL_GetGamepadAxis(gp, ax);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace InputState
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
// Wrapper de l'estat d'input (polling) amb gating de bloqueig.
|
|
||||||
//
|
|
||||||
// El service menu (i qualsevol UI modal futura) ha de poder "amagar" l'estat
|
|
||||||
// d'entrada als consumidors de polling — el jugador, entitats controlades
|
|
||||||
// per l'usuari, etc. — mentre la UI captura l'atenció, sense interrompre la
|
|
||||||
// resta del joc. Per això tots els consumidors de polling han de llegir a
|
|
||||||
// través d'aquest mòdul i no cridar `SDL_GetKeyboardState` directament. Si
|
|
||||||
// `setBlocked(true)` està actiu, el wrapper retorna un buffer tot-a-zero;
|
|
||||||
// altrament, la crida és transparent (mateix pointer que SDL).
|
|
||||||
//
|
|
||||||
// El menú NO passa per aquí: consumeix events (`SDL_EVENT_KEY_DOWN`) abans
|
|
||||||
// que l'escena els vegi, així que la seva navegació segueix funcionant per
|
|
||||||
// molt que el flag estigui posat.
|
|
||||||
//
|
|
||||||
// El mateix patró aplica al gamepad: `getGamepadButton`/`getGamepadAxis`
|
|
||||||
// retornen zero/false quan el bloqueig és actiu. Els mandos concrets els
|
|
||||||
// obtén el caller via `GamepadManager::get().getPrimary()` o similars.
|
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
|
|
||||||
namespace InputState {
|
|
||||||
|
|
||||||
// Activa/desactiva el bloqueig global de polling. El ServiceMenu el
|
|
||||||
// crida a `toggle()`/`close()`. No es toca des de cap altre lloc.
|
|
||||||
void setBlocked(bool value);
|
|
||||||
|
|
||||||
[[nodiscard]] auto isBlocked() -> bool;
|
|
||||||
|
|
||||||
// Equivalent a `SDL_GetKeyboardState(nullptr)`: retorna un buffer de
|
|
||||||
// `SDL_SCANCODE_COUNT` bools. Si l'input està bloquejat, tots zero —
|
|
||||||
// el consumidor veu com si cap tecla estigués premuda, sense branques
|
|
||||||
// explícites. El buffer és estable mentre dura la sessió; no cal
|
|
||||||
// guardar-lo frame a frame.
|
|
||||||
[[nodiscard]] auto getKeyboardState() -> const bool*;
|
|
||||||
|
|
||||||
// Versió filtrada de `SDL_GetGamepadButton`. Retorna false si el bloqueig
|
|
||||||
// és actiu o si `gp` és nullptr (cas habitual: no hi ha cap mand).
|
|
||||||
[[nodiscard]] auto getGamepadButton(SDL_Gamepad* gp, SDL_GamepadButton b) -> bool;
|
|
||||||
|
|
||||||
// Versió filtrada de `SDL_GetGamepadAxis`. Retorna 0 si el bloqueig
|
|
||||||
// és actiu o si `gp` és nullptr.
|
|
||||||
[[nodiscard]] auto getGamepadAxis(SDL_Gamepad* gp, SDL_GamepadAxis ax) -> Sint16;
|
|
||||||
|
|
||||||
} // namespace InputState
|
|
||||||
@@ -11,7 +11,6 @@
|
|||||||
#include "core/audio/audio.hpp"
|
#include "core/audio/audio.hpp"
|
||||||
#include "core/input/gamepad_bindings.hpp"
|
#include "core/input/gamepad_bindings.hpp"
|
||||||
#include "core/input/gamepad_manager.hpp"
|
#include "core/input/gamepad_manager.hpp"
|
||||||
#include "core/input/input_state.hpp"
|
|
||||||
#include "core/rendering/canvas.hpp"
|
#include "core/rendering/canvas.hpp"
|
||||||
#include "core/rendering/effects/shadow.hpp"
|
#include "core/rendering/effects/shadow.hpp"
|
||||||
#include "core/rendering/screen.hpp"
|
#include "core/rendering/screen.hpp"
|
||||||
@@ -46,12 +45,12 @@ namespace {
|
|||||||
void accumulateGamepadMovement(int player_id, float& vx, float& vy) {
|
void accumulateGamepadMovement(int player_id, float& vx, float& vy) {
|
||||||
SDL_Gamepad* pad = GamepadManager::get().getForPlayer(player_id);
|
SDL_Gamepad* pad = GamepadManager::get().getForPlayer(player_id);
|
||||||
if (pad == nullptr) { return; }
|
if (pad == nullptr) { return; }
|
||||||
if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT)) { vx -= SPEED_PX_PER_S; }
|
if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_LEFT)) { vx -= SPEED_PX_PER_S; }
|
||||||
if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) { vx += SPEED_PX_PER_S; }
|
if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_RIGHT)) { vx += SPEED_PX_PER_S; }
|
||||||
if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP)) { vy -= SPEED_PX_PER_S; }
|
if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_UP)) { vy -= SPEED_PX_PER_S; }
|
||||||
if (InputState::getGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN)) { vy += SPEED_PX_PER_S; }
|
if (SDL_GetGamepadButton(pad, SDL_GAMEPAD_BUTTON_DPAD_DOWN)) { vy += SPEED_PX_PER_S; }
|
||||||
const Sint16 LX = InputState::getGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX);
|
const Sint16 LX = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTX);
|
||||||
const Sint16 LY = InputState::getGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY);
|
const Sint16 LY = SDL_GetGamepadAxis(pad, SDL_GAMEPAD_AXIS_LEFTY);
|
||||||
if (LX < -STICK_DEADZONE) { vx -= SPEED_PX_PER_S; }
|
if (LX < -STICK_DEADZONE) { vx -= SPEED_PX_PER_S; }
|
||||||
if (LX > STICK_DEADZONE) { vx += SPEED_PX_PER_S; }
|
if (LX > STICK_DEADZONE) { vx += SPEED_PX_PER_S; }
|
||||||
if (LY < -STICK_DEADZONE) { vy -= SPEED_PX_PER_S; }
|
if (LY < -STICK_DEADZONE) { vy -= SPEED_PX_PER_S; }
|
||||||
@@ -657,11 +656,7 @@ void Player::tick(float delta_time) {
|
|||||||
// (`KeyRemap::update` que llig `Options::keys_game` cada frame).
|
// (`KeyRemap::update` que llig `Options::keys_game` cada frame).
|
||||||
refreshControlsFromOptions();
|
refreshControlsFromOptions();
|
||||||
|
|
||||||
// Via `InputState` en lloc de `SDL_GetKeyboardState` directe: quan el
|
const bool* keys = SDL_GetKeyboardState(nullptr);
|
||||||
// service menu (o qualsevol UI modal) està obert, el wrapper retorna
|
|
||||||
// un buffer tot-a-zero perquè el jugador no es mogui mentre l'usuari
|
|
||||||
// navega el menú. Polling-blocked; veure `core/input/input_state.hpp`.
|
|
||||||
const bool* keys = InputState::getKeyboardState();
|
|
||||||
float vx = 0.0F;
|
float vx = 0.0F;
|
||||||
float vy = 0.0F;
|
float vy = 0.0F;
|
||||||
if (keys[controls_.left]) { vx -= SPEED_PX_PER_S; }
|
if (keys[controls_.left]) { vx -= SPEED_PX_PER_S; }
|
||||||
@@ -669,9 +664,7 @@ void Player::tick(float delta_time) {
|
|||||||
if (keys[controls_.up]) { vy -= SPEED_PX_PER_S; }
|
if (keys[controls_.up]) { vy -= SPEED_PX_PER_S; }
|
||||||
if (keys[controls_.down]) { vy += SPEED_PX_PER_S; }
|
if (keys[controls_.down]) { vy += SPEED_PX_PER_S; }
|
||||||
|
|
||||||
// Cruceta i stick esquerre del mando assignat (si n'hi ha). També
|
// Cruceta i stick esquerre del mando assignat (si n'hi ha).
|
||||||
// respecten el bloqueig d'`InputState`, així que amb el menú obert
|
|
||||||
// els botons tornen false/0 i no contribueixen a la velocitat.
|
|
||||||
accumulateGamepadMovement(id_, vx, vy);
|
accumulateGamepadMovement(id_, vx, vy);
|
||||||
|
|
||||||
input_vx_ = vx;
|
input_vx_ = vx;
|
||||||
@@ -749,7 +742,7 @@ auto Player::consumeJumpEdge() -> bool {
|
|||||||
// dos passa de no-premut a premut aquest frame, comptem com a salt.
|
// dos passa de no-premut a premut aquest frame, comptem com a salt.
|
||||||
// Sense detecció de flanc, mantenir la tecla provocaria saltar
|
// Sense detecció de flanc, mantenir la tecla provocaria saltar
|
||||||
// immediatament en aterrar.
|
// immediatament en aterrar.
|
||||||
const bool* keys = InputState::getKeyboardState();
|
const bool* keys = SDL_GetKeyboardState(nullptr);
|
||||||
const bool KEY_NOW = keys[controls_.jump];
|
const bool KEY_NOW = keys[controls_.jump];
|
||||||
bool edge = (KEY_NOW && !jump_held_keyboard_);
|
bool edge = (KEY_NOW && !jump_held_keyboard_);
|
||||||
jump_held_keyboard_ = KEY_NOW;
|
jump_held_keyboard_ = KEY_NOW;
|
||||||
@@ -758,8 +751,7 @@ auto Player::consumeJumpEdge() -> bool {
|
|||||||
const auto& slot = (id_ == 0) ? Options::controls().gamepad_p1
|
const auto& slot = (id_ == 0) ? Options::controls().gamepad_p1
|
||||||
: Options::controls().gamepad_p2;
|
: Options::controls().gamepad_p2;
|
||||||
const int JUMP_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::JUMP);
|
const int JUMP_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::JUMP);
|
||||||
const bool PAD_NOW = (pad != nullptr) && !InputState::isBlocked() &&
|
const bool PAD_NOW = (pad != nullptr) && GamepadBindings::isPressed(pad, JUMP_BTN);
|
||||||
GamepadBindings::isPressed(pad, JUMP_BTN);
|
|
||||||
if (PAD_NOW && !jump_held_gamepad_) { edge = true; }
|
if (PAD_NOW && !jump_held_gamepad_) { edge = true; }
|
||||||
jump_held_gamepad_ = PAD_NOW;
|
jump_held_gamepad_ = PAD_NOW;
|
||||||
|
|
||||||
@@ -770,7 +762,7 @@ auto Player::consumeAttackEdge() -> bool {
|
|||||||
// Mateix patró que `consumeJumpEdge` però per al botó d'atac. El botó
|
// Mateix patró que `consumeJumpEdge` però per al botó d'atac. El botó
|
||||||
// físic ve d'`GamepadBindings`, persistit per dispositiu — l'usuari
|
// físic ve d'`GamepadBindings`, persistit per dispositiu — l'usuari
|
||||||
// l'ha pogut remapejar des del menú de servei.
|
// l'ha pogut remapejar des del menú de servei.
|
||||||
const bool* keys = InputState::getKeyboardState();
|
const bool* keys = SDL_GetKeyboardState(nullptr);
|
||||||
const bool KEY_NOW = keys[controls_.attack];
|
const bool KEY_NOW = keys[controls_.attack];
|
||||||
bool edge = (KEY_NOW && !attack_held_keyboard_);
|
bool edge = (KEY_NOW && !attack_held_keyboard_);
|
||||||
attack_held_keyboard_ = KEY_NOW;
|
attack_held_keyboard_ = KEY_NOW;
|
||||||
@@ -779,8 +771,7 @@ auto Player::consumeAttackEdge() -> bool {
|
|||||||
const auto& slot = (id_ == 0) ? Options::controls().gamepad_p1
|
const auto& slot = (id_ == 0) ? Options::controls().gamepad_p1
|
||||||
: Options::controls().gamepad_p2;
|
: Options::controls().gamepad_p2;
|
||||||
const int ATK_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::ATTACK);
|
const int ATK_BTN = GamepadBindings::get().getButton(slot.name, slot.path, GamepadBindings::Action::ATTACK);
|
||||||
const bool PAD_NOW = (pad != nullptr) && !InputState::isBlocked() &&
|
const bool PAD_NOW = (pad != nullptr) && GamepadBindings::isPressed(pad, ATK_BTN);
|
||||||
GamepadBindings::isPressed(pad, ATK_BTN);
|
|
||||||
if (PAD_NOW && !attack_held_gamepad_) { edge = true; }
|
if (PAD_NOW && !attack_held_gamepad_) { edge = true; }
|
||||||
attack_held_gamepad_ = PAD_NOW;
|
attack_held_gamepad_ = PAD_NOW;
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@
|
|||||||
#include "core/audio/audio.hpp"
|
#include "core/audio/audio.hpp"
|
||||||
#include "core/input/gamepad_bindings.hpp"
|
#include "core/input/gamepad_bindings.hpp"
|
||||||
#include "core/input/gamepad_manager.hpp"
|
#include "core/input/gamepad_manager.hpp"
|
||||||
#include "core/input/input_state.hpp"
|
|
||||||
#include "core/input/key_config.hpp"
|
#include "core/input/key_config.hpp"
|
||||||
#include "core/locale/locale.hpp"
|
#include "core/locale/locale.hpp"
|
||||||
#include "core/rendering/canvas.hpp"
|
#include "core/rendering/canvas.hpp"
|
||||||
@@ -246,11 +245,6 @@ void ServiceMenu::toggle() {
|
|||||||
open_ = true;
|
open_ = true;
|
||||||
closing_ = false;
|
closing_ = false;
|
||||||
animated_h_ = 0.0F; // Comença des de 0 perquè la caixa crega.
|
animated_h_ = 0.0F; // Comença des de 0 perquè la caixa crega.
|
||||||
// Bloqueja l'input de polling: els consumidors com `Player` veuran
|
|
||||||
// l'estat del teclat tot-a-zero mentre el menú sigui obert. Els
|
|
||||||
// events (KEY_DOWN, etc.) ja els consumeix `processMenuInput`,
|
|
||||||
// així que la navegació del menú no es veu afectada.
|
|
||||||
InputState::setBlocked(true);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,9 +253,6 @@ void ServiceMenu::close() {
|
|||||||
open_ = false;
|
open_ = false;
|
||||||
closing_ = true; // `open_anim_` decaurà a update; la caixa es veu mentre dura.
|
closing_ = true; // `open_anim_` decaurà a update; la caixa es veu mentre dura.
|
||||||
capturing_ = false;
|
capturing_ = false;
|
||||||
// Allibera el polling de seguida — la closing animation és purament
|
|
||||||
// visual i no ha d'impedir que el jugador es mogui un cop ha tancat.
|
|
||||||
InputState::setBlocked(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// `isVisible` inclou l'animació de tancament: retorna true mentre open_anim_>0
|
// `isVisible` inclou l'animació de tancament: retorna true mentre open_anim_>0
|
||||||
|
|||||||
Reference in new issue
Block a user