2022-04-01 14:21:11 -04:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
|
|
|
|
#include "MVVMBlueprintView.h"
|
|
|
|
|
|
2023-01-12 01:48:34 -05:00
|
|
|
#include "Components/Widget.h"
|
2022-04-01 14:21:11 -04:00
|
|
|
#include "Kismet2/BlueprintEditorUtils.h"
|
2022-12-08 09:06:03 -05:00
|
|
|
#include "MVVMWidgetBlueprintExtension_View.h"
|
2022-04-01 14:21:11 -04:00
|
|
|
|
2022-09-28 01:06:15 -04:00
|
|
|
#include UE_INLINE_GENERATED_CPP_BY_NAME(MVVMBlueprintView)
|
|
|
|
|
|
2022-04-01 14:21:11 -04:00
|
|
|
|
|
|
|
|
FMVVMBlueprintViewModelContext* UMVVMBlueprintView::FindViewModel(FGuid ViewModelId)
|
|
|
|
|
{
|
|
|
|
|
return AvailableViewModels.FindByPredicate([ViewModelId](const FMVVMBlueprintViewModelContext& Other)
|
|
|
|
|
{
|
|
|
|
|
return Other.GetViewModelId() == ViewModelId;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FMVVMBlueprintViewModelContext* UMVVMBlueprintView::FindViewModel(FGuid ViewModelId) const
|
|
|
|
|
{
|
|
|
|
|
return const_cast<UMVVMBlueprintView*>(this)->FindViewModel(ViewModelId);
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-03 14:47:48 -04:00
|
|
|
const FMVVMBlueprintViewModelContext* UMVVMBlueprintView::FindViewModel(FName ViewModel) const
|
|
|
|
|
{
|
|
|
|
|
return AvailableViewModels.FindByPredicate([ViewModel](const FMVVMBlueprintViewModelContext& Other)
|
|
|
|
|
{
|
|
|
|
|
return Other.GetViewModelName() == ViewModel;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:21:11 -04:00
|
|
|
void UMVVMBlueprintView::AddViewModel(const FMVVMBlueprintViewModelContext& NewContext)
|
|
|
|
|
{
|
|
|
|
|
AvailableViewModels.Add(NewContext);
|
|
|
|
|
FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(GetOuterUMVVMWidgetBlueprintExtension_View()->GetWidgetBlueprint());
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2022-06-03 14:47:48 -04:00
|
|
|
bool UMVVMBlueprintView::RemoveViewModel(FGuid ViewModelId)
|
2022-04-01 14:21:11 -04:00
|
|
|
{
|
|
|
|
|
int32 Count = AvailableViewModels.RemoveAll([ViewModelId](const FMVVMBlueprintViewModelContext& VM)
|
|
|
|
|
{
|
|
|
|
|
return VM.GetViewModelId() == ViewModelId;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (Count > 0)
|
|
|
|
|
{
|
|
|
|
|
FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(GetOuterUMVVMWidgetBlueprintExtension_View()->GetWidgetBlueprint());
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}
|
2022-06-03 14:47:48 -04:00
|
|
|
return Count > 0;
|
2022-04-01 14:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
2022-06-03 14:47:48 -04:00
|
|
|
int32 UMVVMBlueprintView::RemoveViewModels(const TArrayView<FGuid> ViewModelIds)
|
2022-04-01 14:21:11 -04:00
|
|
|
{
|
|
|
|
|
int32 Count = 0;
|
|
|
|
|
for (const FGuid& ViewModelId : ViewModelIds)
|
|
|
|
|
{
|
|
|
|
|
Count += AvailableViewModels.RemoveAll([ViewModelId](const FMVVMBlueprintViewModelContext& VM)
|
|
|
|
|
{
|
|
|
|
|
return VM.GetViewModelId() == ViewModelId;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Count > 0)
|
|
|
|
|
{
|
|
|
|
|
FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(GetOuterUMVVMWidgetBlueprintExtension_View()->GetWidgetBlueprint());
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}
|
2022-06-03 14:47:48 -04:00
|
|
|
return Count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UMVVMBlueprintView::RenameViewModel(FName OldViewModelName, FName NewViewModelName)
|
|
|
|
|
{
|
|
|
|
|
FMVVMBlueprintViewModelContext* ViewModelContext = AvailableViewModels.FindByPredicate([OldViewModelName](const FMVVMBlueprintViewModelContext& Other)
|
|
|
|
|
{
|
|
|
|
|
return Other.GetViewModelName() == OldViewModelName;
|
|
|
|
|
});
|
|
|
|
|
if (ViewModelContext)
|
|
|
|
|
{
|
2022-06-29 01:01:28 -04:00
|
|
|
ViewModelContext->ViewModelName = NewViewModelName;
|
2022-06-03 14:47:48 -04:00
|
|
|
|
|
|
|
|
FBlueprintEditorUtils::ReplaceVariableReferences(GetOuterUMVVMWidgetBlueprintExtension_View()->GetWidgetBlueprint(), OldViewModelName, NewViewModelName);
|
|
|
|
|
FBlueprintEditorUtils::ValidateBlueprintChildVariables(GetOuterUMVVMWidgetBlueprintExtension_View()->GetWidgetBlueprint(), NewViewModelName);
|
|
|
|
|
FBlueprintEditorUtils::MarkBlueprintAsStructurallyModified(GetOuterUMVVMWidgetBlueprintExtension_View()->GetWidgetBlueprint());
|
|
|
|
|
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
return ViewModelContext != nullptr;
|
2022-04-01 14:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FMVVMBlueprintViewBinding* UMVVMBlueprintView::FindBinding(const UWidget* Widget, const FProperty* Property) const
|
|
|
|
|
{
|
2022-05-18 12:54:12 -04:00
|
|
|
return const_cast<UMVVMBlueprintView*>(this)->FindBinding(Widget, Property);
|
2022-04-01 14:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMVVMBlueprintViewBinding* UMVVMBlueprintView::FindBinding(const UWidget* Widget, const FProperty* Property)
|
|
|
|
|
{
|
2022-05-18 12:54:12 -04:00
|
|
|
FName WidgetName = Widget->GetFName();
|
|
|
|
|
return Bindings.FindByPredicate([WidgetName, Property](const FMVVMBlueprintViewBinding& Binding)
|
2022-04-01 14:21:11 -04:00
|
|
|
{
|
2023-03-09 08:41:28 -05:00
|
|
|
return Binding.DestinationPath.GetWidgetName() == WidgetName &&
|
|
|
|
|
Binding.DestinationPath.BasePropertyPathContains(UE::MVVM::FMVVMConstFieldVariant(Property));
|
2022-04-01 14:21:11 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UMVVMBlueprintView::RemoveBindingAt(int32 Index)
|
|
|
|
|
{
|
2022-09-07 17:14:59 -04:00
|
|
|
if (Bindings.IsValidIndex(Index))
|
|
|
|
|
{
|
|
|
|
|
Bindings.RemoveAt(Index);
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
}
|
2022-04-01 14:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UMVVMBlueprintView::RemoveBinding(const FMVVMBlueprintViewBinding* Binding)
|
|
|
|
|
{
|
|
|
|
|
int32 Index = 0;
|
|
|
|
|
for (; Index < Bindings.Num(); ++Index)
|
|
|
|
|
{
|
|
|
|
|
if (&Bindings[Index] == Binding)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 17:14:59 -04:00
|
|
|
RemoveBindingAt(Index);
|
2022-04-01 14:21:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMVVMBlueprintViewBinding& UMVVMBlueprintView::AddBinding(const UWidget* Widget, const FProperty* Property)
|
|
|
|
|
{
|
|
|
|
|
FMVVMBlueprintViewBinding& NewBinding = Bindings.AddDefaulted_GetRef();
|
2023-03-09 08:41:28 -05:00
|
|
|
NewBinding.DestinationPath.SetWidgetName(Widget->GetFName());
|
|
|
|
|
NewBinding.DestinationPath.SetBasePropertyPath(UE::MVVM::FMVVMConstFieldVariant(Property));
|
2022-10-05 16:05:24 -04:00
|
|
|
NewBinding.BindingId = FGuid::NewGuid();
|
2022-04-01 14:21:11 -04:00
|
|
|
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
return NewBinding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMVVMBlueprintViewBinding& UMVVMBlueprintView::AddDefaultBinding()
|
|
|
|
|
{
|
|
|
|
|
FMVVMBlueprintViewBinding& NewBinding = Bindings.AddDefaulted_GetRef();
|
2022-10-06 20:08:12 -04:00
|
|
|
NewBinding.BindingId = FGuid::NewGuid();
|
2022-04-01 14:21:11 -04:00
|
|
|
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
return NewBinding;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FMVVMBlueprintViewBinding* UMVVMBlueprintView::GetBindingAt(int32 Index)
|
|
|
|
|
{
|
|
|
|
|
if (Bindings.IsValidIndex(Index))
|
|
|
|
|
{
|
|
|
|
|
return &Bindings[Index];
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FMVVMBlueprintViewBinding* UMVVMBlueprintView::GetBindingAt(int32 Index) const
|
|
|
|
|
{
|
|
|
|
|
if (Bindings.IsValidIndex(Index))
|
|
|
|
|
{
|
|
|
|
|
return &Bindings[Index];
|
|
|
|
|
}
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-26 22:19:39 -04:00
|
|
|
FMVVMBlueprintViewBinding* UMVVMBlueprintView::GetBinding(FGuid Id)
|
|
|
|
|
{
|
|
|
|
|
return Bindings.FindByPredicate([Id](const FMVVMBlueprintViewBinding& Binding){ return Id == Binding.BindingId; });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const FMVVMBlueprintViewBinding* UMVVMBlueprintView::GetBinding(FGuid Id) const
|
|
|
|
|
{
|
|
|
|
|
return Bindings.FindByPredicate([Id](const FMVVMBlueprintViewBinding& Binding) { return Id == Binding.BindingId; });
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-17 16:33:42 -04:00
|
|
|
TArray<FText> UMVVMBlueprintView::GetBindingMessages(FGuid Id, UE::MVVM::EBindingMessageType InMessageType) const
|
|
|
|
|
{
|
|
|
|
|
TArray<FText> Results;
|
|
|
|
|
|
|
|
|
|
if (BindingMessages.Contains(Id))
|
|
|
|
|
{
|
|
|
|
|
const TArray<UE::MVVM::FBindingMessage>& AllBindingMessages = BindingMessages[Id];
|
|
|
|
|
for (const UE::MVVM::FBindingMessage& Message : AllBindingMessages)
|
|
|
|
|
{
|
|
|
|
|
if (Message.MessageType == InMessageType)
|
|
|
|
|
{
|
|
|
|
|
Results.Add(Message.MessageText);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Results;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UMVVMBlueprintView::HasBindingMessage(FGuid Id, UE::MVVM::EBindingMessageType InMessageType) const
|
|
|
|
|
{
|
|
|
|
|
if (const TArray<UE::MVVM::FBindingMessage>* FoundBindingMessages = BindingMessages.Find(Id))
|
|
|
|
|
{
|
|
|
|
|
for (const UE::MVVM::FBindingMessage& Message : *FoundBindingMessages)
|
|
|
|
|
{
|
|
|
|
|
if (Message.MessageType == InMessageType)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UMVVMBlueprintView::AddMessageToBinding(FGuid Id, UE::MVVM::FBindingMessage MessageToAdd)
|
|
|
|
|
{
|
|
|
|
|
TArray<UE::MVVM::FBindingMessage>& FoundBindingMessages = BindingMessages.FindOrAdd(Id);
|
|
|
|
|
FoundBindingMessages.Add(MessageToAdd);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void UMVVMBlueprintView::ResetBindingMessages()
|
|
|
|
|
{
|
|
|
|
|
BindingMessages.Reset();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-05 16:05:24 -04:00
|
|
|
void UMVVMBlueprintView::PostLoad()
|
|
|
|
|
{
|
|
|
|
|
Super::PostLoad();
|
|
|
|
|
|
|
|
|
|
for (FMVVMBlueprintViewBinding& Binding : Bindings)
|
|
|
|
|
{
|
|
|
|
|
if (!Binding.BindingId.IsValid())
|
|
|
|
|
{
|
|
|
|
|
Binding.BindingId = FGuid::NewGuid();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:21:11 -04:00
|
|
|
#if WITH_EDITOR
|
2022-10-06 20:08:12 -04:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
void UMVVMBlueprintView::PostEditUndo()
|
|
|
|
|
{
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}*/
|
|
|
|
|
|
2022-04-01 14:21:11 -04:00
|
|
|
void UMVVMBlueprintView::PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent)
|
|
|
|
|
{
|
|
|
|
|
Super::PostEditChangeProperty(PropertyChangedEvent);
|
|
|
|
|
if (PropertyChangedEvent.Property && PropertyChangedEvent.Property->GetFName() == GET_MEMBER_NAME_CHECKED(UMVVMBlueprintView, Bindings))
|
|
|
|
|
{
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
if (PropertyChangedEvent.Property && PropertyChangedEvent.Property->GetFName() == GET_MEMBER_NAME_CHECKED(UMVVMBlueprintView, AvailableViewModels))
|
|
|
|
|
{
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-20 18:54:53 -04:00
|
|
|
void UMVVMBlueprintView::PostEditChangeChainProperty(FPropertyChangedChainEvent& PropertyChainEvent)
|
|
|
|
|
{
|
|
|
|
|
Super::PostEditChangeChainProperty(PropertyChainEvent);
|
|
|
|
|
if (PropertyChainEvent.PropertyChain.Contains(UMVVMBlueprintView::StaticClass()->FindPropertyByName(GET_MEMBER_NAME_CHECKED(UMVVMBlueprintView, Bindings))))
|
|
|
|
|
{
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
if (PropertyChainEvent.PropertyChain.Contains(UMVVMBlueprintView::StaticClass()->FindPropertyByName(GET_MEMBER_NAME_CHECKED(UMVVMBlueprintView, AvailableViewModels))))
|
|
|
|
|
{
|
|
|
|
|
OnViewModelsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-26 11:58:53 -04:00
|
|
|
void UMVVMBlueprintView::AddAssetTags(TArray<FAssetRegistryTag>& OutTags) const
|
2022-12-08 09:06:03 -05:00
|
|
|
{
|
|
|
|
|
if (AvailableViewModels.Num() > 0)
|
|
|
|
|
{
|
|
|
|
|
TStringBuilder<512> Builder;
|
|
|
|
|
for (const FMVVMBlueprintViewModelContext& Context : AvailableViewModels)
|
|
|
|
|
{
|
|
|
|
|
if (Context.IsValid())
|
|
|
|
|
{
|
|
|
|
|
if (Builder.Len() > 0)
|
|
|
|
|
{
|
|
|
|
|
Builder << TEXT(',');
|
|
|
|
|
}
|
|
|
|
|
Builder << Context.GetViewModelClass()->GetPathName();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Builder.Len() > 0)
|
|
|
|
|
{
|
2023-05-26 11:58:53 -04:00
|
|
|
OutTags.Emplace(FName("Viewmodels"), Builder.ToString(), FAssetRegistryTag::TT_Hidden);
|
2022-12-08 09:06:03 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-01 14:21:11 -04:00
|
|
|
void UMVVMBlueprintView::WidgetRenamed(FName OldObjectName, FName NewObjectName)
|
|
|
|
|
{
|
|
|
|
|
bool bRenamed = false;
|
|
|
|
|
for (FMVVMBlueprintViewBinding& Binding : Bindings)
|
|
|
|
|
{
|
2023-03-09 08:41:28 -05:00
|
|
|
if (Binding.DestinationPath.GetWidgetName() == OldObjectName)
|
2022-04-01 14:21:11 -04:00
|
|
|
{
|
2023-03-09 08:41:28 -05:00
|
|
|
Binding.DestinationPath.SetWidgetName(NewObjectName);
|
2022-04-01 14:21:11 -04:00
|
|
|
bRenamed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bRenamed)
|
|
|
|
|
{
|
|
|
|
|
OnBindingsUpdated.Broadcast();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2022-09-28 01:06:15 -04:00
|
|
|
|