Skip to content

Commit

Permalink
Merge branch 'IF-ACT-#217-support-enum-flags' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrizov committed May 2, 2021
2 parents e4edd04 + b4d6423 commit 7a2d8e1
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 4 deletions.
12 changes: 10 additions & 2 deletions Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public static bool IsEnabled(SerializedProperty property)
Enum value = GetEnumValue(target, enableIfAttribute.Conditions[0]);
if (value != null)
{
return enableIfAttribute.EnumValue.Equals(value) != enableIfAttribute.Inverted;
bool matched = value.GetType().GetCustomAttribute<FlagsAttribute>() == null
? enableIfAttribute.EnumValue.Equals(value)
: value.HasFlag(enableIfAttribute.EnumValue);

return matched != enableIfAttribute.Inverted;
}

string message = enableIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work";
Expand Down Expand Up @@ -131,7 +135,11 @@ public static bool IsVisible(SerializedProperty property)
Enum value = GetEnumValue(target, showIfAttribute.Conditions[0]);
if (value != null)
{
return showIfAttribute.EnumValue.Equals(value) != showIfAttribute.Inverted;
bool matched = value.GetType().GetCustomAttribute<FlagsAttribute>() == null
? showIfAttribute.EnumValue.Equals(value)
: value.HasFlag(showIfAttribute.EnumValue);

return matched != showIfAttribute.Inverted;
}

string message = showIfAttribute.GetType().Name + " needs a valid enum field, property or method name to work";
Expand Down
41 changes: 40 additions & 1 deletion Assets/NaughtyAttributes/Scripts/Test/DisableIfTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using System;
using UnityEngine;

namespace NaughtyAttributes.Test
{
Expand All @@ -7,6 +8,7 @@ public class DisableIfTest : MonoBehaviour
public bool disable1;
public bool disable2;
public DisableIfEnum enum1;
[EnumFlags] public DisableIfEnumFlag enum2;

[DisableIf(EConditionOperator.And, "disable1", "disable2")]
[ReorderableList]
Expand All @@ -20,6 +22,14 @@ public class DisableIfTest : MonoBehaviour
[ReorderableList]
public int[] disableIfEnum;

[DisableIf("enum2", DisableIfEnumFlag.Flag0)]
[ReorderableList]
public int[] disableIfEnumFlag;

[DisableIf("enum2", DisableIfEnumFlag.Flag0 | DisableIfEnumFlag.Flag1)]
[ReorderableList]
public int[] disableIfEnumFlagMulti;

public DisableIfNest1 nest1;
}

Expand All @@ -29,9 +39,11 @@ public class DisableIfNest1
public bool disable1;
public bool disable2;
public DisableIfEnum enum1;
[EnumFlags] public DisableIfEnumFlag enum2;
public bool Disable1 { get { return disable1; } }
public bool Disable2 { get { return disable2; } }
public DisableIfEnum Enum1 { get { return enum1; } }
public DisableIfEnumFlag Enum2 { get { return enum2; } }

[DisableIf(EConditionOperator.And, "Disable1", "Disable2")]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
Expand All @@ -45,6 +57,14 @@ public class DisableIfNest1
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int disableIfEnum = 3;

[DisableIf("Enum2", DisableIfEnumFlag.Flag0)]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int disableIfEnumFlag;

[DisableIf("Enum2", DisableIfEnumFlag.Flag0 | DisableIfEnumFlag.Flag1)]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int disableIfEnumFlagMulti;

public DisableIfNest2 nest2;
}

Expand All @@ -54,9 +74,11 @@ public class DisableIfNest2
public bool disable1;
public bool disable2;
public DisableIfEnum enum1;
[EnumFlags] public DisableIfEnumFlag enum2;
public bool GetDisable1() { return disable1; }
public bool GetDisable2() { return disable2; }
public DisableIfEnum GetEnum1() { return enum1; }
public DisableIfEnumFlag GetEnum2() { return enum2; }

[DisableIf(EConditionOperator.And, "GetDisable1", "GetDisable2")]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
Expand All @@ -69,6 +91,14 @@ public class DisableIfNest2
[DisableIf("GetEnum1", DisableIfEnum.Case2)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 enableIfEnum = new Vector2(0.25f, 0.75f);

[DisableIf("GetEnum2", DisableIfEnumFlag.Flag0)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 disableIfEnumFlag;

[DisableIf("GetEnum2", DisableIfEnumFlag.Flag0 | DisableIfEnumFlag.Flag1)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 disableIfEnumFlagMulti;
}

[System.Serializable]
Expand All @@ -78,4 +108,13 @@ public enum DisableIfEnum
Case1,
Case2
}

[Flags]
public enum DisableIfEnumFlag
{
Flag0 = 1,
Flag1 = 2,
Flag2 = 4,
Flag3 = 8
}
}
39 changes: 39 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/EnableIfTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using UnityEngine;

namespace NaughtyAttributes.Test
Expand All @@ -7,6 +8,7 @@ public class EnableIfTest : MonoBehaviour
public bool enable1;
public bool enable2;
public EnableIfEnum enum1;
[EnumFlags] public EnableIfEnumFlag enum2;

[EnableIf(EConditionOperator.And, "enable1", "enable2")]
[ReorderableList]
Expand All @@ -20,6 +22,14 @@ public class EnableIfTest : MonoBehaviour
[ReorderableList]
public int[] enableIfEnum;

[EnableIf("enum2", EnableIfEnumFlag.Flag0)]
[ReorderableList]
public int[] enableIfEnumFlag;

[EnableIf("enum2", EnableIfEnumFlag.Flag0 | EnableIfEnumFlag.Flag1)]
[ReorderableList]
public int[] enableIfEnumFlagMulti;

public EnableIfNest1 nest1;
}

