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
|
#pragma once
#include "../util/color.h"
#include "../util/allocator.h"
// ======================================= [ colorscheme ] ========================================
static const struct {
CLR txt = CLR::WHITE();
CLR txt_inactive = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.3f );
CLR border = CLR::WHITE();
CLR border_inactive = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.55f );
CLR bg = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.92f );
CLR bg_sec = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.97f );
CLR bg_alt = CLR::blend( CLR::WHITE(), CLR::BLACK(), 0.985f );
} ui_clr;
// ========================================== [ base ] ============================================
const U32 GUI_STR_BUF_MAX = 16384;
const U32 GUI_TEXTBOX_MAX = 1024;
const U32 GUI_NAME_LEN = 512;
/* impl */
extern void gui_init( struct GAME_DATA* game );
extern void gui_draw( struct GAME_DATA* game );
extern void gui_input( struct GAME_DATA* game );
extern void gui_onframe( struct GAME_DATA* game );
extern void gui_end();
/* */
extern I32 gui_relx( struct GUI_BASE* node ); //relative x pos for node
extern I32 gui_rely( struct GUI_BASE* node ); //relative y pos for node
extern struct GUI_BASE* gui_find_node( struct GUI_BASE* root, const char* name );
extern struct GUI_WINDOW* gui_get_parent_wnd( struct GUI_BASE* node );
extern void gui_empty_children( struct GUI_BASE* node );
extern void gui_free( struct GUI_BASE* node );
extern struct GUI_VIEW* gui_get_view(); // gets currently edited view
extern void gui_set_view( struct GUI_VIEW* view ); // sets currently edited view
extern struct GUI_WINDOW* gui_get_window(); // gets currently edited window
extern void gui_set_window( struct GUI_WINDOW* wnd ); // sets currently edited window
extern void gui_bring_to_top( struct GUI_WINDOW* wnd ); // pushes window to be rendered last
extern U8 gui_is_fg_window( struct GUI_BASE* node ); // 1 if parent window (or window itself) is rendered last
// ======================================= [ components ] =========================================
struct GUI_LIST_ENTRY {
I32 val;
char title[256];
};
typedef void( *GUI_CALLBACK )( void* ptr );
extern struct GUI_VIEW* gui_view( I32 x, I32 y, I32 w, I32 h ); // sets current view
extern struct GUI_WINDOW* gui_window( I32 w, I32 h ); // sets current view and window
extern struct GUI_WINDOW* gui_window( I32 x, I32 y, I32 w, I32 h ); // sets current view and window
extern struct GUI_TITLE* gui_title( const char* title );
extern struct GUI_LABEL* gui_label( I32 x, I32 y, const char* title, ... );
extern struct GUI_BUTTON* gui_button( I32 x, I32 y, I32 w, I32 h, const char* title, GUI_CALLBACK cb );
extern struct GUI_TEXTBOX* gui_textbox( I32 x, I32 y, I32 w, I32 h, const char* title, U32 maxlen, U8 allow_newl = 0 );
extern struct GUI_CHECKBOX* gui_checkbox( I32 x, I32 y, const char* title, U8* pval );
extern struct GUI_LIST* gui_list( I32 x, I32 y, I32 w, I32 h, const char* title, LIST<GUI_LIST_ENTRY>* list, I32* pval );
extern struct GUI_LIST_ENTRY* gui_list_get_selected( struct GUI_LIST* list );
extern struct GUI_FLOATINPUT* gui_floatinput(
I32 x, I32 y, I32 w,
const char* title,
F32* pval,
F32 min,
F32 max,
F32 step = 1.f,
const char* printfmt = "%.2f"
);
extern struct GUI_VECTORINPUT* gui_vectorinput(
I32 x, I32 y, I32 w,
const char* title,
F32* pval,
U32 valc,
F32 min,
F32 max,
F32 step = 1.f,
const char* letters = "xyzw",
const char* printfmt = "%.2f"
);
extern struct GUI_COLORINPUT* gui_colorinput(
I32 x,
I32 y,
I32 w,
const char* title,
CLR* pval,
U8 showalpha = 1
);
// ======================================= [ definitions ] ========================================
enum GuiTxtAlign_t {
ALIGN_L,
ALIGN_R,
ALIGN_C
};
enum GuiFont_t {
FNT_JPN12,
FNT_JPN16,
FNT_LAST
};
typedef void( *GUI_DRAW_FN )( void* el );
typedef void( *GUI_INPUT_FN )( void* el );
extern void gui_base_input_fn( void* );
extern void gui_view_draw_fn( void* );
struct GUI_BASE {
I32 x{}, y{};
I32 w{}, h{};
U8 enabled{1};
U8 canvas{};
char name[GUI_NAME_LEN];
I32 xbound{}, ybound{};
I32 xoff{}, yoff{};
GUI_BASE* parent{};
LIST<GUI_BASE*> children{};
GUI_DRAW_FN draw_fn{};
GUI_INPUT_FN input_fn = gui_base_input_fn;
};
struct GUI_VIEW : GUI_BASE {
U8 initheld;
};
struct GUI_WINDOW : GUI_VIEW {
U8 locked{};
U8 ontop{};
};
struct GUI_LABEL : GUI_BASE {};
struct GUI_TITLE : GUI_BASE {
I32 xoff, yoff;
U8 held{};
};
struct GUI_LIST : GUI_BASE {
LIST<GUI_LIST_ENTRY>* plist;
I32* pval;
U8 held;
GUI_CALLBACK cb;
};
struct GUI_BUTTON : GUI_BASE {
GUI_CALLBACK cb;
U8 held;
void* extra;
};
struct GUI_CHECKBOX : GUI_BASE {
U8 held;
U8* pval;
GUI_CALLBACK cb;
};
struct GUI_TEXTBOX : GUI_BASE {
char value[GUI_TEXTBOX_MAX];
U32 len;
U32 maxlen;
U8 keys[0xff];
U8 prevkeys[0xff];
U8 heldkey;
F32 heldtime;
F32 repeattime;
F32 blinktime;
U8 blink;
U8 allow_newl;
U8 active;
U8 m1held;
GUI_CALLBACK cb;
};
struct GUI_FLOATINPUT : GUI_BASE {
const char* valfmt;
F32* pval;
F32 min;
F32 max;
F32 step;
I32 lastmx;
U8 heldoutbounds;
U8 held;
// for color sliders
CLR bgcol;
U8 customclr;
U8 wraparound;
GUI_CALLBACK cb;
};
struct GUI_VECTORINPUT : GUI_VIEW {
const char* valfmt;
F32* pval;
U32 valc;
const char* letters;
F32 min;
F32 max;
F32 step;
GUI_VIEW* inputview;
LIST<GUI_FLOATINPUT*> inputs;
GUI_CALLBACK cb;
};
struct GUI_COLORINPUT : GUI_VECTORINPUT {};
// ======================================== [ internal ] ==========================================
extern void gui_push_callback( GUI_CALLBACK cb );
extern void gui_push_callback( void* data, GUI_CALLBACK cb );
extern void gui_run_callbacks();
extern U8 gui_check_target();
extern void gui_draw_line( I32 x0, I32 y0, I32 x1, I32 y1, CLR col );
extern void gui_draw_rect( I32 x, I32 y, I32 w, I32 h, CLR col );
extern void gui_draw_frect( I32 x, I32 y, I32 w, I32 h, CLR col );
extern void gui_draw_circle( I32 x, I32 y, I32 r, CLR col );
extern void gui_draw_str( I32 x, I32 y, U8 align, U32 fnt, CLR col, const char* fmt, ... );
extern void gui_draw_get_str_bounds( I32* w, I32* h, U32 fnt, const char* fmt, ... );
extern void gui_draw_get_clip( I32* x, I32* y, I32* w, I32* h );
extern void gui_draw_set_clip( I32 x, I32 y, I32 w, I32 h );
extern void gui_draw_reset_clip();
extern void gui_draw_push_clip( I32 x, I32 y, I32 w, I32 h );
extern void gui_draw_pop_clip();
extern F32 gui_frametime();
extern F32 gui_time();
extern void gui_cursor_pos( I32* x, I32* y );
extern U8 gui_key_down( U8 key );
extern U8 gui_mbutton_down( U8 button );
extern void gui_capture_scroll();
enum __gui_internal_mouse_button {
GUI_MBTNLEFT,
GUI_MBTNRIGHT,
GUI_MBTNMIDDLE,
GUI_MBTNSCROLL
};
struct __gui_internal {
GUI_VIEW* cur_view;
GUI_WINDOW* cur_window;
LIST<GUI_WINDOW*> windows;
struct clip_rect {
I32 x,y,w,h;
};
LIST<clip_rect> clip_rects;
struct callback_entry {
GUI_CALLBACK callback;
void* data;
};
LIST<callback_entry> callbacks;
struct __font {
struct GL_FONT* glfnt;
};
struct {
__font jpn12;
__font jpn16;
} fonts;
// draw stuff below
struct GL_SHADER_PROGRAM* gl2d;
struct GL_SHADER_PROGRAM* gl2d_font;
};
void __gui_internal_vectorinput_init(
GUI_VECTORINPUT* input,
I32 x, I32 y, I32 w,
const char* title,
F32* pval,
U32 valc,
F32 min,
F32 max,
F32 step,
const char* letters,
const char* printfmt
);
extern __gui_internal _gui;
|