1- using TriInspector . Utilities ;
1+ using System ;
2+ using TriInspector . Utilities ;
23using UnityEditor ;
34using UnityEngine ;
45
56namespace TriInspector . Elements
67{
78 public class TriHorizontalGroupElement : TriPropertyCollectionBaseElement
89 {
10+ private readonly float [ ] _sizes ;
11+ private readonly float _totalFixedSize ;
12+
13+ public TriHorizontalGroupElement ( float [ ] sizes = null )
14+ {
15+ _sizes = sizes ?? Array . Empty < float > ( ) ;
16+ _totalFixedSize = 0f ;
17+
18+ for ( var index = 0 ; index < _sizes . Length ; index ++ )
19+ {
20+ if ( TryGetFixedSizeByIndex ( index , out var fixedSize ) )
21+ {
22+ _totalFixedSize += fixedSize ;
23+ }
24+ }
25+ }
26+
927 public override float GetHeight ( float width )
1028 {
1129 if ( ChildrenCount == 0 )
1230 {
1331 return 0f ;
1432 }
1533
16- var height = 0f ;
17-
1834 var spacing = EditorGUIUtility . standardVerticalSpacing ;
19- var totalWidth = width - spacing * ( ChildrenCount - 1 ) ;
20- var childWidth = totalWidth / ChildrenCount ;
35+ var totalSpacing = spacing * ( ChildrenCount - 1 ) ;
36+ var totalDynamic = width - totalSpacing - _totalFixedSize ;
37+ var dynamicChildCount = GetDynamicChildCount ( ) ;
38+
39+ var height = 0f ;
2140
2241 for ( var i = 0 ; i < ChildrenCount ; i ++ )
2342 {
43+ var childWidth = GetChildWidth ( i , totalDynamic , dynamicChildCount ) ;
2444 var child = GetChild ( i ) ;
2545 var childHeight = child . GetHeight ( childWidth ) ;
2646
@@ -38,24 +58,68 @@ public override void OnGUI(Rect position)
3858 }
3959
4060 var spacing = EditorGUIUtility . standardVerticalSpacing ;
41- var totalWidth = position . width - spacing * ( ChildrenCount - 1 ) ;
42- var childWidth = totalWidth / ChildrenCount ;
61+ var totalSpacing = spacing * ( ChildrenCount - 1 ) ;
62+ var totalDynamic = position . width - totalSpacing - _totalFixedSize ;
63+ var dynamicChildCount = GetDynamicChildCount ( ) ;
4364
65+ var xOffset = 0f ;
4466 for ( var i = 0 ; i < ChildrenCount ; i ++ )
4567 {
68+ var childWidth = GetChildWidth ( i , totalDynamic , dynamicChildCount ) ;
4669 var child = GetChild ( i ) ;
4770 var childRect = new Rect ( position )
4871 {
4972 width = childWidth ,
5073 height = child . GetHeight ( childWidth ) ,
51- x = position . x + i * ( childWidth + spacing ) ,
74+ x = position . xMin + xOffset ,
5275 } ;
5376
5477 using ( TriGuiHelper . PushLabelWidth ( EditorGUIUtility . labelWidth / ChildrenCount ) )
5578 {
5679 child . OnGUI ( childRect ) ;
5780 }
81+
82+ xOffset += childWidth + spacing ;
5883 }
5984 }
85+
86+ private float GetDynamicChildCount ( )
87+ {
88+ var count = 0f ;
89+
90+ for ( var i = 0 ; i < ChildrenCount ; i ++ )
91+ {
92+ if ( TryGetFixedSizeByIndex ( i , out _ ) )
93+ {
94+ continue ;
95+ }
96+
97+ count ++ ;
98+ }
99+
100+ return count ;
101+ }
102+
103+ private float GetChildWidth ( int i , float totalDynamic , float dynamicChildCount )
104+ {
105+ if ( TryGetFixedSizeByIndex ( i , out var fixedSize ) )
106+ {
107+ return fixedSize ;
108+ }
109+
110+ return totalDynamic / dynamicChildCount ;
111+ }
112+
113+ private bool TryGetFixedSizeByIndex ( int index , out float fixedSize )
114+ {
115+ if ( index < _sizes . Length && _sizes [ index ] > 0f )
116+ {
117+ fixedSize = _sizes [ index ] ;
118+ return true ;
119+ }
120+
121+ fixedSize = 0f ;
122+ return false ;
123+ }
60124 }
61125}
0 commit comments