From 849da7e8a76d45cc60bb4bfe17da5fcf244593e4 Mon Sep 17 00:00:00 2001 From: VladV Date: Tue, 18 Jan 2022 20:19:15 +0300 Subject: [PATCH] Move TriValidationResult to runtime assembly --- Editor.Extras/Drawers/InlineEditorDrawer.cs | 2 +- Editor/Elements/TriInfoBoxElement.cs | 23 ++++++++++--- Editor/TriProperty.cs | 10 +++--- Editor/TriValidator.cs | 24 -------------- Runtime/TriValidationResult.cs | 36 +++++++++++++++++++++ Runtime/TriValidationResult.cs.meta | 3 ++ 6 files changed, 63 insertions(+), 35 deletions(-) create mode 100644 Runtime/TriValidationResult.cs create mode 100644 Runtime/TriValidationResult.cs.meta diff --git a/Editor.Extras/Drawers/InlineEditorDrawer.cs b/Editor.Extras/Drawers/InlineEditorDrawer.cs index 9f83e2a..acf8da1 100644 --- a/Editor.Extras/Drawers/InlineEditorDrawer.cs +++ b/Editor.Extras/Drawers/InlineEditorDrawer.cs @@ -19,7 +19,7 @@ namespace TriInspector.Drawers { var stack = new TriElement(); stack.AddChild(new TriInfoBoxElement($"InlineEditor valid only on Object fields", - MessageType.Error)); + TriMessageType.Error)); stack.AddChild(next); return stack; diff --git a/Editor/Elements/TriInfoBoxElement.cs b/Editor/Elements/TriInfoBoxElement.cs index 624b6b8..234c758 100644 --- a/Editor/Elements/TriInfoBoxElement.cs +++ b/Editor/Elements/TriInfoBoxElement.cs @@ -9,9 +9,10 @@ namespace TriInspector.Elements private readonly GUIContent _message; private readonly Color _color; - public TriInfoBoxElement(string message, MessageType type = MessageType.None, Color? color = null) + public TriInfoBoxElement(string message, TriMessageType type = TriMessageType.None, Color? color = null) { - _message = new GUIContent(message, EditorGUIUtilityProxy.GetHelpIcon(type)); + var messageType = GetMessageType(type); + _message = new GUIContent(message, EditorGUIUtilityProxy.GetHelpIcon(messageType)); _color = color ?? GetColor(type); } @@ -30,14 +31,14 @@ namespace TriInspector.Elements GUI.Label(position, _message, Styles.InfoBoxContent); } - private static Color GetColor(MessageType type) + private static Color GetColor(TriMessageType type) { switch (type) { - case MessageType.Error: + case TriMessageType.Error: return new Color(1f, 0.4f, 0.4f); - case MessageType.Warning: + case TriMessageType.Warning: return new Color(1f, 0.8f, 0.2f); default: @@ -45,6 +46,18 @@ namespace TriInspector.Elements } } + private static MessageType GetMessageType(TriMessageType type) + { + switch (type) + { + case TriMessageType.None: return MessageType.None; + case TriMessageType.Info: return MessageType.Info; + case TriMessageType.Warning: return MessageType.Warning; + case TriMessageType.Error: return MessageType.Error; + default: return MessageType.None; + } + } + private static class Styles { public static readonly GUIStyle InfoBoxBg; diff --git a/Editor/TriProperty.cs b/Editor/TriProperty.cs index dd4046d..1f22b71 100644 --- a/Editor/TriProperty.cs +++ b/Editor/TriProperty.cs @@ -13,7 +13,7 @@ namespace TriInspector { private static readonly IReadOnlyList EmptyValidationResults = new List(); - + private readonly TriPropertyDefinition _definition; private readonly int _propertyIndex; private readonly ITriPropertyParent _parent; @@ -43,7 +43,7 @@ namespace TriInspector [PublicAPI] public string DisplayName => DisplayNameContent.text; - + [PublicAPI] public GUIContent DisplayNameContent { @@ -231,7 +231,7 @@ namespace TriInspector // actualize PropertyTree.SerializedObject.Update(); Update(); - + PropertyTree.RequestValidation(); } @@ -325,10 +325,10 @@ namespace TriInspector { _validationResults = _definition.Validators .Select(it => it.Validate(this)) - .Where(it => it.MessageType != MessageType.None) + .Where(it => !it.IsValid) .ToList(); } - + if (_childrenProperties != null) { foreach (var childrenProperty in _childrenProperties) diff --git a/Editor/TriValidator.cs b/Editor/TriValidator.cs index 5965304..2f593d2 100644 --- a/Editor/TriValidator.cs +++ b/Editor/TriValidator.cs @@ -38,28 +38,4 @@ namespace TriInspector [PublicAPI] public abstract TriValidationResult Validate(TriValue propertyValue); } - - public readonly struct TriValidationResult - { - public static TriValidationResult Valid => new TriValidationResult(null, MessageType.None); - - private TriValidationResult(string message, MessageType messageType) - { - Message = message; - MessageType = messageType; - } - - public string Message { get; } - public MessageType MessageType { get; } - - public static TriValidationResult Error(string error) - { - return new TriValidationResult(error, MessageType.Error); - } - - public static TriValidationResult Warning(string error) - { - return new TriValidationResult(error, MessageType.Warning); - } - } } \ No newline at end of file diff --git a/Runtime/TriValidationResult.cs b/Runtime/TriValidationResult.cs new file mode 100644 index 0000000..cd87ec6 --- /dev/null +++ b/Runtime/TriValidationResult.cs @@ -0,0 +1,36 @@ +namespace TriInspector +{ + public readonly struct TriValidationResult + { + public static TriValidationResult Valid => new TriValidationResult(true, null, TriMessageType.None); + + private TriValidationResult(bool valid, string message, TriMessageType messageType) + { + IsValid = valid; + Message = message; + MessageType = messageType; + } + + public bool IsValid { get; } + public string Message { get; } + public TriMessageType MessageType { get; } + + public static TriValidationResult Error(string error) + { + return new TriValidationResult(false, error, TriMessageType.Error); + } + + public static TriValidationResult Warning(string error) + { + return new TriValidationResult(false, error, TriMessageType.Warning); + } + } + + public enum TriMessageType + { + None, + Info, + Warning, + Error, + } +} \ No newline at end of file diff --git a/Runtime/TriValidationResult.cs.meta b/Runtime/TriValidationResult.cs.meta new file mode 100644 index 0000000..7607542 --- /dev/null +++ b/Runtime/TriValidationResult.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 428f8881a0d34edf8148591af6f9a0e7 +timeCreated: 1642525992 \ No newline at end of file