Skip to content

Commit 7226a57

Browse files
committed
Merge branch 'develop' into feature/forcing-default-lists
2 parents 3d0ee6f + 481c28e commit 7226a57

8 files changed

Lines changed: 89 additions & 30 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Regular/SerializedTypeDrawer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ private void UpdateConstraint(TypeConstraintAttribute attribute)
5353
sharedConstraint.ApplyTarget(attribute.AssemblyType);
5454
if (sharedConstraint is TypeConstraintStandard constraint)
5555
{
56+
constraint.IsOrdered = attribute.OrderTypes;
5657
constraint.AllowAbstract = attribute.AllowAbstract;
5758
constraint.AllowObsolete = attribute.AllowObsolete;
5859
constraint.Settings = attribute.TypeSettings;

Assets/Editor Toolbox/Editor/Drawers/Toolbox/PropertySelf/ReferencePickerAttributeDrawer.cs

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13+
private const float neededLabelWidth = 100.0f;
14+
private const float labelWidthOffset = -80.0f;
15+
1316
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
1417
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
1518
private static readonly TypeField typeField = new TypeField(sharedConstraint, sharedAppearance);
@@ -20,11 +23,27 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2023
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
2124
}
2225

23-
private void CreateTypeProperty(SerializedProperty property, Type parentType)
26+
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
27+
{
28+
property.GetFieldInfo(out Type propertyType);
29+
var candidateType = attribute.ParentType;
30+
if (candidateType != null)
31+
{
32+
if (propertyType.IsAssignableFrom(candidateType))
33+
{
34+
return candidateType;
35+
}
36+
37+
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
38+
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
39+
}
40+
41+
return propertyType;
42+
}
43+
44+
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)
2445
{
2546
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
26-
var position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
27-
position = EditorGUI.IndentedRect(position);
2847
typeField.OnGui(position, true, (type) =>
2948
{
3049
try
@@ -61,39 +80,48 @@ private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
6180
property.serializedObject.ApplyModifiedProperties();
6281
}
6382

64-
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
83+
private Rect PrepareTypePropertyPosition(in Rect labelPosition, in Rect inputPosition, bool isPropertyExpanded)
6584
{
66-
property.GetFieldInfo(out Type propertyType);
67-
var candidateType = attribute.ParentType;
68-
if (candidateType != null)
85+
var position = new Rect(inputPosition);
86+
var baseLabelWidth = EditorGUIUtility.labelWidth + labelWidthOffset;
87+
var realLabelWidth = labelPosition.width;
88+
var labelWidth = Mathf.Max(baseLabelWidth, realLabelWidth);
89+
if (isPropertyExpanded)
6990
{
70-
if (propertyType.IsAssignableFrom(candidateType))
91+
//property is expanded and we have place to move it to the next row
92+
if (labelWidth < neededLabelWidth)
7193
{
72-
return candidateType;
94+
position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
95+
position = EditorGUI.IndentedRect(position);
96+
return position;
7397
}
74-
75-
ToolboxEditorLog.AttributeUsageWarning(attribute, property,
76-
$"Provided {nameof(attribute.ParentType)} ({candidateType}) cannot be used because it's not assignable from: '{propertyType}'");
7798
}
7899

79-
return propertyType;
100+
//adjust position to already rendered label
101+
position.xMin += labelWidth;
102+
return position;
80103
}
81104

105+
82106
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReferencePickerAttribute attribute)
83107
{
84108
using (var propertyScope = new PropertyScope(property, label))
85109
{
86-
if (!propertyScope.IsVisible)
87-
{
88-
return;
89-
}
90-
91110
UpdateContexts(attribute);
92-
var parentType = GetParentType(property, attribute);
93111

112+
var isPropertyExpanded = propertyScope.IsVisible;
94113
EditorGUI.indentLevel++;
95-
CreateTypeProperty(property, parentType);
96-
ToolboxEditorGui.DrawPropertyChildren(property);
114+
var labelRect = propertyScope.LabelRect;
115+
var inputRect = propertyScope.InputRect;
116+
var position = PrepareTypePropertyPosition(in labelRect, in inputRect, isPropertyExpanded);
117+
118+
var parentType = GetParentType(property, attribute);
119+
CreateTypeProperty(position, property, parentType);
120+
if (isPropertyExpanded)
121+
{
122+
ToolboxEditorGui.DrawPropertyChildren(property);
123+
}
124+
97125
EditorGUI.indentLevel--;
98126
}
99127
}

Assets/Editor Toolbox/Editor/Internal/PropertyScope.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ internal class PropertyScope : IDisposable
1717
public PropertyScope(SerializedProperty property, GUIContent label)
1818
{
1919
this.property = property;
20-
ToolboxEditorGui.BeginProperty(property, ref label, out var labelRect);
21-
HandleEvents(labelRect);
22-
TryDrawLabel(labelRect, label);
20+
ToolboxEditorGui.BeginProperty(property, ref label, out var rect);
21+
HandleEvents(rect);
22+
TryDrawLabel(rect, label);
2323
}
2424

2525

@@ -33,8 +33,14 @@ private void HandleEvents(Rect rect)
3333

3434
private void TryDrawLabel(Rect rect, GUIContent label)
3535
{
36+
LabelRect = rect;
37+
InputRect = rect;
3638
if (property.hasChildren)
3739
{
40+
var size = EditorStyles.label.CalcSize(label);
41+
size.x = Mathf.Max(EditorGuiUtility.FoldoutOffset, size.x);
42+
rect.xMax = rect.xMin + size.x;
43+
LabelRect = rect;
3844
property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label, true);
3945
}
4046
else
@@ -51,5 +57,7 @@ public void Dispose()
5157

5258

5359
public bool IsVisible => property.isExpanded;
60+
public Rect LabelRect { get; private set; }
61+
public Rect InputRect { get; private set; }
5462
}
55-
}
63+
}

