Skip to content

Commit

Permalink
Merge pull request #12 from techno-dwarf-works/feature/extensions
Browse files Browse the repository at this point in the history
Version 0.0.9
  • Loading branch information
OpOpYaDev committed May 30, 2024
1 parent e6a1b2a commit 9fe2947
Show file tree
Hide file tree
Showing 39 changed files with 592 additions and 1,454 deletions.
12 changes: 5 additions & 7 deletions Editor/CustomEditors/Base/ExtendedEditor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

namespace Better.Commons.EditorAddons.CustomEditors.Base
{
Expand All @@ -23,21 +22,20 @@ protected ExtendedEditor(Object target, SerializedObject serializedObject)
/// This method called just right after instance created
/// </summary>
public abstract void OnEnable();

/// <summary>
/// Use to draw your inspector
/// </summary>
public abstract VisualElement CreateInspectorGUI();

public abstract void OnInspectorGUI();
/// <summary>
/// This method called than editor disables
/// </summary>
public abstract void OnDisable();

/// <summary>
/// Called when object data in editor is changed
/// </summary>
/// <param name="serializedObject"></param>
public abstract void OnChanged(SerializedObject serializedObject);
public abstract void OnChanged();
}
}
52 changes: 19 additions & 33 deletions Editor/CustomEditors/MultiEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.Runtime.Extensions;
using UnityEditor;
using UnityEditor.UIElements;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace Better.Commons.EditorAddons.CustomEditors
Expand Down Expand Up @@ -92,47 +90,35 @@ private void Iterate(IReadOnlyList<(Type type, MultiEditorAttribute)> extensions
}
}

public override VisualElement CreateInspectorGUI()
public override void OnInspectorGUI()
{
var container = new VisualElement();

for (var i = 0; i < _preEditors.Count; i++)
using (var change = new EditorGUI.ChangeCheckScope())
{
var element = _preEditors[i].CreateInspectorGUI();
if (element != null)
for (var i = 0; i < _preEditors.Count; i++)
{
container.Add(element);
_preEditors[i].OnInspectorGUI();
}
}

if (!_overrideDefault)
{
InspectorElement.FillDefaultInspector(container, serializedObject, this);
}

for (var i = 0; i < _postEditors.Count; i++)
{
var element = _postEditors[i].CreateInspectorGUI();
if (element != null)
if (!_overrideDefault)
{
container.Add(element);
base.OnInspectorGUI();
}
}

container.TrackSerializedObjectValue(serializedObject, OnSerializedObjectTrack);
return container;
}
for (var i = 0; i < _postEditors.Count; i++)
{
_postEditors[i].OnInspectorGUI();
}

private void OnSerializedObjectTrack(SerializedObject serializedObject)
{
for (var i = 0; i < _preEditors.Count; i++)
{
_preEditors[i].OnChanged(serializedObject);
}
if (!change.changed) return;
for (var i = 0; i < _preEditors.Count; i++)
{
_preEditors[i].OnChanged();
}

for (var i = 0; i < _postEditors.Count; i++)
{
_postEditors[i].OnChanged(serializedObject);
for (var i = 0; i < _postEditors.Count; i++)
{
_postEditors[i].OnChanged();
}
}
}

Expand Down
79 changes: 76 additions & 3 deletions Editor/Drawers/Base/FieldDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,92 @@ public abstract class FieldDrawer
{
protected readonly FieldInfo _fieldInfo;
protected readonly MultiPropertyAttribute _attribute;
protected FieldDrawer _nextDrawer;

protected FieldDrawer(FieldInfo fieldInfo, MultiPropertyAttribute attribute)
{
Selection.selectionChanged += OnSelectionChanged;
_fieldInfo = fieldInfo;
_attribute = attribute;
}

public virtual void Initialize()
~FieldDrawer()
{
EditorApplication.update += DeconstructOnMainThread;
}

protected internal abstract void Deconstruct();
public virtual void Initialize(FieldDrawer drawer)
{
_nextDrawer = drawer;
}

private void DeconstructOnMainThread()
{
EditorApplication.update -= DeconstructOnMainThread;
Selection.selectionChanged -= OnSelectionChanged;
Deconstruct();
}

private void OnSelectionChanged()
{
Selection.selectionChanged -= OnSelectionChanged;
Deconstruct();
}

protected abstract void Deconstruct();

protected virtual void DrawField(Rect position, SerializedProperty property, GUIContent label)
{
var buffer = PreparePropertyRect(position);
EditorGUI.PropertyField(buffer, property, label, true);
}

internal void DrawFieldInternal(Rect position, SerializedProperty property, GUIContent label)
{
if (_nextDrawer != null)
{
_nextDrawer.DrawFieldInternal(position, property, label);
return;
}

DrawField(position, property, label);
}

internal bool PreDrawInternal(ref Rect position, SerializedProperty property, GUIContent label)
{
if (_nextDrawer != null)
{
return PreDraw(ref position, property, label) && _nextDrawer.PreDrawInternal(ref position, property, label);
}

return PreDraw(ref position, property, label);
}

internal void PostDrawInternal(Rect position, SerializedProperty property, GUIContent label)
{
_nextDrawer?.PostDrawInternal(position, property, label);
PostDraw(position, property, label);
}

protected internal abstract void PopulateContainer(ElementsContainer container);
internal HeightCacheValue GetPropertyHeightInternal(SerializedProperty property, GUIContent label)
{
var propertyHeight = GetPropertyHeight(property, label);
if (_nextDrawer != null)
{
var heightInternal = _nextDrawer.GetPropertyHeightInternal(property, label);
return propertyHeight + heightInternal;
}

return propertyHeight;
}

protected abstract bool PreDraw(ref Rect position, SerializedProperty property, GUIContent label);
protected abstract Rect PreparePropertyRect(Rect original);
protected abstract void PostDraw(Rect position, SerializedProperty property, GUIContent label);

protected virtual HeightCacheValue GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return HeightCacheValue.GetFull(EditorGUI.GetPropertyHeight(property, label, true));
}
}
}
34 changes: 17 additions & 17 deletions Editor/Drawers/Base/MultiFieldDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

