Prepare for Odin integration

This commit is contained in:
VladV 2022-05-17 13:33:23 +03:00
parent 36a78379c4
commit 7d48204c2d
7 changed files with 51 additions and 32 deletions

View File

@ -65,8 +65,7 @@ namespace TriInspector.Drawers
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
var allowSceneObjects = _property.PropertyTree.TargetObjects[0] is var targetObject && var allowSceneObjects = _property.PropertyTree.TargetIsPersistent;
targetObject != null && !EditorUtility.IsPersistent(targetObject);
var value = (Object) _property.Value; var value = (Object) _property.Value;
value = EditorGUI.ObjectField(pickerRect, GUIContent.none, value, value = EditorGUI.ObjectField(pickerRect, GUIContent.none, value,

View File

@ -40,8 +40,7 @@ namespace TriInspector.Drawers
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
var allowSceneObjects = _propertyValue.Property.PropertyTree.TargetObjects[0] is var targetObject && var allowSceneObjects = _propertyValue.Property.PropertyTree.TargetIsPersistent;
targetObject != null && !EditorUtility.IsPersistent(targetObject);
value = (T) EditorGUI.ObjectField(position, _propertyValue.Property.DisplayNameContent, value, value = (T) EditorGUI.ObjectField(position, _propertyValue.Property.DisplayNameContent, value,
_propertyValue.Property.FieldType, allowSceneObjects); _propertyValue.Property.FieldType, allowSceneObjects);

View File

@ -1,12 +1,15 @@
namespace TriInspector.Elements using System;
using System.Collections.Generic;
namespace TriInspector.Elements
{ {
internal class TriInspectorElement : TriPropertyCollectionBaseElement internal class TriInspectorElement : TriPropertyCollectionBaseElement
{ {
public TriInspectorElement(TriPropertyTree propertyTree) public TriInspectorElement(Type targetObjectType, IReadOnlyList<TriProperty> properties)
{ {
DeclareGroups(propertyTree.TargetObjectType); DeclareGroups(targetObjectType);
foreach (var childProperty in propertyTree.Properties) foreach (var childProperty in properties)
{ {
AddProperty(childProperty); AddProperty(childProperty);
} }

View File

@ -23,7 +23,7 @@ namespace TriInspector.Resolvers
[PublicAPI] [PublicAPI]
public void InvokeForAllTargets(TriProperty property) 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); InvokeForTarget(property, targetIndex);
} }

View File

@ -29,7 +29,7 @@ namespace TriInspector
private void OnDisable() private void OnDisable()
{ {
_inspector?.Destroy(); _inspector?.Dispose();
_inspector = null; _inspector = null;
} }

View File

@ -27,7 +27,7 @@ namespace TriInspector
private string _isExpandedPrefsKey; private string _isExpandedPrefsKey;
internal TriProperty( internal TriProperty(
TriPropertyTree propertyTree, ITriPropertyTree propertyTree,
ITriPropertyParent parent, ITriPropertyParent parent,
TriPropertyDefinition definition, TriPropertyDefinition definition,
int propertyIndex, int propertyIndex,
@ -219,7 +219,7 @@ namespace TriInspector
: throw new InvalidOperationException("Cannot read ArrayElementProperties for " + PropertyType); : throw new InvalidOperationException("Cannot read ArrayElementProperties for " + PropertyType);
[PublicAPI] [PublicAPI]
public TriPropertyTree PropertyTree { get; } public ITriPropertyTree PropertyTree { get; }
[PublicAPI] [PublicAPI]
[CanBeNull] [CanBeNull]
@ -249,22 +249,19 @@ namespace TriInspector
public void ModifyAndRecordForUndo(Action<int> call) public void ModifyAndRecordForUndo(Action<int> call)
{ {
// save any pending changes // save any pending changes
PropertyTree.ApplySerializedObjectModifiedProperties(); PropertyTree.PrepareForValueModification();
// record object state for undp // record object state for undp
Undo.RegisterCompleteObjectUndo(PropertyTree.TargetObjects, "Inspector");
Undo.FlushUndoRecordObjects(); Undo.FlushUndoRecordObjects();
// set value for all targets // 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); call.Invoke(targetIndex);
EditorUtility.SetDirty(PropertyTree.TargetObjects[targetIndex]);
} }
// actualize // actualize
PropertyTree.ForceUpdateSerializedObject(); PropertyTree.UpdateAfterValueModification();
Update(); Update();
NotifyValueChanged(); NotifyValueChanged();
@ -281,10 +278,9 @@ namespace TriInspector
{ {
_serializedProperty?.serializedObject.ApplyModifiedProperties(); _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); _definition.OnValueChanged.InvokeForTarget(this, targetIndex);
EditorUtility.SetDirty(PropertyTree.TargetObjects[targetIndex]);
} }
} }
@ -461,7 +457,7 @@ namespace TriInspector
{ {
newValue = property.GetValue(0); newValue = property.GetValue(0);
if (property.PropertyTree.TargetObjects.Length == 1) if (property.PropertyTree.TargetsCount == 1)
{ {
isMixed = false; isMixed = false;
return; return;
@ -472,7 +468,7 @@ namespace TriInspector
case TriPropertyType.Array: case TriPropertyType.Array:
{ {
var list = (IList) newValue; 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) if (list == null)
{ {
@ -491,7 +487,7 @@ namespace TriInspector
} }
case TriPropertyType.Reference: 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); var otherValue = property.GetValue(i);
@ -513,7 +509,7 @@ namespace TriInspector
} }
case TriPropertyType.Primitive: 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); var otherValue = property.GetValue(i);
if (!AreValuesEqual(property.FieldType, otherValue, newValue)) if (!AreValuesEqual(property.FieldType, otherValue, newValue))

View File

@ -9,7 +9,7 @@ using Object = UnityEngine.Object;
namespace TriInspector namespace TriInspector
{ {
public sealed class TriPropertyTree : ITriPropertyParent public sealed class TriPropertyTree : ITriPropertyParent, ITriPropertyTree
{ {
private readonly TriEditorMode _mode; private readonly TriEditorMode _mode;
private readonly TriInspectorElement _inspectorElement; private readonly TriInspectorElement _inspectorElement;
@ -19,7 +19,6 @@ namespace TriInspector
SerializedObject = serializedObject ?? throw new ArgumentNullException(nameof(serializedObject)); SerializedObject = serializedObject ?? throw new ArgumentNullException(nameof(serializedObject));
TargetObjects = serializedObject.targetObjects; TargetObjects = serializedObject.targetObjects;
TargetObjectType = TargetObjects[0].GetType(); TargetObjectType = TargetObjects[0].GetType();
Root = this;
Properties = TriTypeDefinition.GetCached(TargetObjectType) Properties = TriTypeDefinition.GetCached(TargetObjectType)
.Properties .Properties
@ -31,7 +30,7 @@ namespace TriInspector
.ToList(); .ToList();
_mode = mode; _mode = mode;
_inspectorElement = new TriInspectorElement(this); _inspectorElement = new TriInspectorElement(TargetObjectType, Properties);
_inspectorElement.AttachInternal(); _inspectorElement.AttachInternal();
Update(); Update();
@ -47,12 +46,16 @@ namespace TriInspector
[PublicAPI] [PublicAPI]
public Type TargetObjectType { get; } public Type TargetObjectType { get; }
[PublicAPI]
public int TargetsCount => TargetObjects.Length;
private SerializedObject SerializedObject { get; } private SerializedObject SerializedObject { get; }
public TriPropertyTree Root { get; }
public bool IsInlineEditor => (_mode & TriEditorMode.InlineEditor) != 0; 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 RepaintRequired { get; set; }
internal bool ValidationRequired { get; set; } internal bool ValidationRequired { get; set; }
@ -64,7 +67,7 @@ namespace TriInspector
return new TriPropertyTree(scriptableObject, mode); return new TriPropertyTree(scriptableObject, mode);
} }
internal void Destroy() public void Dispose()
{ {
if (!_inspectorElement.IsAttached) if (!_inspectorElement.IsAttached)
{ {
@ -101,23 +104,31 @@ namespace TriInspector
_inspectorElement.OnGUI(rect); _inspectorElement.OnGUI(rect);
} }
public void ForceUpdateSerializedObject() public void UpdateAfterValueModification()
{ {
SerializedObject.SetIsDifferentCacheDirty(); SerializedObject.SetIsDifferentCacheDirty();
SerializedObject.Update(); SerializedObject.Update();
} }
public void ApplySerializedObjectModifiedProperties() public void PrepareForValueModification()
{ {
if (SerializedObject.ApplyModifiedProperties()) if (SerializedObject.ApplyModifiedProperties())
{ {
RequestValidation(); RequestValidation();
RequestRepaint(); RequestRepaint();
} }
Undo.RegisterCompleteObjectUndo(TargetObjects, "Inspector");
Undo.FlushUndoRecordObjects();
} }
public void NotifyValueChanged(TriProperty property) public void NotifyValueChanged(TriProperty property)
{ {
foreach (var targetObject in TargetObjects)
{
EditorUtility.SetDirty(targetObject);
}
RequestValidation(); 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] [Flags]
public enum TriEditorMode public enum TriEditorMode
{ {