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.
355 lines
12 KiB
355 lines
12 KiB
|
6 years ago
|
#include "controls/game_controls.h"
|
||
|
|
|
||
|
|
#include <cstdint>
|
||
|
|
|
||
|
|
#include "controls/controller.h"
|
||
|
|
#include "controls/controller_motion.h"
|
||
|
|
#include "controls/devices/game_controller.h"
|
||
|
|
#include "controls/devices/joystick.h"
|
||
|
|
#include "controls/menu_controls.h"
|
||
|
6 years ago
|
#include "controls/modifier_hints.h"
|
||
|
6 years ago
|
#include "controls/plrctrls.h"
|
||
|
5 years ago
|
#include "gmenu.h"
|
||
|
5 years ago
|
#include "options.h"
|
||
|
5 years ago
|
#include "stores.h"
|
||
|
6 years ago
|
|
||
|
5 years ago
|
namespace devilution {
|
||
|
6 years ago
|
|
||
|
5 years ago
|
bool start_modifier_active = false;
|
||
|
|
bool select_modifier_active = false;
|
||
|
5 years ago
|
|
||
|
6 years ago
|
namespace {
|
||
|
|
|
||
|
|
DWORD translate_controller_button_to_key(ControllerButton controller_button)
|
||
|
|
{
|
||
|
|
switch (controller_button) {
|
||
|
6 years ago
|
case ControllerButton_BUTTON_A: // Bottom button
|
||
|
6 years ago
|
return questlog ? DVL_VK_SPACE : DVL_VK_ESCAPE;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_B: // Right button
|
||
|
6 years ago
|
return sgpCurrentMenu || stextflag || questlog ? DVL_VK_RETURN : DVL_VK_SPACE;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_Y: // Top button
|
||
|
6 years ago
|
return DVL_VK_RETURN;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_LEFTSTICK:
|
||
|
6 years ago
|
return DVL_VK_TAB; // Map
|
||
|
5 years ago
|
case ControllerButton_BUTTON_BACK:
|
||
|
6 years ago
|
case ControllerButton_BUTTON_START:
|
||
|
6 years ago
|
return DVL_VK_ESCAPE;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_LEFT:
|
||
|
6 years ago
|
return DVL_VK_LEFT;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_RIGHT:
|
||
|
6 years ago
|
return DVL_VK_RIGHT;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_UP:
|
||
|
6 years ago
|
return DVL_VK_UP;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_DOWN:
|
||
|
6 years ago
|
return DVL_VK_DOWN;
|
||
|
|
default:
|
||
|
|
return 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
bool HandleStartAndSelect(const ControllerButtonEvent &ctrl_event, GameAction *action)
|
||
|
6 years ago
|
{
|
||
|
6 years ago
|
const bool in_game_menu = InGameMenu();
|
||
|
|
|
||
|
5 years ago
|
const bool start_is_down = IsControllerButtonPressed(ControllerButton_BUTTON_START);
|
||
|
|
const bool select_is_down = IsControllerButtonPressed(ControllerButton_BUTTON_BACK);
|
||
|
|
start_modifier_active = !in_game_menu && start_is_down;
|
||
|
|
select_modifier_active = !in_game_menu && select_is_down && !start_modifier_active;
|
||
|
|
|
||
|
|
// Tracks whether we've received both START and SELECT down events.
|
||
|
|
//
|
||
|
|
// Using `IsControllerButtonPressed()` for this would be incorrect.
|
||
|
|
// If both buttons are pressed simultaneously, SDL sends 2 events for which both buttons are in the pressed state.
|
||
|
|
// This allows us to avoid triggering START+SELECT action twice in this case.
|
||
|
|
static bool start_down_received = false;
|
||
|
|
static bool select_down_received = false;
|
||
|
|
switch (ctrl_event.button) {
|
||
|
|
case ControllerButton_BUTTON_BACK:
|
||
|
|
select_down_received = !ctrl_event.up;
|
||
|
|
break;
|
||
|
|
case ControllerButton_BUTTON_START:
|
||
|
|
start_down_received = !ctrl_event.up;
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
return false;
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
5 years ago
|
if (start_down_received && select_down_received) {
|
||
|
|
*action = GameActionSendKey { DVL_VK_ESCAPE, ctrl_event.up };
|
||
|
6 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
if (in_game_menu && (start_is_down || select_is_down) && !ctrl_event.up) {
|
||
|
|
// If both are down, do nothing because `both_received` will trigger soon.
|
||
|
5 years ago
|
if (start_is_down && select_is_down)
|
||
|
|
return true;
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_ESCAPE, ctrl_event.up };
|
||
|
6 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
5 years ago
|
bool GetGameAction(const SDL_Event &event, ControllerButtonEvent ctrl_event, GameAction *action)
|
||
|
5 years ago
|
{
|
||
|
|
const bool in_game_menu = InGameMenu();
|
||
|
|
|
||
|
|
if (HandleStartAndSelect(ctrl_event, action))
|
||
|
|
return true;
|
||
|
|
|
||
|
5 years ago
|
// Stick clicks simulate the mouse both in menus and in-game.
|
||
|
|
switch (ctrl_event.button) {
|
||
|
|
case ControllerButton_BUTTON_LEFTSTICK:
|
||
|
|
if (select_modifier_active) {
|
||
|
|
if (!IsAutomapActive())
|
||
|
|
*action = GameActionSendMouseClick { GameActionSendMouseClick::LEFT, ctrl_event.up };
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
case ControllerButton_BUTTON_RIGHTSTICK:
|
||
|
|
if (!IsAutomapActive()) {
|
||
|
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
|
*action = GameActionSendMouseClick { GameActionSendMouseClick::RIGHT, ctrl_event.up };
|
||
|
|
else
|
||
|
|
*action = GameActionSendMouseClick { GameActionSendMouseClick::LEFT, ctrl_event.up };
|
||
|
|
}
|
||
|
|
return true;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
if (!in_game_menu) {
|
||
|
6 years ago
|
switch (ctrl_event.button) {
|
||
|
6 years ago
|
case ControllerButton_BUTTON_LEFTSHOULDER:
|
||
|
5 years ago
|
if ((select_modifier_active && !sgOptions.Controller.bSwapShoulderButtonMode) || (sgOptions.Controller.bSwapShoulderButtonMode && !select_modifier_active)) {
|
||
|
6 years ago
|
if (!IsAutomapActive())
|
||
|
5 years ago
|
*action = GameActionSendMouseClick { GameActionSendMouseClick::LEFT, ctrl_event.up };
|
||
|
6 years ago
|
return true;
|
||
|
|
}
|
||
|
|
break;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_RIGHTSHOULDER:
|
||
|
5 years ago
|
if ((select_modifier_active && !sgOptions.Controller.bSwapShoulderButtonMode) || (sgOptions.Controller.bSwapShoulderButtonMode && !select_modifier_active)) {
|
||
|
6 years ago
|
if (!IsAutomapActive())
|
||
|
5 years ago
|
*action = GameActionSendMouseClick { GameActionSendMouseClick::RIGHT, ctrl_event.up };
|
||
|
6 years ago
|
return true;
|
||
|
|
}
|
||
|
|
break;
|
||
|
6 years ago
|
case ControllerButton_AXIS_TRIGGERLEFT: // ZL (aka L2)
|
||
|
6 years ago
|
if (!ctrl_event.up) {
|
||
|
5 years ago
|
if (select_modifier_active)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_QUEST_LOG);
|
||
|
6 years ago
|
else
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO);
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_AXIS_TRIGGERRIGHT: // ZR (aka R2)
|
||
|
6 years ago
|
if (!ctrl_event.up) {
|
||
|
5 years ago
|
if (select_modifier_active)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_SPELL_BOOK);
|
||
|
6 years ago
|
else
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY);
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
return true;
|
||
|
5 years ago
|
case ControllerButton_IGNORE:
|
||
|
6 years ago
|
case ControllerButton_BUTTON_START:
|
||
|
5 years ago
|
case ControllerButton_BUTTON_BACK:
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
break;
|
||
|
6 years ago
|
default:
|
||
|
|
break;
|
||
|
6 years ago
|
}
|
||
|
5 years ago
|
if (sgOptions.Controller.bDpadHotkeys) {
|
||
|
5 years ago
|
switch (ctrl_event.button) {
|
||
|
|
case ControllerButton_BUTTON_DPAD_UP:
|
||
|
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F6, ctrl_event.up };
|
||
|
5 years ago
|
else
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_ESCAPE, ctrl_event.up };
|
||
|
5 years ago
|
return true;
|
||
|
|
case ControllerButton_BUTTON_DPAD_RIGHT:
|
||
|
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F8, ctrl_event.up };
|
||
|
|
else if (!ctrl_event.up)
|
||
|
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY);
|
||
|
5 years ago
|
return true;
|
||
|
|
case ControllerButton_BUTTON_DPAD_DOWN:
|
||
|
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F7, ctrl_event.up };
|
||
|
5 years ago
|
else
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_TAB, ctrl_event.up };
|
||
|
5 years ago
|
return true;
|
||
|
|
case ControllerButton_BUTTON_DPAD_LEFT:
|
||
|
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F5, ctrl_event.up };
|
||
|
|
else if (!ctrl_event.up)
|
||
|
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO);
|
||
|
5 years ago
|
return true;
|
||
|
5 years ago
|
default:
|
||
|
|
break;
|
||
|
5 years ago
|
}
|
||
|
5 years ago
|
}
|
||
|
5 years ago
|
if (start_modifier_active) {
|
||
|
6 years ago
|
switch (ctrl_event.button) {
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_UP:
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_ESCAPE, ctrl_event.up };
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_RIGHT:
|
||
|
6 years ago
|
if (!ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_DOWN:
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_TAB, ctrl_event.up };
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_LEFT:
|
||
|
6 years ago
|
if (!ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_Y: // Top button
|
||
|
5 years ago
|
#ifdef __3DS__
|
||
|
|
if (!ctrl_event.up) {
|
||
|
|
zoomflag = !zoomflag;
|
||
|
|
CalcViewportGeometry();
|
||
|
|
}
|
||
|
|
#else
|
||
|
5 years ago
|
// Not mapped. Reserved for future use.
|
||
|
5 years ago
|
#endif
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_B: // Right button
|
||
|
6 years ago
|
// Not mapped. TODO: map to attack in place.
|
||
|
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_A: // Bottom button
|
||
|
6 years ago
|
if (!ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_SPELL_BOOK);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_X: // Left button
|
||
|
6 years ago
|
if (!ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_QUEST_LOG);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_LEFTSHOULDER:
|
||
|
6 years ago
|
if (!ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_RIGHTSHOULDER:
|
||
|
6 years ago
|
if (!ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
default:
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
6 years ago
|
|
||
|
|
// Bottom button: Closes menus or opens quick spell book if nothing is open.
|
||
|
6 years ago
|
if (ctrl_event.button == ControllerButton_BUTTON_A) { // Bottom button
|
||
|
5 years ago
|
if (ctrl_event.up)
|
||
|
|
return true;
|
||
|
6 years ago
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
6 years ago
|
*action = GameActionSendKey { DVL_VK_F7, ctrl_event.up };
|
||
|
|
else if (invflag)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_INVENTORY);
|
||
|
6 years ago
|
else if (sbookflag)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_SPELL_BOOK);
|
||
|
6 years ago
|
else if (questlog)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_QUEST_LOG);
|
||
|
6 years ago
|
else if (chrflag)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_CHARACTER_INFO);
|
||
|
6 years ago
|
else
|
||
|
6 years ago
|
*action = GameAction(GameActionType_TOGGLE_QUICK_SPELL_MENU);
|
||
|
6 years ago
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
if (!questlog && !sbookflag) {
|
||
|
|
switch (ctrl_event.button) {
|
||
|
6 years ago
|
case ControllerButton_IGNORE:
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_B: // Right button
|
||
|
6 years ago
|
if (!ctrl_event.up) {
|
||
|
6 years ago
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F8, ctrl_event.up };
|
||
|
6 years ago
|
else
|
||
|
6 years ago
|
*action = GameAction(GameActionType_PRIMARY_ACTION);
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_Y: // Top button
|
||
|
6 years ago
|
if (!ctrl_event.up) {
|
||
|
6 years ago
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F6, ctrl_event.up };
|
||
|
6 years ago
|
else
|
||
|
6 years ago
|
*action = GameAction(GameActionType_SECONDARY_ACTION);
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_X: // Left button
|
||
|
6 years ago
|
if (!ctrl_event.up) {
|
||
|
6 years ago
|
if (IsControllerButtonPressed(ControllerButton_BUTTON_BACK))
|
||
|
5 years ago
|
*action = GameActionSendKey { DVL_VK_F5, ctrl_event.up };
|
||
|
6 years ago
|
else
|
||
|
6 years ago
|
*action = GameAction(GameActionType_CAST_SPELL);
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_LEFTSHOULDER:
|
||
|
6 years ago
|
if (!stextflag && !ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_USE_HEALTH_POTION);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_RIGHTSHOULDER:
|
||
|
6 years ago
|
if (!stextflag && !ctrl_event.up)
|
||
|
6 years ago
|
*action = GameAction(GameActionType_USE_MANA_POTION);
|
||
|
6 years ago
|
return true;
|
||
|
6 years ago
|
case ControllerButton_BUTTON_DPAD_UP:
|
||
|
|
case ControllerButton_BUTTON_DPAD_DOWN:
|
||
|
|
case ControllerButton_BUTTON_DPAD_LEFT:
|
||
|
|
case ControllerButton_BUTTON_DPAD_RIGHT:
|
||
|
6 years ago
|
// The rest of D-Pad actions are handled in charMovement() on every game_logic() call.
|
||
|
6 years ago
|
return true;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
|
}
|
||
|
6 years ago
|
}
|
||
|
6 years ago
|
|
||
|
5 years ago
|
if (ctrl_event.button == ControllerButton_BUTTON_BACK) {
|
||
|
|
return true; // Ignore mod button
|
||
|
|
}
|
||
|
6 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
// DPad navigation is handled separately for these.
|
||
|
5 years ago
|
if (gmenu_is_active() || questlog || stextflag != STORE_NONE) {
|
||
|
5 years ago
|
switch (ctrl_event.button) {
|
||
|
5 years ago
|
case ControllerButton_BUTTON_DPAD_UP:
|
||
|
|
case ControllerButton_BUTTON_DPAD_DOWN:
|
||
|
|
case ControllerButton_BUTTON_DPAD_LEFT:
|
||
|
|
case ControllerButton_BUTTON_DPAD_RIGHT:
|
||
|
|
return true;
|
||
|
|
default:
|
||
|
|
break;
|
||
|
5 years ago
|
}
|
||
|
|
}
|
||
|
|
|
||
|
6 years ago
|
// By default, map to a keyboard key.
|
||
|
6 years ago
|
if (ctrl_event.button != ControllerButton_NONE) {
|
||
|
5 years ago
|
*action = GameActionSendKey { translate_controller_button_to_key(ctrl_event.button),
|
||
|
6 years ago
|
ctrl_event.up };
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
#ifndef USE_SDL1
|
||
|
5 years ago
|
// Ignore unhandled joystick events where a GameController is open for this joystick.
|
||
|
|
// This is because SDL sends both game controller and joystick events in this case.
|
||
|
|
const Joystick *const joystick = Joystick::Get(event);
|
||
|
|
if (joystick != NULL && GameController::Get(joystick->instance_id()) != NULL) {
|
||
|
6 years ago
|
return true;
|
||
|
|
}
|
||
|
6 years ago
|
if (event.type == SDL_CONTROLLERAXISMOTION) {
|
||
|
|
return true; // Ignore releasing the trigger buttons
|
||
|
|
}
|
||
|
6 years ago
|
#endif
|
||
|
|
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
5 years ago
|
AxisDirection GetMoveDirection()
|
||
|
6 years ago
|
{
|
||
|
5 years ago
|
return GetLeftStickOrDpadDirection(/*allow_dpad=*/!sgOptions.Controller.bDpadHotkeys);
|
||
|
6 years ago
|
}
|
||
|
|
|
||
|
5 years ago
|
} // namespace devilution
|