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

Trigger deactivate event for virtual gamepad #7711

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading