diff --git a/Editor.Extras/Drawers/ButtonDrawer.cs b/Editor.Extras/Drawers/ButtonDrawer.cs index ee7715b..ab963ba 100644 --- a/Editor.Extras/Drawers/ButtonDrawer.cs +++ b/Editor.Extras/Drawers/ButtonDrawer.cs @@ -14,21 +14,15 @@ namespace TriInspector.Drawers { private ValueResolver _nameResolver; - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { - base.Initialize(propertyDefinition); - - _nameResolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Name); - } - - public override string CanDraw(TriProperty property) - { - var isValidMethod = property.MemberInfo is MethodInfo mi && mi.GetParameters().Length == 0; + var isValidMethod = propertyDefinition.MemberInfo is MethodInfo mi && mi.GetParameters().Length == 0; if (!isValidMethod) { return "[Button] valid only on methods without parameters"; } + _nameResolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Name); if (_nameResolver.TryGetErrorString(out var error)) { return error; diff --git a/Editor.Extras/Drawers/EnumToggleButtonsDrawer.cs b/Editor.Extras/Drawers/EnumToggleButtonsDrawer.cs index f3aa434..d9b593a 100644 --- a/Editor.Extras/Drawers/EnumToggleButtonsDrawer.cs +++ b/Editor.Extras/Drawers/EnumToggleButtonsDrawer.cs @@ -14,14 +14,14 @@ namespace TriInspector.Drawers { public class EnumToggleButtonsDrawer : TriAttributeDrawer { - public override string CanDraw(TriProperty property) + public override string Initialize(TriPropertyDefinition propertyDefinition) { - if (!property.FieldType.IsEnum) + if (!propertyDefinition.FieldType.IsEnum) { return "EnumToggleButtons attribute can be used only on enums"; } - return base.CanDraw(property); + return null; } public override TriElement CreateElement(TriProperty property, TriElement next) diff --git a/Editor.Extras/Drawers/InlineEditorDrawer.cs b/Editor.Extras/Drawers/InlineEditorDrawer.cs index 2432e04..695cbc7 100644 --- a/Editor.Extras/Drawers/InlineEditorDrawer.cs +++ b/Editor.Extras/Drawers/InlineEditorDrawer.cs @@ -13,9 +13,9 @@ namespace TriInspector.Drawers { public class InlineEditorDrawer : TriAttributeDrawer { - public override string CanDraw(TriProperty property) + public override string Initialize(TriPropertyDefinition propertyDefinition) { - if (!typeof(Object).IsAssignableFrom(property.FieldType)) + if (!typeof(Object).IsAssignableFrom(propertyDefinition.FieldType)) { return "[InlineEditor] valid only on Object fields"; } diff --git a/Editor.Extras/Drawers/OnValueChangedDrawer.cs b/Editor.Extras/Drawers/OnValueChangedDrawer.cs index 03c11ed..84994bb 100644 --- a/Editor.Extras/Drawers/OnValueChangedDrawer.cs +++ b/Editor.Extras/Drawers/OnValueChangedDrawer.cs @@ -10,21 +10,17 @@ namespace TriInspector.Drawers { private ActionResolver _actionResolver; - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { base.Initialize(propertyDefinition); _actionResolver = ActionResolver.Resolve(propertyDefinition, Attribute.Method); - } - - public override string CanDraw(TriProperty property) - { if (_actionResolver.TryGetErrorString(out var error)) { return error; } - return base.CanDraw(property); + return null; } public override TriElement CreateElement(TriProperty property, TriElement next) diff --git a/Editor.Extras/Drawers/TableListDrawer.cs b/Editor.Extras/Drawers/TableListDrawer.cs index 589af20..d9eaeeb 100644 --- a/Editor.Extras/Drawers/TableListDrawer.cs +++ b/Editor.Extras/Drawers/TableListDrawer.cs @@ -16,9 +16,9 @@ namespace TriInspector.Drawers { public class TableListDrawer : TriAttributeDrawer { - public override string CanDraw(TriProperty property) + public override string Initialize(TriPropertyDefinition propertyDefinition) { - if (property.PropertyType != TriPropertyType.Array) + if (!propertyDefinition.IsArray) { return "[TableList] valid only on lists"; } diff --git a/Editor.Extras/Drawers/TitleDrawer.cs b/Editor.Extras/Drawers/TitleDrawer.cs index 02d5e2f..58ae516 100644 --- a/Editor.Extras/Drawers/TitleDrawer.cs +++ b/Editor.Extras/Drawers/TitleDrawer.cs @@ -17,21 +17,18 @@ namespace TriInspector.Drawers private ValueResolver _titleResolver; - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { base.Initialize(propertyDefinition); _titleResolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Title); - } - public override string CanDraw(TriProperty property) - { if (_titleResolver.TryGetErrorString(out var error)) { return error; } - return base.CanDraw(property); + return null; } public override float GetHeight(float width, TriProperty property, TriElement next) diff --git a/Editor.Extras/Processors/DisableIfProcessor.cs b/Editor.Extras/Processors/DisableIfProcessor.cs index 5215414..7e38f34 100644 --- a/Editor.Extras/Processors/DisableIfProcessor.cs +++ b/Editor.Extras/Processors/DisableIfProcessor.cs @@ -19,11 +19,17 @@ namespace TriInspector.Processors _inverse = inverse; } - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { base.Initialize(propertyDefinition); _conditionResolver = ValueResolver.Resolve(propertyDefinition, Attribute.Condition); + if (_conditionResolver.TryGetErrorString(out var error)) + { + return error; + } + + return null; } public sealed override bool IsDisabled(TriProperty property) diff --git a/Editor.Extras/Processors/HideIfProcessor.cs b/Editor.Extras/Processors/HideIfProcessor.cs index 1da9eea..ec7abe9 100644 --- a/Editor.Extras/Processors/HideIfProcessor.cs +++ b/Editor.Extras/Processors/HideIfProcessor.cs @@ -19,11 +19,18 @@ namespace TriInspector.Processors _inverse = inverse; } - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { base.Initialize(propertyDefinition); _conditionResolver = ValueResolver.Resolve(propertyDefinition, Attribute.Condition); + + if (_conditionResolver.TryGetErrorString(out var error)) + { + return error; + } + + return null; } public sealed override bool IsHidden(TriProperty property) diff --git a/Editor.Extras/Validators/InfoBoxValidator.cs b/Editor.Extras/Validators/InfoBoxValidator.cs index 8c53219..dc0a166 100644 --- a/Editor.Extras/Validators/InfoBoxValidator.cs +++ b/Editor.Extras/Validators/InfoBoxValidator.cs @@ -11,14 +11,25 @@ namespace TriInspector.Validators private ValueResolver _resolver; private ValueResolver _visibleIfResolver; - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { - base.Initialize(propertyDefinition); - _resolver = ValueResolver.ResolveString(propertyDefinition, Attribute.Text); _visibleIfResolver = Attribute.VisibleIf != null ? ValueResolver.Resolve(propertyDefinition, Attribute.VisibleIf) : null; + + if (_resolver.TryGetErrorString(out var error)) + { + return error; + } + + if (_visibleIfResolver != null && + _visibleIfResolver.TryGetErrorString(out var error2)) + { + return error2; + } + + return null; } public override TriValidationResult Validate(TriProperty property) diff --git a/Editor.Extras/Validators/ValidateInputValidator.cs b/Editor.Extras/Validators/ValidateInputValidator.cs index 40be6f8..1390aa8 100644 --- a/Editor.Extras/Validators/ValidateInputValidator.cs +++ b/Editor.Extras/Validators/ValidateInputValidator.cs @@ -10,11 +10,18 @@ namespace TriInspector.Validators { private ValueResolver _resolver; - public override void Initialize(TriPropertyDefinition propertyDefinition) + public override string Initialize(TriPropertyDefinition propertyDefinition) { base.Initialize(propertyDefinition); _resolver = ValueResolver.Resolve(propertyDefinition, Attribute.Method); + + if (_resolver.TryGetErrorString(out var error)) + { + return error; + } + + return null; } public override TriValidationResult Validate(TriProperty property) diff --git a/Editor/Elements/TriPropertyElement.cs b/Editor/Elements/TriPropertyElement.cs index c217df6..0fae7ad 100644 --- a/Editor/Elements/TriPropertyElement.cs +++ b/Editor/Elements/TriPropertyElement.cs @@ -1,5 +1,4 @@ using System; -using TriInspector.Utilities; using TriInspectorUnityInternalBridge; using UnityEditor; using UnityEngine; @@ -25,16 +24,7 @@ namespace TriInspector.Elements var drawers = property.AllDrawers; for (var index = drawers.Count - 1; index >= 0; index--) { - var drawer = drawers[index]; - - var canDrawMessage = drawer.CanDraw(_property); - if (!string.IsNullOrEmpty(canDrawMessage)) - { - AddChild(new TriInfoBoxElement(canDrawMessage, TriMessageType.Error)); - continue; - } - - element = drawer.CreateElementInternal(property, element); + element = drawers[index].CreateElementInternal(property, element); } AddChild(element); diff --git a/Editor/TriCustomDrawer.cs b/Editor/TriCustomDrawer.cs index dba2621..910bea5 100644 --- a/Editor/TriCustomDrawer.cs +++ b/Editor/TriCustomDrawer.cs @@ -1,22 +1,8 @@ -using JetBrains.Annotations; - -namespace TriInspector +namespace TriInspector { - public abstract class TriCustomDrawer + public abstract class TriCustomDrawer : TriPropertyExtension { internal int Order { get; set; } - internal bool? ApplyOnArrayElement { get; set; } - - [PublicAPI] - public virtual void Initialize(TriPropertyDefinition propertyDefinition) - { - } - - [PublicAPI] - public virtual string CanDraw(TriProperty property) - { - return null; - } public abstract TriElement CreateElementInternal(TriProperty property, TriElement next); } diff --git a/Editor/TriPropertyDefinition.cs b/Editor/TriPropertyDefinition.cs index 055b874..5eaa1a1 100644 --- a/Editor/TriPropertyDefinition.cs +++ b/Editor/TriPropertyDefinition.cs @@ -103,13 +103,8 @@ namespace TriInspector { _hideProcessorsBackingField = TriDrawersUtilities.CreateHideProcessorsFor(Attributes) - .Where(it => CanApplyOn(this, applyOnArrayElement: false)) + .Where(CanApplyExtensionOnSelf) .ToList(); - - foreach (var processor in _hideProcessorsBackingField) - { - processor.Initialize(this); - } } return _hideProcessorsBackingField; @@ -124,13 +119,8 @@ namespace TriInspector { _disableProcessorsBackingField = TriDrawersUtilities.CreateDisableProcessorsFor(Attributes) - .Where(it => CanApplyOn(this, applyOnArrayElement: false)) + .Where(CanApplyExtensionOnSelf) .ToList(); - - foreach (var processor in _disableProcessorsBackingField) - { - processor.Initialize(this); - } } return _disableProcessorsBackingField; @@ -150,14 +140,9 @@ namespace TriInspector { new ValidatorsDrawer {Order = TriDrawerOrder.Validator,}, }) - .Where(it => CanApplyOn(this, it.ApplyOnArrayElement)) + .Where(CanApplyExtensionOnSelf) .OrderBy(it => it.Order) .ToList(); - - foreach (var drawer in _drawersBackingField) - { - drawer.Initialize(this); - } } return _drawersBackingField; @@ -173,13 +158,8 @@ namespace TriInspector _validatorsBackingField = Enumerable.Empty() .Concat(TriDrawersUtilities.CreateValueValidatorsFor(FieldType)) .Concat(TriDrawersUtilities.CreateAttributeValidatorsFor(Attributes)) - .Where(it => CanApplyOn(this, it.ApplyOnArrayElement)) + .Where(CanApplyExtensionOnSelf) .ToList(); - - foreach (var validator in _validatorsBackingField) - { - validator.Initialize(this); - } } return _validatorsBackingField; @@ -296,16 +276,23 @@ namespace TriInspector }; } - private static bool CanApplyOn(TriPropertyDefinition definition, bool? applyOnArrayElement) + private bool CanApplyExtensionOnSelf(TriPropertyExtension propertyExtension) { - if (!applyOnArrayElement.HasValue) + if (propertyExtension.ApplyOnArrayElement.HasValue) { - return true; + if (IsArrayElement && !propertyExtension.ApplyOnArrayElement.Value || + IsArray && propertyExtension.ApplyOnArrayElement.Value) + { + return false; + } } - if (definition.IsArrayElement && !applyOnArrayElement.Value || - definition.IsArray && applyOnArrayElement.Value) + var error = propertyExtension.Initialize(this); + if (error != null) { + var message = $"Extension {propertyExtension.GetType()} cannot be applied " + + $"on property '{MemberInfo?.DeclaringType}.{Name}': {error}"; + Debug.LogError(message); return false; } diff --git a/Editor/TriPropertyDisableProcessor.cs b/Editor/TriPropertyDisableProcessor.cs index b626003..eb45ffe 100644 --- a/Editor/TriPropertyDisableProcessor.cs +++ b/Editor/TriPropertyDisableProcessor.cs @@ -3,15 +3,10 @@ using JetBrains.Annotations; namespace TriInspector { - public abstract class TriPropertyDisableProcessor + public abstract class TriPropertyDisableProcessor : TriPropertyExtension { internal Attribute RawAttribute { get; set; } - [PublicAPI] - public virtual void Initialize(TriPropertyDefinition propertyDefinition) - { - } - [PublicAPI] public abstract bool IsDisabled(TriProperty property); } diff --git a/Editor/TriPropertyExtension.cs b/Editor/TriPropertyExtension.cs new file mode 100644 index 0000000..d3f984f --- /dev/null +++ b/Editor/TriPropertyExtension.cs @@ -0,0 +1,15 @@ +using JetBrains.Annotations; + +namespace TriInspector +{ + public abstract class TriPropertyExtension + { + public bool? ApplyOnArrayElement { get; internal set; } + + [PublicAPI] + public virtual string Initialize(TriPropertyDefinition propertyDefinition) + { + return null; + } + } +} \ No newline at end of file diff --git a/Editor/TriPropertyExtension.cs.meta b/Editor/TriPropertyExtension.cs.meta new file mode 100644 index 0000000..e9751fa --- /dev/null +++ b/Editor/TriPropertyExtension.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4da7122af7f84f1e8c0f2986e064e161 +timeCreated: 1654255038 \ No newline at end of file diff --git a/Editor/TriPropertyHideProcessor.cs b/Editor/TriPropertyHideProcessor.cs index e0230fa..958956c 100644 --- a/Editor/TriPropertyHideProcessor.cs +++ b/Editor/TriPropertyHideProcessor.cs @@ -3,15 +3,10 @@ using JetBrains.Annotations; namespace TriInspector { - public abstract class TriPropertyHideProcessor + public abstract class TriPropertyHideProcessor : TriPropertyExtension { internal Attribute RawAttribute { get; set; } - [PublicAPI] - public virtual void Initialize(TriPropertyDefinition propertyDefinition) - { - } - [PublicAPI] public abstract bool IsHidden(TriProperty property); } diff --git a/Editor/TriValidator.cs b/Editor/TriValidator.cs index d0c8710..e0925ef 100644 --- a/Editor/TriValidator.cs +++ b/Editor/TriValidator.cs @@ -1,18 +1,10 @@ using System; using JetBrains.Annotations; -using UnityEditor; namespace TriInspector { - public abstract class TriValidator + public abstract class TriValidator : TriPropertyExtension { - internal bool ApplyOnArrayElement { get; set; } - - [PublicAPI] - public virtual void Initialize(TriPropertyDefinition propertyDefinition) - { - } - [PublicAPI] public abstract TriValidationResult Validate(TriProperty property); } diff --git a/package.json b/package.json index 5bc4168..d6a49cc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "com.codewriter.triinspector", "displayName": "Tri Inspector", "description": "Advanced inspector attributes for Unity", - "version": "1.2.0", + "version": "1.3.0", "unity": "2020.3", "author": "CodeWriter (https://github.com/orgs/codewriter-packages)", "homepage": "https://github.com/codewriter-packages/Tri-Inspector#readme",