Files
UnrealEngineUWP/Engine/Source/Editor/DetailCustomizations/Private/SkeletalMeshComponentDetails.cpp
robert manuszewski d1443992e1 Deprecating ANY_PACKAGE.
This change consists of multiple changes:

Core:
- Deprecation of ANY_PACKAGE macro. Added ANY_PACKAGE_DEPRECATED macro which can still be used for backwards compatibility purposes (only used in CoreUObject)
- Deprecation of StaticFindObjectFast* functions that take bAnyPackage parameter
- Added UStruct::GetStructPathName function that returns FTopLevelAssetPath representing the path name (package + object FName, super quick compared to UObject::GetPathName) + wrapper UClass::GetClassPathName to make it look better when used with UClasses
- Added (Static)FindFirstObject* functions that find a first object given its Name (no Outer). These functions are used in places I consider valid to do global UObject (UClass) lookups like parsing command line parameters / checking for unique object names
- Added static UClass::TryFindType function which serves a similar purpose as FindFirstObject however it's going to throw a warning (with a callstack / maybe ensure in the future?) if short class name is provided. This function is used  in places that used to use short class names but now should have been converted to use path names to catch any potential regressions and or edge cases I missed.
- Added static UClass::TryConvertShortNameToPathName utility function
- Added static UClass::TryFixShortClassNameExportPath utility function
- Object text export paths will now also include class path (Texture2D'/Game/Textures/Grass.Grass' -> /Script/Engine.Texture2D'/Game/Textures/Grass.Grass')
- All places that manually generated object export paths for objects will now use FObjectPropertyBase::GetExportPath
- Added a new startup test that checks for short type names in UClass/FProperty MetaData values

AssetRegistry:
- Deprecated any member variables (FAssetData / FARFilter) or functions that use FNames to represent class names and replaced them with FTopLevelAssetPath
- Added new member variables and new function overloads that use FTopLevelAssetPath to represent class names
- This also applies to a few other modules' APIs to match AssetRegistry changes

Everything else:
- Updated code that used ANY_PACKAGE (depending on the use case) to use FindObject(nullptr, PathToObject), UClass::TryFindType (used when path name is expected, warns if it's a short name) or FindFirstObject (usually for finding types based on user input but there's been a few legitimate use cases not related to user input)
- Updated code that used AssetRegistry API to use FTopLevelAssetPaths and USomeClass::StaticClass()->GetClassPathName() instead of GetFName()
- Updated meta data and hardcoded FindObject(ANY_PACKAGE, "EEnumNameOrClassName") calls to use path names

#jira UE-99463
#rb many.people
[FYI] Marcus.Wassmer
#preflight 629248ec2256738f75de9b32

#codereviewnumbers 20320742, 20320791, 20320799, 20320756, 20320809, 20320830, 20320840, 20320846, 20320851, 20320863, 20320780, 20320765, 20320876, 20320786

#ROBOMERGE-OWNER: robert.manuszewski
#ROBOMERGE-AUTHOR: robert.manuszewski
#ROBOMERGE-SOURCE: CL 20430220 via CL 20433854 via CL 20435474 via CL 20435484
#ROBOMERGE-BOT: UE5 (Release-Engine-Staging -> Main) (v949-20362246)

[CL 20448496 by robert manuszewski in ue5-main branch]
2022-06-01 03:46:59 -04:00

