Skip to content

Commit d42e47a

Browse files
committed
Minor refactor changes
1 parent 903cfe5 commit d42e47a

4 files changed

Lines changed: 58 additions & 29 deletions

File tree

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

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ namespace Toolbox.Editor.Drawers
1010

1111
public class ReferencePickerAttributeDrawer : ToolboxSelfPropertyDrawer<ReferencePickerAttribute>
1212
{
13-
private const float minTypeFieldWidth = 30.0f;
13+
private const float minLabelWidth = 100.0f;
14+
private const float labelWidthOffset = -80.0f;
1415

1516
private static readonly TypeConstraintContext sharedConstraint = new TypeConstraintReference(null);
1617
private static readonly TypeAppearanceContext sharedAppearance = new TypeAppearanceContext(sharedConstraint, TypeGrouping.None, true);
@@ -22,6 +23,24 @@ private void UpdateContexts(ReferencePickerAttribute attribute)
2223
sharedAppearance.TypeGrouping = attribute.TypeGrouping;
2324
}
2425

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+
2544
private void CreateTypeProperty(Rect position, SerializedProperty property, Type parentType)
2645
{
2746
TypeUtilities.TryGetTypeFromManagedReferenceFullTypeName(property.managedReferenceFullTypename, out var currentType);
@@ -61,42 +80,43 @@ private void UpdateTypeProperty(SerializedProperty property, Type referenceType)
6180
property.serializedObject.ApplyModifiedProperties();
6281
}
6382

64-
private Type GetParentType(SerializedProperty property, ReferencePickerAttribute attribute)
83+
private Rect CreateTypePropertyPosition(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+
//NOTE: property is expanded and we have place to move it to the next row
92+
if (labelWidth < minLabelWidth)
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+
position.xMin += labelWidth;
101+
return position;
80102
}
81103

104+
82105
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, ReferencePickerAttribute attribute)
83106
{
84107
using (var propertyScope = new PropertyScope(property, label))
85108
{
86109
UpdateContexts(attribute);
87-
var parentType = GetParentType(property, attribute);
88-
EditorGUI.indentLevel++;
89110

90-
var position = GUILayoutUtility.GetLastRect();
91-
position.xMax = position.xMin + EditorGUIUtility.currentViewWidth;
92-
var labelSize = EditorStyles.label.CalcSize(label);
93-
var x = Mathf.Max(EditorGUIUtility.labelWidth, labelSize.x);
94-
position.xMin = position.xMin + x;
95-
//position = EditorGUILayout.GetControlRect(false, EditorGUIUtility.singleLineHeight);
96-
//position = EditorGUI.IndentedRect(position);
111+
var isPropertyExpanded = propertyScope.IsVisible;
112+
EditorGUI.indentLevel++;
113+
var labelRect = propertyScope.LabelRect;
114+
var inputRect = propertyScope.InputRect;
115+
var position = CreateTypePropertyPosition(in labelRect, in inputRect, isPropertyExpanded);
97116

117+
var parentType = GetParentType(property, attribute);
98118
CreateTypeProperty(position, property, parentType);
99-
if (propertyScope.IsVisible)
119+
if (isPropertyExpanded)
100120
{
101121
ToolboxEditorGui.DrawPropertyChildren(property);
102122
}

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

Lines changed: 10 additions & 5 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,11 +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
{
3840
var size = EditorStyles.label.CalcSize(label);
39-
size.x = Mathf.Max(16, size.x);
40-
rect.xMax = rect.xMin + size.x;
41+
size.x = Mathf.Max(EditorGuiUtility.FoldoutOffset, size.x);
42+
rect.xMax = rect.xMin + size.x;
43+
LabelRect = rect;
4144
property.isExpanded = EditorGUI.Foldout(rect, property.isExpanded, label, true);
4245
}
4346
else
@@ -54,5 +57,7 @@ public void Dispose()
5457

5558

5659
public bool IsVisible => property.isExpanded;
60+
public Rect LabelRect { get; private set; }
61+
public Rect InputRect { get; private set; }
5762
}
5863
}

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/Examples/Scripts/SampleBehaviour6.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public class SampleBehaviour6 : MonoBehaviour
1313
public ClassWithInterfaceBase var2;
1414
[SerializeReference, ReferencePicker(ParentType = typeof(ClassWithInterface2))]
1515
public ClassWithInterfaceBase var3;
16-
public int a;
1716
[SerializeField, SerializeReference, ReferencePicker]
1817
public Interface1[] vars;
1918
#endif
2019

21-
public interface Interface1 { }
20+
public interface Interface1
21+
{ }
2222

2323
[Serializable]
2424
public struct Struct : Interface1
@@ -27,7 +27,8 @@ public struct Struct : Interface1
2727
public bool var2;
2828
}
2929

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

3233
[Serializable]
3334
public class ClassWithInterface1 : ClassWithInterfaceBase

0 commit comments

Comments
 (0)