Make MenuStack able to restore input modes, game pause state and mouse pointer on exit

This commit is contained in:
Steve Streeting 2020-11-18 15:47:50 +00:00
parent 9e13cc8d53
commit f75706abc4
3 changed files with 48 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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")
};

View File

@ -27,6 +27,9 @@ class STEVESUEHELPERS_API UMenuStack : public UFocusableInputInterceptorUserWidg
protected:
EInputMode LastInputMode;
EInputModeChange PreviousInputMode;
EMousePointerVisibilityChange PreviousMouseVisibility;
EGamePauseChange PreviousPauseState;
TArray<UMenuBase*> 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;