Tri-Inspector/Editor/TriPropertyTree.cs

81 lines
2.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
{
private readonly TriInspectorElement _inspectorElement;
private TriPropertyTree(SerializedObject serializedObject)
{
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();
_inspectorElement = new TriInspectorElement(this);
_inspectorElement.AttachInternal();
}
[PublicAPI]
public IReadOnlyList<TriProperty> Properties { get; }
[PublicAPI]
public Object[] TargetObjects { get; }
[PublicAPI]
public Type TargetObjectType { get; }
public TriPropertyTree Root { get; }
object ITriPropertyParent.GetValue(int targetIndex) => TargetObjects[targetIndex];
void ITriPropertyParent.ApplyChildValueModifications(int targetIndex)
{
EditorUtility.SetDirty(TargetObjects[targetIndex]);
}
internal static TriPropertyTree Create(SerializedObject scriptableObject)
{
return new TriPropertyTree(scriptableObject);
}
internal void Destroy()
{
_inspectorElement.DetachInternal();
}
internal void Update()
{
foreach (var property in Properties)
{
property.Update();
}
_inspectorElement.Update();
}
internal void DoLayout()
{
var width = EditorGUIUtility.currentViewWidth;
var height = _inspectorElement.GetHeight(width);
var rect = GUILayoutUtility.GetRect(width, height);
_inspectorElement.OnGUI(rect);
}
}
}