InputAction → enum class Input::Action
This commit is contained in:
+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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user