425 lines
14 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "SkeletalMeshComponentDetails.h"
#include "Modules/ModuleManager.h"
#include "Widgets/SBoxPanel.h"
#include "Widgets/Layout/SBorder.h"
#include "Widgets/Text/STextBlock.h"
#include "Widgets/Layout/SBox.h"
#include "Widgets/Input/SComboButton.h"
#include "Styling/AppStyle.h"
#include "Animation/AnimInstance.h"
#include "Animation/AnimBlueprint.h"
#include "Editor.h"
#include "EditorCategoryUtils.h"
#include "DetailLayoutBuilder.h"
#include "IDetailPropertyRow.h"
#include "DetailCategoryBuilder.h"
#include "PropertyCustomizationHelpers.h"
#include "ClassViewerModule.h"
#include "ClassViewerFilter.h"
#include "Engine/Selection.h"
#include "Animation/AnimBlueprintGeneratedClass.h"
#include "Widgets/Images/SImage.h"
#include "PhysicsEngine/PhysicsSettings.h"
#define LOCTEXT_NAMESPACE "SkeletalMeshComponentDetails"
// Filter class for animation blueprint picker
class FAnimBlueprintFilter : public IClassViewerFilter
{
public:
virtual bool IsClassAllowed(const FClassViewerInitializationOptions& InInitOptions, const UClass* InClass, TSharedRef< class FClassViewerFilterFuncs > InFilterFuncs ) override
{
if(InFilterFuncs->IfInChildOfClassesSet(AllowedChildrenOfClasses, InClass) != EFilterReturn::Failed)
{
return true;
}
return false;
}
virtual bool IsUnloadedClassAllowed(const FClassViewerInitializationOptions& InInitOptions, const TSharedRef< const class IUnloadedBlueprintData > InUnloadedClassData, TSharedRef< class FClassViewerFilterFuncs > InFilterFuncs) override
{
return InFilterFuncs->IfInChildOfClassesSet(AllowedChildrenOfClasses, InUnloadedClassData) != EFilterReturn::Failed;
}
/** Only children of the classes in this set will be unfiltered */
TSet<const UClass*> AllowedChildrenOfClasses;
};
FSkeletalMeshComponentDetails::FSkeletalMeshComponentDetails()
: CurrentDetailBuilder(NULL)
, Skeleton(nullptr)
, bAnimPickerEnabled(false)
{
}
FSkeletalMeshComponentDetails::~FSkeletalMeshComponentDetails()
{
UnregisterAllMeshPropertyChangedCallers();
}
TSharedRef<IDetailCustomization> FSkeletalMeshComponentDetails::MakeInstance()
{
return MakeShareable(new FSkeletalMeshComponentDetails);
}
void FSkeletalMeshComponentDetails::CustomizeDetails(IDetailLayoutBuilder& DetailBuilder)
{
if(!CurrentDetailBuilder)
{
CurrentDetailBuilder = &DetailBuilder;
}
DetailBuilder.EditCategory("SkeletalMesh", FText::GetEmpty(), ECategoryPriority::TypeSpecific);
DetailBuilder.EditCategory("Materials", FText::GetEmpty(), ECategoryPriority::TypeSpecific);
DetailBuilder.EditCategory("Physics", FText::GetEmpty(), ECategoryPriority::TypeSpecific);
DetailBuilder.HideProperty("bCastStaticShadow", UPrimitiveComponent::StaticClass());
DetailBuilder.HideProperty("LightmapType", UPrimitiveComponent::StaticClass());
DetailBuilder.EditCategory("Animation", FText::GetEmpty(), ECategoryPriority::Important);
PerformInitialRegistrationOfSkeletalMeshes(DetailBuilder);
UpdateAnimationCategory(DetailBuilder);
UpdatePhysicsCategory(DetailBuilder);
}
void FSkeletalMeshComponentDetails::UpdateAnimationCategory(IDetailLayoutBuilder& DetailBuilder)
{
// Custom skeletal mesh components may hide the animation category, so we won't assume it's visible
if (DetailBuilder.GetBaseClass() && FEditorCategoryUtils::IsCategoryHiddenFromClass(DetailBuilder.GetBaseClass(), "Animation"))
{
return;
}
UpdateSkeletonNameAndPickerVisibility();
IDetailCategoryBuilder& AnimationCategory = DetailBuilder.EditCategory("Animation", FText::GetEmpty(), ECategoryPriority::Important);
// Force the mode switcher to be first
AnimationModeHandle = DetailBuilder.GetProperty(GET_MEMBER_NAME_CHECKED(USkeletalMeshComponent, AnimationMode));
check (AnimationModeHandle->IsValidHandle());
const FName AnimationBlueprintName = GET_MEMBER_NAME_CHECKED(USkeletalMeshComponent, AnimClass);
AnimationBlueprintHandle = DetailBuilder.GetProperty(AnimationBlueprintName);
check(AnimationBlueprintHandle->IsValidHandle());
AnimationCategory.AddProperty(AnimationModeHandle);
// Place the blueprint property next (which may be hidden, depending on the mode)
TAttribute<EVisibility> BlueprintVisibility( this, &FSkeletalMeshComponentDetails::VisibilityForBlueprintMode );
DetailBuilder.HideProperty(AnimationBlueprintHandle);
AnimationCategory.AddCustomRow(AnimationBlueprintHandle->GetPropertyDisplayName())
.RowTag(AnimationBlueprintName)
.Visibility(BlueprintVisibility)
.NameContent()
[
AnimationBlueprintHandle->CreatePropertyNameWidget()
]
.ValueContent()
.MinDesiredWidth(125.f)
.MaxDesiredWidth(250.f)
[
SNew(SHorizontalBox)
+ SHorizontalBox::Slot()
.FillWidth(1.0f)
[
SAssignNew(ClassPickerComboButton, SComboButton)
.OnGetMenuContent(this, &FSkeletalMeshComponentDetails::GetClassPickerMenuContent)
.ContentPadding(0)
.ButtonContent()
[
SNew(STextBlock)
.Font(IDetailLayoutBuilder::GetDetailFont())
.Text(this, &FSkeletalMeshComponentDetails::GetSelectedAnimBlueprintName)
.MinDesiredWidth(200.f)
]
]
+ SHorizontalBox::Slot()
.AutoWidth()
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
.Padding(2.0f, 1.0f)
[
PropertyCustomizationHelpers::MakeBrowseButton(FSimpleDelegate::CreateSP(this, &FSkeletalMeshComponentDetails::OnBrowseToAnimBlueprint))
]
+ SHorizontalBox::Slot()
.AutoWidth()
.HAlign(HAlign_Center)
.VAlign(VAlign_Center)
.Padding(2.0f, 1.0f)
[
PropertyCustomizationHelpers::MakeUseSelectedButton(FSimpleDelegate::CreateSP(this, &FSkeletalMeshComponentDetails::UseSelectedAnimBlueprint))
]
];
// Hide the parent AnimationData property, and inline the children with custom visibility delegates
const FName AnimationDataFName(GET_MEMBER_NAME_CHECKED(USkeletalMeshComponent, AnimationData));
TSharedPtr<IPropertyHandle> AnimationDataHandle = DetailBuilder.GetProperty(AnimationDataFName);
check(AnimationDataHandle->IsValidHandle());
TAttribute<EVisibility> SingleAnimVisibility(this, &FSkeletalMeshComponentDetails::VisibilityForSingleAnimMode);
DetailBuilder.HideProperty(AnimationDataFName);
// Process Animation asset selection
uint32 TotalChildren=0;
AnimationDataHandle->GetNumChildren(TotalChildren);
for (uint32 ChildIndex=0; ChildIndex < TotalChildren; ++ChildIndex)
{
TSharedPtr<IPropertyHandle> ChildHandle = AnimationDataHandle->GetChildHandle(ChildIndex);
if (ChildHandle->GetProperty()->GetFName() == GET_MEMBER_NAME_CHECKED(FSingleAnimationPlayData, AnimToPlay))
{
// Hide the property, as we're about to add it differently
DetailBuilder.HideProperty(ChildHandle);
// Add it differently
TSharedPtr<SWidget> NameWidget = ChildHandle->CreatePropertyNameWidget();
TSharedRef<SWidget> PropWidget = SNew(SObjectPropertyEntryBox)
.ThumbnailPool(DetailBuilder.GetThumbnailPool())
.PropertyHandle(ChildHandle)
.AllowedClass(UAnimationAsset::StaticClass())
.AllowClear(true)
.OnShouldFilterAsset(FOnShouldFilterAsset::CreateSP(this, &FSkeletalMeshComponentDetails::OnShouldFilterAnimAsset));
TAttribute<bool> AnimPickerEnabledAttr(this, &FSkeletalMeshComponentDetails::AnimPickerIsEnabled);
AnimationCategory.AddCustomRow(ChildHandle->GetPropertyDisplayName())
.Visibility(SingleAnimVisibility)
.IsEnabled(AnimPickerEnabledAttr)
.NameContent()
[
NameWidget.ToSharedRef()
]
.ValueContent()
.MinDesiredWidth(600)
.MaxDesiredWidth(600)
[
PropWidget
]
.PropertyHandleList({ ChildHandle });
}
else
{
AnimationCategory.AddProperty(ChildHandle).Visibility(SingleAnimVisibility);
}
}
}
void FSkeletalMeshComponentDetails::UpdatePhysicsCategory(IDetailLayoutBuilder& DetailBuilder)
{
}
EVisibility FSkeletalMeshComponentDetails::VisibilityForAnimationMode(EAnimationMode::Type AnimationMode) const
{
uint8 AnimationModeValue=0;
FPropertyAccess::Result Ret = AnimationModeHandle.Get()->GetValue(AnimationModeValue);
if (Ret == FPropertyAccess::Result::Success)
{
return (AnimationModeValue == AnimationMode) ? EVisibility::Visible : EVisibility::Hidden;
}
return EVisibility::Hidden; //Hidden if we get fail or MultipleValues from the property
}
bool FSkeletalMeshComponentDetails::OnShouldFilterAnimAsset( const FAssetData& AssetData )
{
// Check the compatible skeletons.
if (Skeleton && Skeleton->IsCompatibleSkeletonByAssetData(AssetData))
{
return false;
}
return true;
}
void FSkeletalMeshComponentDetails::SkeletalMeshPropertyChanged()
{
UpdateSkeletonNameAndPickerVisibility();
}
void FSkeletalMeshComponentDetails::UpdateSkeletonNameAndPickerVisibility()
{
// Update the selected skeleton name and the picker visibility
Skeleton = GetValidSkeletonFromRegisteredMeshes();
if (Skeleton)
{
bAnimPickerEnabled = true;
SelectedSkeletonName = FObjectPropertyBase::GetExportPath(Skeleton);
}
else
{
bAnimPickerEnabled = false;
SelectedSkeletonName = "";
}
}
void FSkeletalMeshComponentDetails::RegisterSkeletalMeshPropertyChanged(TWeakObjectPtr<USkeletalMeshComponent> Mesh)
{
if(Mesh.IsValid() && OnSkeletalMeshPropertyChanged.IsBound())
{
OnSkeletalMeshPropertyChangedDelegateHandles.Add(Mesh.Get(), Mesh->RegisterOnSkeletalMeshPropertyChanged(OnSkeletalMeshPropertyChanged));
}
}
void FSkeletalMeshComponentDetails::UnregisterSkeletalMeshPropertyChanged(TWeakObjectPtr<USkeletalMeshComponent> Mesh)
{
if(Mesh.IsValid())
{
Mesh->UnregisterOnSkeletalMeshPropertyChanged(OnSkeletalMeshPropertyChangedDelegateHandles.FindRef(Mesh.Get()));
OnSkeletalMeshPropertyChangedDelegateHandles.Remove(Mesh.Get());
}
}
void FSkeletalMeshComponentDetails::UnregisterAllMeshPropertyChangedCallers()
{
for(auto MeshIter = SelectedObjects.CreateIterator() ; MeshIter ; ++MeshIter)
{
if(USkeletalMeshComponent* Mesh = Cast<USkeletalMeshComponent>(MeshIter->Get()))
{
Mesh->UnregisterOnSkeletalMeshPropertyChanged(OnSkeletalMeshPropertyChangedDelegateHandles.FindRef(Mesh));
OnSkeletalMeshPropertyChangedDelegateHandles.Remove(Mesh);
}
}
}
bool FSkeletalMeshComponentDetails::AnimPickerIsEnabled() const
{
return bAnimPickerEnabled;
}
TSharedRef<SWidget> FSkeletalMeshComponentDetails::GetClassPickerMenuContent()
{
TSharedPtr<FAnimBlueprintFilter> Filter = MakeShareable(new FAnimBlueprintFilter);
Filter->AllowedChildrenOfClasses.Add(UAnimInstance::StaticClass());
FClassViewerModule& ClassViewerModule = FModuleManager::LoadModuleChecked<FClassViewerModule>("ClassViewer");
FClassViewerInitializationOptions InitOptions;
InitOptions.Mode = EClassViewerMode::ClassPicker;
InitOptions.ClassFilters.Add(Filter.ToSharedRef());
InitOptions.bShowNoneOption = true;
return SNew(SBorder)
.Padding(3)
.BorderImage(FAppStyle::GetBrush("Menu.Background"))
.ForegroundColor(FAppStyle::GetColor("DefaultForeground"))
[
SNew(SBox)
.WidthOverride(280)
[
ClassViewerModule.CreateClassViewer(InitOptions, FOnClassPicked::CreateSP(this, &FSkeletalMeshComponentDetails::OnClassPicked))
]
];
}
FText FSkeletalMeshComponentDetails::GetSelectedAnimBlueprintName() const
{
check(AnimationBlueprintHandle->IsValidHandle());
UObject* Object = NULL;
AnimationBlueprintHandle->GetValue(Object);
if(Object)
{
return FText::FromString(Object->GetName());
}
else
{
return LOCTEXT("None", "None");
}
}
void FSkeletalMeshComponentDetails::OnClassPicked( UClass* PickedClass )
{
check(AnimationBlueprintHandle->IsValidHandle());
ClassPickerComboButton->SetIsOpen(false);
AnimationBlueprintHandle->SetValue(PickedClass);
}
void FSkeletalMeshComponentDetails::OnBrowseToAnimBlueprint()
{
check(AnimationBlueprintHandle->IsValidHandle());
UObject* Object = NULL;
AnimationBlueprintHandle->GetValue(Object);
TArray<UObject*> Objects;
Objects.Add(Object);
GEditor->SyncBrowserToObjects(Objects);
}
void FSkeletalMeshComponentDetails::UseSelectedAnimBlueprint()
{
FEditorDelegates::LoadSelectedAssetsIfNeeded.Broadcast();
USelection* AssetSelection = GEditor->GetSelectedObjects();
if (AssetSelection && AssetSelection->Num() == 1)
{
UAnimBlueprint* AnimBlueprintToAssign = AssetSelection->GetTop<UAnimBlueprint>();
if (AnimBlueprintToAssign)
{
if(USkeleton* AnimBlueprintSkeleton = AnimBlueprintToAssign->TargetSkeleton)
{
if (Skeleton && Skeleton->IsCompatible(AnimBlueprintSkeleton))
{
OnClassPicked(AnimBlueprintToAssign->GetAnimBlueprintGeneratedClass());
}
}
}
}
}
void FSkeletalMeshComponentDetails::PerformInitialRegistrationOfSkeletalMeshes(IDetailLayoutBuilder& DetailBuilder)
{
OnSkeletalMeshPropertyChanged = USkeletalMeshComponent::FOnSkeletalMeshPropertyChanged::CreateSP(this, &FSkeletalMeshComponentDetails::SkeletalMeshPropertyChanged);
DetailBuilder.GetObjectsBeingCustomized(SelectedObjects);
check(SelectedObjects.Num() > 0);
for (auto ObjectIter = SelectedObjects.CreateIterator(); ObjectIter; ++ObjectIter)
{
if (USkeletalMeshComponent* Mesh = Cast<USkeletalMeshComponent>(ObjectIter->Get()))
{
RegisterSkeletalMeshPropertyChanged(Mesh);
}
}
}
USkeleton* FSkeletalMeshComponentDetails::GetValidSkeletonFromRegisteredMeshes() const
{
USkeleton* ResultSkeleton = NULL;
for (auto ObjectIter = SelectedObjects.CreateConstIterator(); ObjectIter; ++ObjectIter)
{
USkeletalMeshComponent* const Mesh = Cast<USkeletalMeshComponent>(ObjectIter->Get());
if ( !Mesh || !Mesh->SkeletalMesh )
{
continue;
}
// If we've not come across a valid skeleton yet, store this one.
if (!ResultSkeleton)
{
ResultSkeleton = Mesh->SkeletalMesh->GetSkeleton();
continue;
}
// We've encountered a valid skeleton before.
// If this skeleton is not the same one, that means there are multiple
// skeletons selected, so we don't want to take any action.
if (Mesh->SkeletalMesh->GetSkeleton() != ResultSkeleton)
{
return NULL;
}
}
return ResultSkeleton;
}
#undef LOCTEXT_NAMESPACE