// Copyright Epic Games, Inc. All Rights Reserved. #include "ToolMenuContext.h" #include "ToolMenus.h" #include "IToolMenusModule.h" #include "Textures/SlateIcon.h" #include "Framework/Commands/UIAction.h" #include "Framework/MultiBox/MultiBox.h" #include "Framework/MultiBox/MultiBoxBuilder.h" #include "Internationalization/Internationalization.h" #include UE_INLINE_GENERATED_CPP_BY_NAME(ToolMenuContext) FToolMenuContext::FToolMenuContext(UObject* InContext) { if (InContext) { ContextObjects.Add(InContext); } } FToolMenuContext::FToolMenuContext(UObject* InContext, FContextObjectCleanup&& InCleanup) { if (InContext) { ContextObjects.Add(InContext); if (InCleanup) { ContextObjectCleanupFuncs.Add(InContext, MoveTemp(InCleanup)); } } } FToolMenuContext::FToolMenuContext(TSharedPtr InCommandList, TSharedPtr InExtender, UObject* InContext) { if (InContext) { ContextObjects.Add(InContext); } if (InExtender.IsValid()) { AddExtender(InExtender); } AppendCommandList(InCommandList); } UObject* FToolMenuContext::FindByClass(UClass* InClass) const { for (UObject* ContextObject : ContextObjects) { if (ContextObject && ContextObject->IsA(InClass)) { return ContextObject; } } return nullptr; } void FToolMenuContext::AppendCommandList(const TSharedRef& InCommandList) { const TSharedPtr List = InCommandList; AppendCommandList(List); } void FToolMenuContext::AppendCommandList(const TSharedPtr& InCommandList) { if (InCommandList.IsValid()) { CommandLists.Add(InCommandList); if (CommandLists.Num() == 1) { CommandList = InCommandList; } else if (CommandLists.Num() == 2) { CommandList = MakeShared(); CommandList->Append(CommandLists[0].ToSharedRef()); CommandList->Append(InCommandList.ToSharedRef()); } else { CommandList->Append(InCommandList.ToSharedRef()); } } } const FUIAction* FToolMenuContext::GetActionForCommand(TSharedPtr Command, TSharedPtr& OutCommandList) const { for (const TSharedPtr& CommandListIter : CommandLists) { if (CommandListIter.IsValid()) { if (const FUIAction* Result = CommandListIter->GetActionForCommand(Command)) { OutCommandList = CommandListIter; return Result; } } } return nullptr; } const FUIAction* FToolMenuContext::GetActionForCommand(TSharedPtr Command) const { for (const TSharedPtr& CommandListIter : CommandLists) { if (CommandListIter.IsValid()) { if (const FUIAction* Result = CommandListIter->GetActionForCommand(Command)) { return Result; } } } return nullptr; } void FToolMenuContext::AddExtender(const TSharedPtr& InExtender) { Extenders.AddUnique(InExtender); } TSharedPtr FToolMenuContext::GetAllExtenders() const { return FExtender::Combine(Extenders); } void FToolMenuContext::ResetExtenders() { Extenders.Reset(); } void FToolMenuContext::AppendObjects(const TArray& InObjects) { for (UObject* Object : InObjects) { AddObject(Object); } } void FToolMenuContext::AddObject(UObject* InObject) { ContextObjects.AddUnique(InObject); } void FToolMenuContext::AddObject(UObject* InObject, FContextObjectCleanup&& InCleanup) { ContextObjects.AddUnique(InObject); if (InCleanup) { ContextObjectCleanupFuncs.Add(InObject, MoveTemp(InCleanup)); } } void FToolMenuContext::AddCleanup(FContextCleanup&& InCleanup) { if (InCleanup) { ContextCleanupFuncs.Add(MoveTemp(InCleanup)); } } void FToolMenuContext::CleanupObjects() { for (const TTuple, FContextObjectCleanup>& CleanupPair : ContextObjectCleanupFuncs) { CleanupPair.Value(CleanupPair.Key); } for (const FContextCleanup& CleanupFunc : ContextCleanupFuncs) { CleanupFunc(); } } void FToolMenuContext::Empty() { ContextObjects.Empty(); ContextObjectCleanupFuncs.Empty(); ContextCleanupFuncs.Empty(); CommandLists.Empty(); CommandList.Reset(); Extenders.Empty(); }