You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
261 lines
9.2 KiB
261 lines
9.2 KiB
#include "controls/game_controls.h" |
|
|
|
#include <cstdint> |
|
|
|
#include "controls/controller_motion.h" |
|
#include "miniwin/misc_msg.h" |
|
#ifndef USE_SDL1 |
|
#include "controls/devices/game_controller.h" |
|
#endif |
|
#include "controls/devices/joystick.h" |
|
#include "controls/plrctrls.h" |
|
#include "controls/touch/gamepad.h" |
|
#include "doom.h" |
|
#include "gmenu.h" |
|
#include "options.h" |
|
#include "stores.h" |
|
|
|
namespace devilution { |
|
|
|
bool PadMenuNavigatorActive = false; |
|
bool PadHotspellMenuActive = false; |
|
ControllerButton SuppressedButton = ControllerButton_NONE; |
|
|
|
namespace { |
|
|
|
SDL_Keycode TranslateControllerButtonToGameMenuKey(ControllerButton controllerButton) |
|
{ |
|
switch (TranslateTo(GamepadType, controllerButton)) { |
|
case ControllerButton_BUTTON_A: |
|
case ControllerButton_BUTTON_BACK: |
|
case ControllerButton_BUTTON_START: |
|
return SDLK_ESCAPE; |
|
case ControllerButton_BUTTON_B: |
|
case ControllerButton_BUTTON_Y: |
|
return SDLK_RETURN; |
|
case ControllerButton_BUTTON_LEFTSTICK: |
|
return SDLK_TAB; // Map |
|
default: |
|
return SDLK_UNKNOWN; |
|
} |
|
} |
|
|
|
SDL_Keycode TranslateControllerButtonToMenuKey(ControllerButton controllerButton) |
|
{ |
|
switch (TranslateTo(GamepadType, controllerButton)) { |
|
case ControllerButton_BUTTON_A: |
|
case ControllerButton_BUTTON_BACK: |
|
case ControllerButton_BUTTON_START: |
|
return SDLK_ESCAPE; |
|
case ControllerButton_BUTTON_B: |
|
return SDLK_SPACE; |
|
case ControllerButton_BUTTON_Y: |
|
return SDLK_RETURN; |
|
case ControllerButton_BUTTON_LEFTSTICK: |
|
return SDLK_TAB; // Map |
|
case ControllerButton_BUTTON_DPAD_LEFT: |
|
return SDLK_LEFT; |
|
case ControllerButton_BUTTON_DPAD_RIGHT: |
|
return SDLK_RIGHT; |
|
case ControllerButton_BUTTON_DPAD_UP: |
|
return SDLK_UP; |
|
case ControllerButton_BUTTON_DPAD_DOWN: |
|
return SDLK_DOWN; |
|
default: |
|
return SDLK_UNKNOWN; |
|
} |
|
} |
|
|
|
SDL_Keycode TranslateControllerButtonToQuestLogKey(ControllerButton controllerButton) |
|
{ |
|
switch (TranslateTo(GamepadType, controllerButton)) { |
|
case ControllerButton_BUTTON_A: |
|
return SDLK_SPACE; |
|
case ControllerButton_BUTTON_B: |
|
case ControllerButton_BUTTON_Y: |
|
return SDLK_RETURN; |
|
case ControllerButton_BUTTON_LEFTSTICK: |
|
return SDLK_TAB; // Map |
|
default: |
|
return SDLK_UNKNOWN; |
|
} |
|
} |
|
|
|
SDL_Keycode TranslateControllerButtonToSpellbookKey(ControllerButton controllerButton) |
|
{ |
|
switch (TranslateTo(GamepadType, controllerButton)) { |
|
case ControllerButton_BUTTON_B: |
|
return SDLK_SPACE; |
|
case ControllerButton_BUTTON_Y: |
|
return SDLK_RETURN; |
|
case ControllerButton_BUTTON_LEFTSTICK: |
|
return SDLK_TAB; // Map |
|
case ControllerButton_BUTTON_DPAD_LEFT: |
|
return SDLK_LEFT; |
|
case ControllerButton_BUTTON_DPAD_RIGHT: |
|
return SDLK_RIGHT; |
|
case ControllerButton_BUTTON_DPAD_UP: |
|
return SDLK_UP; |
|
case ControllerButton_BUTTON_DPAD_DOWN: |
|
return SDLK_DOWN; |
|
default: |
|
return SDLK_UNKNOWN; |
|
} |
|
} |
|
|
|
} // namespace |
|
|
|
ControllerButton TranslateTo(GamepadLayout layout, ControllerButton button) |
|
{ |
|
if (layout != GamepadLayout::Nintendo) |
|
return button; |
|
|
|
switch (button) { |
|
case ControllerButton_BUTTON_A: |
|
return ControllerButton_BUTTON_B; |
|
case ControllerButton_BUTTON_B: |
|
return ControllerButton_BUTTON_A; |
|
case ControllerButton_BUTTON_X: |
|
return ControllerButton_BUTTON_Y; |
|
case ControllerButton_BUTTON_Y: |
|
return ControllerButton_BUTTON_X; |
|
default: |
|
return button; |
|
} |
|
} |
|
|
|
bool SkipsMovie(ControllerButtonEvent ctrlEvent) |
|
{ |
|
return IsAnyOf(ctrlEvent.button, |
|
ControllerButton_BUTTON_A, |
|
ControllerButton_BUTTON_B, |
|
ControllerButton_BUTTON_START, |
|
ControllerButton_BUTTON_BACK); |
|
} |
|
|
|
bool GetGameAction(const SDL_Event &event, ControllerButtonEvent ctrlEvent, GameAction *action) |
|
{ |
|
const bool inGameMenu = InGameMenu(); |
|
|
|
#ifndef USE_SDL1 |
|
if (ControlMode == ControlTypes::VirtualGamepad) { |
|
if (event.type == SDL_FINGERDOWN) { |
|
if (VirtualGamepadState.menuPanel.charButton.isHeld && VirtualGamepadState.menuPanel.charButton.didStateChange) { |
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO); |
|
return true; |
|
} |
|
if (VirtualGamepadState.menuPanel.questsButton.isHeld && VirtualGamepadState.menuPanel.questsButton.didStateChange) { |
|
*action = GameAction(GameActionType_TOGGLE_QUEST_LOG); |
|
return true; |
|
} |
|
if (VirtualGamepadState.menuPanel.inventoryButton.isHeld && VirtualGamepadState.menuPanel.inventoryButton.didStateChange) { |
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY); |
|
return true; |
|
} |
|
if (VirtualGamepadState.menuPanel.mapButton.isHeld && VirtualGamepadState.menuPanel.mapButton.didStateChange) { |
|
*action = GameActionSendKey { SDLK_TAB, false }; |
|
return true; |
|
} |
|
if (VirtualGamepadState.primaryActionButton.isHeld && VirtualGamepadState.primaryActionButton.didStateChange) { |
|
if (!inGameMenu && !QuestLogIsOpen && !sbookflag) { |
|
*action = GameAction(GameActionType_PRIMARY_ACTION); |
|
if (ControllerActionHeld == GameActionType_NONE) { |
|
ControllerActionHeld = GameActionType_PRIMARY_ACTION; |
|
} |
|
} else if (sgpCurrentMenu != nullptr || stextflag != STORE_NONE || QuestLogIsOpen) { |
|
*action = GameActionSendKey { SDLK_RETURN, false }; |
|
} else { |
|
*action = GameActionSendKey { SDLK_SPACE, false }; |
|
} |
|
return true; |
|
} |
|
if (VirtualGamepadState.secondaryActionButton.isHeld && VirtualGamepadState.secondaryActionButton.didStateChange) { |
|
if (!inGameMenu && !QuestLogIsOpen && !sbookflag) { |
|
*action = GameAction(GameActionType_SECONDARY_ACTION); |
|
if (ControllerActionHeld == GameActionType_NONE) |
|
ControllerActionHeld = GameActionType_SECONDARY_ACTION; |
|
} |
|
return true; |
|
} |
|
if (VirtualGamepadState.spellActionButton.isHeld && VirtualGamepadState.spellActionButton.didStateChange) { |
|
if (!inGameMenu && !QuestLogIsOpen && !sbookflag) { |
|
*action = GameAction(GameActionType_CAST_SPELL); |
|
if (ControllerActionHeld == GameActionType_NONE) |
|
ControllerActionHeld = GameActionType_CAST_SPELL; |
|
} |
|
return true; |
|
} |
|
if (VirtualGamepadState.cancelButton.isHeld && VirtualGamepadState.cancelButton.didStateChange) { |
|
if (inGameMenu || DoomFlag || spselflag) |
|
*action = GameActionSendKey { SDLK_ESCAPE, false }; |
|
else if (invflag) |
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY); |
|
else if (sbookflag) |
|
*action = GameAction(GameActionType_TOGGLE_SPELL_BOOK); |
|
else if (QuestLogIsOpen) |
|
*action = GameAction(GameActionType_TOGGLE_QUEST_LOG); |
|
else if (chrflag) |
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO); |
|
return true; |
|
} |
|
if (VirtualGamepadState.healthButton.isHeld && VirtualGamepadState.healthButton.didStateChange) { |
|
if (!QuestLogIsOpen && !sbookflag && stextflag == STORE_NONE) |
|
*action = GameAction(GameActionType_USE_HEALTH_POTION); |
|
return true; |
|
} |
|
if (VirtualGamepadState.manaButton.isHeld && VirtualGamepadState.manaButton.didStateChange) { |
|
if (!QuestLogIsOpen && !sbookflag && stextflag == STORE_NONE) |
|
*action = GameAction(GameActionType_USE_MANA_POTION); |
|
return true; |
|
} |
|
} else if (event.type == SDL_FINGERUP) { |
|
if ((!VirtualGamepadState.primaryActionButton.isHeld && ControllerActionHeld == GameActionType_PRIMARY_ACTION) |
|
|| (!VirtualGamepadState.secondaryActionButton.isHeld && ControllerActionHeld == GameActionType_SECONDARY_ACTION) |
|
|| (!VirtualGamepadState.spellActionButton.isHeld && ControllerActionHeld == GameActionType_CAST_SPELL)) { |
|
ControllerActionHeld = GameActionType_NONE; |
|
LastMouseButtonAction = MouseActionType::None; |
|
} |
|
} |
|
} |
|
#endif |
|
|
|
if (PadMenuNavigatorActive || PadHotspellMenuActive) |
|
return false; |
|
|
|
SDL_Keycode translation = SDLK_UNKNOWN; |
|
|
|
if (gmenu_is_active() || stextflag != STORE_NONE) |
|
translation = TranslateControllerButtonToGameMenuKey(ctrlEvent.button); |
|
else if (inGameMenu) |
|
translation = TranslateControllerButtonToMenuKey(ctrlEvent.button); |
|
else if (QuestLogIsOpen) |
|
translation = TranslateControllerButtonToQuestLogKey(ctrlEvent.button); |
|
else if (sbookflag) |
|
translation = TranslateControllerButtonToSpellbookKey(ctrlEvent.button); |
|
|
|
if (translation != SDLK_UNKNOWN) { |
|
*action = GameActionSendKey { static_cast<uint32_t>(translation), ctrlEvent.up }; |
|
return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
bool IsSimulatedMouseClickBinding(ControllerButtonEvent ctrlEvent) |
|
{ |
|
ControllerButtonCombo leftMouseClickBinding1 = sgOptions.Padmapper.ButtonComboForAction("LeftMouseClick1"); |
|
ControllerButtonCombo leftMouseClickBinding2 = sgOptions.Padmapper.ButtonComboForAction("LeftMouseClick2"); |
|
ControllerButtonCombo rightMouseClickBinding1 = sgOptions.Padmapper.ButtonComboForAction("RightMouseClick1"); |
|
ControllerButtonCombo rightMouseClickBinding2 = sgOptions.Padmapper.ButtonComboForAction("RightMouseClick2"); |
|
return (ctrlEvent.button == leftMouseClickBinding1.button && IsControllerButtonComboPressed(leftMouseClickBinding1)) |
|
|| (ctrlEvent.button == leftMouseClickBinding2.button && IsControllerButtonComboPressed(leftMouseClickBinding2)) |
|
|| (ctrlEvent.button == rightMouseClickBinding1.button && IsControllerButtonComboPressed(rightMouseClickBinding1)) |
|
|| (ctrlEvent.button == rightMouseClickBinding2.button && IsControllerButtonComboPressed(rightMouseClickBinding2)); |
|
} |
|
|
|
AxisDirection GetMoveDirection() |
|
{ |
|
return GetLeftStickOrDpadDirection(true); |
|
} |
|
|
|
} // namespace devilution
|
|
|