Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Virtual Gamepad] Add button for standing still to attack #3281

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 44 additions & 9 deletions Source/controls/plrctrls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "controls/controller.h"
#include "controls/controller_motion.h"
#include "controls/game_controls.h"
#include "controls/touch/gamepad.h"
#include "cursor.h"
#include "doom.h"
#include "engine/point.hpp"
Expand Down Expand Up @@ -53,6 +54,13 @@ namespace {
int Slot = SLOTXY_INV_FIRST;
int PreviousInventoryColumn = -1;

const Direction FaceDir[3][3] = {
// NONE UP DOWN
{ Direction::South, Direction::North, Direction::South }, // NONE
{ Direction::West, Direction::NorthWest, Direction::SouthWest }, // LEFT
{ Direction::East, Direction::NorthEast, Direction::SouthEast }, // RIGHT
};

/**
* Number of angles to turn to face the coordinate
* @param destination Tile coordinates
Expand Down Expand Up @@ -441,13 +449,42 @@ void Interact()
{
if (leveltype == DTYPE_TOWN && pcursmonst != -1) {
NetSendCmdLocParam1(true, CMD_TALKXY, Towners[pcursmonst].position, pcursmonst);
} else if (pcursmonst != -1) {
return;
}

bool stand = false;
#ifdef VIRTUAL_GAMEPAD
stand = VirtualGamepadState.standButton.isHeld;
#endif

if (leveltype != DTYPE_TOWN && stand) {
auto &myPlayer = Players[MyPlayerId];
Direction pdir = myPlayer._pdir;
AxisDirection moveDir = GetMoveDirection();
bool motion = moveDir.x != AxisDirectionX_NONE || moveDir.y != AxisDirectionY_NONE;
if (motion) {
pdir = FaceDir[static_cast<std::size_t>(moveDir.x)][static_cast<std::size_t>(moveDir.y)];
}

Point position = myPlayer.position.tile + pdir;
if (pcursmonst != -1 && !motion) {
position = Monsters[pcursmonst].position.tile;
}

NetSendCmdLoc(MyPlayerId, true, Players[MyPlayerId].UsesRangedWeapon() ? CMD_RATTACKXY : CMD_SATTACKXY, position);
return;
}

if (pcursmonst != -1) {
if (!Players[MyPlayerId].UsesRangedWeapon() || CanTalkToMonst(Monsters[pcursmonst])) {
NetSendCmdParam1(true, CMD_ATTACKID, pcursmonst);
} else {
NetSendCmdParam1(true, CMD_RATTACKID, pcursmonst);
}
} else if (leveltype != DTYPE_TOWN && pcursplr != -1 && !gbFriendlyMode) {
return;
}

if (leveltype != DTYPE_TOWN && pcursplr != -1 && !gbFriendlyMode) {
NetSendCmdParam1(true, Players[MyPlayerId].UsesRangedWeapon() ? CMD_RATTACKPID : CMD_ATTACKPID, pcursplr);
}
}
Expand Down Expand Up @@ -1044,13 +1081,6 @@ void SpellBookMove(AxisDirection dir)
}
}

const Direction FaceDir[3][3] = {
// NONE UP DOWN
{ Direction::South, Direction::North, Direction::South }, // NONE
{ Direction::West, Direction::NorthWest, Direction::SouthWest }, // LEFT
{ Direction::East, Direction::NorthEast, Direction::SouthEast }, // RIGHT
};

/**
* @brief check if stepping in direction (dir) from position is blocked.
*
Expand Down Expand Up @@ -1093,6 +1123,11 @@ void WalkInDir(int playerId, AxisDirection dir)
if (!player.IsWalking() && player.CanChangeAction())
player._pdir = pdir;

#ifdef VIRTUAL_GAMEPAD
if (VirtualGamepadState.standButton.isHeld)
return;
#endif

if (PosOkPlayer(player, delta) && IsPathBlocked(player.position.future, pdir))
return; // Don't start backtrack around obstacles

Expand Down
20 changes: 16 additions & 4 deletions Source/controls/touch/event_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ bool VirtualGamepadEventHandler::Handle(const SDL_Event &event)
if (directionPadEventHandler.Handle(event))
return true;

if (standButtonEventHandler.Handle(event))
return true;

if (primaryActionButtonEventHandler.Handle(event))
return true;

Expand Down Expand Up @@ -259,7 +262,11 @@ bool VirtualButtonEventHandler::HandleFingerDown(const SDL_TouchFingerEvent &eve
if (!virtualButton->Contains(touchCoordinates))
return false;

virtualButton->isHeld = true;
if (toggles)
virtualButton->isHeld = !virtualButton->isHeld;
else
virtualButton->isHeld = true;

virtualButton->didStateChange = true;
activeFinger = event.fingerId;
isActive = true;
Expand All @@ -271,10 +278,12 @@ bool VirtualButtonEventHandler::HandleFingerUp(const SDL_TouchFingerEvent &event
if (!isActive || event.fingerId != activeFinger)
return false;

if (virtualButton->isHeld)
virtualButton->didStateChange = true;
if (!toggles) {
if (virtualButton->isHeld)
virtualButton->didStateChange = true;
virtualButton->isHeld = false;
}

virtualButton->isHeld = false;
isActive = false;
return true;
}
Expand All @@ -284,6 +293,9 @@ bool VirtualButtonEventHandler::HandleFingerMotion(const SDL_TouchFingerEvent &e
if (!isActive || event.fingerId != activeFinger)
return false;

if (toggles)
return true;

float x = event.x;
float y = event.y;
Point touchCoordinates = ScaleToScreenCoordinates(x, y);
Expand Down
6 changes: 5 additions & 1 deletion Source/controls/touch/event_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ class VirtualDirectionPadEventHandler {

class VirtualButtonEventHandler {
public:
VirtualButtonEventHandler(VirtualButton *virtualButton)
VirtualButtonEventHandler(VirtualButton *virtualButton, bool toggles = false)
: virtualButton(virtualButton)
, activeFinger(0)
, isActive(false)
, toggles(toggles)
{
}

Expand All @@ -44,6 +45,7 @@ class VirtualButtonEventHandler {
VirtualButton *virtualButton;
SDL_FingerID activeFinger;
bool isActive;
bool toggles;

bool HandleFingerDown(const SDL_TouchFingerEvent &event);
bool HandleFingerUp(const SDL_TouchFingerEvent &event);
Expand All @@ -58,6 +60,7 @@ class VirtualGamepadEventHandler {
, inventoryMenuButtonEventHandler(&virtualGamepad->menuPanel.inventoryButton)
, mapMenuButtonEventHandler(&virtualGamepad->menuPanel.mapButton)
, directionPadEventHandler(&virtualGamepad->directionPad)
, standButtonEventHandler(&virtualGamepad->standButton, true)
, primaryActionButtonEventHandler(&virtualGamepad->primaryActionButton)
, secondaryActionButtonEventHandler(&virtualGamepad->secondaryActionButton)
, spellActionButtonEventHandler(&virtualGamepad->spellActionButton)
Expand All @@ -76,6 +79,7 @@ class VirtualGamepadEventHandler {
VirtualButtonEventHandler mapMenuButtonEventHandler;

VirtualDirectionPadEventHandler directionPadEventHandler;
VirtualButtonEventHandler standButtonEventHandler;

VirtualButtonEventHandler primaryActionButtonEventHandler;
VirtualButtonEventHandler secondaryActionButtonEventHandler;
Expand Down
16 changes: 12 additions & 4 deletions Source/controls/touch/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,18 @@ void InitializeVirtualGamepad()
menuPanelArea.size.height = 162 * menuButtonWidth / menuPanelButtonSize.width;

VirtualDirectionPad &directionPad = VirtualGamepadState.directionPad;
directionPad.area.position.x = inputMargin + directionPadSize / 2;
directionPad.area.position.y = gnScreenHeight - inputMargin - directionPadSize / 2;
directionPad.area.radius = directionPadSize / 2;
directionPad.position = directionPad.area.position;
Circle &directionPadArea = directionPad.area;
directionPadArea.position.x = inputMargin + directionPadSize / 2;
directionPadArea.position.y = gnScreenHeight - inputMargin - directionPadSize / 2;
directionPadArea.radius = directionPadSize / 2;
directionPad.position = directionPadArea.position;

int standButtonDiagonalOffset = directionPadArea.radius + padButtonSpacing / 2 + padButtonSize / 2;
int standButtonOffset = round(standButtonDiagonalOffset / std::sqrt(2));
Circle &standButtonArea = VirtualGamepadState.standButton.area;
standButtonArea.position.x = directionPadArea.position.x - standButtonOffset;
standButtonArea.position.y = directionPadArea.position.y + standButtonOffset;
standButtonArea.radius = padButtonSize / 2;

Circle &primaryActionButtonArea = VirtualGamepadState.primaryActionButton.area;
primaryActionButtonArea.position.x = padButtonRight;
Expand Down
1 change: 1 addition & 0 deletions Source/controls/touch/gamepad.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct VirtualMenuPanel {
struct VirtualGamepad {
VirtualMenuPanel menuPanel;
VirtualDirectionPad directionPad;
VirtualPadButton standButton;

VirtualPadButton primaryActionButton;
VirtualPadButton secondaryActionButton;
Expand Down
6 changes: 6 additions & 0 deletions Source/controls/touch/renderers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ void VirtualGamepadRenderer::Render(RenderFunction renderFunction)
healthButtonRenderer.RenderPotion(renderFunction, potionArt);
manaButtonRenderer.RenderPotion(renderFunction, potionArt);

standButtonRenderer.Render(renderFunction, buttonArt);
directionPadRenderer.Render(renderFunction);
menuPanelRenderer.Render(renderFunction);
}
Expand Down Expand Up @@ -367,6 +368,11 @@ std::optional<VirtualGamepadPotionType> PotionButtonRenderer::GetPotionType()
return std::nullopt;
}

VirtualGamepadButtonType StandButtonRenderer::GetButtonType()
{
return GetStandButtonType(virtualPadButton->isHeld);
}

VirtualGamepadButtonType PrimaryActionButtonRenderer::GetButtonType()
{
// NEED: Confirm surface
Expand Down
13 changes: 13 additions & 0 deletions Source/controls/touch/renderers.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,17 @@ class VirtualPadButtonRenderer {
virtual VirtualGamepadButtonType GetButtonType() = 0;
};

class StandButtonRenderer : public VirtualPadButtonRenderer {
public:
StandButtonRenderer(VirtualPadButton *standButton)
: VirtualPadButtonRenderer(standButton)
{
}

private:
VirtualGamepadButtonType GetButtonType();
};

class PrimaryActionButtonRenderer : public VirtualPadButtonRenderer {
public:
PrimaryActionButtonRenderer(VirtualPadButton *primaryActionButton)
Expand Down Expand Up @@ -171,6 +182,7 @@ class VirtualGamepadRenderer {
VirtualGamepadRenderer(VirtualGamepad *virtualGamepad)
: menuPanelRenderer(&virtualGamepad->menuPanel)
, directionPadRenderer(&virtualGamepad->directionPad)
, standButtonRenderer(&virtualGamepad->standButton)
, primaryActionButtonRenderer(&virtualGamepad->primaryActionButton)
, secondaryActionButtonRenderer(&virtualGamepad->secondaryActionButton)
, spellActionButtonRenderer(&virtualGamepad->spellActionButton)
Expand All @@ -187,6 +199,7 @@ class VirtualGamepadRenderer {
private:
VirtualMenuPanelRenderer menuPanelRenderer;
VirtualDirectionPadRenderer directionPadRenderer;
StandButtonRenderer standButtonRenderer;

PrimaryActionButtonRenderer primaryActionButtonRenderer;
SecondaryActionButtonRenderer secondaryActionButtonRenderer;
Expand Down