Skip to content

Commit

Permalink
Trigger deactivate event for virtual gamepad
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenCWills committed Feb 5, 2025
1 parent a3eea0f commit 29b2d17
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
48 changes: 42 additions & 6 deletions Source/controls/touch/event_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ namespace devilution {

namespace {

#if SDL_VERSION_ATLEAST(2, 0, 0)
using SdlEventType = uint16_t;
#else
using SdlEventType = uint8_t;
#endif

VirtualGamepadEventHandler Handler(&VirtualGamepadState);

Point ScaleToScreenCoordinates(float x, float y)
Expand Down Expand Up @@ -130,6 +136,17 @@ void HandleStashPanelInteraction(const SDL_Event &event)
}
}

SdlEventType GetDeactivateEventType()
{
static SdlEventType customEventType = SDL_RegisterEvents(1);
return customEventType;
}

bool IsDeactivateEvent(const SDL_Event &event)
{
return event.type == GetDeactivateEventType();
}

} // namespace

void HandleTouchEvent(const SDL_Event &event)
Expand Down Expand Up @@ -161,14 +178,23 @@ void HandleTouchEvent(const SDL_Event &event)
HandleStashPanelInteraction(event);
}

void DeactivateTouchEventHandlers()
{
SDL_Event event;
event.type = GetDeactivateEventType();
HandleTouchEvent(event);
}

bool VirtualGamepadEventHandler::Handle(const SDL_Event &event)
{
if (!VirtualGamepadState.isActive || !IsAnyOf(event.type, SDL_FINGERDOWN, SDL_FINGERUP, SDL_FINGERMOTION)) {
VirtualGamepadState.primaryActionButton.didStateChange = false;
VirtualGamepadState.secondaryActionButton.didStateChange = false;
VirtualGamepadState.spellActionButton.didStateChange = false;
VirtualGamepadState.cancelButton.didStateChange = false;
return false;
if (!IsDeactivateEvent(event)) {
if (!VirtualGamepadState.isActive || !IsAnyOf(event.type, SDL_FINGERDOWN, SDL_FINGERUP, SDL_FINGERMOTION)) {
VirtualGamepadState.primaryActionButton.didStateChange = false;
VirtualGamepadState.secondaryActionButton.didStateChange = false;
VirtualGamepadState.spellActionButton.didStateChange = false;
VirtualGamepadState.cancelButton.didStateChange = false;
return false;
}
}

if (charMenuButtonEventHandler.Handle(event))
Expand Down Expand Up @@ -212,6 +238,11 @@ bool VirtualGamepadEventHandler::Handle(const SDL_Event &event)

bool VirtualDirectionPadEventHandler::Handle(const SDL_Event &event)
{
if (IsDeactivateEvent(event)) {
isActive = false;
return false;
}

switch (event.type) {
case SDL_FINGERDOWN:
return HandleFingerDown(event.tfinger);
Expand Down Expand Up @@ -271,6 +302,11 @@ bool VirtualDirectionPadEventHandler::HandleFingerMotion(const SDL_TouchFingerEv

bool VirtualButtonEventHandler::Handle(const SDL_Event &event)
{
if (IsDeactivateEvent(event)) {
isActive = false;
return false;
}

if (!virtualButton->isUsable()) {
virtualButton->didStateChange = virtualButton->isHeld;
virtualButton->isHeld = false;
Expand Down
1 change: 1 addition & 0 deletions Source/controls/touch/event_handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ class VirtualGamepadEventHandler {
};

void HandleTouchEvent(const SDL_Event &event);
void DeactivateTouchEventHandlers();

} // namespace devilution
2 changes: 2 additions & 0 deletions Source/controls/touch/gamepad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <SDL.h>

#include "control.h"
#include "controls/touch/event_handlers.h"
#include "controls/touch/gamepad.h"
#include "quests.h"
#include "utils/display.h"
Expand Down Expand Up @@ -188,6 +189,7 @@ void ActivateVirtualGamepad()
void DeactivateVirtualGamepad()
{
VirtualGamepadState.Deactivate();
DeactivateTouchEventHandlers();
}

void VirtualGamepad::Deactivate()
Expand Down

0 comments on commit 29b2d17

Please sign in to comment.