namespace Better.Commons.EditorAddons.Drawers.Base
{
public abstract class MultiFieldDrawer<T> : FieldDrawer where T : SerializedPropertyHandler
public abstract class MultiFieldDrawer<T> : FieldDrawer where T : UtilityWrapper
{
private static readonly CacheValue CacheValueField = new CacheValue();

protected HandlerCollection<T> _handlers;
protected WrapperCollection<T> _wrappers;

protected class CacheValue : CacheValue<CollectionValue<T>>
protected class CacheValue : CacheValue<WrapperCollectionValue<T>>
{
}

Expand All @@ -24,20 +24,20 @@ protected MultiFieldDrawer(FieldInfo fieldInfo, MultiPropertyAttribute attribute
}

/// <summary>
/// Method generates explicit typed collection inherited from <see cref="HandlerCollection{T}"/>
/// Method generates explicit typed collection inherited from <see cref="WrapperCollection{T}"/>
/// </summary>
/// <returns></returns>
protected abstract HandlerCollection<T> GenerateCollection();
protected abstract WrapperCollection<T> GenerateCollection();

public override void Initialize()
public override void Initialize(FieldDrawer drawer)
{
base.Initialize();
_handlers = GenerateCollection();
base.Initialize(drawer);
_wrappers = GenerateCollection();
}

protected internal override void Deconstruct()
protected override void Deconstruct()
{
_handlers?.Deconstruct();
_wrappers.Deconstruct();
}

protected virtual Type GetFieldOrElementType()
Expand All @@ -50,16 +50,16 @@ protected virtual Type GetFieldOrElementType()
}

/// <summary>
/// Validates if <see cref="_handlers"/> contains property by <see cref="SerializedPropertyComparer"/>
/// Validates if <see cref="_wrappers"/> contains property by <see cref="SerializedPropertyComparer"/>
/// </summary>
/// <param name="property">SerializedProperty what will be stored into <see cref="_handlers"/></param>
/// <param name="handler"><see cref="HandlerMap{THandler}"/> used to validate current stored wrappers and gets instance for recently added property</param>
/// <param name="property">SerializedProperty what will be stored into <see cref="_wrappers"/></param>
/// <param name="handler"><see cref="BaseUtility{THandler}"/> used to validate current stored wrappers and gets instance for recently added property</param>
/// <typeparam name="THandler"></typeparam>
/// <returns>Returns true if wrapper for <paramref name="property"/> was already stored into <see cref="_handlers"/></returns>
protected CacheValue ValidateCachedProperties<THandler>(SerializedProperty property, HandlerMap<THandler> handler)
where THandler : HandlerMap<THandler>, new()
/// <returns>Returns true if wrapper for <paramref name="property"/> was already stored into <see cref="_wrappers"/></returns>
protected CacheValue ValidateCachedProperties<THandler>(SerializedProperty property, BaseUtility<THandler> handler)
where THandler : new()
{
ValidateCachedPropertiesUtility.Validate(_handlers, CacheValueField, property, GetFieldOrElementType(), _attribute.GetType(), handler);
ValidateCachedPropertiesUtility.Validate(_wrappers, CacheValueField, property, GetFieldOrElementType(), _attribute.GetType(), handler);
return CacheValueField;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace Better.Commons.EditorAddons.Drawers.Base
{
public class HandlerCollection<T> : Dictionary<SerializedProperty, CollectionValue<T>>
where T : SerializedPropertyHandler
public class WrapperCollection<T> : Dictionary<SerializedProperty, WrapperCollectionValue<T>>
where T : UtilityWrapper
{
public HandlerCollection() : base(SerializedPropertyComparer.Instance)
public WrapperCollection() : base(SerializedPropertyComparer.Instance)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Better.Commons.EditorAddons.Drawers.Base
{
public class CollectionValue<T> where T : SerializedPropertyHandler
public class WrapperCollectionValue<T> where T : UtilityWrapper
{
public CollectionValue(T wrapper, Type type)
public WrapperCollectionValue(T wrapper, Type type)
{
Wrapper = wrapper;
Type = type;
Expand Down
File renamed without changes.
Loading

0 comments on commit 9fe2947

Please sign in to comment.