Files
patrick boutot 5ea06e3951 MVVM: Add flag on viewmodel context to allow edit, remove and rename in the editor.
#rb editor-ui-systems

[CL 26853829 by patrick boutot in 5.3 branch]
2023-08-04 13:59:09 -04:00

163 lines
5.1 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "Templates/SubclassOf.h"
#include "MVVMViewModelBase.h"
#include "MVVMBlueprintViewModelContext.generated.h"
class UMVVMViewModelContextResolver;
/**
*
*/
UENUM()
enum class EMVVMBlueprintViewModelContextCreationType : uint8
{
// The viewmodel will be assigned later.
Manual,
// A new instance of the viewmodel will be created when the widget is created.
CreateInstance,
// The viewmodel exists and is added to the MVVMSubsystem. It will be fetched there.
GlobalViewModelCollection,
// The viewmodel will be fetched by evaluating a function or a property path.
PropertyPath,
// The viewmodel will be fetched by evaluating the resolver object.
Resolver,
};
namespace UE::MVVM
{
#if WITH_EDITOR
UE_NODISCARD MODELVIEWVIEWMODELBLUEPRINT_API TArray<EMVVMBlueprintViewModelContextCreationType> GetAllowedContextCreationType(const UClass* Class);
#endif
}
/**
*
*/
USTRUCT()
struct MODELVIEWVIEWMODELBLUEPRINT_API FMVVMBlueprintViewModelContext
{
GENERATED_BODY()
public:
FMVVMBlueprintViewModelContext() = default;
FMVVMBlueprintViewModelContext(const UClass* InClass, FName InViewModelName);
FGuid GetViewModelId() const
{
return ViewModelContextId;
}
FName GetViewModelName() const
{
return ViewModelName;
}
FText GetDisplayName() const;
UClass* GetViewModelClass() const
{
return NotifyFieldValueClass;
}
void PostSerialize(const FArchive& Ar)
{
if (Ar.IsLoading())
{
if (ViewModelName.IsNone())
{
ViewModelName = *OverrideDisplayName_DEPRECATED.ToString();
}
if (ViewModelName.IsNone() )
{
ViewModelName = *ViewModelContextId.ToString();
}
if (ViewModelClass_DEPRECATED.Get())
{
NotifyFieldValueClass = ViewModelClass_DEPRECATED.Get();
}
}
}
bool IsValid() const
{
return NotifyFieldValueClass != nullptr;
}
private:
/** When the view is spawn, create an instance of the viewmodel. */
UPROPERTY(VisibleAnywhere, AdvancedDisplay, Category = "Viewmodel", meta = (DisplayName = "Viewmodel Context Id"))
FGuid ViewModelContextId;
UPROPERTY(VisibleAnywhere, Category = "Viewmodel", NoClear, meta = (DisallowCreateNew, AllowedClasses = "/Script/UMG.NotifyFieldValueChanged", DisallowedClasses = "/Script/UMG.Widget"))
TObjectPtr<UClass> NotifyFieldValueClass = nullptr;
UPROPERTY()
TSubclassOf<UMVVMViewModelBase> ViewModelClass_DEPRECATED;
UPROPERTY()
FText OverrideDisplayName_DEPRECATED;
public:
/** Property name that will be generated. */
UPROPERTY(EditAnywhere, Category = "Viewmodel", meta = (DisplayName = "Viewmodel Name"))
FName ViewModelName;
/** When the view is spawn, create an instance of the viewmodel. */
UPROPERTY(EditAnywhere, Category = "Viewmodel")
EMVVMBlueprintViewModelContextCreationType CreationType = EMVVMBlueprintViewModelContextCreationType::CreateInstance;
/** Identifier of an already registered viewmodel. */
UPROPERTY(EditAnywhere, Category = "Viewmodel", AdvancedDisplay, meta = (DisplayName = "Global Viewmodel Identifier", EditCondition = "CreationType == EMVVMBlueprintViewModelContextCreationType::GlobalViewModelCollection && bCanEdit", EditConditionHides))
FName GlobalViewModelIdentifier;
/** The Path to get the viewmodel instance. */
UPROPERTY(EditAnywhere, Category = "Viewmodel", AdvancedDisplay, meta = (DisplayName = "Viewmodel Property Path", EditCondition = "CreationType == EMVVMBlueprintViewModelContextCreationType::PropertyPath && bCanEdit", EditConditionHides))
FString ViewModelPropertyPath;
UPROPERTY(EditAnywhere, Category = "Viewmodel", Instanced, AdvancedDisplay, meta = (EditInline, EditCondition = "CreationType == EMVVMBlueprintViewModelContextCreationType::Resolver && bCanEdit", EditConditionHides))
TObjectPtr<UMVVMViewModelContextResolver> Resolver = nullptr;
/**
* Generate a public setter for this viewmodel.
* @note Always true when the Creation Type is Manual.
*/
UPROPERTY(EditAnywhere, Category = "Viewmodel", AdvancedDisplay, meta = (DisplanName="Create Public Setter", EditCondition = "CreationType != EMVVMBlueprintViewModelContextCreationType::Manual && bCanEdit", EditConditionHides))
bool bCreateSetterFunction = false;
/**
* Optional. Will not warn if the instance is not set or found.
* @note Always true when the Creation Type is Manual.
*/
UPROPERTY(EditAnywhere, Category = "Viewmodel", AdvancedDisplay, meta = (EditCondition = "(CreationType == EMVVMBlueprintViewModelContextCreationType::GlobalViewModelCollection || CreationType == EMVVMBlueprintViewModelContextCreationType::PropertyPath) && bCanEdit", EditConditionHides))
bool bOptional = false;
/** Can change the name in the editor. */
UPROPERTY()
bool bCanRename = true;
/** Can change properties in the editor. */
UPROPERTY()
bool bCanEdit = true;
/** Can remove the viewmodel in the editor. */
UPROPERTY()
bool bCanRemove = true;
};
template<>
struct TStructOpsTypeTraits<FMVVMBlueprintViewModelContext> : public TStructOpsTypeTraitsBase2<FMVVMBlueprintViewModelContext>
{
enum
{
WithPostSerialize = true,
};
};
#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_2
#include "CoreMinimal.h"
#endif