@@ -76,6 +76,7 @@ function menu(
76
76
ArrangedElements . stringified . push ( [ "<No Options Provided>" ] )
77
77
ArrangedElements . data . push ( [ null ] ) ; // not needed, but nice to keep data and string in sync.
78
78
}
79
+
79
80
if ( ! callback ) {
80
81
//overload 1, just display a menu with no callback
81
82
Call . menu ( target . con , registeredListeners . none , title , description , ArrangedElements . stringified ) ;
@@ -124,9 +125,11 @@ function menu(
124
125
export function pageMenu ( title :string , description :string , elements :GUI_Element [ ] [ ] , target :FishPlayer , callback : ( opts : { data :any , text :string , sender :FishPlayer , outputSuccess :( message :string ) => void , outputFail :( message :string ) => void ; } ) => void ) {
125
126
let pages = elements . length
126
127
function drawpage ( index :number ) {
127
- let e :GUI_Element [ ] = [ new GUI_Page ( index + 1 , pages ) ]
128
+ let e :GUI_Element [ ] = [ ] ;
128
129
e . push ( ...elements [ index ] )
130
+ e . push ( new GUI_Page ( index + 1 , pages ) )
129
131
menu ( title , description , e , target , ( res ) => {
132
+ // handle control element of the ui
130
133
if ( typeof res . data === 'string' ) {
131
134
switch ( res . data ) {
132
135
case "left" :
@@ -141,20 +144,23 @@ export function pageMenu(title:string, description:string, elements:GUI_Element[
141
144
default :
142
145
callback ( res ) ;
143
146
}
147
+ } else {
148
+ callback ( res . data ) ;
144
149
}
145
150
} )
146
151
return ;
147
152
}
153
+ drawpage ( 0 ) ;
148
154
149
155
}
150
- //auto formats a array into a page menu
151
156
//TODO make list a GUI_Element[] instead of a single Container
152
- export function listMenu ( title :string , description :string , list :GUI_Container , target :FishPlayer , callback : ( opts : { data :any , text :string , sender :FishPlayer , outputSuccess :( message :string ) => void , outputFail :( message :string ) => void ; } ) => void , pageSize :number = 10 ) {
153
- let buttons = { data :[ ] as any [ ] [ ] , }
154
- list . data ( ) [ 0 ] . reduce ( ( result , _ , index ) => { if ( index % pageSize === 0 ) { buttons . data . push ( buttons . data . slice ( index , index + pageSize ) ) ; } return result ; } ) ;
155
- let pages :GUI_Element [ ] [ ] = [ ] ;
156
- buttons . data . forEach ( page => { pages . push ( [ new GUI_Container ( page , 1 , list . stringifier ) ] ) } ) //wrap each page in a container
157
- pageMenu ( title , description , pages , target , callback ) ;
157
+ export function listMenu ( title :string , description :string , list :GUI_Container , target :FishPlayer , callback : ( opts : { data :any , text :string , sender :FishPlayer , outputSuccess :( message :string ) => void , outputFail :( message :string ) => void ; } ) => void , pageSize :number = 10 ) {
158
+ let pooledData :any [ ] = [ ] ;
159
+ list . data ( ) . flat ( ) . forEach ( ( data ) => { pooledData . push ( data ) } ) ;
160
+ let pagedData :any [ ] [ ] = pooledData . reduce ( ( res , _ , index ) => { if ( index % pageSize === 0 ) { res . push ( pooledData . slice ( index , index + pageSize ) ) ; } return res ; } , [ ] as any [ ] [ ] ) ;
161
+ let pagesElements :GUI_Element [ ] [ ] = [ ] ;
162
+ pagedData . forEach ( pageData => pagesElements . push ( [ new GUI_Container ( pageData , 1 , list . stringifier ) ] ) ) ;
163
+ pageMenu ( title , description , pagesElements , target , callback ) ;
158
164
}
159
165
//#endregion
160
166
//#region GUI Elements
@@ -163,8 +169,6 @@ interface GUI_Element{
163
169
format ( ) :string [ ] [ ] ,
164
170
data ( ) :any [ ] [ ]
165
171
}
166
-
167
- //const reservedStrings = ["left", "center", "right"] // strings used for paged menus, cannot be handled correct
168
172
export class GUI_Container implements GUI_Element {
169
173
constructor (
170
174
public options :any [ ] ,
@@ -183,8 +187,8 @@ export class GUI_Page implements GUI_Element{
183
187
public currentPage :number ,
184
188
public pages :number
185
189
) { }
186
- public format = ( ) => { return ( [ [ "<--" ] , [ `${ this . currentPage } /${ this . pages } ` ] , [ "-->" ] ] ) } ;
187
- public data = ( ) => { return ( [ [ "left" , "center" , "left " ] ] ) }
190
+ public format = ( ) => { return ( to2DArray ( [ "<--" , `${ this . currentPage } /${ this . pages } ` , "-->" ] , 3 ) ) } ;
191
+ public data = ( ) => { return ( [ [ "left" , "center" , "right " ] ] ) }
188
192
189
193
}
190
194
export class GUI_Confirm implements GUI_Element {
@@ -197,5 +201,4 @@ export {
197
201
registeredListeners as listeners ,
198
202
menu
199
203
} ;
200
-
201
-
204
+ //#endregion
0 commit comments