Skip to content

Commit 8e23635

Browse files
committed
Merge branch '#167-enum-support' of https://github.com/IF-ACT/NaughtyAttributes into IF-ACT-#167-enum-support
2 parents 2db5e79 + 67ae0d2 commit 8e23635

File tree

11 files changed

+191
-0
lines changed

11 files changed

+191
-0
lines changed

Scripts/Core/MetaAttributes/DisableIfAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ public DisableIfAttribute(EConditionOperator conditionOperator, params string[]
1616
{
1717
Inverted = true;
1818
}
19+
20+
public DisableIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
21+
{
22+
Inverted = true;
23+
}
1924
}
2025
}

Scripts/Core/MetaAttributes/EnableIfAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ public EnableIfAttribute(EConditionOperator conditionOperator, params string[] c
1616
{
1717
Inverted = false;
1818
}
19+
20+
public EnableIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
21+
{
22+
Inverted = false;
23+
}
1924
}
2025
}

Scripts/Core/MetaAttributes/EnableIfAttributeBase.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ public abstract class EnableIfAttributeBase : MetaAttribute
77
public string[] Conditions { get; private set; }
88
public EConditionOperator ConditionOperator { get; private set; }
99
public bool Inverted { get; protected set; }
10+
11+
/// <summary>
12+
/// If this not null, <see cref="Conditions"/>[0] is name of an enum variable.
13+
/// </summary>
14+
public Enum EnumValue { get; private set; }
1015

1116
public EnableIfAttributeBase(string condition)
1217
{
@@ -19,5 +24,12 @@ public EnableIfAttributeBase(EConditionOperator conditionOperator, params string
1924
ConditionOperator = conditionOperator;
2025
Conditions = conditions;
2126
}
27+
28+
public EnableIfAttributeBase(string enumName, Enum enumValue) : this(enumName)
29+
{
30+
if (enumName is null)
31+
throw new ArgumentNullException(nameof(enumValue), "This parameter must be an enum value.");
32+
EnumValue = enumValue;
33+
}
2234
}
2335
}

Scripts/Core/MetaAttributes/HideIfAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ public HideIfAttribute(EConditionOperator conditionOperator, params string[] con
1616
{
1717
Inverted = true;
1818
}
19+
20+
public HideIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
21+
{
22+
Inverted = true;
23+
}
1924
}
2025
}

Scripts/Core/MetaAttributes/ShowIfAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,10 @@ public ShowIfAttribute(EConditionOperator conditionOperator, params string[] con
1616
{
1717
Inverted = false;
1818
}
19+
20+
public ShowIfAttribute(string enumName, object enumValue) : base(enumName, enumValue as Enum)
21+
{
22+
Inverted = false;
23+
}
1924
}
2025
}

Scripts/Core/MetaAttributes/ShowIfAttributeBase.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ public class ShowIfAttributeBase : MetaAttribute
77
public string[] Conditions { get; private set; }
88
public EConditionOperator ConditionOperator { get; private set; }
99
public bool Inverted { get; protected set; }
10+
11+
/// <summary>
12+
/// If this not null, <see cref="Conditions"/>[0] is name of an enum variable.
13+
/// </summary>
14+
public Enum EnumValue { get; private set; }
1015

1116
public ShowIfAttributeBase(string condition)
1217
{
@@ -19,5 +24,12 @@ public ShowIfAttributeBase(EConditionOperator conditionOperator, params string[]
1924
ConditionOperator = conditionOperator;
2025
Conditions = conditions;
2126
}
27+
28+
public ShowIfAttributeBase(string enumName, Enum enumValue) : this(enumName)
29+
{
30+
if (enumName is null)
31+
throw new ArgumentNullException(nameof(enumValue), "This parameter must be an enum value.");
32+
EnumValue = enumValue;
33+
}
2234
}
2335
}

Scripts/Editor/Utility/PropertyUtility.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ public static bool IsEnabled(SerializedProperty property)
8484

8585
object target = GetTargetObjectWithProperty(property);
8686

87+
// deal with enum conditions
88+
if (enableIfAttribute.EnumValue != null)
89+
{
90+
Enum value = GetEnumValue(target, enableIfAttribute.Conditions[0]);
91+
if (value != null) return enableIfAttribute.EnumValue.Equals(value) != enableIfAttribute.Inverted;
92+
93+
string message = enableIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work";
94+
Debug.LogWarning(message, property.serializedObject.targetObject);
95+
return false;
96+
}
97+
8798
List<bool> conditionValues = GetConditionValues(target, enableIfAttribute.Conditions);
8899
if (conditionValues.Count > 0)
89100
{
@@ -109,6 +120,17 @@ public static bool IsVisible(SerializedProperty property)
109120

110121
object target = GetTargetObjectWithProperty(property);
111122

123+
// deal with enum conditions
124+
if (showIfAttribute.EnumValue != null)
125+
{
126+
Enum value = GetEnumValue(target, showIfAttribute.Conditions[0]);
127+
if (value != null) return showIfAttribute.EnumValue.Equals(value) != showIfAttribute.Inverted;
128+
129+
string message = showIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work";
130+
Debug.LogWarning(message, property.serializedObject.targetObject);
131+
return false;
132+
}
133+
112134
List<bool> conditionValues = GetConditionValues(target, showIfAttribute.Conditions);
113135
if (conditionValues.Count > 0)
114136
{
@@ -124,6 +146,29 @@ public static bool IsVisible(SerializedProperty property)
124146
}
125147
}
126148

