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

Feature/compound condition #43

Open
wants to merge 2 commits into
base: develop
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEditor;
using System;
using UnityEditor;
using UnityEngine;

namespace Toolbox.Editor.Drawers
Expand All @@ -7,24 +8,79 @@ public abstract class ComparisonAttributeDrawer<T> : ToolboxConditionDrawer<T> 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 And
if (attribute.LogicAnd)
{
foreach (var resultItem in resultItems)
{
if (resultItem != PropertyCondition.Valid)
{
return resultItem;
}
}
}
// Logic Or
else
{
foreach (var resultItem in resultItems)
{
if (resultItem == PropertyCondition.Valid)
{
return PropertyCondition.Valid;
}
}

foreach (var resultItem in resultItems)
{
if (resultItem != PropertyCondition.Valid)
{
return resultItem;
}
}
}

return PropertyCondition.Valid;
}

protected abstract PropertyCondition OnComparisonResult(bool result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,31 @@ public abstract class ComparisonAttribute : ToolboxConditionAttribute
/// <param name="valueToMatch">R-value or the flag if <see cref="Comparison"/> is set to <see cref="UnityComparisonMethod.Mask"/></param>
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;

/// <summary>
/// Indicates what method will be used to compare value of the target property and <see cref="ValueToMatch"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{ }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

/// <summary>
Expand Down