11namespace user_interface_base {
2-
32 export interface INavigator {
43 clear : ( ) => void
54 addButtons : ( btns : Button [ ] ) => void
@@ -8,6 +7,7 @@ namespace user_interface_base {
87 screenToButton : ( x : number , y : number ) => Button
98 initialCursor : ( row : number , col : number ) => Button
109 updateAria : ( ) => void
10+ drawComponents ( ) : void ;
1111 }
1212
1313 export const BACK_BUTTON_ERROR_KIND = "back_button"
@@ -41,6 +41,16 @@ namespace user_interface_base {
4141 this . buttonGroups . push ( btns )
4242 }
4343
44+
45+ /**
46+ * Invoke .draw() on each button
47+ */
48+ public drawComponents ( ) {
49+ this . buttonGroups . forEach ( row => {
50+ row . forEach ( btn => { btn . draw ( ) } )
51+ } )
52+ }
53+
4454 public screenToButton ( x : number , y : number ) : Button {
4555 const p = new Vec2 ( x , y )
4656 for ( let row = 0 ; row < this . buttonGroups . length ; row ++ ) {
@@ -110,6 +120,7 @@ namespace user_interface_base {
110120 break
111121 }
112122 }
123+
113124 const btn = this . buttonGroups [ this . row ] [ this . col ]
114125 this . reportAria ( btn )
115126 return btn
@@ -145,29 +156,50 @@ namespace user_interface_base {
145156 }
146157 }
147158
148-
149-
150159 export class GridNavigator extends RowNavigator {
151160 private height : number ;
152161 private widths : number [ ] ;
153162
154- constructor ( height : number , width ?: number , widths ?: number [ ] ) {
163+ constructor ( btns ?: Button [ ] [ ] ) {
155164 super ( )
156- this . height = height
157165
158- if ( widths != null ) {
159- this . widths = widths
160- }
161- else {
162- width = ( width != null ) ? width : 1
166+ if ( btns ) {
167+ this . buttonGroups = btns
168+ this . widths = btns . map ( row => row . length )
169+ this . height = btns . length
170+ } else {
171+ this . height = 0 ;
163172 this . widths = [ ]
164- for ( let _ = 0 ; _ < width ; _ ++ )
165- this . widths . push ( width )
166173 }
167174 }
168175
169- public addButtons ( btns : Button [ ] ) {
176+ public setGrid ( btns : Button [ ] [ ] ) {
177+ this . buttonGroups = btns
178+ this . widths = btns . map ( row => row . length )
179+ this . height = btns . length
180+ }
181+
182+
183+ /**
184+ * Append a row during runtime.
185+ * @param btns
186+ */
187+ public addRow ( btns : Button [ ] ) {
170188 this . buttonGroups . push ( btns )
189+ this . widths . push ( btns . length )
190+ this . height ++
191+ }
192+
193+ /**
194+ * Append a col during runtime.
195+ * Does not need to be the same size as the grid height
196+ * @param btns
197+ */
198+ public addCol ( btns : Button [ ] ) {
199+ for ( let i = 0 ; i < btns . length ; i ++ ) {
200+ this . buttonGroups [ i ] . push ( btns [ i ] ) ;
201+ this . widths [ i ] ++ ;
202+ }
171203 }
172204
173205 public move ( dir : CursorDir ) {
@@ -188,18 +220,23 @@ namespace user_interface_base {
188220 // Row below could have less cols, adjust if neccessary:
189221 if ( this . widths [ this . row ] <= this . col )
190222 this . col = this . widths [ this . row ] - 1
223+
191224 break
192225 }
193226
194227 case CursorDir . Left : {
195- if ( this . col == 0 )
228+ if ( this . widths [ this . row ] == 1 )
229+ break
230+ else if ( this . col == 0 )
196231 this . col = this . widths [ this . row ] - 1
197232 else
198233 this . col -= 1
199234 break
200235 }
201236
202237 case CursorDir . Right : {
238+ if ( this . widths [ this . row ] == 1 )
239+ break
203240 if ( this . col == this . widths [ this . row ] )
204241 this . col = 0
205242 else
@@ -215,15 +252,13 @@ namespace user_interface_base {
215252 }
216253 }
217254
218- const index = this . widths . slice ( 0 , this . row ) . reduce ( ( p , c ) => p + c , 0 )
219- const btn = this . buttonGroups [ 0 ] [ index + this . col ]
255+ const btn = this . buttonGroups [ this . row ] [ this . col ]
220256 this . reportAria ( btn )
221257 return btn
222258 }
223259
224260 public getCurrent ( ) : Button {
225- const index = this . widths . slice ( 0 , this . row ) . reduce ( ( p , c ) => p + c , 0 )
226- return this . buttonGroups [ 0 ] [ index + this . col ]
261+ return this . buttonGroups [ this . row ] [ this . col ] ;
227262 }
228263 }
229264
@@ -289,6 +324,8 @@ namespace user_interface_base {
289324 this . deleteButton = btn
290325 }
291326
327+ public drawComponents ( ) { }
328+
292329 getCurrent ( ) {
293330 // console.log(`row: ${this.row}, col: ${this.col}`)
294331 if ( this . row == - 1 ) {
0 commit comments