Tri-Inspector/Editor.Integrations/Odin/OdinFieldValidator.cs

81 lines
2.1 KiB
C#
Raw Normal View History

using System;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector.Editor.Validation;
using TriInspector.Editor.Integrations.Odin;
#if TRIINSPECTOR_ODIN_FIELDS_INJECTOR
[assembly: RegisterValidator(typeof(OdinFieldValidator<>))]
namespace TriInspector.Editor.Integrations.Odin
{
public class OdinFieldValidator<T> : ValueValidator<T>, IDisposable
{
2022-06-06 12:17:31 -04:00
private bool _initialized;
private TriPropertyTreeForOdin<T> _propertyTree;
public override RevalidationCriteria RevalidationCriteria { get; }
= RevalidationCriteria.OnValueChangeOrChildValueChange;
public override bool CanValidateProperty(InspectorProperty property)
{
if (property.IsTreeRoot)
{
return false;
}
2022-06-06 12:17:31 -04:00
var type = property.Info.TypeOfValue;
if (type == null)
{
return false;
}
2022-08-28 08:29:49 -04:00
if (!TriOdinUtility.IsDrawnByTri(type))
{
return false;
}
2022-08-28 08:29:49 -04:00
if (typeof(UnityEngine.Object).IsAssignableFrom(type))
{
return false;
}
for (var parent = property.Parent; parent != null; parent = parent.Parent)
{
var valueEntry = parent.ValueEntry;
if (valueEntry == null)
{
continue;
}
if (TriOdinUtility.IsDrawnByTri(valueEntry.TypeOfValue))
{
return false;
}
}
return true;
}
public void Dispose()
{
2022-06-06 12:17:31 -04:00
_propertyTree?.Dispose();
}
protected override void Validate(ValidationResult result)
{
2022-06-06 12:17:31 -04:00
if (!_initialized)
{
_initialized = true;
_propertyTree = new TriPropertyTreeForOdin<T>(ValueEntry);
}
_propertyTree.Update();
_propertyTree.RunValidation();
_propertyTree.CopyValidationResultsTo(result);
}
}
}
#endif