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