Add useful functions for detecting whether a menu is the top of stack

This commit is contained in:
Steve Streeting 2024-07-24 15:17:28 +01:00
parent 1e5fca94a2
commit b9ab8feb16
4 changed files with 60 additions and 1 deletions

View File

@ -27,6 +27,7 @@ void UMenuBase::AddedToStack(UMenuStack* Parent)
ParentStack = MakeWeakObjectPtr(Parent); ParentStack = MakeWeakObjectPtr(Parent);
Open(false); Open(false);
OnAddedToStack(Parent);
} }
@ -45,11 +46,22 @@ void UMenuBase::InputModeChanged(EInputMode OldMode, EInputMode NewMode)
} }
} }
bool UMenuBase::IsTopOfStack() const
{
if (ParentStack.IsValid())
{
return ParentStack->GetTopMenu() == this;
}
return true;
}
void UMenuBase::RemovedFromStack(UMenuStack* Parent) void UMenuBase::RemovedFromStack(UMenuStack* Parent)
{ {
// This works whether embedded or not // This works whether embedded or not
RemoveFromParent(); RemoveFromParent();
PreviousFocusWidget.Reset(); PreviousFocusWidget.Reset();
OnRemovedFromStack(Parent);
} }
void UMenuBase::SupercededInStack(UMenuBase* ByMenu) void UMenuBase::SupercededInStack(UMenuBase* ByMenu)
@ -66,12 +78,13 @@ void UMenuBase::SupercededInStack(UMenuBase* ByMenu)
if (bHideWhenSuperceded) if (bHideWhenSuperceded)
SetVisibility(ESlateVisibility::Collapsed); SetVisibility(ESlateVisibility::Collapsed);
} }
OnSupercededInStack(ByMenu);
} }
void UMenuBase::RegainedFocusInStack() void UMenuBase::RegainedFocusInStack()
{ {
Open(true); Open(true);
OnRegainedFocusInStack();
} }
void UMenuBase::EmbedInParent() void UMenuBase::EmbedInParent()
@ -156,3 +169,19 @@ bool UMenuBase::ValidateClose_Implementation(bool bWasCancel)
// Default always pass // Default always pass
return true; return true;
} }
void UMenuBase::OnSupercededInStack_Implementation(UMenuBase* ByMenu)
{
}
void UMenuBase::OnRegainedFocusInStack_Implementation()
{
}
void UMenuBase::OnAddedToStack_Implementation(UMenuStack* Parent)
{
}
void UMenuBase::OnRemovedFromStack_Implementation(UMenuStack* Parent)
{
}

View File

@ -269,6 +269,15 @@ void UMenuStack::RemoveFromParent()
} }
UMenuBase* UMenuStack::GetTopMenu() const
{
if (Menus.Num() > 0)
{
return Menus.Top();
}
return nullptr;
}
UMenuStack::UMenuStack() UMenuStack::UMenuStack()
{ {
// Default to enabling automatic focus for menus (can still be overridden in serialized properties) // Default to enabling automatic focus for menus (can still be overridden in serialized properties)

View File

@ -62,6 +62,19 @@ protected:
UFUNCTION(BlueprintNativeEvent) UFUNCTION(BlueprintNativeEvent)
bool ValidateClose(bool bWasCancel); bool ValidateClose(bool bWasCancel);
/// Called when this menu is superceded by another menu being pushed on to this stack
UFUNCTION(BlueprintNativeEvent)
void OnSupercededInStack(UMenuBase* ByMenu);
/// Called when this menu is superceded by another menu being pushed on to this stack
UFUNCTION(BlueprintNativeEvent)
void OnRegainedFocusInStack();
UFUNCTION(BlueprintNativeEvent)
void OnAddedToStack(UMenuStack* Parent);
UFUNCTION(BlueprintNativeEvent)
void OnRemovedFromStack(UMenuStack* Parent);
public: public:
/** /**
@ -96,4 +109,9 @@ public:
void SupercededInStack(UMenuBase* ByMenu); void SupercededInStack(UMenuBase* ByMenu);
void RegainedFocusInStack(); void RegainedFocusInStack();
void InputModeChanged(EInputMode OldMode, EInputMode NewMode); void InputModeChanged(EInputMode OldMode, EInputMode NewMode);
/// Return whether this menu is currently at the top of the menu stack
/// Note: if this menu is not owned by a stack, will always return true
UFUNCTION(BlueprintCallable)
bool IsTopOfStack() const;
}; };

View File

@ -139,6 +139,9 @@ public:
virtual void PopMenuIfTop(UMenuBase* UiMenuBase, bool bWasCancel); virtual void PopMenuIfTop(UMenuBase* UiMenuBase, bool bWasCancel);
virtual void RemoveFromParent() override; virtual void RemoveFromParent() override;
/// Return the menu which is currently top of the stack
UFUNCTION(BlueprintCallable)
UMenuBase* GetTopMenu() const;
UMenuStack(); UMenuStack();