Add a minimum time before "Close All" button is accepted on a menu

This is to avoid insta-close on some controllers when the same button is mapped to open menu and close
This commit is contained in:
Steve Streeting 2022-02-03 15:38:52 +00:00
parent fc958d2ca0
commit 2d50eadfdf
2 changed files with 19 additions and 4 deletions

View File

@ -6,7 +6,6 @@
#include "StevesUI/MenuBase.h"
#include "Kismet/GameplayStatics.h"
void UMenuStack::NativeConstruct()
{
Super::NativeConstruct();
@ -156,10 +155,17 @@ bool UMenuStack::HandleKeyDownEvent(const FKeyEvent& InKeyEvent)
else if (InstantCloseKeys.Contains(Key))
{
// Shortcut to exit all menus
// Make sure we're open long enough
FDateTime CurrTime = FDateTime::Now();
FTimespan Diff = CurrTime - TimeFirstOpen;
if (Diff.GetTicks() > (int64)(MinTimeOpen * ETimespan::TicksPerSecond))
{
CloseAll(true);
return true;
}
}
return false;
}
@ -236,7 +242,8 @@ void UMenuStack::PopMenuIfTop(UMenuBase* UiMenuBase, bool bWasCancel)
void UMenuStack::FirstMenuOpened()
{
// Nothing to do now but keep for future use
// Don't use world time (even real time) since map can change while open
TimeFirstOpen = FDateTime::Now();
}
void UMenuStack::RemoveFromParent()

View File

@ -52,6 +52,8 @@ protected:
UFUNCTION()
void InputModeChanged(int PlayerIndex, EInputMode NewMode);
FDateTime TimeFirstOpen;
public:
/// Input keys which go back a level in the menu stack (default Esc and B gamepad button)
/// Clear this list if you don't want this behaviour
@ -98,6 +100,10 @@ public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Behavior")
EGamePauseChange GamePauseSettingOnClose = EGamePauseChange::DoNotChange;
/// Minimum amount of time a menu should be open before responding to instant close key (prevent fast close because of leaked input)
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Behaviour")
float MinTimeOpen = 0.5f;
/// Push a new menu level by class. This will instantiate the new menu, display it, and inform the previous menu that it's
/// been superceded. Use the returned instance if you want to cache it
UFUNCTION(BlueprintCallable)
@ -131,4 +137,6 @@ public:
UMenuStack();
};