149+
/// <summary>
150+
/// Gets an enum value from reflection.
151+
/// </summary>
152+
/// <param name="target">The target object.</param>
153+
/// <param name="enumName">Name of a field, property, or method that returns an enum.</param>
154+
/// <returns>Null if can't find an enum value.</returns>
155+
internal static Enum GetEnumValue(object target, string enumName)
156+
{
157+
FieldInfo enumField = ReflectionUtility.GetField(target, enumName);
158+
if (enumField != null && enumField.FieldType.IsSubclassOf(typeof(Enum)))
159+
return (Enum) enumField.GetValue(target);
160+
161+
PropertyInfo enumProperty = ReflectionUtility.GetProperty(target, enumName);
162+
if (enumProperty != null && enumProperty.PropertyType.IsSubclassOf(typeof(Enum)))
163+
return (Enum) enumProperty.GetValue(target);
164+
165+
MethodInfo enumMethod = ReflectionUtility.GetMethod(target, enumName);
166+
if (enumMethod != null && enumMethod.ReturnType.IsSubclassOf(typeof(Enum)))
167+
return (Enum) enumMethod.Invoke(target, null);
168+
169+
return null;
170+
}
171+
127172
internal static List<bool> GetConditionValues(object target, string[] conditions)
128173
{
129174
List<bool> conditionValues = new List<bool>();

Scripts/Test/DisableIfTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class DisableIfTest : MonoBehaviour
66
{
77
public bool disable1;
88
public bool disable2;
9+
public DisableIfEnum enum1;
910

1011
[DisableIf(EConditionOperator.And, "disable1", "disable2")]
1112
[ReorderableList]
@@ -15,6 +16,10 @@ public class DisableIfTest : MonoBehaviour
1516
[ReorderableList]
1617
public int[] disableIfAny;
1718

19+
[DisableIf("enum1", DisableIfEnum.Case0)]
20+
[ReorderableList]
21+
public int[] disableIfEnum;
22+
1823
public DisableIfNest1 nest1;
1924
}
2025

@@ -23,8 +28,10 @@ public class DisableIfNest1
2328
{
2429
public bool disable1;
2530
public bool disable2;
31+
public DisableIfEnum enum1;
2632
public bool Disable1 { get { return disable1; } }
2733
public bool Disable2 { get { return disable2; } }
34+
public DisableIfEnum Enum1 { get { return enum1; } }
2835

2936
[DisableIf(EConditionOperator.And, "Disable1", "Disable2")]
3037
[AllowNesting] // Because it's nested we need to explicitly allow nesting
@@ -34,6 +41,10 @@ public class DisableIfNest1
3441
[AllowNesting] // Because it's nested we need to explicitly allow nesting
3542
public int disableIfAny = 2;
3643

44+
[DisableIf("Enum1", DisableIfEnum.Case1)]
45+
[AllowNesting]
46+
public int disableIfEnum = 3;
47+
3748
public DisableIfNest2 nest2;
3849
}
3950

@@ -42,8 +53,10 @@ public class DisableIfNest2
4253
{
4354
public bool disable1;
4455
public bool disable2;
56+
public DisableIfEnum enum1;
4557
public bool GetDisable1() { return disable1; }
4658
public bool GetDisable2() { return disable2; }
59+
public DisableIfEnum GetEnum1() { return enum1; }
4760

4861
[DisableIf(EConditionOperator.And, "GetDisable1", "GetDisable2")]
4962
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
@@ -52,5 +65,18 @@ public class DisableIfNest2
5265
[DisableIf(EConditionOperator.Or, "GetDisable1", "GetDisable2")]
5366
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
5467
public Vector2 enableIfAny = new Vector2(0.25f, 0.75f);
68+
69+
[DisableIf("GetEnum1", DisableIfEnum.Case2)]
70+
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
71+
public Vector2 enableIfEnum = new Vector2(0.25f, 0.75f);
72+
}
73+
74+
[System.Serializable]
75+
public enum DisableIfEnum
76+
{
77+
Case0,
78+
Case1,
79+
Case2,
80+
Case3
5581
}
5682
}

Scripts/Test/EnableIfTest.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class EnableIfTest : MonoBehaviour
66
{
77
public bool enable1;
88
public bool enable2;
9+
public EnableIfEnum enum1;
910

1011
[EnableIf(EConditionOperator.And, "enable1", "enable2")]
1112
[ReorderableList]
@@ -15,6 +16,10 @@ public class EnableIfTest : MonoBehaviour
1516
[ReorderableList]
1617
public int[] enableIfAny;
1718

19+
[EnableIf("enum1", EnableIfEnum.Case0)]
20+
[ReorderableList]
21+
public int[] enableIfEnum;
22+
1823
public EnableIfNest1 nest1;
1924
}
2025

