55
66namespace YAFC . UI
77{
8- public class DataColumn < TData >
8+ public abstract class DataColumn < TData >
99 {
10- public readonly Action < ImGui , TData > build ;
11- public readonly GuiBuilder menuBuilder ;
12- public readonly string header ;
1310 public readonly float minWidth ;
1411 public readonly float maxWidth ;
1512 public readonly bool isFixedSize ;
1613 public float width ;
1714
18- public DataColumn ( string header , Action < ImGui , TData > build , GuiBuilder menuBuilder , float width , float minWidth = 0f , float maxWidth = 0f )
15+ public DataColumn ( float width , float minWidth = 0f , float maxWidth = 0f )
1916 {
20- this . build = build ;
21- this . menuBuilder = menuBuilder ;
22-
23- this . header = header ;
2417 this . width = width ;
2518 this . minWidth = minWidth == 0f ? width : minWidth ;
2619 this . maxWidth = maxWidth == 0f ? width : maxWidth ;
2720 isFixedSize = minWidth == maxWidth ;
2821 }
22+
23+ public abstract void BuildHeader ( ImGui gui ) ;
24+ public abstract void BuildElement ( ImGui gui , TData data ) ;
2925 }
30-
26+
27+ public abstract class TextDataColumn < TData > : DataColumn < TData >
28+ {
29+ public readonly string header ;
30+ private readonly bool hasMenu ;
31+
32+ protected TextDataColumn ( string header , float width , float minWidth = 0 , float maxWidth = 0 , bool hasMenu = false ) : base ( width , minWidth , maxWidth )
33+ {
34+ this . header = header ;
35+ this . hasMenu = hasMenu ;
36+ }
37+ public override void BuildHeader ( ImGui gui )
38+ {
39+ gui . BuildText ( header ) ;
40+ if ( hasMenu )
41+ {
42+ var rect = gui . statePosition ;
43+ var menuRect = new Rect ( rect . Right - 1.7f , rect . Y , 1.5f , 1.5f ) ;
44+ if ( gui . isBuilding )
45+ gui . DrawIcon ( menuRect , Icon . DropDown , SchemeColor . BackgroundText ) ;
46+ if ( gui . BuildButton ( menuRect , SchemeColor . None , SchemeColor . Grey ) )
47+ gui . ShowDropDown ( menuRect , BuildMenu , new Padding ( 1f ) ) ;
48+ }
49+ }
50+
51+ public virtual void BuildMenu ( ImGui gui ) { }
52+ }
53+
3154 public class DataGrid < TData > where TData : class
3255 {
33- private readonly DataColumn < TData > [ ] columns ;
56+ public readonly List < DataColumn < TData > > columns ;
3457 private readonly Padding innerPadding = new Padding ( 0.2f ) ;
3558 public float width { get ; private set ; }
3659 private readonly float spacing ;
3760 private Vector2 buildingStart ;
3861 private ImGui contentGui ;
62+ public float headerHeight = 1.3f ;
3963
40- public DataGrid ( DataColumn < TData > [ ] columns )
64+ public DataGrid ( params DataColumn < TData > [ ] columns )
4165 {
42- this . columns = columns ;
66+ this . columns = new List < DataColumn < TData > > ( columns ) ;
4367 spacing = innerPadding . left + innerPadding . right ;
4468 }
69+
4570
4671 private void BuildHeaderResizer ( ImGui gui , DataColumn < TData > column , Rect rect )
4772 {
@@ -83,34 +108,26 @@ public void BuildHeader(ImGui gui)
83108 var x = 0f ;
84109 var topSeparator = gui . AllocateRect ( 0f , 0.1f ) ;
85110 var y = gui . statePosition . Y ;
86- using ( var group = gui . EnterFixedPositioning ( 0f , 1f , innerPadding ) )
111+ using ( var group = gui . EnterFixedPositioning ( 0f , headerHeight , innerPadding ) )
87112 {
88- foreach ( var column in columns )
113+ for ( var index = 0 ; index < columns . Count ; index ++ ) // Do not change to foreach
89114 {
115+ var column = columns [ index ] ;
90116 if ( column . width < column . minWidth )
91117 column . width = column . minWidth ;
92118 var rect = new Rect ( x , y , column . width , 0f ) ;
93- group . SetManualRectRaw ( rect , RectAllocator . LeftRow ) ;
94- gui . BuildText ( column . header ) ;
119+ @ group. SetManualRectRaw ( rect , RectAllocator . LeftRow ) ;
120+ column . BuildHeader ( gui ) ;
95121 rect . Bottom = gui . statePosition . Y ;
96122 x += column . width + spacing ;
97123
98124 if ( ! column . isFixedSize )
99125 {
100- BuildHeaderResizer ( gui , column , new Rect ( x - 0.7f , y , 1f , 2.2f ) ) ;
101- }
102-
103- if ( column . menuBuilder != null )
104- {
105- var menuRect = new Rect ( rect . Right - 1.7f , rect . Y + 0.3f , 1.5f , 1.5f ) ;
106- if ( gui . isBuilding )
107- gui . DrawIcon ( menuRect , Icon . DropDown , SchemeColor . BackgroundText ) ;
108- if ( gui . BuildButton ( menuRect , SchemeColor . None , SchemeColor . Grey ) )
109- gui . ShowDropDown ( menuRect , column . menuBuilder , new Padding ( 1f ) ) ;
126+ BuildHeaderResizer ( gui , column , new Rect ( x - 0.7f , y , 1f , headerHeight + 0.9f ) ) ;
110127 }
111128 }
112129 }
113- width = x + 0.2f - spacing ;
130+ width = MathF . Max ( x + 0.2f - spacing , gui . width - 1f ) ;
114131
115132 var separator = gui . AllocateRect ( x , 0.1f ) ;
116133 if ( gui . isBuilding )
@@ -138,7 +155,7 @@ public Rect BuildRow(ImGui gui, TData element, float startX = 0f)
138155 if ( column . width < column . minWidth )
139156 column . width = column . minWidth ;
140157 @group . SetManualRect ( new Rect ( x , 0 , column . width , 0f ) , RectAllocator . LeftRow ) ;
141- column . build ( gui , element ) ;
158+ column . BuildElement ( gui , element ) ;
142159 x += column . width + spacing ;
143160 }
144161 }
@@ -148,7 +165,7 @@ public Rect BuildRow(ImGui gui, TData element, float startX = 0f)
148165 var rect = gui . lastRect ;
149166 var bottom = gui . lastRect . Bottom ;
150167 if ( gui . isBuilding )
151- gui . DrawRectangle ( new Rect ( startX , bottom - 0.1f , x - startX , 0.1f ) , SchemeColor . Grey ) ;
168+ gui . DrawRectangle ( new Rect ( startX , bottom - 0.1f , width - startX , 0.1f ) , SchemeColor . Grey ) ;
152169 return rect ;
153170 }
154171
@@ -164,16 +181,18 @@ public Rect EndBuildingContent(ImGui gui)
164181 return new Rect ( buildingStart . X , buildingStart . Y , width , bottom - buildingStart . Y ) ;
165182 }
166183
167- public bool BuildContent ( ImGui gui , IReadOnlyList < TData > data , out ( TData from , TData to ) reorder , out Rect rect )
184+ public bool BuildContent ( ImGui gui , IReadOnlyList < TData > data , out ( TData from , TData to ) reorder , out Rect rect , Func < TData , bool > filter = null )
168185 {
169186 BeginBuildingContent ( gui ) ;
170187 reorder = default ;
171188 var hasReorder = false ;
172189 for ( var i = 0 ; i < data . Count ; i ++ ) // do not change to foreach
173190 {
174191 var t = data [ i ] ;
192+ if ( filter != null && ! filter ( t ) )
193+ continue ;
175194 var rowRect = BuildRow ( gui , t ) ;
176- if ( gui . DoListReordering ( rowRect , rowRect , t , out var from ) )
195+ if ( ! hasReorder && gui . DoListReordering ( rowRect , rowRect , t , out var from , SchemeColor . PureBackground , false ) )
177196 {
178197 reorder = ( @from , t ) ;
179198 hasReorder = true ;
0 commit comments