Tri-Inspector/Editor/TriPropertyTree.cs

120 lines
3.3 KiB
C#
Raw Normal View History

2021-12-07 10:20:36 -05:00
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
using TriInspector.Elements;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
namespace TriInspector
{
public sealed class TriPropertyTree : ITriPropertyParent
{
2022-01-08 12:45:11 -05:00
private readonly TriEditorMode _mode;
2021-12-07 10:20:36 -05:00
private readonly TriInspectorElement _inspectorElement;
2022-01-08 12:45:11 -05:00
private TriPropertyTree([NotNull] SerializedObject serializedObject, TriEditorMode mode)
2021-12-07 10:20:36 -05:00
{
2022-01-07 10:36:38 -05:00
SerializedObject = serializedObject ?? throw new ArgumentNullException(nameof(serializedObject));
2021-12-07 10:20:36 -05:00
TargetObjects = serializedObject.targetObjects;
TargetObjectType = TargetObjects[0].GetType();
Root = this;
Properties = TriTypeDefinition.GetCached(TargetObjectType)
.Properties
2022-01-06 10:43:09 -05:00
.Select((propertyDefinition, index) =>
2021-12-07 10:20:36 -05:00
{
var serializedProperty = serializedObject.FindProperty(propertyDefinition.Name);
2022-01-06 10:43:09 -05:00
return new TriProperty(this, this, propertyDefinition, index, serializedProperty);
2021-12-07 10:20:36 -05:00
})
.ToList();
2022-01-15 11:25:12 -05:00
ValidationRequired = true;
2022-01-08 12:45:11 -05:00
_mode = mode;
2021-12-07 10:20:36 -05:00
_inspectorElement = new TriInspectorElement(this);
_inspectorElement.AttachInternal();
}
[PublicAPI]
public IReadOnlyList<TriProperty> Properties { get; }
[PublicAPI]
public Object[] TargetObjects { get; }
[PublicAPI]
public Type TargetObjectType { get; }
2022-01-07 10:36:38 -05:00
[PublicAPI]
public SerializedObject SerializedObject { get; }
2021-12-07 10:20:36 -05:00
public TriPropertyTree Root { get; }
2022-01-08 12:45:11 -05:00
public bool IsInlineEditor => (_mode & TriEditorMode.InlineEditor) != 0;
internal bool RepaintRequired { get; set; }
2022-01-15 11:25:12 -05:00
internal bool ValidationRequired { get; set; }
2022-01-08 12:45:11 -05:00
2021-12-07 10:20:36 -05:00
object ITriPropertyParent.GetValue(int targetIndex) => TargetObjects[targetIndex];
2022-01-08 12:45:11 -05:00
internal static TriPropertyTree Create(SerializedObject scriptableObject,
TriEditorMode mode = TriEditorMode.None)
2021-12-07 10:20:36 -05:00
{
2022-01-08 12:45:11 -05:00
return new TriPropertyTree(scriptableObject, mode);
2021-12-07 10:20:36 -05:00
}
internal void Destroy()
{
2022-01-08 12:45:11 -05:00
if (!_inspectorElement.IsAttached)
{
return;
}
2021-12-07 10:20:36 -05:00
_inspectorElement.DetachInternal();
}
internal void Update()
{
foreach (var property in Properties)
{
property.Update();
}
_inspectorElement.Update();
}
2022-01-15 11:25:12 -05:00
internal void RunValidation()
{
foreach (var property in Properties)
{
property.RunValidation();
}
}
2021-12-07 10:20:36 -05:00
internal void DoLayout()
{
var width = EditorGUIUtility.currentViewWidth;
var height = _inspectorElement.GetHeight(width);
var rect = GUILayoutUtility.GetRect(width, height);
_inspectorElement.OnGUI(rect);
}
2022-01-08 12:45:11 -05:00
public void RequestRepaint()
{
RepaintRequired = true;
}
2022-01-15 11:25:12 -05:00
public void RequestValidation()
{
ValidationRequired = true;
}
2022-01-08 12:45:11 -05:00
}
[Flags]
public enum TriEditorMode
{
None = 0,
InlineEditor = 1 << 0,
2021-12-07 10:20:36 -05:00
}
}