|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include "Algo/Reverse.h" |
| 4 | +#include "CoreMinimal.h" |
| 5 | +#include "Settings/FGUserSetting.h" |
| 6 | + |
| 7 | +#include "SMLIntValueSelector.generated.h" |
| 8 | + |
| 9 | +UCLASS(Blueprintable, |
| 10 | + DefaultToInstanced, |
| 11 | + EditInlineNew, |
| 12 | + AutoExpandCategories = "Value", |
| 13 | + meta = (DisplayName = "Integer Selector (Extended)")) |
| 14 | +class SML_API USMLIntValueSelector : public UFGUserSetting_IntSelector { |
| 15 | + GENERATED_BODY() |
| 16 | + |
| 17 | +#if WITH_EDITORONLY_DATA |
| 18 | + protected: |
| 19 | + /** |
| 20 | + * Auto populate the integer selection values from this enum class's values. |
| 21 | + */ |
| 22 | + UPROPERTY(BlueprintReadOnly, EditDefaultsOnly, Category = "Value") |
| 23 | + class UEnum* EnumClass; |
| 24 | + |
| 25 | + /** |
| 26 | + * If true, the enum values will be displayed in reverse order. |
| 27 | + */ |
| 28 | + UPROPERTY(BlueprintReadOnly, |
| 29 | + EditDefaultsOnly, |
| 30 | + Category = "Value", |
| 31 | + meta = (EditCondition = "EnumClass != nullptr", EditConditionHides)) |
| 32 | + bool bReverseEnumOrder; |
| 33 | + |
| 34 | + void PostEditChangeProperty(FPropertyChangedEvent& PropertyChangedEvent) { |
| 35 | + Super::PostEditChangeProperty(PropertyChangedEvent); |
| 36 | + if (PropertyChangedEvent.GetPropertyName() == GET_MEMBER_NAME_CHECKED(USMLIntValueSelector, EnumClass) || |
| 37 | + PropertyChangedEvent.GetPropertyName() == GET_MEMBER_NAME_CHECKED(USMLIntValueSelector, bReverseEnumOrder)) { |
| 38 | + PopulateIntegerSelectionValues(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private: |
| 43 | + void PopulateIntegerSelectionValues() { |
| 44 | + if (EnumClass == nullptr) { |
| 45 | + // Don't auto empty the values - the user may still want them. |
| 46 | + return; |
| 47 | + } |
| 48 | + IntegerSelectionValues.Empty(); |
| 49 | + int32 EnumCount = EnumClass->ContainsExistingMax() ? EnumClass->NumEnums() - 1 : EnumClass->NumEnums(); |
| 50 | + IntegerSelectionValues.Reserve(EnumCount); |
| 51 | + for (int32 EnumIndex = 0; EnumIndex < EnumCount; ++EnumIndex) { |
| 52 | + IntegerSelectionValues.Add(FIntegerSelection(EnumClass->GetDisplayNameTextByIndex(EnumIndex), EnumIndex)); |
| 53 | + } |
| 54 | + if (bReverseEnumOrder) { |
| 55 | + Algo::Reverse(IntegerSelectionValues); |
| 56 | + } |
| 57 | + } |
| 58 | +#endif |
| 59 | + |
| 60 | +#if WITH_EDITOR |
| 61 | + protected: |
| 62 | + bool CanEditChange(const FProperty* InProperty) const override { |
| 63 | + const bool ParentVal = Super::CanEditChange(InProperty); |
| 64 | + if (!ParentVal) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (InProperty->GetFName() == GET_MEMBER_NAME_CHECKED(USMLIntValueSelector, IntegerSelectionValues)) { |
| 69 | + // Allow editing when the enum class is null. |
| 70 | + return EnumClass == nullptr; |
| 71 | + } |
| 72 | + |
| 73 | + return true; |
| 74 | + } |
| 75 | +#endif |
| 76 | +}; |
0 commit comments