Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Not Operator '!' support to EnableIf, DisableIf, ShowIf & HideIf #248

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<FlagsAttribute>() == null
Expand Down Expand Up @@ -198,26 +199,33 @@ internal static List<bool> GetConditionValues(object target, string[] conditions
List<bool> conditionValues = new List<bool>();
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);
}
}

Expand Down
9 changes: 9 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/ButtonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}

}
}
12 changes: 12 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/DisableIfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/EnableIfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
15 changes: 14 additions & 1 deletion Assets/NaughtyAttributes/Scripts/Test/HideIfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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; } }

Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 13 additions & 0 deletions Assets/NaughtyAttributes/Scripts/Test/ShowIfTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down