diff --git a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs index 8a9b2c59..861defa9 100644 --- a/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs +++ b/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs @@ -133,6 +133,7 @@ public static bool IsVisible(SerializedProperty property) if (showIfAttribute.EnumValue != null) { Enum value = GetEnumValue(target, showIfAttribute.Conditions[0]); + Debug.Log($"Expected: {showIfAttribute.EnumValue}, current: {value}"); if (value != null) { bool matched = value.GetType().GetCustomAttribute() == null @@ -198,26 +199,33 @@ internal static List GetConditionValues(object target, string[] conditions List conditionValues = new List(); foreach (var condition in conditions) { - FieldInfo conditionField = ReflectionUtility.GetField(target, condition); + + string fieldName = condition; + bool inverted = condition[0] == '!'; + if (inverted) + fieldName = condition.Substring(1); + + + FieldInfo conditionField = ReflectionUtility.GetField(target, fieldName); if (conditionField != null && conditionField.FieldType == typeof(bool)) { - conditionValues.Add((bool)conditionField.GetValue(target)); + conditionValues.Add((bool)conditionField.GetValue(target) != inverted); } - PropertyInfo conditionProperty = ReflectionUtility.GetProperty(target, condition); + PropertyInfo conditionProperty = ReflectionUtility.GetProperty(target, fieldName); if (conditionProperty != null && conditionProperty.PropertyType == typeof(bool)) { - conditionValues.Add((bool)conditionProperty.GetValue(target)); + conditionValues.Add((bool)conditionProperty.GetValue(target) != inverted); } - MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, condition); + MethodInfo conditionMethod = ReflectionUtility.GetMethod(target, fieldName); if (conditionMethod != null && conditionMethod.ReturnType == typeof(bool) && conditionMethod.GetParameters().Length == 0) { - conditionValues.Add((bool)conditionMethod.Invoke(target, null)); + conditionValues.Add((bool)conditionMethod.Invoke(target, null) != inverted); } } diff --git a/Assets/NaughtyAttributes/Scripts/Test/ButtonTest.cs b/Assets/NaughtyAttributes/Scripts/Test/ButtonTest.cs index 2594f9d8..f35d4857 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/ButtonTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/ButtonTest.cs @@ -6,6 +6,7 @@ namespace NaughtyAttributes.Test public class ButtonTest : MonoBehaviour { public int myInt; + public bool isZero => myInt == 0; [Button(enabledMode: EButtonEnableMode.Always)] private void IncrementMyInt() @@ -35,5 +36,13 @@ private IEnumerator IncrementMyIntCoroutine() yield return new WaitForSeconds(1.0f); } } + + + [Button("Set Increment to zero"),ShowIf("!isZero")] + public void SetIntToZero() + { + myInt = 0; + } + } } diff --git a/Assets/NaughtyAttributes/Scripts/Test/DisableIfTest.cs b/Assets/NaughtyAttributes/Scripts/Test/DisableIfTest.cs index c99d8d9c..e42d42b1 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/DisableIfTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/DisableIfTest.cs @@ -18,6 +18,10 @@ public class DisableIfTest : MonoBehaviour [ReorderableList] public int[] disableIfAny; + [DisableIf("!disable2")] + [ReorderableList] + public int[] disableIfNotDisable2; + [DisableIf("enum1", DisableIfEnum.Case0)] [ReorderableList] public int[] disableIfEnum; @@ -53,6 +57,10 @@ public class DisableIfNest1 [AllowNesting] // Because it's nested we need to explicitly allow nesting public int disableIfAny = 2; + [DisableIf(EConditionOperator.And, "Disable1", "!Disable2")] + [AllowNesting] + public int disableIfDisable1AndNotDisable2; + [DisableIf("Enum1", DisableIfEnum.Case1)] [AllowNesting] // Because it's nested we need to explicitly allow nesting public int disableIfEnum = 3; @@ -88,6 +96,10 @@ public class DisableIfNest2 [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer public Vector2 enableIfAny = new Vector2(0.25f, 0.75f); + [DisableIf(EConditionOperator.Or, "GetDisable1", "!GetDisable2")] + [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer + public Vector2 disableIfDisable1OrNotDisable2 = new Vector2(0.25f, 0.75f); + [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); diff --git a/Assets/NaughtyAttributes/Scripts/Test/EnableIfTest.cs b/Assets/NaughtyAttributes/Scripts/Test/EnableIfTest.cs index 655be3b9..4cbdb6d2 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/EnableIfTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/EnableIfTest.cs @@ -18,6 +18,10 @@ public class EnableIfTest : MonoBehaviour [ReorderableList] public int[] enableIfAny; + [EnableIf("!enable2")] + [ReorderableList] + public int[] enableIfNotEnable2; + [EnableIf("enum1", EnableIfEnum.Case0)] [ReorderableList] public int[] enableIfEnum; @@ -53,6 +57,11 @@ public class EnableIfNest1 [AllowNesting] // Because it's nested we need to explicitly allow nesting public int enableIfAny; + [EnableIf(EConditionOperator.And, "Enable1", "!Enable2")] + [AllowNesting] + public int enableIfEnable1AndNotEnable2; + + [EnableIf("Enum1", EnableIfEnum.Case1)] [AllowNesting] // Because it's nested we need to explicitly allow nesting public int enableIfEnum; @@ -88,6 +97,11 @@ public class EnableIfNest2 [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer public Vector2 enableIfAny = new Vector2(0.25f, 0.75f); + [EnableIf(EConditionOperator.Or, "GetEnable1", "!GetEnable2")] + [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer + public Vector2 enableIfEnable1OrNotEnable2 = new Vector2(0.25f, 0.75f); + + [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); diff --git a/Assets/NaughtyAttributes/Scripts/Test/HideIfTest.cs b/Assets/NaughtyAttributes/Scripts/Test/HideIfTest.cs index 4f096100..58873082 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/HideIfTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/HideIfTest.cs @@ -18,7 +18,11 @@ public class HideIfTest : MonoBehaviour [ReorderableList] public int[] hideIfAny; - [HideIf("enum1", HideIfEnum.Case0)] + [HideIf("!hide2")] + [ReorderableList] + public int[] hideIfNotHide2; + + [HideIf("enum1", ~HideIfEnum.Case2)] [ReorderableList] public int[] hideIfEnum; @@ -42,6 +46,7 @@ public class HideIfNest1 [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; } } @@ -53,6 +58,10 @@ public class HideIfNest1 [AllowNesting] // Because it's nested we need to explicitly allow nesting public int hideIfAny; + [HideIf(EConditionOperator.And, "Hide1", "!Hide2")] + [AllowNesting] + public int hideIfHide1AndNotHide2; + [HideIf("Enum1", HideIfEnum.Case1)] [AllowNesting] // Because it's nested we need to explicitly allow nesting public int hideIfEnum; @@ -88,6 +97,10 @@ public class HideIfNest2 [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer public Vector2 hideIfAny = new Vector2(0.25f, 0.75f); + [HideIf(EConditionOperator.Or, "GetHide1", "!GetHide2")] + [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer + public Vector2 hideIfHide1OrNotHide2 = new Vector2(0.25f, 0.75f); + [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); diff --git a/Assets/NaughtyAttributes/Scripts/Test/ShowIfTest.cs b/Assets/NaughtyAttributes/Scripts/Test/ShowIfTest.cs index 20c83215..e62ba498 100644 --- a/Assets/NaughtyAttributes/Scripts/Test/ShowIfTest.cs +++ b/Assets/NaughtyAttributes/Scripts/Test/ShowIfTest.cs @@ -18,6 +18,11 @@ public class ShowIfTest : MonoBehaviour [ReorderableList] public int[] showIfAny; + [ShowIf("!show2")] + [ReorderableList] + public int[] showIfNotShow2; + + [ShowIf("enum1", ShowIfEnum.Case0)] [ReorderableList] public int[] showIfEnum; @@ -53,6 +58,10 @@ public class ShowIfNest1 [AllowNesting] // Because it's nested we need to explicitly allow nesting public int showIfAny; + [ShowIf(EConditionOperator.And, "Show1", "!Show2")] + [AllowNesting] + public int showIfShow1AndNotShow2; + [ShowIf("Enum1", ShowIfEnum.Case1)] [AllowNesting] // Because it's nested we need to explicitly allow nesting public int showIfEnum; @@ -88,6 +97,10 @@ public class ShowIfNest2 [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer public Vector2 showIfAny = new Vector2(0.25f, 0.75f); + [ShowIf(EConditionOperator.Or, "GetShow1", "!GetShow2")] + [MinMaxSlider(0.0f, 1.0f)] // AllowNesting attribute is not needed, because the field is already marked with a custom naughty property drawer + public Vector2 showIfShow1OrNotShow2 = new Vector2(0.25f, 0.75f); + [ShowIf("GetEnum1", ShowIfEnum.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 showIfEnum = new Vector2(0.25f, 0.75f);