Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Select serialized type #117

Merged
merged 3 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Commons.EditorAddons.Utility;
using UnityEditor;
Expand Down Expand Up @@ -26,17 +27,23 @@ public override void DrawField(Rect rect, GUIContent label)
return;
}

var enumerator = _property.GetEnumerator();
var copy = new Rect(rect);
while (enumerator.MoveNext())
var serializedProperty = _property.Copy();
var enumerator = serializedProperty.GetEnumerator();
using (var disposable = enumerator as IDisposable)
{
if (!(enumerator.Current is SerializedProperty prop)) continue;
var copy = new Rect(rect);
while (enumerator.MoveNext())
{
if (!(enumerator.Current is SerializedProperty prop)) continue;

var propertyHeight = EditorGUI.GetPropertyHeight(prop, true);
copy.height = propertyHeight;
var propertyHeight = EditorGUI.GetPropertyHeight(prop, true);
copy.height = propertyHeight;

PropertyFieldUtility.PropertyFieldSafe(copy, prop, item);
copy.y += propertyHeight + EditorGUIUtility.standardVerticalSpacing;
PropertyFieldUtility.PropertyFieldSafe(copy, prop, item);
copy.y += propertyHeight + EditorGUIUtility.standardVerticalSpacing;
}

serializedProperty.Dispose();
}
}

Expand All @@ -49,15 +56,20 @@ public override HeightCacheValue GetHeight(GUIContent label)
return HeightCacheValue.GetFull(height);
}

var enumerator = _property.GetEnumerator();
while (enumerator.MoveNext())
var serializedProperty = _property.Copy();
var enumerator = serializedProperty.GetEnumerator();
using (var disposable = enumerator as IDisposable)
{
var prop = enumerator.Current as SerializedProperty;
if (prop == null) continue;
height += EditorGUI.GetPropertyHeight(prop, true) + EditorGUIUtility.standardVerticalSpacing;
}
while (enumerator.MoveNext())
{
var prop = enumerator.Current as SerializedProperty;
if (prop == null) continue;
height += EditorGUI.GetPropertyHeight(prop, true) + EditorGUIUtility.standardVerticalSpacing;
}

return HeightCacheValue.GetFull(height);
serializedProperty.Dispose();
return HeightCacheValue.GetFull(height);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,
_previewSize = ((PreviewAttribute)_attribute).PreviewSize;
if (!Collection.ValidateObject(property))
{
ExtendedGUIUtility.HelpBoxFromRect(position, property, label, Message, IconType.WarningMessage);
var offset = EditorGUI.GetPropertyHeight(property, label, true) + ExtendedGUIUtility.SpaceHeight;
ExtendedGUIUtility.HelpBoxFromRect(position, property, label, Message, IconType.WarningMessage, offset);
return true;
}

label.image = IconType.View.GetIcon();
var copy = ExtendedGUIUtility.GetClickRect(position, label);
copy.height = EditorGUIUtility.singleLineHeight;

Collection.PreDraw(copy, property, _previewSize, _objectChanged);

