Tri-Inspector/Editor/TriPropertyTree.cs

120 lines
3.4 KiB
C#
Raw Normal View History

2021-12-07 10:20:36 -05:00
using System;
using TriInspector.Elements;
using UnityEditor;
using UnityEngine;
2023-09-16 05:48:55 -04:00
using UnityEngine.Profiling;
2021-12-07 10:20:36 -05:00
namespace TriInspector
{
2023-09-26 03:20:32 -04:00
public abstract class TriPropertyTree : IDisposable
2021-12-07 10:20:36 -05:00
{
2022-06-01 03:35:33 -04:00
private TriPropertyElement _rootPropertyElement;
2023-03-22 03:38:33 -04:00
private Rect _cachedOuterRect = new Rect(0, 0, 0, 0);
2021-12-07 10:20:36 -05:00
2022-06-01 03:35:33 -04:00
public TriPropertyDefinition RootPropertyDefinition { get; protected set; }
public TriProperty RootProperty { get; protected set; }
public Type TargetObjectType { get; protected set; }
public int TargetsCount { get; protected set; }
public bool TargetIsPersistent { get; protected set; }
2022-05-29 07:07:15 -04:00
public bool ValidationRequired { get; private set; } = true;
public bool RepaintRequired { get; private set; } = true;
2021-12-07 10:20:36 -05:00
2022-05-29 07:41:40 -04:00
public int RepaintFrame { get; private set; } = 0;
public virtual void Dispose()
2021-12-07 10:20:36 -05:00
{
2022-06-01 03:35:33 -04:00
if (_rootPropertyElement != null && _rootPropertyElement.IsAttached)
2022-01-08 12:45:11 -05:00
{
2022-06-01 03:35:33 -04:00
_rootPropertyElement.DetachInternal();
2022-01-08 12:45:11 -05:00
}
2021-12-07 10:20:36 -05:00
}
2022-06-02 03:18:13 -04:00
public virtual void Update(bool forceUpdate = false)
2021-12-07 10:20:36 -05:00
{
2022-05-29 07:41:40 -04:00
RepaintFrame++;
2021-12-07 10:20:36 -05:00
}
2022-06-02 03:18:13 -04:00
public virtual bool ApplyChanges()
{
return false;
}
2023-03-22 03:49:53 -04:00
public void RunValidationIfRequired()
{
if (!ValidationRequired)
{
return;
}
2023-09-16 05:48:55 -04:00
2023-03-22 03:49:53 -04:00
RunValidation();
}
public void RunValidation()
2022-01-15 11:25:12 -05:00
{
ValidationRequired = false;
2023-09-16 05:48:55 -04:00
Profiler.BeginSample("TriInspector.RunValidation");
2022-06-01 03:35:33 -04:00
RootProperty.RunValidation();
2023-09-16 05:48:55 -04:00
Profiler.EndSample();
2022-01-20 05:05:35 -05:00
2022-01-19 05:16:26 -05:00
RequestRepaint();
2022-01-15 11:25:12 -05:00
}
2023-03-22 03:38:33 -04:00
public virtual void Draw()
2021-12-07 10:20:36 -05:00
{
RepaintRequired = false;
2022-06-01 03:35:33 -04:00
if (_rootPropertyElement == null)
{
2022-06-01 03:35:33 -04:00
_rootPropertyElement = new TriPropertyElement(RootProperty, new TriPropertyElement.Props
{
2022-08-17 13:46:26 -04:00
forceInline = !RootProperty.TryGetMemberInfo(out _),
2022-06-01 03:35:33 -04:00
});
_rootPropertyElement.AttachInternal();
}
2023-09-16 05:48:55 -04:00
Profiler.BeginSample("TriInspector.UpdateRootPropertyElement");
2022-06-01 03:35:33 -04:00
_rootPropertyElement.Update();
2023-09-16 05:48:55 -04:00
Profiler.EndSample();
2022-08-27 08:24:46 -04:00
2023-03-22 03:38:33 -04:00
var rectOuter = GUILayoutUtility.GetRect(0, 9999, 0, 0);
_cachedOuterRect = Event.current.type == EventType.Layout ? _cachedOuterRect : rectOuter;
2022-08-27 08:24:46 -04:00
2023-03-22 03:38:33 -04:00
var rect = new Rect(_cachedOuterRect);
2022-08-28 03:42:45 -04:00
rect = EditorGUI.IndentedRect(rect);
2023-03-22 03:38:33 -04:00
rect.height = _rootPropertyElement.GetHeight(rect.width);
2022-08-28 03:42:45 -04:00
var oldIndent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
2023-03-22 03:38:33 -04:00
GUILayoutUtility.GetRect(_cachedOuterRect.width, rect.height);
2023-09-16 05:48:55 -04:00
Profiler.BeginSample("TriInspector.DrawRootPropertyElement");
2022-06-01 03:35:33 -04:00
_rootPropertyElement.OnGUI(rect);
2023-09-16 05:48:55 -04:00
Profiler.EndSample();
2022-08-28 03:42:45 -04:00
EditorGUI.indentLevel = oldIndent;
2021-12-07 10:20:36 -05:00
}
2022-01-08 12:45:11 -05:00
public void EnumerateValidationResults(Action<TriProperty, TriValidationResult> call)
2022-05-18 02:20:51 -04:00
{
2022-06-01 03:35:33 -04:00
RootProperty.EnumerateValidationResults(call);
2022-05-11 04:54:19 -04:00
}
2022-11-11 12:17:12 -05:00
public void RequestRepaint()
2022-01-08 12:45:11 -05:00
{
RepaintRequired = true;
}
2022-01-15 11:25:12 -05:00
public void RequestValidation()
{
ValidationRequired = true;
RequestRepaint();
2022-01-15 11:25:12 -05:00
}
2022-01-08 12:45:11 -05:00
public abstract void ForceCreateUndoGroup();
2022-05-17 06:33:23 -04:00
}
2021-12-07 10:20:36 -05:00
}