Tri-Inspector/Editor/ValidatorsDrawer.cs

71 lines
1.9 KiB
C#
Raw Normal View History

2022-05-31 03:47:31 -04:00
using System.Collections.Generic;
using TriInspector.Elements;
using UnityEditor;
namespace TriInspector
{
internal class ValidatorsDrawer : TriCustomDrawer
{
public override TriElement CreateElementInternal(TriProperty property, TriElement next)
{
if (!property.HasValidators)
{
return next;
}
var element = new TriElement();
element.AddChild(new TriPropertyValidationResultElement(property));
element.AddChild(next);
return element;
}
2022-05-31 03:47:31 -04:00
public class TriPropertyValidationResultElement : TriElement
{
private readonly TriProperty _property;
private IReadOnlyList<TriValidationResult> _validationResults;
public TriPropertyValidationResultElement(TriProperty property)
{
_property = property;
}
public override float GetHeight(float width)
{
if (ChildrenCount == 0)
{
return -EditorGUIUtility.standardVerticalSpacing;
}
return base.GetHeight(width);
}
public override bool Update()
{
var dirty = base.Update();
dirty |= GenerateValidationResults();
return dirty;
}
private bool GenerateValidationResults()
{
if (ReferenceEquals(_property.ValidationResults, _validationResults))
{
return false;
}
_validationResults = _property.ValidationResults;
RemoveAllChildren();
foreach (var result in _validationResults)
{
AddChild(new TriInfoBoxElement(result.Message, result.MessageType));
}
return true;
}
}
}
}