@@ -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 }
0 commit comments