Expand All @@ -29,9 +39,11 @@ public class EnableIfNest1
public bool enable1;
public bool enable2;
public EnableIfEnum enum1;
[EnumFlags] public EnableIfEnumFlag enum2;
public bool Enable1 { get { return enable1; } }
public bool Enable2 { get { return enable2; } }
public EnableIfEnum Enum1 { get { return enum1; } }
public EnableIfEnumFlag Enum2 { get { return enum2; } }

[EnableIf(EConditionOperator.And, "Enable1", "Enable2")]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
Expand All @@ -45,6 +57,14 @@ public class EnableIfNest1
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int enableIfEnum;

[EnableIf("Enum2", EnableIfEnumFlag.Flag0)]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int enableIfEnumFlag;

[EnableIf("Enum2", EnableIfEnumFlag.Flag0 | EnableIfEnumFlag.Flag1)]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int enableIfEnumFlagMulti;

public EnableIfNest2 nest2;
}

Expand All @@ -54,9 +74,11 @@ public class EnableIfNest2
public bool enable1;
public bool enable2;
public EnableIfEnum enum1;
[EnumFlags] public EnableIfEnumFlag enum2;
public bool GetEnable1() { return enable1; }
public bool GetEnable2() { return enable2; }
public EnableIfEnum GetEnum1() { return enum1; }
public EnableIfEnumFlag GetEnum2() { return enum2; }

[EnableIf(EConditionOperator.And, "GetEnable1", "GetEnable2")]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
Expand All @@ -69,6 +91,14 @@ public class EnableIfNest2
[EnableIf("GetEnum1", EnableIfEnum.Case2)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 enableIfEnum = new Vector2(0.25f, 0.75f);

[EnableIf("GetEnum2", EnableIfEnumFlag.Flag0)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 enableIfEnumFlag;

[EnableIf("GetEnum2", EnableIfEnumFlag.Flag0 | EnableIfEnumFlag.Flag1)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 enableIfEnumFlagMulti;
}

[System.Serializable]
Expand All @@ -78,4 +108,13 @@ public enum EnableIfEnum
Case1,
Case2
}

[Flags]
public enum EnableIfEnumFlag
{
Flag0 = 1,
Flag1 = 2,
Flag2 = 4,
Flag3 = 8
}
}
41 changes: 40 additions & 1 deletion Assets/NaughtyAttributes/Scripts/Test/HideIfTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using System;
using UnityEngine;

namespace NaughtyAttributes.Test
{
Expand All @@ -7,6 +8,7 @@ public class HideIfTest : MonoBehaviour
public bool hide1;
public bool hide2;
public HideIfEnum enum1;
[EnumFlags] public HideIfEnumFlag enum2;

[HideIf(EConditionOperator.And, "hide1", "hide2")]
[ReorderableList]
Expand All @@ -20,6 +22,14 @@ public class HideIfTest : MonoBehaviour
[ReorderableList]
public int[] hideIfEnum;

[HideIf("enum2", HideIfEnumFlag.Flag0)]
[ReorderableList]
public int[] hideIfEnumFlag;

[HideIf("enum2", HideIfEnumFlag.Flag0 | HideIfEnumFlag.Flag1)]
[ReorderableList]
public int[] hideIfEnumFlagMulti;

public HideIfNest1 nest1;
}

Expand All @@ -29,9 +39,11 @@ public class HideIfNest1
public bool hide1;
public bool hide2;
public HideIfEnum enum1;
[EnumFlags] public HideIfEnumFlag enum2;
public bool Hide1 { get { return hide1; } }
public bool Hide2 { get { return hide2; } }
public HideIfEnum Enum1 { get { return enum1; } }
public HideIfEnumFlag Enum2 { get { return enum2; } }

[HideIf(EConditionOperator.And, "Hide1", "Hide2")]
[AllowNesting] // Because it's nested we need to explicitly allow nesting
Expand All @@ -45,6 +57,14 @@ public class HideIfNest1
[AllowNesting] // Because it's nested we need to explicitly allow nesting
public int hideIfEnum;

[HideIf("Enum2", HideIfEnumFlag.Flag0)]
[AllowNesting]
public int hideIfEnumFlag;

[HideIf("Enum2", HideIfEnumFlag.Flag0 | HideIfEnumFlag.Flag1)]
[AllowNesting]
public int hideIfEnumFlagMulti;

public HideIfNest2 nest2;
}

Expand All @@ -54,9 +74,11 @@ public class HideIfNest2
public bool hide1;
public bool hide2;
public HideIfEnum enum1;
[EnumFlags] public HideIfEnumFlag enum2;
public bool GetHide1() { return hide1; }
public bool GetHide2() { return hide2; }
public HideIfEnum GetEnum1() { return enum1; }
public HideIfEnumFlag GetEnum2() { return enum2; }

[HideIf(EConditionOperator.And, "GetHide1", "GetHide2")]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
Expand All @@ -69,6 +91,14 @@ public class HideIfNest2
[HideIf("GetEnum1", HideIfEnum.Case2)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 hideIfEnum = new Vector2(0.25f, 0.75f);

[HideIf("GetEnum2", HideIfEnumFlag.Flag0)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 hideIfEnumFlag;

[HideIf("GetEnum2", HideIfEnumFlag.Flag0 | HideIfEnumFlag.Flag1)]
[MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer
public Vector2 hideIfEnumFlagMulti;
}

public enum HideIfEnum
Expand All @@ -77,4 +107,13 @@ public enum HideIfEnum
Case1,
Case2
}

[Flags]
public enum HideIfEnumFlag
{
Flag0 = 1,
Flag1 = 2,
Flag2 = 4,
Flag3 = 8
}
}

0 comments on commit 7a2d8e1

Please sign in to comment.