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

Commit

Permalink
Update README.md and LICENSE
Browse files Browse the repository at this point in the history
  • Loading branch information
uurha committed Sep 10, 2023
1 parent 04ec43e commit 4353974
Show file tree
Hide file tree
Showing 32 changed files with 775 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public override void DrawField(Rect rect, GUIContent label)
var copy = new Rect(rect);
while (enumerator.MoveNext())
{
var prop = enumerator.Current as SerializedProperty;
if (prop == null) continue;
if (!(enumerator.Current is SerializedProperty prop)) continue;

var propertyHeight = EditorGUI.GetPropertyHeight(prop, true);
copy.height = propertyHeight;
Expand Down
6 changes: 1 addition & 5 deletions Editor/EditorAddons/Drawers/Select/SelectDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ namespace Better.Attributes.EditorAddons.Drawers.Select
{
[MultiCustomPropertyDrawer(typeof(SelectAttribute))]
[MultiCustomPropertyDrawer(typeof(SelectImplementationAttribute))]
[MultiCustomPropertyDrawer(typeof(DropdownAttribute))]
public class SelectDrawer : SelectDrawerBase<SelectAttributeBase>
{
protected override bool CheckSupported(SerializedProperty property)
{
return SelectUtility.Instance.CheckSupportedType(property.propertyType);
}

protected override WrapperCollection<BaseSelectWrapper> GenerateCollection()
{
return new SelectWrappers();
Expand Down
18 changes: 8 additions & 10 deletions Editor/EditorAddons/Drawers/Select/SelectDrawerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Better.Attributes.EditorAddons.Drawers.WrapperCollections;
using Better.Attributes.EditorAddons.Extensions;
using Better.Attributes.Runtime.Select;
using Better.EditorTools;
using Better.EditorTools.Drawers.Base;
using Better.EditorTools.Helpers;
using Better.EditorTools.Helpers.DropDown;
Expand Down Expand Up @@ -38,8 +39,8 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,
try
{
var attribute = (TAttribute)_attribute;
_setupStrategy ??= SelectUtility.Instance.GetSetupStrategy(GetFieldOrElementType(), attribute);
if (_setupStrategy == null || (!CheckSupported(property) && !_setupStrategy.CheckSupported()))
_setupStrategy ??= SelectUtility.Instance.GetSetupStrategy(_fieldInfo, property.GetPropertyContainer(), attribute);
if (_setupStrategy == null || (!_setupStrategy.CheckSupported()))
{
EditorGUI.BeginChangeCheck();
DrawField(position, property, label);
Expand All @@ -59,7 +60,6 @@ protected override bool PreDraw(ref Rect position, SerializedProperty property,

private void PreDrawExtended(Rect position, SerializedProperty property, GUIContent label, TAttribute attribute)
{
var fieldOrElementType = _setupStrategy.GetFieldOrElementType();
var cache = ValidateCachedProperties(property, SelectUtility.Instance);
if (!cache.IsValid)
{
Expand All @@ -69,7 +69,7 @@ private void PreDrawExtended(Rect position, SerializedProperty property, GUICont
var popupPosition = GetPopupPosition(position, label);
if (!_isSetUp)
{
_selectionObjects = _setupStrategy.Setup(fieldOrElementType);
_selectionObjects = _setupStrategy.Setup();
_displayName = attribute.DisplayName;
_displayGrouping = attribute.DisplayGrouping;
SetReady();
Expand Down Expand Up @@ -128,15 +128,15 @@ private DropdownCollection GenerateItemsTree(SerializedProperty serializedProper
var items = new DropdownCollection(new DropdownSubTree(new GUIContent("Root")));
if (_displayGrouping == DisplayGrouping.None)
{
foreach (var type in _selectionObjects)
foreach (var value in _selectionObjects)
{
var guiContent = _setupStrategy.ResolveName(type, _displayName);
if (guiContent.image == null && _setupStrategy.ResolveState(currentValue, type))
var guiContent = _setupStrategy.ResolveName(value, _displayName);
if (guiContent.image == null && _setupStrategy.ResolveState(currentValue, value))
{
guiContent.image = DrawersHelper.GetIcon(IconType.Checkmark);
}

var item = new DropdownItem(guiContent, OnSelectItem, new SelectedItem<object>(serializedProperty, type));
var item = new DropdownItem(guiContent, OnSelectItem, new SelectedItem<object>(serializedProperty, value));
items.AddChild(item);
}
}
Expand Down Expand Up @@ -228,7 +228,5 @@ protected override Rect PreparePropertyRect(Rect original)
protected override void PostDraw(Rect position, SerializedProperty property, GUIContent label)
{
}

protected abstract bool CheckSupported(SerializedProperty property);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Better.Attributes.EditorAddons.Drawers.Utilities;
using Better.Extensions.Runtime;

namespace Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies.DropdownCollection
{
public class DictionaryCollection : IDataCollection
{
private readonly IDictionary _dictionary;
private readonly bool _showDefault;
private readonly bool _showUniqueKey;

public DictionaryCollection(IDictionary dictionary, bool showDefault, bool showUniqueKey)
{
_dictionary = dictionary;
_showDefault = showDefault;
_showUniqueKey = showUniqueKey;
}

public string FindName(object obj)
{
if (obj == null)
{
return SelectUtility.Null;
}

foreach (DictionaryEntry en in _dictionary)
{
if (en.Value.Equals(obj))
{
return en.Key as string;
}
}

return obj.ToString();
}

public List<object> GetValues()
{
Type type = null;
foreach (DictionaryEntry entry in _dictionary)
{
type = entry.Value.GetType();
break;
}

if (type == null) return new List<object>();
var defaultElement = type.GetDefault();
var objects = _dictionary.Values.Cast<object>();

if (_showUniqueKey)
{

}

if (_showDefault)
{
if (!objects.Contains(defaultElement, EqualityComparer<object>.Default))
{
objects = objects.Prepend(defaultElement);
}
}

return objects.ToList();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;

namespace Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies.DropdownCollection
{
public interface IDataCollection
{
public string FindName(object obj);
public List<object> GetValues();
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using Better.Attributes.EditorAddons.Drawers.Utilities;
using Better.Extensions.Runtime;

namespace Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies.DropdownCollection
{
public class ListCollection : IDataCollection
{
private readonly IList _list;
private readonly bool _showDefault;
private readonly bool _showUniqueKey;

public ListCollection(IList list, bool showDefault, bool showUniqueKey)
{
_list = list;
_showDefault = showDefault;
_showUniqueKey = showUniqueKey;
}

public string FindName(object obj)
{
if (obj == null)
{
return SelectUtility.Null;
}

if (_list[_list.Count - 1] is ITuple)
{
foreach (ITuple item in _list)
{
if (item != null && item[1].Equals(obj))
{
return item[0].ToString();
}
}
}

return obj.ToString();
}

private class KeyTupleComparer : IEqualityComparer<ITuple>
{
public bool Equals(ITuple x, ITuple y)
{
if (ReferenceEquals(x, y)) return true;
if (ReferenceEquals(x, null)) return false;
if (ReferenceEquals(y, null)) return false;
if (x.GetType() != y.GetType()) return false;
return Equals(x[0], y[0]);
}

public int GetHashCode(ITuple obj)
{
return obj.Length;
}
}

public List<object> GetValues()
{
var last = _list[_list.Count - 1];
var type = last.GetType();
object defaultElement;
IEnumerable<object> objects;
if (last is ITuple lastTuple)
{
var tupleLength = lastTuple.Length - 1;
defaultElement = lastTuple[tupleLength].GetType().GetDefault();
if (_showUniqueKey)
{
objects = _list.Cast<ITuple>().Distinct(new KeyTupleComparer()).Select(tuple => tuple[tupleLength]);
}
else
{
objects = _list.Cast<ITuple>().Select(tuple => tuple[tupleLength]);
}
}
else
{
defaultElement = type.GetDefault();
objects = _list.Cast<object>();

if (_showUniqueKey)
{
objects = objects.Distinct(EqualityComparer<object>.Default);
}
}

if (_showDefault)
{
if (!objects.Contains(defaultElement, EqualityComparer<object>.Default))
{
objects = objects.Prepend(defaultElement);
}
}

return objects.ToList();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;

namespace Better.Attributes.EditorAddons.Drawers.Select.SetupStrategies.DropdownCollection
{
public class NoneCollection : IDataCollection
{
private const string None = "Selector not found";

public string FindName(object obj)
{
return None;
}

public List<object> GetValues()
{
return new List<object>();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4353974

Please sign in to comment.