Move TriValidationResult to runtime assembly

This commit is contained in:
VladV 2022-01-18 20:19:15 +03:00
parent 2303292347
commit 849da7e8a7
6 changed files with 63 additions and 35 deletions

View File

@ -19,7 +19,7 @@ namespace TriInspector.Drawers
{ {
var stack = new TriElement(); var stack = new TriElement();
stack.AddChild(new TriInfoBoxElement($"InlineEditor valid only on Object fields", stack.AddChild(new TriInfoBoxElement($"InlineEditor valid only on Object fields",
MessageType.Error)); TriMessageType.Error));
stack.AddChild(next); stack.AddChild(next);
return stack; return stack;

View File

@ -9,9 +9,10 @@ namespace TriInspector.Elements
private readonly GUIContent _message; private readonly GUIContent _message;
private readonly Color _color; 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); _color = color ?? GetColor(type);
} }
@ -30,14 +31,14 @@ namespace TriInspector.Elements
GUI.Label(position, _message, Styles.InfoBoxContent); GUI.Label(position, _message, Styles.InfoBoxContent);
} }
private static Color GetColor(MessageType type) private static Color GetColor(TriMessageType type)
{ {
switch (type) switch (type)
{ {
case MessageType.Error: case TriMessageType.Error:
return new Color(1f, 0.4f, 0.4f); return new Color(1f, 0.4f, 0.4f);
case MessageType.Warning: case TriMessageType.Warning:
return new Color(1f, 0.8f, 0.2f); return new Color(1f, 0.8f, 0.2f);
default: 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 private static class Styles
{ {
public static readonly GUIStyle InfoBoxBg; public static readonly GUIStyle InfoBoxBg;

View File

@ -325,7 +325,7 @@ namespace TriInspector
{ {
_validationResults = _definition.Validators _validationResults = _definition.Validators
.Select(it => it.Validate(this)) .Select(it => it.Validate(this))
.Where(it => it.MessageType != MessageType.None) .Where(it => !it.IsValid)
.ToList(); .ToList();
} }

View File

@ -38,28 +38,4 @@ namespace TriInspector
[PublicAPI] [PublicAPI]
public abstract TriValidationResult Validate(TriValue<T> propertyValue); public abstract TriValidationResult Validate(TriValue<T> 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);
}
}
} }

View File

@ -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,
}
}

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 428f8881a0d34edf8148591af6f9a0e7
timeCreated: 1642525992