From 7d48204c2d110066de6197134de9c2f4a13f313d Mon Sep 17 00:00:00 2001 From: VladV Date: Tue, 17 May 2022 13:33:23 +0300 Subject: [PATCH] Prepare for Odin integration --- Editor.Extras/Drawers/InlineEditorDrawer.cs | 3 +- .../Drawers/ObjectReferenceDrawer.cs | 3 +- Editor/Elements/TriInspectorElement.cs | 11 ++++-- Editor/Resolvers/ActionResolver.cs | 2 +- Editor/TriEditor.cs | 2 +- Editor/TriProperty.cs | 24 +++++------- Editor/TriPropertyTree.cs | 38 +++++++++++++++---- 7 files changed, 51 insertions(+), 32 deletions(-) diff --git a/Editor.Extras/Drawers/InlineEditorDrawer.cs b/Editor.Extras/Drawers/InlineEditorDrawer.cs index 870f7df..2432e04 100644 --- a/Editor.Extras/Drawers/InlineEditorDrawer.cs +++ b/Editor.Extras/Drawers/InlineEditorDrawer.cs @@ -65,8 +65,7 @@ namespace TriInspector.Drawers EditorGUI.BeginChangeCheck(); - var allowSceneObjects = _property.PropertyTree.TargetObjects[0] is var targetObject && - targetObject != null && !EditorUtility.IsPersistent(targetObject); + var allowSceneObjects = _property.PropertyTree.TargetIsPersistent; var value = (Object) _property.Value; value = EditorGUI.ObjectField(pickerRect, GUIContent.none, value, diff --git a/Editor.Extras/Drawers/ObjectReferenceDrawer.cs b/Editor.Extras/Drawers/ObjectReferenceDrawer.cs index a2ef879..7e7f597 100644 --- a/Editor.Extras/Drawers/ObjectReferenceDrawer.cs +++ b/Editor.Extras/Drawers/ObjectReferenceDrawer.cs @@ -40,8 +40,7 @@ namespace TriInspector.Drawers EditorGUI.BeginChangeCheck(); - var allowSceneObjects = _propertyValue.Property.PropertyTree.TargetObjects[0] is var targetObject && - targetObject != null && !EditorUtility.IsPersistent(targetObject); + var allowSceneObjects = _propertyValue.Property.PropertyTree.TargetIsPersistent; value = (T) EditorGUI.ObjectField(position, _propertyValue.Property.DisplayNameContent, value, _propertyValue.Property.FieldType, allowSceneObjects); diff --git a/Editor/Elements/TriInspectorElement.cs b/Editor/Elements/TriInspectorElement.cs index 2511520..79b9472 100644 --- a/Editor/Elements/TriInspectorElement.cs +++ b/Editor/Elements/TriInspectorElement.cs @@ -1,12 +1,15 @@ -namespace TriInspector.Elements +using System; +using System.Collections.Generic; + +namespace TriInspector.Elements { internal class TriInspectorElement : TriPropertyCollectionBaseElement { - public TriInspectorElement(TriPropertyTree propertyTree) + public TriInspectorElement(Type targetObjectType, IReadOnlyList properties) { - DeclareGroups(propertyTree.TargetObjectType); + DeclareGroups(targetObjectType); - foreach (var childProperty in propertyTree.Properties) + foreach (var childProperty in properties) { AddProperty(childProperty); } diff --git a/Editor/Resolvers/ActionResolver.cs b/Editor/Resolvers/ActionResolver.cs index 7aa0c08..e7d3b81 100644 --- a/Editor/Resolvers/ActionResolver.cs +++ b/Editor/Resolvers/ActionResolver.cs @@ -23,7 +23,7 @@ namespace TriInspector.Resolvers [PublicAPI] public void InvokeForAllTargets(TriProperty property) { - for (var targetIndex = 0; targetIndex < property.PropertyTree.TargetObjects.Length; targetIndex++) + for (var targetIndex = 0; targetIndex < property.PropertyTree.TargetsCount; targetIndex++) { InvokeForTarget(property, targetIndex); } diff --git a/Editor/TriEditor.cs b/Editor/TriEditor.cs index 8000241..e743672 100644 --- a/Editor/TriEditor.cs +++ b/Editor/TriEditor.cs @@ -29,7 +29,7 @@ namespace TriInspector private void OnDisable() { - _inspector?.Destroy(); + _inspector?.Dispose(); _inspector = null; } diff --git a/Editor/TriProperty.cs b/Editor/TriProperty.cs index 7e7bd38..cd1c243 100644 --- a/Editor/TriProperty.cs +++ b/Editor/TriProperty.cs @@ -27,7 +27,7 @@ namespace TriInspector private string _isExpandedPrefsKey; internal TriProperty( - TriPropertyTree propertyTree, + ITriPropertyTree propertyTree, ITriPropertyParent parent, TriPropertyDefinition definition, int propertyIndex, @@ -219,7 +219,7 @@ namespace TriInspector : throw new InvalidOperationException("Cannot read ArrayElementProperties for " + PropertyType); [PublicAPI] - public TriPropertyTree PropertyTree { get; } + public ITriPropertyTree PropertyTree { get; } [PublicAPI] [CanBeNull] @@ -249,22 +249,19 @@ namespace TriInspector public void ModifyAndRecordForUndo(Action call) { // save any pending changes - PropertyTree.ApplySerializedObjectModifiedProperties(); + PropertyTree.PrepareForValueModification(); // record object state for undp - Undo.RegisterCompleteObjectUndo(PropertyTree.TargetObjects, "Inspector"); Undo.FlushUndoRecordObjects(); // set value for all targets - for (var targetIndex = 0; targetIndex < PropertyTree.TargetObjects.Length; targetIndex++) + for (var targetIndex = 0; targetIndex < PropertyTree.TargetsCount; targetIndex++) { call.Invoke(targetIndex); - - EditorUtility.SetDirty(PropertyTree.TargetObjects[targetIndex]); } // actualize - PropertyTree.ForceUpdateSerializedObject(); + PropertyTree.UpdateAfterValueModification(); Update(); NotifyValueChanged(); @@ -281,10 +278,9 @@ namespace TriInspector { _serializedProperty?.serializedObject.ApplyModifiedProperties(); - for (var targetIndex = 0; targetIndex < PropertyTree.TargetObjects.Length; targetIndex++) + for (var targetIndex = 0; targetIndex < PropertyTree.TargetsCount; targetIndex++) { _definition.OnValueChanged.InvokeForTarget(this, targetIndex); - EditorUtility.SetDirty(PropertyTree.TargetObjects[targetIndex]); } } @@ -461,7 +457,7 @@ namespace TriInspector { newValue = property.GetValue(0); - if (property.PropertyTree.TargetObjects.Length == 1) + if (property.PropertyTree.TargetsCount == 1) { isMixed = false; return; @@ -472,7 +468,7 @@ namespace TriInspector case TriPropertyType.Array: { var list = (IList) newValue; - for (var i = 1; i < property.PropertyTree.TargetObjects.Length; i++) + for (var i = 1; i < property.PropertyTree.TargetsCount; i++) { if (list == null) { @@ -491,7 +487,7 @@ namespace TriInspector } case TriPropertyType.Reference: { - for (var i = 1; i < property.PropertyTree.TargetObjects.Length; i++) + for (var i = 1; i < property.PropertyTree.TargetsCount; i++) { var otherValue = property.GetValue(i); @@ -513,7 +509,7 @@ namespace TriInspector } case TriPropertyType.Primitive: { - for (var i = 1; i < property.PropertyTree.TargetObjects.Length; i++) + for (var i = 1; i < property.PropertyTree.TargetsCount; i++) { var otherValue = property.GetValue(i); if (!AreValuesEqual(property.FieldType, otherValue, newValue)) diff --git a/Editor/TriPropertyTree.cs b/Editor/TriPropertyTree.cs index e8bdf07..52133bc 100644 --- a/Editor/TriPropertyTree.cs +++ b/Editor/TriPropertyTree.cs @@ -9,7 +9,7 @@ using Object = UnityEngine.Object; namespace TriInspector { - public sealed class TriPropertyTree : ITriPropertyParent + public sealed class TriPropertyTree : ITriPropertyParent, ITriPropertyTree { private readonly TriEditorMode _mode; private readonly TriInspectorElement _inspectorElement; @@ -19,7 +19,6 @@ namespace TriInspector SerializedObject = serializedObject ?? throw new ArgumentNullException(nameof(serializedObject)); TargetObjects = serializedObject.targetObjects; TargetObjectType = TargetObjects[0].GetType(); - Root = this; Properties = TriTypeDefinition.GetCached(TargetObjectType) .Properties @@ -31,7 +30,7 @@ namespace TriInspector .ToList(); _mode = mode; - _inspectorElement = new TriInspectorElement(this); + _inspectorElement = new TriInspectorElement(TargetObjectType, Properties); _inspectorElement.AttachInternal(); Update(); @@ -47,12 +46,16 @@ namespace TriInspector [PublicAPI] public Type TargetObjectType { get; } + [PublicAPI] + public int TargetsCount => TargetObjects.Length; + private SerializedObject SerializedObject { get; } - public TriPropertyTree Root { get; } - public bool IsInlineEditor => (_mode & TriEditorMode.InlineEditor) != 0; + public bool TargetIsPersistent => TargetObjects[0] is var targetObject && + targetObject != null && !EditorUtility.IsPersistent(targetObject); + internal bool RepaintRequired { get; set; } internal bool ValidationRequired { get; set; } @@ -64,7 +67,7 @@ namespace TriInspector return new TriPropertyTree(scriptableObject, mode); } - internal void Destroy() + public void Dispose() { if (!_inspectorElement.IsAttached) { @@ -101,23 +104,31 @@ namespace TriInspector _inspectorElement.OnGUI(rect); } - public void ForceUpdateSerializedObject() + public void UpdateAfterValueModification() { SerializedObject.SetIsDifferentCacheDirty(); SerializedObject.Update(); } - public void ApplySerializedObjectModifiedProperties() + public void PrepareForValueModification() { if (SerializedObject.ApplyModifiedProperties()) { RequestValidation(); RequestRepaint(); } + + Undo.RegisterCompleteObjectUndo(TargetObjects, "Inspector"); + Undo.FlushUndoRecordObjects(); } public void NotifyValueChanged(TriProperty property) { + foreach (var targetObject in TargetObjects) + { + EditorUtility.SetDirty(targetObject); + } + RequestValidation(); } @@ -132,6 +143,17 @@ namespace TriInspector } } + public interface ITriPropertyTree : IDisposable + { + Type TargetObjectType { get; } + int TargetsCount { get; } + bool TargetIsPersistent { get; } + + void PrepareForValueModification(); + void UpdateAfterValueModification(); + void RequestRepaint(); + } + [Flags] public enum TriEditorMode {