1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
#include <string.h>
#include "base.h"
#include "../game.h"
// =========== [ impl ] ===========
#include "../util/input.h"
#include "../render/gl_2d.h"
#include "../render/gl_2d_font.h"
// ================================
__gui_internal _gui;
void gui_load_font( GL_DATA* gl, __gui_internal::__font* fnt, const char* name ) {
// todo: this probably crashes when fonts r re-created
GL_FONT** fptr = gl->fonts.where( fn( GL_FONT** ptr ) {
GL_FONT* f = *ptr;
return strstr( f->name, name ) != 0;
} );
if( !fptr ) {
dlog( "gui_load_font() : error loading font %s\n", name );
return;
}
fnt->glfnt = *fptr;
}
void gui_init( GAME_DATA* game ) {
GL_DATA* gl = game->gl;
gui_load_font( gl, &_gui.fonts.jpn12, "jpn12" );
gui_load_font( gl, &_gui.fonts.jpn16, "jpn16" );
}
void gui_end() {
_gui.cur_window = 0;
}
void gui_draw( GAME_DATA* game ) {
_gui.gl2d = game->shaders.gl2d;
_gui.gl2d_font = game->shaders.gl2d_texcoord;
_gui.windows.each( fn( GUI_WINDOW** w ) {
GUI_WINDOW* wnd = *w;
if( !wnd->enabled ) return;
wnd->draw_fn( wnd );
} );
}
void gui_input( GAME_DATA* game ) {
for( I32 i = (I32)_gui.windows.size - 1; i >= 0; --i ) {
GUI_WINDOW* wnd = _gui.windows[i];
if( !wnd->enabled ) continue;
wnd->input_fn( wnd );
break;
}
}
void gui_onframe( GAME_DATA* game ) {
gui_run_callbacks();
gui_input( game );
gui_draw( game );
}
void gui_free( GUI_BASE* node ) {
node->children.each( fn( GUI_BASE** ptr ) {
GUI_BASE* child = *ptr;
gui_free( child );
} );
node->children.clear();
if( !node->parent ) {
I32 idx = _gui.windows.idx_of( (GUI_WINDOW*)node );
if( idx != -1 )
_gui.windows.erase( idx );
if( _gui.cur_window == node ) {
if( _gui.windows.size > 0 ) {
idx = idx > _gui.windows.size - 1 ? _gui.windows.size - 1 : 0;
_gui.cur_window = _gui.windows[idx];
}
}
}
else if( _gui.cur_view == node ) {
_gui.cur_view = 0;
for( U32 i = 0; i < node->parent->children.size; ++i ) {
GUI_BASE* n = node->parent->children.data[i];
if( n == node )
continue;
if( !strcmp( n->name, "BASE_VIEW" ) ) {
_gui.cur_view = (GUI_VIEW*)n;
break;
}
n = gui_find_node( n, "BASE_VIEW" );
if( n != node ) {
_gui.cur_view = (GUI_VIEW*)n;
break;
}
}
}
delete node;
}
I32 gui_relx( GUI_BASE* node ) {
I32 x = node->x;
for( GUI_BASE* p = node->parent; !!p; p = p->parent ) {
x += p->x;
}
return x;
}
I32 gui_rely( GUI_BASE* node ) {
I32 y = node->y;
for( GUI_BASE* p = node->parent; !!p; p = p->parent ) {
y += p->y;
}
return y;
}
GUI_BASE* gui_find_node( GUI_BASE* root, const char* name ) {
if( !root )
return 0;
if( !strcmp( root->name, name ) )
return root;
for( U32 i = 0; i < root->children.size; ++i ) {
GUI_BASE* child = root->children.data[i];
GUI_BASE* node = gui_find_node( child, name );
if( node )
return node;
}
return 0;
}
GUI_WINDOW* gui_get_parent_wnd( GUI_BASE* node ) {
if( !node->parent )
return (GUI_WINDOW*)node;
for( GUI_BASE* b = node; !!b; b = b->parent ) {
if( !b->parent )
return (GUI_WINDOW*)b;
}
return (GUI_WINDOW*)node;
}
void gui_empty_children( GUI_BASE* node ) {
node->children.each( fn( GUI_BASE** ptr ) {
gui_free( *ptr );
} );
node->children.clear();
}
GUI_VIEW* gui_get_view() {
return _gui.cur_view;
}
void gui_set_view( struct GUI_VIEW* view ) {
_gui.cur_view = view;
}
GUI_WINDOW* gui_get_window() {
return _gui.cur_window;
}
void gui_set_window( GUI_WINDOW* window ) {
_gui.cur_window = window;
}
void gui_bring_to_top( GUI_WINDOW* window ) {
for( U32 i = 0; i < _gui.windows.size; ++i ) {
if( _gui.windows[i] == window ) {
_gui.windows.erase( i );
break;
}
}
_gui.windows.push( window );
}
U8 gui_is_fg_window( GUI_BASE* node ) {
GUI_WINDOW* top_wnd;
for( I32 i = _gui.windows.size - 1; i >= 0; --i ) {
GUI_WINDOW* wnd = _gui.windows[i];
if( !wnd->enabled )
continue;
top_wnd = wnd;
break;
}
if( !node->parent ) {
return top_wnd == node;
}
return gui_get_parent_wnd( node ) == top_wnd;
}
U8 gui_check_target() {
if( !_gui.cur_view ) {
dlog( "gui_check_target() : no view" );
return 0;
}
return 1;
}
void gui_run_callbacks() {
LIST<__gui_internal::callback_entry> cb_copy = _gui.callbacks;
_gui.callbacks.clear();
for( I32 i = cb_copy.size - 1; i >= 0; --i ) {
__gui_internal::callback_entry* e = &cb_copy[i];
if( e->callback )
e->callback( e->data );
}
}
void gui_push_callback( GUI_CALLBACK cb ) {
__gui_internal::callback_entry e;
e.data = 0;
e.callback = cb;
_gui.callbacks.push( e );
}
void gui_push_callback( void* data, GUI_CALLBACK cb ) {
__gui_internal::callback_entry e;
e.data = data;
e.callback = cb;
_gui.callbacks.push( e );
}
void gui_base_input_fn( void* ptr ) {
GUI_BASE* e = (GUI_BASE*)ptr;
if( !e ) return;
e->children.each( fn( GUI_BASE** ptr ) {
GUI_BASE* child = *ptr;
if( child->enabled && child->input_fn )
child->input_fn( child );
} );
}
void gui_draw_frect( I32 x, I32 y, I32 w, I32 h, CLR col ) {
gl_2d_frect( _gui.gl2d, { (F32)x, (F32)y }, { (F32)w, (F32)h }, col );
}
void gui_draw_rect( I32 x, I32 y, I32 w, I32 h, CLR col ) {
gl_2d_rect( _gui.gl2d, { (F32)x, (F32)y }, { (F32)w, (F32)h }, col );
}
void gui_draw_line( I32 x0, I32 y0, I32 x1, I32 y1, CLR col ) {
gl_2d_line( _gui.gl2d, { (F32)x0, (F32)y0 }, { (F32)x1, (F32)y1 }, col );
}
GL_FONT* gui_font_from_idx( U32 fnt ) {
if( fnt > FNT_LAST ) {
dlog( "gui_font_from_idx() : invalid font index %d", fnt );
return 0;
}
__gui_internal::__font* pfnt;
switch( fnt ) {
case FNT_JPN12: pfnt = &_gui.fonts.jpn12; break;
case FNT_JPN16: pfnt = &_gui.fonts.jpn16; break;
default: return 0;
};
return pfnt->glfnt;
}
void gui_draw_str_internal( I32 x, I32 y, U8 align, U32 fnt, CLR col, const char* str ) {
GL_FONT* pfnt = gui_font_from_idx( fnt );
if( !pfnt ) return;
I32 w;
gui_draw_get_str_bounds( &w, 0, fnt, str );
switch( align ) {
case ALIGN_R: x -= w; break;
case ALIGN_C: x -= (I32)( w * 0.5f ); break;
}
gl_font_draw( pfnt, _gui.gl2d_font, { (F32)x, (F32)y }, str, col );
}
void gui_draw_str( I32 x, I32 y, U8 align, U32 fnt, CLR col, const char* fmt, ... ) {
va_list args;
va_start( args, fmt );
char buf[GUI_STR_BUF_MAX];
vsprintf( buf, fmt, args );
va_end( args );
gui_draw_str_internal( x, y, align, fnt, col, buf );
}
void gui_draw_get_str_bounds_internal( I32* w, I32* h, U32 fnt, const char* buf ) {
GL_FONT* pfnt = gui_font_from_idx( fnt );
if( !pfnt ) return;
VEC2 dim = gl_font_dim( pfnt, buf );
if( w ) *w = dim.x;
if( h ) *h = dim.y;
}
void gui_draw_get_str_bounds( I32* w, I32* h, U32 fnt, const char *fmt, ... ) {
va_list args;
va_start( args, fmt );
char buf[GUI_STR_BUF_MAX];
vsprintf( buf, fmt, args );
va_end( args );
gui_draw_get_str_bounds_internal( w, h, fnt, buf );
}
void gui_draw_get_clip( I32 *x, I32 *y, I32 *w, I32 *h ) {
VEC2 start, dim;
gl_get_clip( _gui.gl2d->gl, &start, &dim );
if( x ) *x = (I32)start.x;
if( y ) *y = (I32)start.y;
if( w ) *w = (I32)dim.x;
if( h ) *h = (I32)dim.y;
}
void gui_draw_set_clip( I32 x, I32 y, I32 w, I32 h ) {
VEC2 start, dim;
start.x = (F32)x;
start.y = (F32)y;
dim.x = (F32)w;
dim.y = (F32)h;
gl_set_clip( _gui.gl2d->gl, start, dim );
}
void gui_draw_reset_clip() {
gl_reset_clip( _gui.gl2d->gl );
}
void gui_draw_push_clip( I32 x, I32 y, I32 w, I32 h ) {
I32 cx,cy,cw,ch;
gui_draw_get_clip( &cx, &cy, &cw, &ch );
__gui_internal::clip_rect rect{ cx, cy, cw, ch };
_gui.clip_rects.push( rect );
gui_draw_set_clip( x, y, w, h );
}
void gui_draw_pop_clip() {
if( !_gui.clip_rects.size ) {
dlog( "gui_draw_pop_clip() : clip stack empty\n" );
return gui_draw_reset_clip();
}
__gui_internal::clip_rect rect = _gui.clip_rects.pop();
gui_draw_set_clip( rect.x, rect.y, rect.w, rect.h );
}
void gui_cursor_pos( I32 *x, I32 *y ) {
if( x ) *x = input.mouse.pos.x;
if( y ) *y = input.mouse.pos.y;
}
U8 gui_mbutton_down( U8 button ) {
switch( button ) {
case GUI_MBTNLEFT:
return input.mouse.left;
case GUI_MBTNRIGHT:
return input.mouse.right;
case GUI_MBTNMIDDLE:
return input.mouse.middle;
case GUI_MBTNSCROLL:
return input.mouse.wheel;
};
return 0;
}
void gui_capture_scroll() {
input.mouse.wheel = 0;
}
U8 gui_key_down( U8 key ) {
return input.keys[key];
}
F32 gui_frametime() {
return _gui.gl2d->gl->frametime;
}
F32 gui_time() {
return u_time();
}
|