return true;
Expand All @@ -65,7 +65,8 @@ protected override HeightCacheValue GetPropertyHeight(SerializedProperty propert
if (!Collection.ValidateObject(property))
{
var additive = ExtendedGUIUtility.GetHelpBoxHeight(EditorGUIUtility.currentViewWidth, Message, IconType.WarningMessage);
return HeightCacheValue.GetAdditive(additive + ExtendedGUIUtility.SpaceHeight * 2);
var height = HeightCacheValue.GetAdditive(additive + ExtendedGUIUtility.SpaceHeight * 2);
return height;
}

return HeightCacheValue.GetAdditive(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override WrapperCollection<BaseSelectWrapper> GenerateCollection()

protected override void DrawField(Rect position, SerializedProperty property, GUIContent label)
{
if (_wrappers.TryGetValue(property, out var value) && value.Wrapper.SkipFieldDraw())
if (_setupStrategy.SkipFieldDraw())
{
var rect = PreparePropertyRect(position);
// rect.height = value.Wrapper.GetHeight().Value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,22 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,
try
{
var attribute = (TAttribute)_attribute;
_setupStrategy ??= SelectUtility.Instance.GetSetupStrategy(_fieldInfo, property.GetLastNonCollectionContainer(), attribute);
InitializeSetupStrategy(property, attribute);
if (_setupStrategy == null || !_setupStrategy.CheckSupported())
{
EditorGUI.BeginChangeCheck();
DrawField(position, property, label);
ExtendedGUIUtility.NotSupportedAttribute(position, property, label, GetFieldOrElementType(), _attribute.GetType());
var offset = 0f;
if (_setupStrategy != null)
{
if (!_setupStrategy.SkipFieldDraw())
{
var includeChildren = property.isExpanded;
offset = EditorGUI.GetPropertyHeight(property, includeChildren) + ExtendedGUIUtility.SpaceHeight;
}
}

ExtendedGUIUtility.NotSupportedAttribute(position, property, label, GetFieldOrElementType(), _attribute.GetType(), offset);
return false;
}

Expand All @@ -75,7 +85,7 @@ private void PreDrawExtended(Rect position, SerializedProperty property, GUICont
var cache = ValidateCachedProperties(property, SelectUtility.Instance);
if (!cache.IsValid)
{
cache.Value.Wrapper.SetProperty(property, _fieldInfo);
cache.Value.Wrapper.Setup(property, _fieldInfo, _attribute, _setupStrategy);
}

var popupPosition = GetPopupPosition(position, label);
Expand Down Expand Up @@ -169,21 +179,30 @@ private DropdownCollection GenerateItemsTree(SerializedProperty serializedProper
protected override HeightCacheValue GetPropertyHeight(SerializedProperty property, GUIContent label)
{
var cache = ValidateCachedProperties(property, SelectUtility.Instance);
var attribute = (TAttribute)_attribute;
InitializeSetupStrategy(property, attribute);
if (!cache.IsValid)
{
if (cache.Value == null) return HeightCacheValue.GetAdditive(0f);
var selectWrapper = cache.Value.Wrapper;
selectWrapper.SetProperty(property, _fieldInfo);
return selectWrapper.GetHeight();
selectWrapper.Setup(property, _fieldInfo, _attribute, _setupStrategy);
var value = selectWrapper.GetHeight();
return value;
}

var valueWrapper = cache.Value.Wrapper;
if (!valueWrapper.Verify())
{
valueWrapper.SetProperty(property, _fieldInfo);
valueWrapper.Setup(property, _fieldInfo, _attribute, _setupStrategy);
}

return valueWrapper.GetHeight();
var height = valueWrapper.GetHeight();
return height;
}

private void InitializeSetupStrategy(SerializedProperty property, TAttribute attribute)
{
_setupStrategy ??= SelectUtility.Instance.GetSetupStrategy(_fieldInfo, property.GetLastNonCollectionContainer(), attribute);
}

private object GetCurrentValue(SerializedProperty property)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public DropdownStrategy(FieldInfo fieldInfo, object propertyContainer, SelectAtt
{
_dropdownAttribute = (DropdownAttribute)selectAttributeBase;
}

public override bool SkipFieldDraw()
{
return true;
}

private bool TryGetType(IEnumerable<string> path, out Type type)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public PredefinedValues(string name, int value)
public int Value { get; }
}

public override bool SkipFieldDraw()
{
return true;
}

public override string GetButtonName(object currentValue)
{
var intValue = (int)currentValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Better.Attributes.Runtime.Select;
using Better.Commons.EditorAddons.Enums;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Extensions;
using UnityEngine;

namespace Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies
{
public class SelectImplementationStrategy : SelectTypeStrategy
public class SelectImplementationStrategy : SelectSerializedTypeStrategy
{
public SelectImplementationStrategy(FieldInfo fieldInfo, object container, SelectAttributeBase selectAttributeBase) : base(fieldInfo, container,
selectAttributeBase)
{
}

public override List<object> Setup()
{
var selectionObjects = GetFieldOrElementType().GetAllInheritedTypesWithoutUnityObject().Cast<object>().ToList();
selectionObjects.Insert(0, null);
return selectionObjects;
}

public override bool SkipFieldDraw()
{
return false;
}

public override bool CheckSupported()
{
var baseType = GetFieldOrElementType();
return baseType.IsAbstract || baseType.IsInterface;
}

public override GUIContent[] ResolveGroupedName(object value, DisplayGrouping grouping)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@

namespace Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies
{
public class SelectTypeStrategy : SetupStrategy
public class SelectSerializedTypeStrategy : SetupStrategy
{
public SelectTypeStrategy(FieldInfo fieldInfo, object propertyContainer, SelectAttributeBase selectAttributeBase) : base(fieldInfo, propertyContainer,
public override bool SkipFieldDraw()
{
return true;
}

public SelectSerializedTypeStrategy(FieldInfo fieldInfo, object propertyContainer, SelectAttributeBase selectAttributeBase) : base(fieldInfo, propertyContainer,
selectAttributeBase)
{
}
Expand Down Expand Up @@ -55,8 +60,7 @@ public override bool Validate(object item)

public override bool CheckSupported()
{
var baseType = GetFieldOrElementType();
return baseType.IsAbstract || baseType.IsInterface;
return false;
}

public override GUIContent GenerateHeader()
Expand Down Expand Up @@ -135,7 +139,7 @@ public override string GetButtonName(object currentValue)

public override List<object> Setup()
{
var selectionObjects = GetFieldOrElementType().GetAllInheritedTypesWithoutUnityObject().Cast<object>().ToList();
var selectionObjects = GetFieldOrElementType().GetAllInheritedTypes().Cast<object>().ToList();
selectionObjects.Insert(0, null);
return selectionObjects;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected SetupStrategy(FieldInfo fieldInfo, object propertyContainer, SelectAtt
public abstract bool Validate(object item);
public abstract bool CheckSupported();
public abstract GUIContent GenerateHeader();
public abstract bool SkipFieldDraw();

public virtual Type GetFieldOrElementType()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
using System.Reflection;
using Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies;
using Better.Attributes.Runtime.Select;
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Commons.EditorAddons.Drawers.Utility;
using Better.Commons.EditorAddons.Enums;
using Better.Commons.EditorAddons.Extensions;
using Better.Commons.EditorAddons.Utility;
using Better.Commons.Runtime.Drawers.Attributes;
using Better.Commons.Runtime.Extensions;
using UnityEditor;

namespace Better.Attributes.EditorAddons.Drawers.Select.Wrappers
Expand All @@ -10,29 +16,64 @@ public abstract class BaseSelectWrapper : UtilityWrapper
{
protected SerializedProperty _property;
protected FieldInfo _fieldInfo;
protected MultiPropertyAttribute _attribute;
protected SetupStrategy _setupStrategy;

public override void Deconstruct()
public virtual void Setup(SerializedProperty property, FieldInfo fieldInfo, MultiPropertyAttribute attribute, SetupStrategy setupStrategy)
{
_property = null;
_property = property;
_fieldInfo = fieldInfo;
_attribute = attribute;
_setupStrategy = setupStrategy;
}

public abstract bool SkipFieldDraw();
public virtual HeightCacheValue GetHeight()
{
var copy = _property.Copy();
var type = _fieldInfo.FieldType;
if (type.IsArrayOrList())
{
type = type.GetCollectionElementType();
}

public abstract HeightCacheValue GetHeight();
var propertyHeight = GetPropertyHeight(copy);
if (_setupStrategy == null || !_setupStrategy.CheckSupported())
{
var message = ExtendedGUIUtility.NotSupportedMessage(copy.name, type, _attribute.GetType());
propertyHeight += ExtendedGUIUtility.GetHelpBoxHeight(EditorGUIUtility.currentViewWidth, message, IconType.ErrorMessage);
propertyHeight += ExtendedGUIUtility.SpaceHeight;
}

public abstract void Update(object value);
var full = HeightCacheValue.GetFull(propertyHeight);
copy.Dispose();
return full;
}

public virtual void SetProperty(SerializedProperty property, FieldInfo fieldInfo)
protected virtual float GetPropertyHeight(SerializedProperty copy)
{
_property = property;
_fieldInfo = fieldInfo;
var includeChildren = !_setupStrategy.SkipFieldDraw();
var propertyHeight = 0f;
if (includeChildren)
{
includeChildren = copy.isExpanded;
propertyHeight = EditorGUI.GetPropertyHeight(copy, includeChildren);
}

return propertyHeight;
}

public abstract void Update(object value);

public abstract object GetCurrentValue();

public virtual bool Verify()
{
return _property.Verify();
}

public abstract object GetCurrentValue();
public override void Deconstruct()
{
_property = null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
using Better.Commons.EditorAddons.Drawers.Caching;
using Better.Commons.EditorAddons.Extensions;
using UnityEditor;
using Better.Commons.EditorAddons.Extensions;

namespace Better.Attributes.EditorAddons.Drawers.Select.Wrappers
{
public class DropdownWrapper : BaseSelectWrapper
{
public override bool SkipFieldDraw()
{
return true;
}

public override HeightCacheValue GetHeight()
{
return HeightCacheValue.GetFull(EditorGUI.GetPropertyHeight(_property, false));
}

public override void Update(object value)
{
Expand Down
Loading
Loading