|
|
|
|
@ -7,9 +7,54 @@
|
|
|
|
|
#endif |
|
|
|
|
#include "controls/devices/joystick.h" |
|
|
|
|
#include "controls/devices/kbcontroller.h" |
|
|
|
|
#include "controls/game_controls.h" |
|
|
|
|
#include "controls/plrctrls.h" |
|
|
|
|
#include "player.h" |
|
|
|
|
|
|
|
|
|
namespace devilution { |
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
|
bool skipTick = false; |
|
|
|
|
bool heldControllerButtonEventsLocked = false; |
|
|
|
|
ControllerButton actionButtons[2] = { ControllerButton_ATTACK, ControllerButton_CAST_SPELL }; |
|
|
|
|
|
|
|
|
|
bool CreateActionButtonEvent(SDL_Event *event, ControllerButton button) |
|
|
|
|
{ |
|
|
|
|
SDL_JoystickID which; |
|
|
|
|
|
|
|
|
|
#ifndef USE_SDL1 |
|
|
|
|
if (GameController::IsPressedOnAnyController(button, &which)) { |
|
|
|
|
event->type = SDL_CONTROLLERBUTTONDOWN; |
|
|
|
|
event->cbutton.button = GameController::ToSdlGameControllerButton(button); |
|
|
|
|
event->cbutton.state = ControllerButtonState_HELD; |
|
|
|
|
event->cbutton.which = which; |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
#if HAS_KBCTRL == 1 |
|
|
|
|
if (IsKbCtrlButtonPressed(button)) { |
|
|
|
|
event->type = SDL_KEYDOWN; |
|
|
|
|
event->key.keysym.sym = ControllerButtonToKbCtrlKeyCode(button); |
|
|
|
|
event->key.state = ControllerButtonState_HELD; |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
if (Joystick::IsPressedOnAnyJoystick(button)) { |
|
|
|
|
event->type = SDL_JOYBUTTONDOWN; |
|
|
|
|
event->jbutton.button = Joystick::ToSdlJoyButton(button); |
|
|
|
|
event->jbutton.state = ControllerButtonState_HELD; |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
void UnlockControllerState(const SDL_Event &event) |
|
|
|
|
{ |
|
|
|
|
#ifndef USE_SDL1 |
|
|
|
|
@ -22,7 +67,7 @@ void UnlockControllerState(const SDL_Event &event)
|
|
|
|
|
|
|
|
|
|
ControllerButtonEvent ToControllerButtonEvent(const SDL_Event &event) |
|
|
|
|
{ |
|
|
|
|
ControllerButtonEvent result { ControllerButton_NONE, false }; |
|
|
|
|
ControllerButtonEvent result { ControllerButton_NONE, false, ControllerButtonState_RELEASED }; |
|
|
|
|
switch (event.type) { |
|
|
|
|
#ifndef USE_SDL1 |
|
|
|
|
case SDL_CONTROLLERBUTTONUP: |
|
|
|
|
@ -36,6 +81,8 @@ ControllerButtonEvent ToControllerButtonEvent(const SDL_Event &event)
|
|
|
|
|
} |
|
|
|
|
#if HAS_KBCTRL == 1 |
|
|
|
|
result.button = KbCtrlToControllerButton(event); |
|
|
|
|
result.state = event.key.state; |
|
|
|
|
|
|
|
|
|
if (result.button != ControllerButton_NONE) |
|
|
|
|
return result; |
|
|
|
|
#endif |
|
|
|
|
@ -43,14 +90,18 @@ ControllerButtonEvent ToControllerButtonEvent(const SDL_Event &event)
|
|
|
|
|
GameController *const controller = GameController::Get(event); |
|
|
|
|
if (controller != nullptr) { |
|
|
|
|
result.button = controller->ToControllerButton(event); |
|
|
|
|
result.state = event.cbutton.state; |
|
|
|
|
|
|
|
|
|
if (result.button != ControllerButton_NONE) |
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
const Joystick *joystick = Joystick::Get(event); |
|
|
|
|
if (joystick != nullptr) |
|
|
|
|
if (joystick != nullptr) { |
|
|
|
|
result.button = devilution::Joystick::ToControllerButton(event); |
|
|
|
|
result.state = event.jbutton.state; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
@ -93,4 +144,40 @@ bool HandleControllerAddedOrRemovedEvent(const SDL_Event &event)
|
|
|
|
|
#endif |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void UnlockHeldControllerButtonEvents(const SDL_Event &event) |
|
|
|
|
{ |
|
|
|
|
ControllerButtonEvent ctrlEvent = ToControllerButtonEvent(event); |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) |
|
|
|
|
if (actionButtons[i] == ctrlEvent.button) { |
|
|
|
|
heldControllerButtonEventsLocked = !CanControlHero(); |
|
|
|
|
|
|
|
|
|
break; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int PollActionButtonPressed(SDL_Event *event) |
|
|
|
|
{ |
|
|
|
|
if (heldControllerButtonEventsLocked) |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
if (skipTick) { |
|
|
|
|
skipTick = false; |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!Players[MyPlayerId].CanChangeAction()) |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i) |
|
|
|
|
if (CreateActionButtonEvent(event, actionButtons[i])) { |
|
|
|
|
skipTick = true; // 1 tick is skipped for allowing the player animation to change
|
|
|
|
|
|
|
|
|
|
return 1; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} // namespace devilution
|
|
|
|
|
|