Add property hide and disable processors

This commit is contained in:
VladV 2022-01-05 15:24:23 +03:00
parent 356b11e251
commit b58fe280b5
27 changed files with 371 additions and 72 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 3746e0c31b82450b804ea460d753f4fa
timeCreated: 1641384540

View File

@ -0,0 +1,25 @@
using Editor.Extras.Processors;
using TriInspector;
using UnityEngine;
[assembly: RegisterTriPropertyDisableProcessor(typeof(DisableInPlayModeProcessor))]
[assembly: RegisterTriPropertyDisableProcessor(typeof(EnableInPlayModeProcessor))]
namespace Editor.Extras.Processors
{
public class DisableInPlayModeProcessor : TriPropertyDisableProcessor<DisableInPlayModeAttribute>
{
public override bool IsDisabled(TriProperty property)
{
return Application.isPlaying;
}
}
public class EnableInPlayModeProcessor : TriPropertyDisableProcessor<EnableInPlayModeAttribute>
{
public override bool IsDisabled(TriProperty property)
{
return !Application.isPlaying;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 397dcddedf9a4f91931e5bdc40c668aa
timeCreated: 1641385800

View File

@ -0,0 +1,25 @@
using Editor.Extras.Processors;
using TriInspector;
using UnityEngine;
[assembly: RegisterTriPropertyHideProcessor(typeof(HideInPlayModeProcessor))]
[assembly: RegisterTriPropertyHideProcessor(typeof(ShowInPlayModeProcessor))]
namespace Editor.Extras.Processors
{
public class HideInPlayModeProcessor : TriPropertyHideProcessor<HideInPlayModeAttribute>
{
public override bool IsHidden(TriProperty property)
{
return Application.isPlaying;
}
}
public class ShowInPlayModeProcessor : TriPropertyHideProcessor<ShowInPlayModeAttribute>
{
public override bool IsHidden(TriProperty property)
{
return !Application.isPlaying;
}
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 99642f3dada346b48762044cabacc69e
timeCreated: 1641384551

View File

@ -26,4 +26,26 @@ namespace TriInspector
public Type DrawerType { get; }
}
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public class RegisterTriPropertyHideProcessor : Attribute
{
public RegisterTriPropertyHideProcessor(Type processorType)
{
ProcessorType = processorType;
}
public Type ProcessorType { get; }
}
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public class RegisterTriPropertyDisableProcessor : Attribute
{
public RegisterTriPropertyDisableProcessor(Type processorType)
{
ProcessorType = processorType;
}
public Type ProcessorType { get; }
}
}

View File

@ -46,7 +46,7 @@ namespace TriInspector.Elements
var oldEnabled = GUI.enabled;
GUI.enabled = _property.IsEnabled;
GUI.enabled &= _property.IsEnabled;
base.OnGUI(position);
GUI.enabled = oldEnabled;
}

View File

@ -19,7 +19,6 @@ namespace TriInspector
private List<TriProperty> _childrenProperties;
private GUIContent _displayNameBackingField;
private bool? _isReadOnlyBackingField;
internal TriProperty(
TriPropertyTree propertyTree,
@ -67,17 +66,39 @@ namespace TriInspector
}
[PublicAPI]
public bool IsVisible => true;
public bool IsVisible
{
get
{
foreach (var processor in _definition.HideProcessors)
{
if (processor.IsHidden(this))
{
return false;
}
}
return true;
}
}
[PublicAPI]
public bool IsEnabled
{
get
{
if (IsReadOnly)
if (_definition.IsReadOnly)
{
return false;
}
foreach (var processor in _definition.DisableProcessors)
{
if (processor.IsDisabled(this))
{
return false;
}
}
return true;
}
@ -143,20 +164,6 @@ namespace TriInspector
[PublicAPI]
public TriPropertyTree PropertyTree { get; }
[PublicAPI]
public bool IsReadOnly
{
get
{
if (_isReadOnlyBackingField == null)
{
_isReadOnlyBackingField = _definition.IsReadOnly || _parent.IsReadOnly;
}
return _isReadOnlyBackingField.Value;
}
}
public void ApplyChildValueModifications(int targetIndex)
{
var parentValue = _parent.GetValue(targetIndex);
@ -358,7 +365,6 @@ namespace TriInspector
public interface ITriPropertyParent
{
object GetValue(int targetIndex);
bool IsReadOnly { get; }
void ApplyChildValueModifications(int targetIndex);
}

View File

@ -15,6 +15,8 @@ namespace TriInspector
[CanBeNull] private readonly Action<object, object> _valueSetter;
private IReadOnlyList<TriCustomDrawer> _drawersBackingField;
private IReadOnlyList<TriPropertyHideProcessor> _hideProcessorsBackingField;
private IReadOnlyList<TriPropertyDisableProcessor> _disableProcessorsBackingField;
internal TriPropertyDefinition(int order, FieldInfo fi)
: this(order, fi.Name, fi.FieldType, fi.GetValue, fi.SetValue, fi.GetCustomAttributes(), false)
@ -68,6 +70,58 @@ namespace TriInspector
public bool IsArray { get; }
public IReadOnlyList<TriPropertyHideProcessor> HideProcessors
{
get
{
if (_hideProcessorsBackingField == null)
{
_hideProcessorsBackingField = (
from attribute in Attributes
from processor in TriDrawersUtilities.AllHideProcessors
where TriDrawersUtilities.IsHideProcessorFor(processor.ProcessorType, attribute)
select CreateHideProcessor(processor, attribute)
).ToList();
static TriPropertyHideProcessor CreateHideProcessor(RegisterTriPropertyHideProcessor processor,
Attribute attribute)
{
var instance = (TriPropertyHideProcessor) Activator.CreateInstance(processor.ProcessorType);
instance.RawAttribute = attribute;
return instance;
}
}
return _hideProcessorsBackingField;
}
}
public IReadOnlyList<TriPropertyDisableProcessor> DisableProcessors
{
get
{
if (_disableProcessorsBackingField == null)
{
_disableProcessorsBackingField = (
from attribute in Attributes
from processor in TriDrawersUtilities.AllDisableProcessors
where TriDrawersUtilities.IsDisableProcessorFor(processor.ProcessorType, attribute)
select CreateDisableProcessor(processor, attribute)
).ToList();
static TriPropertyDisableProcessor CreateDisableProcessor(RegisterTriPropertyDisableProcessor processor,
Attribute attribute)
{
var instance = (TriPropertyDisableProcessor) Activator.CreateInstance(processor.ProcessorType);
instance.RawAttribute = attribute;
return instance;
}
}
return _disableProcessorsBackingField;
}
}
public IReadOnlyList<TriCustomDrawer> Drawers
{
get

View File

@ -0,0 +1,16 @@
using System;
namespace TriInspector
{
public abstract class TriPropertyDisableProcessor
{
internal Attribute RawAttribute { get; set; }
public abstract bool IsDisabled(TriProperty property);
}
public abstract class TriPropertyDisableProcessor<TAttribute> : TriPropertyDisableProcessor
where TAttribute : DisableBaseAttribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2c01ea19c766412ba28df648a968abba
timeCreated: 1641385496

View File

@ -0,0 +1,16 @@
using System;
namespace TriInspector
{
public abstract class TriPropertyHideProcessor
{
internal Attribute RawAttribute { get; set; }
public abstract bool IsHidden(TriProperty property);
}
public abstract class TriPropertyHideProcessor<TAttribute> : TriPropertyHideProcessor
where TAttribute : HideBaseAttribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: de11bb184a7d4c7f9c6a4f2a75756278
timeCreated: 1641384283

View File

@ -44,7 +44,6 @@ namespace TriInspector
public TriPropertyTree Root { get; }
object ITriPropertyParent.GetValue(int targetIndex) => TargetObjects[targetIndex];
bool ITriPropertyParent.IsReadOnly => false;
void ITriPropertyParent.ApplyChildValueModifications(int targetIndex)
{

View File

@ -10,6 +10,8 @@ namespace TriInspector.Utilities
private static IDictionary<Type, TriGroupDrawer> _allGroupDrawersCacheBackingField;
private static IReadOnlyList<RegisterTriDrawerAttribute> _allAttributeDrawerTypesBackingField;
private static IReadOnlyList<RegisterTriDrawerAttribute> _allValueDrawerTypesBackingField;
private static IReadOnlyList<RegisterTriPropertyHideProcessor> _allHideProcessorTypesBackingField;
private static IReadOnlyList<RegisterTriPropertyDisableProcessor> _allDisableProcessorTypesBackingField;
private static IDictionary<Type, TriGroupDrawer> AllGroupDrawersCache
{
@ -68,6 +70,42 @@ namespace TriInspector.Utilities
}
}
public static IReadOnlyList<RegisterTriPropertyHideProcessor> AllHideProcessors
{
get
{
if (_allHideProcessorTypesBackingField == null)
{
_allHideProcessorTypesBackingField = (
from asm in TriReflectionUtilities.Assemblies
from attr in asm.GetCustomAttributes<RegisterTriPropertyHideProcessor>()
where IsHideProcessorType(attr.ProcessorType, out _)
select attr
).ToList();
}
return _allHideProcessorTypesBackingField;
}
}
public static IReadOnlyList<RegisterTriPropertyDisableProcessor> AllDisableProcessors
{
get
{
if (_allDisableProcessorTypesBackingField == null)
{
_allDisableProcessorTypesBackingField = (
from asm in TriReflectionUtilities.Assemblies
from attr in asm.GetCustomAttributes<RegisterTriPropertyDisableProcessor>()
where IsDisableProcessorType(attr.ProcessorType, out _)
select attr
).ToList();
}
return _allDisableProcessorTypesBackingField;
}
}
public static TriPropertyCollectionBaseElement TryCreateGroupElementFor(DeclareGroupBaseAttribute attribute)
{
if (!AllGroupDrawersCache.TryGetValue(attribute.GetType(), out var attr))
@ -115,40 +153,65 @@ namespace TriInspector.Utilities
private static bool IsValueDrawerType(Type type, out Type valueType)
{
valueType = null;
if (type.IsAbstract)
{
return false;
}
if (type.GetConstructor(Type.EmptyTypes) == null)
{
return false;
}
var drawerType = type.BaseType;
if (drawerType == null)
{
return false;
}
if (!drawerType.IsGenericType)
{
return false;
}
if (drawerType.GetGenericTypeDefinition() != typeof(TriValueDrawer<>))
{
return false;
}
valueType = drawerType.GetGenericArguments()[0];
return true;
return TryGetBaseGenericTargetType(type, typeof(TriValueDrawer<>), out valueType);
}
private static bool IsAttributeDrawerType(Type type, out Type attributeType)
{
return TryGetBaseGenericTargetType(type, typeof(TriAttributeDrawer<>), out attributeType);
}
private static bool IsHideProcessorType(Type type, out Type attributeType)
{
return TryGetBaseGenericTargetType(type, typeof(TriPropertyHideProcessor<>), out attributeType);
}
private static bool IsDisableProcessorType(Type type, out Type attributeType)
{
return TryGetBaseGenericTargetType(type, typeof(TriPropertyDisableProcessor<>), out attributeType);
}
public static bool IsValueDrawerFor(Type drawerType, Type valueType)
{
if (IsValueDrawerType(drawerType, out var valType))
{
return valType == valueType;
}
return false;
}
public static bool IsAttributeDrawerFor(Type drawerType, Attribute attribute)
{
if (IsAttributeDrawerType(drawerType, out var attributeType))
{
return attributeType == attribute.GetType();
}
return false;
}
public static bool IsHideProcessorFor(Type processorType, Attribute attribute)
{
if (IsHideProcessorType(processorType, out var attributeType))
{
return attributeType == attribute.GetType();
}
return false;
}
public static bool IsDisableProcessorFor(Type processorType, Attribute attribute)
{
if (IsDisableProcessorType(processorType, out var attributeType))
{
return attributeType == attribute.GetType();
}
return false;
}
private static bool TryGetBaseGenericTargetType(Type type, Type expectedGenericType, out Type attributeType)
{
attributeType = null;
@ -174,7 +237,7 @@ namespace TriInspector.Utilities
return false;
}
if (drawerType.GetGenericTypeDefinition() != typeof(TriAttributeDrawer<>))
if (drawerType.GetGenericTypeDefinition() != expectedGenericType)
{
return false;
}
@ -182,25 +245,5 @@ namespace TriInspector.Utilities
attributeType = drawerType.GetGenericArguments()[0];
return true;
}
public static bool IsValueDrawerFor(Type drawerType, Type valueType)
{
if (IsValueDrawerType(drawerType, out var valType))
{
return valType == valueType;
}
return false;
}
public static bool IsAttributeDrawerFor(Type drawerType, Attribute attribute)
{
if (IsAttributeDrawerType(drawerType, out var attributeType))
{
return attributeType == attribute.GetType();
}
return false;
}
}
}

View File

@ -0,0 +1,8 @@
using System;
namespace TriInspector
{
public abstract class DisableBaseAttribute : Attribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 62ef6624296046438d41bb5ef66cc422
timeCreated: 1641386025

View File

@ -0,0 +1,11 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class DisableInPlayModeAttribute : DisableBaseAttribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fe853178832f4dde824c372a06a22ae0
timeCreated: 1641385956

View File

@ -0,0 +1,11 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class EnableInPlayModeAttribute : DisableBaseAttribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 9dc8b9775fe6475f8d9df0fe00e15c25
timeCreated: 1641385940

View File

@ -0,0 +1,8 @@
using System;
namespace TriInspector
{
public abstract class HideBaseAttribute : Attribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ff02ae4b7c9a4f2598212bef121bb141
timeCreated: 1641386002

View File

@ -0,0 +1,11 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class HideInPlayModeAttribute : HideBaseAttribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6686c4df436342e8a75b5ace13a2c590
timeCreated: 1641385927

View File

@ -0,0 +1,11 @@
using System;
using System.Diagnostics;
namespace TriInspector
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
[Conditional("UNITY_EDITOR")]
public class ShowInPlayModeAttribute : HideBaseAttribute
{
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 1129642ac27240c8844f2865f1d38e92
timeCreated: 1641384123