@@ -23,8 +28,10 @@ public class EnableIfNest1
2328
{
2429
public bool enable1;
2530
public bool enable2;
31+
public EnableIfEnum enum1;
2632
public bool Enable1 { get { return enable1; } }
2733
public bool Enable2 { get { return enable2; } }
34+
public EnableIfEnum Enum1 { get { return enum1; } }
2835

2936
[EnableIf(EConditionOperator.And, "Enable1", "Enable2")]
3037
[AllowNesting] // Because it's nested we need to explicitly allow nesting
@@ -34,6 +41,10 @@ public class EnableIfNest1
3441
[AllowNesting] // Because it's nested we need to explicitly allow nesting
3542
public int enableIfAny;
3643

44+
[EnableIf("Enum1", EnableIfEnum.Case1)]
45+
[AllowNesting]
46+
public int enableIfEnum;
47+
3748
public EnableIfNest2 nest2;
3849
}
3950

@@ -42,8 +53,10 @@ public class EnableIfNest2
4253
{
4354
public bool enable1;
4455
public bool enable2;
56+
public EnableIfEnum enum1;
4557
public bool GetEnable1() { return enable1; }
4658
public bool GetEnable2() { return enable2; }
59+
public EnableIfEnum GetEnum1() { return enum1; }
4760

4861
[EnableIf(EConditionOperator.And, "GetEnable1", "GetEnable2")]
4962
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
@@ -52,5 +65,18 @@ public class EnableIfNest2
5265
[EnableIf(EConditionOperator.Or, "GetEnable1", "GetEnable2")]
5366
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
5467
public Vector2 enableIfAny = new Vector2(0.25f, 0.75f);
68+
69+
[EnableIf("GetEnum1", EnableIfEnum.Case2)]
70+
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
71+
public Vector2 enableIfEnum = new Vector2(0.25f, 0.75f);
72+
}
73+
74+
[System.Serializable]
75+
public enum EnableIfEnum
76+
{
77+
Case0,
78+
Case1,
79+
Case2,
80+
Case3,
5581
}
5682
}

Scripts/Test/HideIfTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ public class HideIfTest : MonoBehaviour
66
{
77
public bool hide1;
88
public bool hide2;
9+
public HideIfEnum enum1;
910

1011
[HideIf(EConditionOperator.And, "hide1", "hide2")]
1112
[ReorderableList]
@@ -14,6 +15,10 @@ public class HideIfTest : MonoBehaviour
1415
[HideIf(EConditionOperator.Or, "hide1", "hide2")]
1516
[ReorderableList]
1617
public int[] hideIfAny;
18+
19+
[HideIf("enum1", HideIfEnum.Case0)]
20+
[ReorderableList]
21+
public int[] hideIfEnum;
1722

1823
public HideIfNest1 nest1;
1924
}
@@ -23,8 +28,10 @@ public class HideIfNest1
2328
{
2429
public bool hide1;
2530
public bool hide2;
31+
public HideIfEnum enum1;
2632
public bool Hide1 { get { return hide1; } }
2733
public bool Hide2 { get { return hide2; } }
34+
public HideIfEnum Enum1 { get { return enum1; } }
2835

2936
[HideIf(EConditionOperator.And, "Hide1", "Hide2")]
3037
[AllowNesting] // Because it's nested we need to explicitly allow nesting
@@ -33,6 +40,10 @@ public class HideIfNest1
3340
[HideIf(EConditionOperator.Or, "Hide1", "Hide2")]
3441
[AllowNesting] // Because it's nested we need to explicitly allow nesting
3542
public int hideIfAny;
43+
44+
[HideIf("enum1", HideIfEnum.Case1)]
45+
[AllowNesting]
46+
public int hideIfEnum;
3647

3748
public HideIfNest2 nest2;
3849
}
@@ -42,8 +53,10 @@ public class HideIfNest2
4253
{
4354
public bool hide1;
4455
public bool hide2;
56+
public HideIfEnum enum1;
4557
public bool GetHide1() { return hide1; }
4658
public bool GetHide2() { return hide2; }
59+
public HideIfEnum GetEnum1() { return enum1; }
4760

4861
[HideIf(EConditionOperator.And, "GetHide1", "GetHide2")]
4962
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
@@ -52,5 +65,17 @@ public class HideIfNest2
5265
[HideIf(EConditionOperator.Or, "GetHide1", "GetHide2")]
5366
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
5467
public Vector2 hideIfAny = new Vector2(0.25f, 0.75f);
68+
69+
[HideIf("GetEnum1", HideIfEnum.Case2)]
70+
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
71+
public Vector2 hideIfEnum = new Vector2(0.25f, 0.75f);
72+
}
73+
74+
public enum HideIfEnum
75+
{
76+
Case0,
77+
Case1,
78+
Case2,
79+
Case3
5580
}
5681
}

0 commit comments

Comments
 (0)