I have to change the children list every time the enum variable property is changed from widget editor.
None of the functions I've tried so far seem to have worked (virtual void OnEndEditByDesigner(); virtual void PostEditChangeProperty(struct FPropertyChangedEvent & PropertyChangedEvent)).
SynchronizeProperties docs state that
It can also be called by the editor to update modified state, so ensure all initialization to a widgets properties are performed here, or the property and visual state may become unsynced.
So I'm not entirely sure that it is the right function to use/overwrite (as it only "can" be called by the editor to update modified state, not will be called on property editor edit)
Here is some sample code to show an example of what I've been trying to do so far.
Sample code:
SampleButton.h
/* Includes */
#include "CoreMinimal.h"
#include "UObject/Object.h"
#include "Components/Button.h"
#include "Components/Image.h"
#include "Components/CanvasPanel.h"
#include "Components/ButtonSlot.h"
#include "Widgets/SOverlay.h"
#include "Components/Overlay.h"
#include "Components/TextBlock.h"
#include "Styling/SlateBrush.h"
#include "UUIW_MasterButton.generated.h"
//
UENUM(BlueprintType)
enum class EContentType : uint8
{
WithIcon,
WithText,
}
class SAMPLE_API SampleButton : public UButton
{
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintSetter=SetType, meta = (MakeEditWidget = ""))
EContentType ContentType = EContentType::WithIcon;
UPROPERTY(meta = (EditCondition = "ContentType == EContentType::WithIcon || ContentType == EContentType::WithTextAndIcon", EditConditionHides))
UImage* Icon;
UPROPERTY(meta = (EditCondition = "ContentType == EContentType::WithText || ContentType == EContentType::WithTextAndIcon", EditConditionHides))
UTextBlock* TextBlock;
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;
UOverlay* Overlay;
virtual void SynchronizeProperties();
}
SampleButton.cpp
#include "UI/SampleButton.h"
TSharedRef<SWidget> SampleButton::RebuildWidget()
{
Overlay = NewObject<UOverlay>();
auto ButtonRef = Super::RebuildWidget();
AddChild(Overlay);
Icon = NewObject<UImage>();
Icon->SetDisplayLabel(TEXT("Icon"));
TextBlock = NewObject<UTextBlock>();
TextBlock->SetDisplayLabel(TEXT("Text"));
switch (ContentType)
{
case EContentType::WithIcon:
Overlay->AddChild(Icon);
break;
case EContentType::WithText:
Overlay->AddChild(TextBlock);
break;
case EContentType::WithTextAndIcon:
Overlay->AddChild(Icon);
Overlay->AddChild(TextBlock);
break;
}
if ( GetChildrenCount() > 0 )
{
Cast<UButtonSlot>(GetContentSlot())->BuildSlot(MyButton.ToSharedRef());
}
return ButtonRef;
}
void UUUIW_MasterButton::SynchronizeProperties()
{
Super::SynchronizeProperties();
Overlay->ClearChildren();
switch (ContentType)
{
case EContentType::WithIcon:
Overlay->AddChild(Icon);
break;
case EContentType::WithText:
Overlay->AddChild(TextBlock);
break;
case EContentType::WithTextAndIcon:
Overlay->AddChild(Icon);
Overlay->AddChild(TextBlock);
break;
}
}
```