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