Files
UnrealEngineUWP/Engine/Source/Editor/ActorPickerMode/Private/ActorPickerMode.cpp
brooke hubert 1cab67f804 [Backout] - CL33723201
Original CL Desc
-----------------------------------------------------------------
[Mode Manager] Fix some singleton startup pattern that was allocating at incorrect timings to speculatively fix some allocate on shutdown crash(es)

Also move some of the global access to the level editor mode manager to pipe through the level editor where there were some early accessors on startup.

#jira UE-213541
#rb patrick.enfedaque ross.smith2


#p4v-cherrypick 33703049

[CL 33736118 by brooke hubert in ue5-main branch]
2024-05-17 16:28:49 -04:00

91 lines
2.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "ActorPickerMode.h"
#include "Modules/ModuleManager.h"
#include "EditorModeRegistry.h"
#include "EditorModeManager.h"
#include "EditorModes.h"
#include "EditorModeActorPicker.h"
#include "Framework/Application/SlateApplication.h"
#include "LevelEditor.h"
IMPLEMENT_MODULE( FActorPickerModeModule, ActorPickerMode );
void FActorPickerModeModule::StartupModule()
{
// Ensure the level editor module is loaded for grabbing the mode manager later
FModuleManager::Get().LoadModuleChecked("LevelEditor");
FEditorModeRegistry::Get().RegisterMode<FEdModeActorPicker>(FBuiltinEditorModes::EM_ActorPicker);
if (FSlateApplication::IsInitialized())
{
OnApplicationDeactivatedHandle = FSlateApplication::Get().OnApplicationActivationStateChanged().Add(TDelegate<void(const bool)>::CreateRaw(this, &FActorPickerModeModule::OnApplicationDeactivated));
}
}
void FActorPickerModeModule::ShutdownModule()
{
if (FSlateApplication::IsInitialized())
{
FSlateApplication::Get().OnApplicationActivationStateChanged().Remove(OnApplicationDeactivatedHandle);
OnApplicationDeactivatedHandle.Reset();
}
FEditorModeRegistry::Get().UnregisterMode(FBuiltinEditorModes::EM_ActorPicker);
}
void FActorPickerModeModule::BeginActorPickingMode(FOnGetAllowedClasses InOnGetAllowedClasses, FOnShouldFilterActor InOnShouldFilterActor, FOnActorSelected InOnActorSelected) const
{
if (FEditorModeTools* ModeTools = GetLevelEditorModeManager())
{
// Activate the mode
ModeTools->ActivateMode(FBuiltinEditorModes::EM_ActorPicker);
// Set the required delegates
FEdModeActorPicker* Mode = ModeTools->GetActiveModeTyped<FEdModeActorPicker>(FBuiltinEditorModes::EM_ActorPicker);
if (ensure(Mode))
{
Mode->OnActorSelected = InOnActorSelected;
Mode->OnGetAllowedClasses = InOnGetAllowedClasses;
Mode->OnShouldFilterActor = InOnShouldFilterActor;
}
}
}
void FActorPickerModeModule::OnApplicationDeactivated(const bool IsActive) const
{
if (!IsActive)
{
EndActorPickingMode();
}
}
void FActorPickerModeModule::EndActorPickingMode() const
{
if (FEditorModeTools* ModeTools = GetLevelEditorModeManager())
{
ModeTools->DeactivateMode(FBuiltinEditorModes::EM_ActorPicker);
}
}
bool FActorPickerModeModule::IsInActorPickingMode() const
{
if (FEditorModeTools* ModeTools = GetLevelEditorModeManager())
{
return ModeTools->IsModeActive(FBuiltinEditorModes::EM_ActorPicker);
}
return false;
}
FEditorModeTools* FActorPickerModeModule::GetLevelEditorModeManager()
{
FLevelEditorModule& LevelEditorModule = FModuleManager::GetModuleChecked<FLevelEditorModule>("LevelEditor");
TSharedPtr<ILevelEditor> FirstLevelEditor = LevelEditorModule.GetFirstLevelEditor();
if (FirstLevelEditor.IsValid())
{
return &FirstLevelEditor->GetEditorModeManager();
}
return nullptr;
}