Skip to content

Commit

Permalink
Rework - introduces root property
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Jun 1, 2022
1 parent d154c36 commit b1e66c6
Show file tree
Hide file tree
Showing 11 changed files with 196 additions and 134 deletions.
5 changes: 5 additions & 0 deletions Editor.Extras/Drawers/ObjectReferenceDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class ObjectReferenceDrawer<T> : TriValueDrawer<T>
{
public override TriElement CreateElement(TriValue<T> value, TriElement next)
{
if (value.Property.IsRootProperty)
{
return next;
}

if (value.Property.TryGetSerializedProperty(out _))
{
return next;
Expand Down
3 changes: 1 addition & 2 deletions Editor.Extras/Drawers/TableListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,7 @@ public TableListPropertyOverrideContext(TriProperty grandParentProperty)
public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName)
{
if (property.PropertyType == TriPropertyType.Primitive &&
property.Parent is TriProperty parentProperty &&
parentProperty.Parent == _grandParentProperty &&
property.Parent?.Parent == _grandParentProperty &&
!property.TryGetAttribute(out GroupAttribute _))
{
displayName = _noneLabel;
Expand Down
43 changes: 32 additions & 11 deletions Editor.Integrations/Odin/OdinFieldDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace TriInspector.Editor.Integrations.Odin
public class OdinFieldDrawer<T> : OdinValueDrawer<T>, IDisposable
{
private TriPropertyTree _propertyTree;
private LabelOverrideContext _labelOverrideContext;

public override bool CanDrawTypeFilter(Type type)
{
Expand Down Expand Up @@ -53,6 +54,7 @@ protected override void Initialize()
base.Initialize();

_propertyTree = new TriPropertyTreeForOdin<T>(ValueEntry);
_labelOverrideContext = new LabelOverrideContext(_propertyTree);
}

public void Dispose()
Expand All @@ -62,23 +64,42 @@ public void Dispose()

protected override void DrawPropertyLayout(GUIContent label)
{
var propertyState = ValueEntry.Property.State;
_propertyTree.Update();

propertyState.Expanded = SirenixEditorGUI.Foldout(propertyState.Expanded, label);
if (_propertyTree.ValidationRequired)
{
_propertyTree.RunValidation();
}

if (propertyState.Expanded)
_labelOverrideContext.Label = label ?? GUIContent.none;

using (TriPropertyOverrideContext.BeginOverride(_labelOverrideContext))
{
using (TriGuiHelper.PushIndentLevel())
{
_propertyTree.Update();
_propertyTree.Draw();
}
}

if (_propertyTree.ValidationRequired)
{
_propertyTree.RunValidation();
}
private class LabelOverrideContext : TriPropertyOverrideContext
{
private readonly TriPropertyTree _tree;

_propertyTree.Draw();
public LabelOverrideContext(TriPropertyTree tree)
{
_tree = tree;
}

public GUIContent Label { get; set; }

public override bool TryGetDisplayName(TriProperty property, out GUIContent displayName)
{
if (property == _tree.RootProperty)
{
displayName = Label;
return true;
}

displayName = default;
return false;
}
}
}
Expand Down
38 changes: 23 additions & 15 deletions Editor.Integrations/Odin/TriPropertyTreeForOdin.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Linq;
using Sirenix.OdinInspector.Editor;
using Sirenix.OdinInspector.Editor;
using Sirenix.Utilities.Editor;
using UnityEditor;
using Object = UnityEngine.Object;
Expand Down Expand Up @@ -30,14 +29,28 @@ public TriPropertyTreeForOdin(IPropertyValueEntry<T> odinValueEntry)

_odinProperty.Update();

Properties = TriTypeDefinition.GetCached(odinValueEntry.TypeOfValue)
.Properties
.Select((propertyDefinition, index) =>
RootPropertyDefinition = new TriPropertyDefinition(
memberInfo: odinValueEntry.Property.Info.GetMemberInfo(),
order: -1,
fieldName: odinValueEntry.Property.Name,
fieldType: odinValueEntry.TypeOfValue,
valueGetter: (self, targetIndex) => _odinValueEntry.Values[targetIndex],
valueSetter: (self, targetIndex, value) =>
{
var serializedProperty = _serializedProperty.FindPropertyRelative(propertyDefinition.Name);
return new TriProperty(this, this, propertyDefinition, index, serializedProperty);
})
.ToList();
_odinValueEntry.Values[targetIndex] = (T) value;
return null;
},
isArrayElement: false
);
RootProperty = new TriProperty(this, null, RootPropertyDefinition, -1, _serializedProperty);
RootProperty.ValueChanged += OnRootPropertyChanged;
}

public override void Dispose()
{
RootProperty.ValueChanged -= OnRootPropertyChanged;

base.Dispose();
}

public override void Update()
Expand Down Expand Up @@ -84,12 +97,7 @@ public override void RequestRepaint()
GUIHelper.RequestRepaint();
}

public override object GetValue(int targetIndex)
{
return _odinValueEntry.Values[targetIndex];
}

public override void NotifyValueChanged(TriProperty property)
private void OnRootPropertyChanged(TriProperty _, TriProperty changedProperty)
{
ApplyEmittedScriptableObject();

Expand Down
18 changes: 0 additions & 18 deletions Editor/Elements/TriInspectorElement.cs

This file was deleted.

3 changes: 0 additions & 3 deletions Editor/Elements/TriInspectorElement.cs.meta

This file was deleted.

69 changes: 48 additions & 21 deletions Editor/TriProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

namespace TriInspector
{
public sealed class TriProperty : ITriPropertyParent
public sealed class TriProperty
{
private static readonly IReadOnlyList<TriValidationResult> EmptyValidationResults =
new List<TriValidationResult>();

private readonly TriPropertyDefinition _definition;
private readonly int _propertyIndex;
private readonly ITriPropertyParent _parent;
[CanBeNull] private readonly SerializedObject _serializedObject;
[CanBeNull] private readonly SerializedProperty _serializedProperty;
private List<TriProperty> _childrenProperties;
private List<TriValidationResult> _validationResults;
Expand All @@ -33,17 +33,37 @@ public sealed class TriProperty : ITriPropertyParent
[CanBeNull] private Type _valueType;
private bool _isValueMixed;

public event ValueChangedDelegate ValueChanged;

internal TriProperty(
TriPropertyTree propertyTree,
TriProperty parent,
TriPropertyDefinition definition,
SerializedObject serializedObject
)
{
Parent = parent;
_definition = definition;
_propertyIndex = -1;
_serializedProperty = null;
_serializedObject = serializedObject;

PropertyTree = propertyTree;
PropertyType = GetPropertyType(this);
}

internal TriProperty(
TriPropertyTree propertyTree,
ITriPropertyParent parent,
TriProperty parent,
TriPropertyDefinition definition,
int propertyIndex,
[CanBeNull] SerializedProperty serializedProperty)
{
_parent = parent;
Parent = parent;
_definition = definition;
_propertyIndex = propertyIndex;
_serializedProperty = serializedProperty?.Copy();
_serializedObject = _serializedProperty?.serializedObject;

PropertyTree = propertyTree;
PropertyType = GetPropertyType(this);
Expand All @@ -55,6 +75,12 @@ internal TriProperty(
[PublicAPI]
public TriPropertyTree PropertyTree { get; }

[PublicAPI]
public TriProperty Parent { get; }

[PublicAPI]
public bool IsRootProperty => Parent == null;

[PublicAPI]
public string RawName => _definition.Name;

Expand Down Expand Up @@ -161,8 +187,6 @@ public bool IsEnabled

public IReadOnlyList<TriCustomDrawer> AllDrawers => _definition.Drawers;

public ITriPropertyParent Parent => _parent;

public bool HasValidators => _definition.Validators.Count != 0;

public IReadOnlyList<TriValidationResult> ValidationResults =>
Expand Down Expand Up @@ -320,22 +344,24 @@ public void ModifyAndRecordForUndo(Action<int> call)

public void NotifyValueChanged()
{
((ITriPropertyParent) this).NotifyValueChanged(this);
NotifyValueChanged(this);
}

void ITriPropertyParent.NotifyValueChanged(TriProperty property)
private void NotifyValueChanged(TriProperty property)
{
if (_definition.OnValueChanged != null)
{
_serializedProperty?.serializedObject.ApplyModifiedProperties();
_serializedObject?.ApplyModifiedProperties();

for (var targetIndex = 0; targetIndex < PropertyTree.TargetsCount; targetIndex++)
{
_definition.OnValueChanged.InvokeForTarget(this, targetIndex);
}
}

_parent.NotifyValueChanged(property);
ValueChanged?.Invoke(this, property);

Parent?.NotifyValueChanged(property);
}

private void UpdateIfRequired(bool forceUpdate = false)
Expand Down Expand Up @@ -387,8 +413,9 @@ private void UpdateIfRequired(bool forceUpdate = false)
for (var index = 0; index < properties.Count; index++)
{
var childDefinition = properties[index];
var childSerializedProperty =
_serializedProperty?.FindPropertyRelative(childDefinition.Name);
var childSerializedProperty = _serializedProperty != null
? _serializedProperty.FindPropertyRelative(childDefinition.Name)
: _serializedObject?.FindProperty(childDefinition.Name);
var childProperty = new TriProperty(PropertyTree, this,
childDefinition, index, childSerializedProperty);

Expand Down Expand Up @@ -532,10 +559,10 @@ private static void SetValueRecursive(TriProperty property, object value, int ta
// because we cannot directly modify structs
// but we can re-set entire parent value
while (property._definition.SetValue(property, value, targetIndex, out var parentValue) &&
property.Parent is TriProperty parentProperty &&
parentProperty.ValueType != null && parentProperty.ValueType.IsValueType)
property.Parent != null &&
property.Parent.FieldType.IsValueType)
{
property = parentProperty;
property = property.Parent;
value = parentValue;
}
}
Expand Down Expand Up @@ -642,6 +669,11 @@ private static TriPropertyType GetPropertyType(TriProperty property)
return TriPropertyType.Primitive;
}

if (property._serializedObject != null)
{
return TriPropertyType.Generic;
}

if (property._definition.FieldType.IsPrimitive ||
property._definition.FieldType == typeof(string) ||
typeof(UnityEngine.Object).IsAssignableFrom(property._definition.FieldType))
Expand All @@ -661,13 +693,8 @@ private static TriPropertyType GetPropertyType(TriProperty property)

return TriPropertyType.Reference;
}
}

public interface ITriPropertyParent
{
object GetValue(int targetIndex);

void NotifyValueChanged(TriProperty property);
public delegate void ValueChangedDelegate(TriProperty self, TriProperty changedProperty);
}

public enum TriPropertyType
Expand Down
Loading

0 comments on commit b1e66c6

Please sign in to comment.