From 29b2d17eae114f78d0db80a4e6ea918bbd7dd8fd Mon Sep 17 00:00:00 2001 From: staphen Date: Wed, 5 Feb 2025 00:26:00 -0500 Subject: [PATCH] Trigger deactivate event for virtual gamepad --- Source/controls/touch/event_handlers.cpp | 48 +++++++++++++++++++++--- Source/controls/touch/event_handlers.h | 1 + Source/controls/touch/gamepad.cpp | 2 + 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Source/controls/touch/event_handlers.cpp b/Source/controls/touch/event_handlers.cpp index 8cfbfdb3868..f3d47290a8c 100644 --- a/Source/controls/touch/event_handlers.cpp +++ b/Source/controls/touch/event_handlers.cpp @@ -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) @@ -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) @@ -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)) @@ -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); @@ -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; diff --git a/Source/controls/touch/event_handlers.h b/Source/controls/touch/event_handlers.h index afa0e3a25b4..5e9e8b0732d 100644 --- a/Source/controls/touch/event_handlers.h +++ b/Source/controls/touch/event_handlers.h @@ -89,5 +89,6 @@ class VirtualGamepadEventHandler { }; void HandleTouchEvent(const SDL_Event &event); +void DeactivateTouchEventHandlers(); } // namespace devilution diff --git a/Source/controls/touch/gamepad.cpp b/Source/controls/touch/gamepad.cpp index 733506bf0b2..9375eb7330a 100644 --- a/Source/controls/touch/gamepad.cpp +++ b/Source/controls/touch/gamepad.cpp @@ -3,6 +3,7 @@ #include #include "control.h" +#include "controls/touch/event_handlers.h" #include "controls/touch/gamepad.h" #include "quests.h" #include "utils/display.h" @@ -188,6 +189,7 @@ void ActivateVirtualGamepad() void DeactivateVirtualGamepad() { VirtualGamepadState.Deactivate(); + DeactivateTouchEventHandlers(); } void VirtualGamepad::Deactivate()