From 9e062fabf4efbaee976b12257b60ec1c142ce628 Mon Sep 17 00:00:00 2001 From: petchanliu Date: Mon, 16 May 2022 21:07:40 +0800 Subject: [PATCH 1/2] Comparison suport multiple conditions. --- .../Condition/ComparisonAttributeDrawer.cs | 80 ++++++++++++++++--- .../ComparisonAttribute.cs | 21 ++++- .../ConditionAttributes/DisableIfAttribute.cs | 4 + .../ConditionAttributes/EnableIfAttribute.cs | 4 + .../HideDisabledIfAttribute.cs | 4 + .../ConditionAttributes/HideIfAttribute.cs | 4 + .../ShowDisabledIfAttribute.cs | 4 + .../ConditionAttributes/ShowIfAttribute.cs | 4 + .../ShowWarningIfAttribute.cs | 6 ++ 9 files changed, 117 insertions(+), 14 deletions(-) diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs index 8b31cc6d..08e36551 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs @@ -1,4 +1,5 @@ -using UnityEditor; +using System; +using UnityEditor; using UnityEngine; namespace Toolbox.Editor.Drawers @@ -7,24 +8,79 @@ public abstract class ComparisonAttributeDrawer : ToolboxConditionDrawer w { protected override PropertyCondition OnGuiValidateSafe(SerializedProperty property, T attribute) { - var sourceHandle = attribute.SourceHandle; - if (!ValueExtractionHelper.TryGetValue(sourceHandle, property, out var value, out var hasMixedValues)) + if (attribute.SourceHandles == null || attribute.ValueToMatches == null) { - ToolboxEditorLog.MemberNotFoundWarning(attribute, property, sourceHandle); return PropertyCondition.Valid; } - - var comparison = (ValueComparisonMethod)attribute.Comparison; - var targetValue = attribute.ValueToMatch; - if (!ValueComparisonHelper.TryCompare(value, targetValue, comparison, out var result)) + + var validLength = Math.Min(attribute.SourceHandles.Length, attribute.ValueToMatches.Length); + if (validLength == 0) { - ToolboxEditorLog.AttributeUsageWarning(attribute, property, - string.Format("Invalid comparison input: source:{0}, target:{1}, method:{2}.", - value?.GetType(), targetValue?.GetType(), comparison)); return PropertyCondition.Valid; } - return OnComparisonResult(hasMixedValues ? false : result); + PropertyCondition[] resultItems = new PropertyCondition[validLength]; + for (int i = 0; i < validLength; i++) + { + var sourceHandle = attribute.SourceHandles[i]; + if (!ValueExtractionHelper.TryGetValue(sourceHandle, property, out var value, out var hasMixedValues)) + { + ToolboxEditorLog.MemberNotFoundWarning(attribute, property, sourceHandle); + resultItems[i] = PropertyCondition.Valid; + continue; + } + + var comparison = (ValueComparisonMethod)attribute.Comparison; + if (attribute.IndividualComparisons != null && attribute.IndividualComparisons.Length > i) + { + comparison = (ValueComparisonMethod)attribute.IndividualComparisons[i]; + } + + var targetValue = attribute.ValueToMatches[i]; + if (!ValueComparisonHelper.TryCompare(value, targetValue, comparison, out var result)) + { + ToolboxEditorLog.AttributeUsageWarning(attribute, property, + string.Format("Invalid comparison input: source:{0}, target:{1}, method:{2}.", + value?.GetType(), targetValue?.GetType(), comparison)); + resultItems[i] = PropertyCondition.Valid; + continue; + } + + resultItems[i] = OnComparisonResult(hasMixedValues ? false : result); + } + + // Logic Or + if (!attribute.LogicAnd) + { + foreach (var resultItem in resultItems) + { + if (resultItem == PropertyCondition.Valid) + { + return PropertyCondition.Valid; + } + } + + foreach (var resultItem in resultItems) + { + if (resultItem != PropertyCondition.Valid) + { + return resultItem; + } + } + } + // Logic And + else + { + foreach (var resultItem in resultItems) + { + if (resultItem != PropertyCondition.Valid) + { + return resultItem; + } + } + } + + return PropertyCondition.Valid; } protected abstract PropertyCondition OnComparisonResult(bool result); diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ComparisonAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ComparisonAttribute.cs index 2a63da21..2d956eff 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ComparisonAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ComparisonAttribute.cs @@ -11,14 +11,31 @@ public abstract class ComparisonAttribute : ToolboxConditionAttribute /// R-value or the flag if is set to public ComparisonAttribute(string sourceHandle, object valueToMatch) { - SourceHandle = sourceHandle; - ValueToMatch = valueToMatch; + SourceHandles = new string[] {sourceHandle}; + ValueToMatches = new object[] {valueToMatch}; + } + + public ComparisonAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + { + SourceHandles = sourceHandles; + ValueToMatches = valueToMatches; + LogicAnd = logicAnd; } + [Obsolete("Use SourceHandles property instead.")] public string SourceHandle { get; private set; } + [Obsolete("Use ValueToMatches property instead.")] public object ValueToMatch { get; private set; } + public string[] SourceHandles { get; private set; } = null; + + public object[] ValueToMatches { get; private set; } = null; + + public UnityComparisonMethod[] IndividualComparisons { get; set; } = null; + + public bool LogicAnd { get; private set; } = true; + /// /// Indicates what method will be used to compare value of the target property and . /// diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/DisableIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/DisableIfAttribute.cs index c357d471..7b2a7061 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/DisableIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/DisableIfAttribute.cs @@ -16,5 +16,9 @@ public class DisableIfAttribute : ComparisonAttribute { public DisableIfAttribute(string sourceHandle, object valueToMatch) : base(sourceHandle, valueToMatch) { } + + public DisableIfAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + : base(sourceHandles, valueToMatches, !logicAnd) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/EnableIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/EnableIfAttribute.cs index 776a4a85..8979f61a 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/EnableIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/EnableIfAttribute.cs @@ -16,5 +16,9 @@ public class EnableIfAttribute : ComparisonAttribute { public EnableIfAttribute(string sourceHandle, object valueToMatch) : base(sourceHandle, valueToMatch) { } + + public EnableIfAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + : base(sourceHandles, valueToMatches, logicAnd) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideDisabledIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideDisabledIfAttribute.cs index 2b099bd8..df5cebec 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideDisabledIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideDisabledIfAttribute.cs @@ -16,5 +16,9 @@ public class HideDisabledIfAttribute : ComparisonAttribute { public HideDisabledIfAttribute(string sourceHandle, object valueToMatch) : base(sourceHandle, valueToMatch) { } + + public HideDisabledIfAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + : base(sourceHandles, valueToMatches, !logicAnd) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideIfAttribute.cs index d98ce21d..08d4a789 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/HideIfAttribute.cs @@ -16,5 +16,9 @@ public class HideIfAttribute : ComparisonAttribute { public HideIfAttribute(string sourceHandle, object valueToMatch) : base(sourceHandle, valueToMatch) { } + + public HideIfAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + : base(sourceHandles, valueToMatches, !logicAnd) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowDisabledIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowDisabledIfAttribute.cs index 944c6551..ffeadea8 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowDisabledIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowDisabledIfAttribute.cs @@ -16,5 +16,9 @@ public class ShowDisabledIfAttribute : ComparisonAttribute { public ShowDisabledIfAttribute(string sourceHandle, object valueToMatch) : base(sourceHandle, valueToMatch) { } + + public ShowDisabledIfAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + : base(sourceHandles, valueToMatches, logicAnd) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowIfAttribute.cs index ac27c752..a307d0bd 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowIfAttribute.cs @@ -16,5 +16,9 @@ public class ShowIfAttribute : ComparisonAttribute { public ShowIfAttribute(string sourceHandle, object valueToMatch) : base(sourceHandle, valueToMatch) { } + + public ShowIfAttribute(string[] sourceHandles, object[] valueToMatches, bool logicAnd = true) + : base(sourceHandles, valueToMatches, logicAnd) + { } } } \ No newline at end of file diff --git a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowWarningIfAttribute.cs b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowWarningIfAttribute.cs index a63dfafc..1f10f717 100644 --- a/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowWarningIfAttribute.cs +++ b/Assets/Editor Toolbox/Runtime/Attributes/Toolbox/ConditionAttributes/ShowWarningIfAttribute.cs @@ -19,6 +19,12 @@ public ShowWarningIfAttribute(string sourceHandle, object valueToMatch, string m Message = message; } + public ShowWarningIfAttribute(string[] sourceHandles, object[] valueToMatches, string message, bool logicAnd = true) + : base(sourceHandles, valueToMatches, logicAnd) + { + Message = message; + } + public string Message { get; private set; } /// From 607a1a4415ea4b112261a45187b1cd2c660ed2ab Mon Sep 17 00:00:00 2001 From: petchanliu Date: Mon, 16 May 2022 21:11:15 +0800 Subject: [PATCH 2/2] Comparison suport multiple conditions. --- .../Condition/ComparisonAttributeDrawer.cs | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs index 08e36551..692dca93 100644 --- a/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs +++ b/Assets/Editor Toolbox/Editor/Drawers/Toolbox/Condition/ComparisonAttributeDrawer.cs @@ -49,17 +49,9 @@ protected override PropertyCondition OnGuiValidateSafe(SerializedProperty proper resultItems[i] = OnComparisonResult(hasMixedValues ? false : result); } - // Logic Or - if (!attribute.LogicAnd) + // Logic And + if (attribute.LogicAnd) { - foreach (var resultItem in resultItems) - { - if (resultItem == PropertyCondition.Valid) - { - return PropertyCondition.Valid; - } - } - foreach (var resultItem in resultItems) { if (resultItem != PropertyCondition.Valid) @@ -68,9 +60,17 @@ protected override PropertyCondition OnGuiValidateSafe(SerializedProperty proper } } } - // Logic And + // Logic Or else { + foreach (var resultItem in resultItems) + { + if (resultItem == PropertyCondition.Valid) + { + return PropertyCondition.Valid; + } + } + foreach (var resultItem in resultItems) { if (resultItem != PropertyCondition.Valid) @@ -79,7 +79,7 @@ protected override PropertyCondition OnGuiValidateSafe(SerializedProperty proper } } } - + return PropertyCondition.Valid; }