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

108 lines
2.9 KiB
C#
Raw Normal View History

using System;
using Sirenix.OdinInspector.Editor;
2022-11-11 12:17:12 -05:00
using Sirenix.Utilities.Editor;
using UnityEngine;
namespace TriInspector.Editor.Integrations.Odin
{
[DrawerPriority(0.0, 10000.0, 1.0)]
public class OdinFieldDrawer<T> : OdinValueDrawer<T>, IDisposable
{
2022-06-06 12:17:31 -04:00
private bool _initialized;
private TriPropertyTree _propertyTree;
2022-06-01 03:35:33 -04:00
private LabelOverrideContext _labelOverrideContext;
public override bool CanDrawTypeFilter(Type type)
{
2022-06-06 12:17:31 -04:00
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;
}
return true;
}
protected override bool CanDrawValueProperty(InspectorProperty property)
{
if (property.IsTreeRoot)
{
return false;
}
for (var parent = property.Parent; parent != null; parent = parent.Parent)
{
var parentType = parent.ValueEntry.TypeOfValue;
2022-08-28 08:29:49 -04:00
if (TriOdinUtility.IsDrawnByTri(parentType))
{
return false;
}
}
return true;
}
public void Dispose()
{
_propertyTree?.Dispose();
}
protected override void DrawPropertyLayout(GUIContent label)
{
2022-06-06 12:17:31 -04:00
if (!_initialized)
{
_initialized = true;
_propertyTree = new TriPropertyTreeForOdin<T>(ValueEntry);
_labelOverrideContext = new LabelOverrideContext(_propertyTree);
}
2022-06-01 03:35:33 -04:00
_propertyTree.Update();
2023-03-22 03:49:53 -04:00
_propertyTree.RunValidationIfRequired();
2022-06-01 03:35:33 -04:00
_labelOverrideContext.Label = label ?? GUIContent.none;
using (TriPropertyOverrideContext.BeginOverride(_labelOverrideContext))
{
2022-06-01 03:35:33 -04:00
_propertyTree.Draw();
}
2022-11-11 12:17:12 -05:00
if (_propertyTree.RepaintRequired)
{
GUIHelper.RequestRepaint();
}
2022-06-01 03:35:33 -04:00
}
2022-06-01 03:35:33 -04:00
private class LabelOverrideContext : TriPropertyOverrideContext
{
private readonly TriPropertyTree _tree;
2022-06-01 03:35:33 -04:00
public LabelOverrideContext(TriPropertyTree tree)
{
_tree = tree;
}
public GUIContent Label { get; set; }
public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName)
{
if (property == _tree.RootProperty)
{
displayName = Label;
return true;
}
2022-06-01 03:35:33 -04:00
displayName = default;
return false;
}
}
}
}