Fix #1 - Add compared value to ShowIf, HideIf, EnableIf, DisableIf

This commit is contained in:
VladV 2022-05-14 10:20:25 +03:00
parent ce6611ddd8
commit 36b01b0617
15 changed files with 67 additions and 43 deletions

View File

@ -8,11 +8,11 @@ using TriInspector.Resolvers;
namespace TriInspector.Processors
{
public abstract class DisableIfProcessorBase<T> : TriPropertyDisableProcessor<T>
where T : ConditionalDisableAttribute
where T : ConditionalDisableBaseAttribute
{
private readonly bool _inverse;
private ValueResolver<bool> _conditionResolver;
private ValueResolver<object> _conditionResolver;
protected DisableIfProcessorBase(bool inverse)
{
@ -23,12 +23,14 @@ namespace TriInspector.Processors
{
base.Initialize(propertyDefinition);
_conditionResolver = ValueResolver.Resolve<bool>(propertyDefinition, Attribute.Condition);
_conditionResolver = ValueResolver.Resolve<object>(propertyDefinition, Attribute.Condition);
}
public sealed override bool IsDisabled(TriProperty property)
{
return _conditionResolver.GetValue(property) != _inverse;
var val = _conditionResolver.GetValue(property);
var equal = val?.Equals(Attribute.Value) ?? Attribute.Value == null;
return equal != _inverse;
}
}

View File

@ -8,11 +8,11 @@ using TriInspector.Resolvers;
namespace TriInspector.Processors
{
public abstract class HideIfProcessorBase<T> : TriPropertyHideProcessor<T>
where T : ConditionalHideAttribute
where T : ConditionalHideBaseAttribute
{
private readonly bool _inverse;
private ValueResolver<bool> _conditionResolver;
private ValueResolver<object> _conditionResolver;
protected HideIfProcessorBase(bool inverse)
{
@ -23,12 +23,14 @@ namespace TriInspector.Processors
{
base.Initialize(propertyDefinition);
_conditionResolver = ValueResolver.Resolve<bool>(propertyDefinition, Attribute.Condition);
_conditionResolver = ValueResolver.Resolve<object>(propertyDefinition, Attribute.Condition);
}
public sealed override bool IsHidden(TriProperty property)
{
return _conditionResolver.GetValue(property) != _inverse;
var val = _conditionResolver.GetValue(property);
var equal = val?.Equals(Attribute.Value) ?? Attribute.Value == null;
return equal != _inverse;
}
}

View File

@ -23,7 +23,7 @@ namespace TriInspector.Resolvers
foreach (var fieldInfo in parentType.GetFields(flags))
{
if (fieldInfo.Name == expression &&
fieldInfo.FieldType == typeof(T))
typeof(T).IsAssignableFrom(fieldInfo.FieldType))
{
resolver = new InstanceFieldValueResolver<T>(fieldInfo);
return true;

View File

@ -23,7 +23,7 @@ namespace TriInspector.Resolvers
foreach (var methodInfo in parentType.GetMethods(flags))
{
if (methodInfo.Name == expression &&
methodInfo.ReturnType == typeof(T) &&
typeof(T).IsAssignableFrom(methodInfo.ReturnType) &&
methodInfo.GetParameters() is var parameterInfos &&
parameterInfos.Length == 0)
{

View File

@ -23,7 +23,7 @@ namespace TriInspector.Resolvers
foreach (var propertyInfo in parentType.GetProperties(flags))
{
if (propertyInfo.Name == expression &&
propertyInfo.PropertyType == typeof(T) &&
typeof(T).IsAssignableFrom(propertyInfo.PropertyType) &&
propertyInfo.CanRead)
{
resolver = new InstancePropertyValueResolver<T>(propertyInfo);

View File

@ -1,12 +0,0 @@
namespace TriInspector
{
public abstract class ConditionalDisableAttribute : DisableBaseAttribute
{
protected ConditionalDisableAttribute(string condition)
{
Condition = condition;
}
public string Condition { get; }
}
}

View File

@ -0,0 +1,14 @@
namespace TriInspector
{
public abstract class ConditionalDisableBaseAttribute : DisableBaseAttribute
{
protected ConditionalDisableBaseAttribute(string condition, object value)
{
Condition = condition;
Value = value;
}
public string Condition { get; }
public object Value { get; }
}
}

View File

@ -1,12 +0,0 @@
namespace TriInspector
{
public abstract class ConditionalHideAttribute : HideBaseAttribute
{
protected ConditionalHideAttribute(string condition)
{
Condition = condition;
}
public string Condition { get; }
}
}

View File

@ -0,0 +1,14 @@
namespace TriInspector
{
public abstract class ConditionalHideBaseAttribute : HideBaseAttribute
{
protected ConditionalHideBaseAttribute(string condition, object value)
{
Condition = condition;
Value = value;
}
public string Condition { get; }
public object Value { get; }
}
}

View File

@ -5,9 +5,13 @@ namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class DisableIfAttribute : ConditionalDisableAttribute
public class DisableIfAttribute : ConditionalDisableBaseAttribute
{
public DisableIfAttribute(string condition) : base(condition)
public DisableIfAttribute(string condition) : this(condition, true)
{
}
public DisableIfAttribute(string condition, object value) : base(condition, value)
{
}
}

View File

@ -5,9 +5,13 @@ namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class EnableIfAttribute : ConditionalDisableAttribute
public class EnableIfAttribute : ConditionalDisableBaseAttribute
{
public EnableIfAttribute(string condition) : base(condition)
public EnableIfAttribute(string condition) : this(condition, true)
{
}
public EnableIfAttribute(string condition, object value) : base(condition, value)
{
}
}

View File

@ -5,9 +5,13 @@ namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class HideIfAttribute : ConditionalHideAttribute
public class HideIfAttribute : ConditionalHideBaseAttribute
{
public HideIfAttribute(string condition) : base(condition)
public HideIfAttribute(string condition) : this(condition, true)
{
}
public HideIfAttribute(string condition, object value) : base(condition, value)
{
}
}

View File

@ -5,9 +5,13 @@ namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class ShowIfAttribute : ConditionalHideAttribute
public class ShowIfAttribute : ConditionalHideBaseAttribute
{
public ShowIfAttribute(string condition) : base(condition)
public ShowIfAttribute(string condition) : this(condition, true)
{
}
public ShowIfAttribute(string condition, object value) : base(condition, value)
{
}
}