From f75706abc49ba887b2ee5c9bc4c539928c54b901 Mon Sep 17 00:00:00 2001 From: Steve Streeting Date: Wed, 18 Nov 2020 15:47:50 +0000 Subject: [PATCH] Make MenuStack able to restore input modes, game pause state and mouse pointer on exit --- .../Private/StevesUI/MenuStack.cpp | 41 +++++++++++++++++++ .../Public/StevesHelperCommon.h | 3 ++ .../Public/StevesUI/MenuStack.h | 4 ++ 3 files changed, 48 insertions(+) diff --git a/Source/StevesUEHelpers/Private/StevesUI/MenuStack.cpp b/Source/StevesUEHelpers/Private/StevesUI/MenuStack.cpp index adbc45e..b236fc2 100644 --- a/Source/StevesUEHelpers/Private/StevesUI/MenuStack.cpp +++ b/Source/StevesUEHelpers/Private/StevesUI/MenuStack.cpp @@ -20,6 +20,8 @@ void UMenuStack::NativeConstruct() LastInputMode = GS->GetLastInputModeUsed(); } + SavePreviousInputMousePauseState(); + ApplyInputModeChange(InputModeSettingOnOpen); ApplyMousePointerVisibility(MousePointerVisibilityOnOpen); ApplyGamePauseChange(GamePauseSettingOnOpen); @@ -35,6 +37,36 @@ void UMenuStack::NativeDestruct() } + +void UMenuStack::SavePreviousInputMousePauseState() +{ + auto PC = GetOwningPlayer(); + UGameViewportClient* GVC = GetWorld()->GetGameViewport(); + + if (GVC->IgnoreInput()) + { + PreviousInputMode = EInputModeChange::UIOnly; + } + else + { + const auto CaptureMode = GVC->CaptureMouseOnClick(); + // Game-only mode captures permanently, that seems to be the best way to detect + if (CaptureMode == EMouseCaptureMode::CapturePermanently || + CaptureMode == EMouseCaptureMode::CapturePermanently_IncludingInitialMouseDown) + { + PreviousInputMode = EInputModeChange::GameOnly; + } + else + { + PreviousInputMode = EInputModeChange::GameAndUI; + } + } + PreviousMouseVisibility = PC->bShowMouseCursor ? EMousePointerVisibilityChange::Visible : EMousePointerVisibilityChange::Hidden; + PreviousPauseState = UGameplayStatics::IsGamePaused(GetWorld()) ? EGamePauseChange::Paused : EGamePauseChange::Unpaused; + + +} + void UMenuStack::ApplyInputModeChange(EInputModeChange Change) const { auto PC = GetOwningPlayer(); @@ -51,6 +83,9 @@ void UMenuStack::ApplyInputModeChange(EInputModeChange Change) const case EInputModeChange::GameOnly: PC->SetInputMode(FInputModeGameOnly()); break; + case EInputModeChange::Restore: + ApplyInputModeChange(PreviousInputMode); + break; } } @@ -67,6 +102,9 @@ void UMenuStack::ApplyMousePointerVisibility(EMousePointerVisibilityChange Chang case EMousePointerVisibilityChange::Hidden: PC->bShowMouseCursor = false; break; + case EMousePointerVisibilityChange::Restore: + ApplyMousePointerVisibility(PreviousMouseVisibility); + break; } } @@ -82,6 +120,9 @@ void UMenuStack::ApplyGamePauseChange(EGamePauseChange Change) const case EGamePauseChange::Unpaused: UGameplayStatics::SetGamePaused(GetWorld(), false); break; + case EGamePauseChange::Restore: + ApplyGamePauseChange(PreviousPauseState); + break; } } diff --git a/Source/StevesUEHelpers/Public/StevesHelperCommon.h b/Source/StevesUEHelpers/Public/StevesHelperCommon.h index d3df577..bae210e 100644 --- a/Source/StevesUEHelpers/Public/StevesHelperCommon.h +++ b/Source/StevesUEHelpers/Public/StevesHelperCommon.h @@ -14,6 +14,7 @@ UENUM(BlueprintType) enum class EInputModeChange : uint8 { DoNotChange UMETA(DisplayName="No Change"), + Restore UMETA(DisplayName="Restore To Previous"), UIOnly UMETA(DisplayName="UI Only"), GameAndUI UMETA(DisplayName="Game And UI"), GameOnly UMETA(DisplayName="Game Only") @@ -23,6 +24,7 @@ UENUM(BlueprintType) enum class EMousePointerVisibilityChange : uint8 { DoNotChange UMETA(DisplayName="No Change"), + Restore UMETA(DisplayName="Restore To Previous"), Visible UMETA(DisplayName="Pointer Visible"), Hidden UMETA(DisplayName="Pointer Hidden") }; @@ -31,6 +33,7 @@ UENUM(BlueprintType) enum class EGamePauseChange : uint8 { DoNotChange UMETA(DisplayName="No Change"), + Restore UMETA(DisplayName="Restore To Previous"), Paused UMETA(DisplayName="Pause Game"), Unpaused UMETA(DisplayName="Unpause Game") }; diff --git a/Source/StevesUEHelpers/Public/StevesUI/MenuStack.h b/Source/StevesUEHelpers/Public/StevesUI/MenuStack.h index 4c3accb..f1c826a 100644 --- a/Source/StevesUEHelpers/Public/StevesUI/MenuStack.h +++ b/Source/StevesUEHelpers/Public/StevesUI/MenuStack.h @@ -27,6 +27,9 @@ class STEVESUEHELPERS_API UMenuStack : public UFocusableInputInterceptorUserWidg protected: EInputMode LastInputMode; + EInputModeChange PreviousInputMode; + EMousePointerVisibilityChange PreviousMouseVisibility; + EGamePauseChange PreviousPauseState; TArray Menus; @@ -35,6 +38,7 @@ protected: virtual void NativeConstruct() override; virtual void NativeDestruct() override; + void SavePreviousInputMousePauseState(); virtual void ApplyInputModeChange(EInputModeChange Change) const; virtual void ApplyMousePointerVisibility(EMousePointerVisibilityChange Change) const; virtual void ApplyGamePauseChange(EGamePauseChange Change) const;