/ Source / ReasonablePlanningAIEditor / Private / ComposerBehaviorCustom.cpp
ComposerBehaviorCustom.cpp
 1  // Copyright (C) 2025 Radaway Software LLC. All Rights Reserved.
 2  
 3  #include "ComposerBehaviorCustom.h"
 4  #include "PropertyEditing.h"
 5  #include "Core/RpaiTypes.h"
 6  #include "PropertyCustomizationHelpers.h"
 7  #include "Slate/SStateTypePropertyMultibind.h"
 8  #include "AIController.h"
 9  
10  TSharedRef<IDetailCustomization> ComposerBehaviorCustom::MakeInstance()
11  {
12     return MakeShareable(new ComposerBehaviorCustom());
13  }
14  
15  void ComposerBehaviorCustom::CustomizeDetails(IDetailLayoutBuilder& DetailBuilder)
16  {
17     IDetailCategoryBuilder& RpaiCategory = DetailBuilder.EditCategory("Rpai", FText::GetEmpty(), ECategoryPriority::Important);
18  
19     auto BindingsProperty = DetailBuilder.GetProperty("Bindings");
20     if (BindingsProperty->IsValidHandle())
21     {
22        DetailBuilder.HideProperty(BindingsProperty);
23        auto SourceProperties = BindingsProperty->GetChildHandle(GET_MEMBER_NAME_CHECKED(FRpaiStateBinding, SourceBindings));
24        auto TargetProperties = BindingsProperty->GetChildHandle(GET_MEMBER_NAME_CHECKED(FRpaiStateBinding, TargetBindings));
25  
26        check(SourceProperties.IsValid() && TargetProperties.IsValid());
27        check(SourceProperties->IsValidHandle() && TargetProperties->IsValidHandle());
28        
29        auto SourcePropertiesArray = SourceProperties->AsArray();
30        auto TargetPropertiesArray = TargetProperties->AsArray();
31  
32        check(SourcePropertiesArray.IsValid() && TargetPropertiesArray.IsValid());
33  
34        IDetailGroup& BindingGroup = RpaiCategory.AddGroup("Rpai.Bindings", NSLOCTEXT("Rpai", "RpaiBehaviorCustomBindings", "Bindings"));
35        BindingGroup.AddWidgetRow()
36           .WholeRowWidget
37           [
38              SNew(SListView<TSharedPtr<FText>>)
39                 .ListItemsSource(&Warnings)
40                 .OnGenerateRow(SListView<TSharedPtr<FText>>::FOnGenerateRow::CreateLambda([](TSharedPtr<FText> Item, const TSharedRef<STableViewBase>& Table) -> TSharedRef<ITableRow> {
41                 return SNew(STableRow<TSharedPtr<FText>>, Table)
42                    [
43                       SNew(STextBlock)
44                          .Text(*Item)
45                          .ColorAndOpacity(FSlateColor(EStyleColor::AccentYellow))
46                    ];
47                 }))
48           ];
49  
50        BindingGroup.AddPropertyRow(SourceProperties.ToSharedRef());
51        BindingGroup.AddPropertyRow(TargetProperties.ToSharedRef());
52  
53        OnSourcePropertyElementsChanged = SourcePropertiesArray->SetOnNumElementsChanged(FSimpleDelegate::CreateSP(this, &ComposerBehaviorCustom::OnBindingPropertyElementsChanged, SourcePropertiesArray, TargetPropertiesArray));
54        OnTargetPropertyElementsChanged = SourcePropertiesArray->SetOnNumElementsChanged(FSimpleDelegate::CreateSP(this, &ComposerBehaviorCustom::OnBindingPropertyElementsChanged, TargetPropertiesArray, SourcePropertiesArray));
55  
56        // The number of elements in each need to be equal as the binding is N (Source) M (Properties) mapped to N (Target) M (Properties).
57        uint32 SourceElementCount = 0U;
58        uint32 TargetElementCount = 0U;
59        if (SourcePropertiesArray->GetNumElements(SourceElementCount) == FPropertyAccess::Success && TargetPropertiesArray->GetNumElements(TargetElementCount) == FPropertyAccess::Success)
60        {
61           if (SourceElementCount != TargetElementCount)
62           {
63              TSharedPtr<FText> Msg(new FText(NSLOCTEXT("Rpai", "CountWarning", "Source and Target Bindings should be have same number of elements.")));
64              Warnings.Add(Msg);
65           }
66  
67           for (uint32 Idx = 0U; Idx < SourceElementCount; ++Idx)
68           {
69              auto Property = SourcePropertiesArray->GetElement(Idx);
70              // TODO: Set filter classes
71           }
72        }
73     }
74  }
75  
76  void ComposerBehaviorCustom::OnBindingPropertyElementsChanged(TSharedPtr<IPropertyHandleArray> ChangedArray, TSharedPtr<IPropertyHandleArray> CoupledArray)
77  {
78     uint32 SourceElementCount = 0U;
79     uint32 TargetElementCount = 0U;
80     if (ChangedArray->GetNumElements(SourceElementCount) == FPropertyAccess::Success && CoupledArray->GetNumElements(TargetElementCount) == FPropertyAccess::Success)
81     {
82        if (SourceElementCount != TargetElementCount)
83        {
84           TSharedPtr<FText> Msg(new FText(NSLOCTEXT("Rpai", "CountWarning", "Source and Target Bindings should be have same number of elements.")));
85           Warnings.Add(Msg);
86        }
87     }
88  }