InputAction → enum class Input::Action
This commit is contained in:
@@ -8,29 +8,29 @@ namespace GlobalInputs {
|
||||
auto handle() -> bool {
|
||||
if (Screen::get() == nullptr || Input::get() == nullptr) { return false; }
|
||||
|
||||
if (Input::get()->checkInput(WINDOW_FULLSCREEN, Input::Repeat::OFF)) {
|
||||
if (Input::get()->checkInput(Input::Action::WINDOW_FULLSCREEN, Input::Repeat::OFF)) {
|
||||
Screen::get()->toggleVideoMode();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(WINDOW_DEC_ZOOM, Input::Repeat::OFF)) {
|
||||
if (Input::get()->checkInput(Input::Action::WINDOW_DEC_ZOOM, Input::Repeat::OFF)) {
|
||||
Screen::get()->decWindowZoom();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(WINDOW_INC_ZOOM, Input::Repeat::OFF)) {
|
||||
if (Input::get()->checkInput(Input::Action::WINDOW_INC_ZOOM, Input::Repeat::OFF)) {
|
||||
Screen::get()->incWindowZoom();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(TOGGLE_SHADER, Input::Repeat::OFF)) {
|
||||
if (Input::get()->checkInput(Input::Action::TOGGLE_SHADER, Input::Repeat::OFF)) {
|
||||
Screen::get()->toggleShaderEnabled();
|
||||
return true;
|
||||
}
|
||||
// F5/F6 només actuen quan el post-procesado està actiu.
|
||||
if (Screen::isShaderEnabled()) {
|
||||
if (Input::get()->checkInput(TOGGLE_SHADER_TYPE, Input::Repeat::OFF)) {
|
||||
if (Input::get()->checkInput(Input::Action::TOGGLE_SHADER_TYPE, Input::Repeat::OFF)) {
|
||||
Screen::get()->toggleActiveShader();
|
||||
return true;
|
||||
}
|
||||
if (Input::get()->checkInput(NEXT_SHADER_PRESET, Input::Repeat::OFF)) {
|
||||
if (Input::get()->checkInput(Input::Action::NEXT_SHADER_PRESET, Input::Repeat::OFF)) {
|
||||
Screen::get()->nextPreset();
|
||||
return true;
|
||||
}
|
||||
|
||||
+17
-15
@@ -65,12 +65,12 @@ Input::Input(std::string file)
|
||||
KeyBindings kb;
|
||||
kb.scancode = 0;
|
||||
kb.active = false;
|
||||
key_bindings_.resize(NUMBER_OF_INPUTS, kb);
|
||||
key_bindings_.resize(static_cast<std::size_t>(Action::NUMBER_OF_INPUTS), kb);
|
||||
|
||||
GameControllerBindings gcb;
|
||||
gcb.button = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
gcb.active = false;
|
||||
game_controller_bindings_.resize(NUMBER_OF_INPUTS, gcb);
|
||||
game_controller_bindings_.resize(static_cast<std::size_t>(Action::NUMBER_OF_INPUTS), gcb);
|
||||
}
|
||||
|
||||
// Destructor
|
||||
@@ -94,17 +94,17 @@ void Input::update() {
|
||||
}
|
||||
|
||||
// Asigna inputs a teclas
|
||||
void Input::bindKey(Uint8 input, SDL_Scancode code) {
|
||||
key_bindings_[input].scancode = code;
|
||||
void Input::bindKey(Action input, SDL_Scancode code) {
|
||||
key_bindings_[static_cast<std::size_t>(input)].scancode = code;
|
||||
}
|
||||
|
||||
// Asigna inputs a botones del mando
|
||||
void Input::bindGameControllerButton(Uint8 input, SDL_GamepadButton button) {
|
||||
game_controller_bindings_[input].button = button;
|
||||
void Input::bindGameControllerButton(Action input, SDL_GamepadButton button) {
|
||||
game_controller_bindings_[static_cast<std::size_t>(input)].button = button;
|
||||
}
|
||||
|
||||
// Comprueba si un input esta activo
|
||||
auto Input::checkInput(Uint8 input, Repeat repeat, Device device, int index) -> bool {
|
||||
auto Input::checkInput(Action input, Repeat repeat, Device device, int index) -> bool {
|
||||
if (!enabled_) {
|
||||
return false;
|
||||
}
|
||||
@@ -127,31 +127,33 @@ auto Input::checkInput(Uint8 input, Repeat repeat, Device device, int index) ->
|
||||
}
|
||||
|
||||
// Helper de checkInput: comprueba el estado de una tecla
|
||||
auto Input::checkKeyboardInput(Uint8 input, Repeat repeat) -> bool {
|
||||
auto Input::checkKeyboardInput(Action input, Repeat repeat) -> bool {
|
||||
const auto IDX = static_cast<std::size_t>(input);
|
||||
const bool *key_states = SDL_GetKeyboardState(nullptr);
|
||||
const bool IS_DOWN = key_states[key_bindings_[input].scancode];
|
||||
const bool IS_DOWN = key_states[key_bindings_[IDX].scancode];
|
||||
|
||||
if (repeat == Repeat::ON) {
|
||||
return IS_DOWN;
|
||||
}
|
||||
|
||||
// Modo edge-trigger: éxito sólo en el frame en que la tecla pasa de up a down
|
||||
const bool PRESS_EDGE = IS_DOWN && !key_bindings_[input].active;
|
||||
key_bindings_[input].active = IS_DOWN;
|
||||
const bool PRESS_EDGE = IS_DOWN && !key_bindings_[IDX].active;
|
||||
key_bindings_[IDX].active = IS_DOWN;
|
||||
return PRESS_EDGE;
|
||||
}
|
||||
|
||||
// Helper de checkInput: comprueba el estado de un botón de mando
|
||||
auto Input::checkGameControllerInput(Uint8 input, Repeat repeat, int index) -> bool {
|
||||
const bool IS_DOWN = SDL_GetGamepadButton(connected_controllers_[index], game_controller_bindings_[input].button);
|
||||
auto Input::checkGameControllerInput(Action input, Repeat repeat, int index) -> bool {
|
||||
const auto IDX = static_cast<std::size_t>(input);
|
||||
const bool IS_DOWN = SDL_GetGamepadButton(connected_controllers_[index], game_controller_bindings_[IDX].button);
|
||||
|
||||
if (repeat == Repeat::ON) {
|
||||
return IS_DOWN;
|
||||
}
|
||||
|
||||
// Modo edge-trigger: éxito sólo en el frame en que el botón pasa de up a down
|
||||
const bool PRESS_EDGE = IS_DOWN && !game_controller_bindings_[input].active;
|
||||
game_controller_bindings_[input].active = IS_DOWN;
|
||||
const bool PRESS_EDGE = IS_DOWN && !game_controller_bindings_[IDX].active;
|
||||
game_controller_bindings_[IDX].active = IS_DOWN;
|
||||
return PRESS_EDGE;
|
||||
}
|
||||
|
||||
|
||||
+34
-34
@@ -6,35 +6,6 @@
|
||||
#include <string> // for string, basic_string
|
||||
#include <vector> // for vector
|
||||
|
||||
enum InputAction : std::uint8_t {
|
||||
// Inputs obligatorios
|
||||
INVALID,
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
PAUSE,
|
||||
EXIT,
|
||||
ACCEPT,
|
||||
CANCEL,
|
||||
|
||||
// Inputs personalizados
|
||||
FIRE_LEFT,
|
||||
FIRE_CENTER,
|
||||
FIRE_RIGHT,
|
||||
WINDOW_FULLSCREEN,
|
||||
WINDOW_INC_ZOOM,
|
||||
WINDOW_DEC_ZOOM,
|
||||
|
||||
// GPU / shaders (hotkeys provisionales hasta que haya menú de opciones)
|
||||
NEXT_SHADER_PRESET,
|
||||
TOGGLE_SHADER,
|
||||
TOGGLE_SHADER_TYPE,
|
||||
|
||||
// Input obligatorio
|
||||
NUMBER_OF_INPUTS
|
||||
};
|
||||
|
||||
enum InputDisable : std::uint8_t {
|
||||
NOT_DISABLED,
|
||||
FOREVER,
|
||||
@@ -54,6 +25,35 @@ class Input {
|
||||
ANY
|
||||
};
|
||||
|
||||
enum class Action : std::uint8_t {
|
||||
// Inputs obligatorios
|
||||
INVALID,
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
PAUSE,
|
||||
EXIT,
|
||||
ACCEPT,
|
||||
CANCEL,
|
||||
|
||||
// Inputs personalizados
|
||||
FIRE_LEFT,
|
||||
FIRE_CENTER,
|
||||
FIRE_RIGHT,
|
||||
WINDOW_FULLSCREEN,
|
||||
WINDOW_INC_ZOOM,
|
||||
WINDOW_DEC_ZOOM,
|
||||
|
||||
// GPU / shaders (hotkeys provisionales hasta que haya menú de opciones)
|
||||
NEXT_SHADER_PRESET,
|
||||
TOGGLE_SHADER,
|
||||
TOGGLE_SHADER_TYPE,
|
||||
|
||||
// Centinela final (usar para sizing)
|
||||
NUMBER_OF_INPUTS
|
||||
};
|
||||
|
||||
// Singleton API
|
||||
static void init(const std::string &game_controller_db_path); // Crea la instancia
|
||||
static void destroy(); // Libera la instancia
|
||||
@@ -62,10 +62,10 @@ class Input {
|
||||
~Input(); // Destructor
|
||||
|
||||
void update(); // Actualiza el estado del objeto
|
||||
void bindKey(Uint8 input, SDL_Scancode code); // Asigna inputs a teclas
|
||||
void bindGameControllerButton(Uint8 input, SDL_GamepadButton button); // Asigna inputs a botones del mando
|
||||
void bindKey(Action input, SDL_Scancode code); // Asigna inputs a teclas
|
||||
void bindGameControllerButton(Action input, SDL_GamepadButton button); // Asigna inputs a botones del mando
|
||||
|
||||
auto checkInput(Uint8 input, Repeat repeat = Repeat::ON, Device device = Device::ANY, int index = 0) -> bool; // Comprueba si un input esta activo
|
||||
auto checkInput(Action input, Repeat repeat = Repeat::ON, Device device = Device::ANY, int index = 0) -> bool; // Comprueba si un input esta activo
|
||||
auto checkAnyInput(Device device = Device::ANY, int index = 0) -> bool; // Comprueba si hay almenos un input activo
|
||||
|
||||
auto discoverGameController() -> bool; // Busca si hay un mando conectado
|
||||
@@ -119,8 +119,8 @@ class Input {
|
||||
static auto buildControllerName(SDL_Gamepad *pad, int pad_index) -> std::string;
|
||||
|
||||
// Helpers de checkInput
|
||||
auto checkKeyboardInput(Uint8 input, Repeat repeat) -> bool;
|
||||
auto checkGameControllerInput(Uint8 input, Repeat repeat, int index) -> bool;
|
||||
auto checkKeyboardInput(Action input, Repeat repeat) -> bool;
|
||||
auto checkGameControllerInput(Action input, Repeat repeat, int index) -> bool;
|
||||
|
||||
// Helpers de discoverGameController
|
||||
void resetGameControllerState();
|
||||
|
||||
Reference in New Issue
Block a user