Assets/Editor Toolbox/Editor/Internal/TypeConstraintContext.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,21 @@ public virtual void ApplyTarget(Type type)
3232

3333
public override bool Equals(object obj)
3434
{
35-
return obj is TypeConstraintContext constraint &&
36-
EqualityComparer<Type>.Default.Equals(targetType, constraint.targetType);
35+
return obj is TypeConstraintContext context &&
36+
EqualityComparer<Type>.Default.Equals(TargetType, context.TargetType) &&
37+
IsOrdered == context.IsOrdered;
3738
}
3839

3940
public override int GetHashCode()
4041
{
41-
return 1673078848 + EqualityComparer<Type>.Default.GetHashCode(targetType);
42+
var hashCode = -509589530;
43+
hashCode = hashCode * -1521134295 + EqualityComparer<Type>.Default.GetHashCode(TargetType);
44+
hashCode = hashCode * -1521134295 + IsOrdered.GetHashCode();
45+
return hashCode;
4246
}
4347

4448

4549
public Type TargetType => targetType;
50+
public bool IsOrdered { get; set; } = true;
4651
}
4752
}

Assets/Editor Toolbox/Editor/Utilities/EditorGuiUtility.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,8 @@ public static Texture GetHelpIcon(MessageType messageType)
9797

9898
return null;
9999
}
100+
101+
102+
public static float FoldoutOffset { get; internal set; } = 28.0f;
100103
}
101104
}

Assets/Editor Toolbox/Editor/Utilities/TypeUtilities.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ public static List<Type> FindTypes(TypeConstraintContext constraint)
106106

107107
typesList.Sort((a, b) => a.FullName.CompareTo(b.FullName));
108108
#endif
109+
if (constraint.IsOrdered)
110+
{
111+
typesList.Sort((t1, t2) => t1.Name.CompareTo(t2.Name));
112+
}
113+
109114
return typesList;
110115
}
111116

Assets/Editor Toolbox/Runtime/Attributes/Regular/TypeConstraintAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public TypeConstraintAttribute(Type assemblyType)
3939
/// </summary>
4040
public bool AddTextSearchField { get; set; }
4141

42+
/// <summary>
43+
/// Indicates if types should be sorted alphabetically.
44+
/// </summary>
45+
public bool OrderTypes { get; set; } = true;
46+
4247
/// <summary>
4348
/// Gets or sets grouping of selectable classes.
4449
/// Defaults to <see cref="ClassGrouping.None"/> unless explicitly specified.

Assets/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ public class SampleBehaviour6 : MonoBehaviour
1313
public ClassWithInterfaceBase var2;
1414
[SerializeReference, ReferencePicker(ParentType = typeof(ClassWithInterface2))]
1515
public ClassWithInterfaceBase var3;
16+
[SerializeField, SerializeReference, ReferencePicker]
17+
public Interface1[] vars;
1618
#endif
1719

18-
public interface Interface1 { }
20+
public interface Interface1
21+
{ }
1922

2023
[Serializable]
2124
public struct Struct : Interface1
@@ -24,7 +27,8 @@ public struct Struct : Interface1
2427
public bool var2;
2528
}
2629

27-
public abstract class ClassWithInterfaceBase : Interface1 { }
30+
public abstract class ClassWithInterfaceBase : Interface1
31+
{ }
2832

2933
[Serializable]
3034
public class ClassWithInterface1 : ClassWithInterfaceBase

0 commit comments

Comments
 (0)