Files
UnrealEngineUWP/Engine/Source/Editor/AdvancedPreviewScene/Private/PreviewProfileController.cpp
Patrick Laflamme 3f2019d1c4 Added a 'Preview Profiles' combo box to the advances preview scene toolbar to quickly change preview profiles.
- The profile combo is only visible when the user has more than one profile
  - This was added to the Material Editor, the Static Mesh Editor and Niagara Editor.

#jira UETOOL-2775  Add preview scene background profile dropdown to asset editor vieport toolbar.
#rb Lauren.Barnes, Brooke.Hubert
#preflight 6130c00617a8610001b8ba61

[CL 17402767 by Patrick Laflamme in ue5-main branch]
2021-09-02 10:01:25 -04:00

112 lines
3.6 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "PreviewProfileController.h"
#include "Editor/EditorPerProjectUserSettings.h"
#include "AssetViewerSettings.h"
FPreviewProfileController::FPreviewProfileController()
{
PerProjectSettings = GetMutableDefault<UEditorPerProjectUserSettings>();
if (PerProjectSettings)
{
AssetViewerSettings = UAssetViewerSettings::Get();
if (AssetViewerSettings)
{
AssetViewerSettingsProfileAddRemoveHandle = AssetViewerSettings->OnAssetViewerProfileAddRemoved().AddLambda([this]()
{
UpdateAssetViewerProfiles();
OnPreviewProfileListChanged().Broadcast();
});
AssetViewerSettingsChangedHandle = AssetViewerSettings->OnAssetViewerSettingsChanged().AddLambda([this](const FName& InPropertyName)
{
FString CurrProfileName = AssetViewerProfileNames[CurrentProfileIndex];
UpdateAssetViewerProfiles();
if (CurrProfileName != AssetViewerProfileNames[CurrentProfileIndex])
{
OnPreviewProfileChanged().Broadcast();
}
});
UpdateAssetViewerProfiles();
}
}
}
FPreviewProfileController::~FPreviewProfileController()
{
if (AssetViewerSettings)
{
AssetViewerSettings->OnAssetViewerProfileAddRemoved().Remove(AssetViewerSettingsProfileAddRemoveHandle);
AssetViewerSettings->OnAssetViewerSettingsChanged().Remove(AssetViewerSettingsChangedHandle);
}
}
void FPreviewProfileController::UpdateAssetViewerProfiles()
{
AssetViewerProfileNames.Empty();
if (AssetViewerSettings && PerProjectSettings)
{
// Rebuild the profile list.
for (const FPreviewSceneProfile& Profile : AssetViewerSettings->Profiles)
{
AssetViewerProfileNames.Add(Profile.ProfileName + (Profile.bSharedProfile ? TEXT(" (Shared)") : TEXT("")));
}
CurrentProfileIndex = PerProjectSettings->AssetViewerProfileIndex;
EnsureProfilesStateCoherence();
}
}
TArray<FString> FPreviewProfileController::GetPreviewProfiles(int32& OutCurrentProfileIndex) const
{
if (PerProjectSettings && AssetViewerSettings)
{
EnsureProfilesStateCoherence();
OutCurrentProfileIndex = CurrentProfileIndex;
}
return AssetViewerProfileNames;
}
bool FPreviewProfileController::SetActiveProfile(const FString& ProfileName)
{
if (PerProjectSettings && AssetViewerSettings)
{
EnsureProfilesStateCoherence();
int32 Index = AssetViewerProfileNames.IndexOfByKey(ProfileName);
if (Index != INDEX_NONE && Index != PerProjectSettings->AssetViewerProfileIndex)
{
// Store the settings.
PerProjectSettings->AssetViewerProfileIndex = Index;
CurrentProfileIndex = Index;
// Notify the observer about the change.
AssetViewerSettings->OnAssetViewerSettingsChanged().Broadcast(GET_MEMBER_NAME_CHECKED(FPreviewSceneProfile, ProfileName));
return true;
}
}
return false;
}
FString FPreviewProfileController::GetActiveProfile() const
{
if (PerProjectSettings && AssetViewerSettings)
{
EnsureProfilesStateCoherence();
return AssetViewerProfileNames[PerProjectSettings->AssetViewerProfileIndex];
}
return FString();
}
void FPreviewProfileController::EnsureProfilesStateCoherence() const
{
ensureMsgf(AssetViewerProfileNames.Num() == AssetViewerSettings->Profiles.Num(), TEXT("List of profiles is out of sync with the list of corresponding profile names."));
ensureMsgf(AssetViewerProfileNames.Num() > 0, TEXT("The list of profiles is expected to always have at least one default profile"));
ensureMsgf(AssetViewerSettings->Profiles.IsValidIndex(PerProjectSettings->AssetViewerProfileIndex), TEXT("The active asset viewer profile is invalid."));
ensureMsgf(CurrentProfileIndex == PerProjectSettings->AssetViewerProfileIndex, TEXT("The cached selected profile index is out of sync."));
}