Skip to content

Commit d6098b3

Browse files
feat(sml): create an extended UserSetting integer ValueSelector
The new `USMLIntValueSelector` extends the existing `UFGUserSetting_IntSelector` adding the ability for users to specify an enum to auto populate the values.
1 parent 4562fdc commit d6098b3

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

Mods/SML/Config/AccessTransformers.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Friend=(Class="UFGUserSetting", FriendClass="USMLSessionSetting")
55
Friend=(Class="UFGUnlockScannableObject", FriendClass="UModContentRegistry")
66
Friend=(Class="AFGSchematicManager", FriendClass="UModContentRegistry")
77
Friend=(Class="UFGResearchTreeProgressionDependency", FriendClass="UModContentRegistry")
8+
Friend=(Class="UFGUserSetting_IntSelector", FriendClass="USMLIntValueSelector")
89
; CSS populates this at build time (presumably with CachePlaylistSongDurations) but we don't have an implementation - make editable so it can be set manually. See GH Issue #372
910
EditAnywhere=(Class="/Script/FactoryGame.SongData", Property="CachedMaximumSongDuration")
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)