-
Notifications
You must be signed in to change notification settings - Fork 79
/
sgl-microui-sapp.c
503 lines (455 loc) · 16.9 KB
/
sgl-microui-sapp.c
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//------------------------------------------------------------------------------
// sgl-microui.c
//
// https://github.com/rxi/microui sample using sokol_gl.h, sokol_gfx.h
// and sokol_app.h
//
// NOTE: for the debugging UI, cimgui is used via sokol_gfx_cimgui.h
// (C bindings to Dear ImGui instead of the ImGui C++ API)
//------------------------------------------------------------------------------
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_WARNINGS (1)
#endif
#include "sokol_gfx.h"
#include "sokol_app.h"
#include "sokol_log.h"
#include "sokol_glue.h"
#include "microui/microui.h"
#include "microui/atlas.inl"
#define SOKOL_GL_IMPL
#include "sokol_gl.h"
#include "cdbgui/cdbgui.h"
#include <stdio.h> // sprintf
typedef struct {
float r, g, b;
} color_t;
static struct {
mu_Context mu_ctx;
char logbuf[64000];
int logbuf_updated;
color_t bg;
} state = {
.bg = { 90.0f, 95.0f, 100.0f }
};
// UI functions
static void test_window(mu_Context* ctx);
static void log_window(mu_Context* ctx);
static void style_window(mu_Context* ctx);
// microui renderer functions (implementation is at the end of this file)
static void r_init(void);
static void r_begin(int disp_width, int disp_height);
static void r_end(void);
static void r_draw(void);
static void r_push_quad(mu_Rect dst, mu_Rect src, mu_Color color);
static void r_draw_rect(mu_Rect rect, mu_Color color);
static void r_draw_text(const char* text, mu_Vec2 pos, mu_Color color);
static void r_draw_icon(int id, mu_Rect rect, mu_Color color);
static int r_get_text_width(const char* text, int len);
static int r_get_text_height(void);
static void r_set_clip_rect(mu_Rect rect);
// callbacks
static int text_width_cb(mu_Font font, const char* text, int len) {
(void)font;
if (len == -1) {
len = (int) strlen(text);
}
return r_get_text_width(text, len);
}
static int text_height_cb(mu_Font font) {
(void)font;
return r_get_text_height();
}
static void write_log(const char* text) {
// FIXME: THIS IS UNSAFE!
if (state.logbuf[0]) {
strcat(state.logbuf, "\n");
}
strcat(state.logbuf, text);
state.logbuf_updated = 1;
}
// initialization
static void init(void) {
// setup sokol-gfx
sg_setup(&(sg_desc){
.environment = sglue_environment(),
.logger.func = slog_func,
});
__cdbgui_setup(1);
// setup sokol-gl
sgl_setup(&(sgl_desc_t){
.logger.func = slog_func,
});
// setup microui renderer
r_init();
// setup microui
mu_init(&state.mu_ctx);
state.mu_ctx.text_width = text_width_cb;
state.mu_ctx.text_height = text_height_cb;
}
static const char key_map[512] = {
[SAPP_KEYCODE_LEFT_SHIFT] = MU_KEY_SHIFT,
[SAPP_KEYCODE_RIGHT_SHIFT] = MU_KEY_SHIFT,
[SAPP_KEYCODE_LEFT_CONTROL] = MU_KEY_CTRL,
[SAPP_KEYCODE_RIGHT_CONTROL] = MU_KEY_CTRL,
[SAPP_KEYCODE_LEFT_ALT] = MU_KEY_ALT,
[SAPP_KEYCODE_RIGHT_ALT] = MU_KEY_ALT,
[SAPP_KEYCODE_ENTER] = MU_KEY_RETURN,
[SAPP_KEYCODE_BACKSPACE] = MU_KEY_BACKSPACE,
};
static void event(const sapp_event* ev) {
// FIXME: need to filter out events consumed by the Dear ImGui debug UI
__cdbgui_event(ev);
switch (ev->type) {
case SAPP_EVENTTYPE_MOUSE_DOWN:
mu_input_mousedown(&state.mu_ctx, (int)ev->mouse_x, (int)ev->mouse_y, (1<<ev->mouse_button));
break;
case SAPP_EVENTTYPE_MOUSE_UP:
mu_input_mouseup(&state.mu_ctx, (int)ev->mouse_x, (int)ev->mouse_y, (1<<ev->mouse_button));
break;
case SAPP_EVENTTYPE_MOUSE_MOVE:
mu_input_mousemove(&state.mu_ctx, (int)ev->mouse_x, (int)ev->mouse_y);
break;
case SAPP_EVENTTYPE_MOUSE_SCROLL:
mu_input_scroll(&state.mu_ctx, 0, (int)ev->scroll_y);
break;
case SAPP_EVENTTYPE_KEY_DOWN:
mu_input_keydown(&state.mu_ctx, key_map[ev->key_code & 511]);
break;
case SAPP_EVENTTYPE_KEY_UP:
mu_input_keyup(&state.mu_ctx, key_map[ev->key_code & 511]);
break;
case SAPP_EVENTTYPE_CHAR:
{
// don't input Backspace as character (required to make Backspace work in text input fields)
if (ev->char_code == 127) { break; }
char txt[2] = { (char)(ev->char_code & 255), 0 };
mu_input_text(&state.mu_ctx, txt);
}
break;
default:
break;
}
}
// do one frame
void frame(void) {
// UI definition
mu_begin(&state.mu_ctx);
test_window(&state.mu_ctx);
log_window(&state.mu_ctx);
style_window(&state.mu_ctx);
mu_end(&state.mu_ctx);
// micro-ui rendering
r_begin(sapp_width(), sapp_height());
mu_Command* cmd = 0;
while(mu_next_command(&state.mu_ctx, &cmd)) {
switch (cmd->type) {
case MU_COMMAND_TEXT: r_draw_text(cmd->text.str, cmd->text.pos, cmd->text.color); break;
case MU_COMMAND_RECT: r_draw_rect(cmd->rect.rect, cmd->rect.color); break;
case MU_COMMAND_ICON: r_draw_icon(cmd->icon.id, cmd->icon.rect, cmd->icon.color); break;
case MU_COMMAND_CLIP: r_set_clip_rect(cmd->clip.rect); break;
}
}
r_end();
// render the sokol-gfx default pass
sg_begin_pass(&(sg_pass) {
.action = {
.colors[0] = {
.load_action = SG_LOADACTION_CLEAR,
.clear_value = { state.bg.r / 255.0f, state.bg.g / 255.0f, state.bg.b / 255.0f, 1.0f }
}
},
.swapchain = sglue_swapchain()
});
r_draw();
__cdbgui_draw();
sg_end_pass();
sg_commit();
}
static void cleanup(void) {
__cdbgui_shutdown();
sgl_shutdown();
sg_shutdown();
}
static void test_window(mu_Context* ctx) {
/* do window */
if (mu_begin_window(ctx, "Demo Window", mu_rect(40, 40, 300, 450))) {
mu_Container *win = mu_get_current_container(ctx);
win->rect.w = mu_max(win->rect.w, 240);
win->rect.h = mu_max(win->rect.h, 300);
/* window info */
if (mu_header(ctx, "Window Info")) {
mu_Container *win = mu_get_current_container(ctx);
char buf[64];
mu_layout_row(ctx, 2, (int[]) { 54, -1 }, 0);
mu_label(ctx,"Position:");
sprintf(buf, "%d, %d", win->rect.x, win->rect.y); mu_label(ctx, buf);
mu_label(ctx, "Size:");
sprintf(buf, "%d, %d", win->rect.w, win->rect.h); mu_label(ctx, buf);
}
/* labels + buttons */
if (mu_header_ex(ctx, "Test Buttons", MU_OPT_EXPANDED)) {
mu_layout_row(ctx, 3, (int[]) { 86, -110, -1 }, 0);
mu_label(ctx, "Test buttons 1:");
if (mu_button(ctx, "Button 1")) { write_log("Pressed button 1"); }
if (mu_button(ctx, "Button 2")) { write_log("Pressed button 2"); }
mu_label(ctx, "Test buttons 2:");
if (mu_button(ctx, "Button 3")) { write_log("Pressed button 3"); }
if (mu_button(ctx, "Popup")) { mu_open_popup(ctx, "Test Popup"); }
if (mu_begin_popup(ctx, "Test Popup")) {
mu_button(ctx, "Hello");
mu_button(ctx, "World");
mu_end_popup(ctx);
}
}
/* tree */
if (mu_header_ex(ctx, "Tree and Text", MU_OPT_EXPANDED)) {
mu_layout_row(ctx, 2, (int[]) { 140, -1 }, 0);
mu_layout_begin_column(ctx);
if (mu_begin_treenode(ctx, "Test 1")) {
if (mu_begin_treenode(ctx, "Test 1a")) {
mu_label(ctx, "Hello");
mu_label(ctx, "world");
mu_end_treenode(ctx);
}
if (mu_begin_treenode(ctx, "Test 1b")) {
if (mu_button(ctx, "Button 1")) { write_log("Pressed button 1"); }
if (mu_button(ctx, "Button 2")) { write_log("Pressed button 2"); }
mu_end_treenode(ctx);
}
mu_end_treenode(ctx);
}
if (mu_begin_treenode(ctx, "Test 2")) {
mu_layout_row(ctx, 2, (int[]) { 54, 54 }, 0);
if (mu_button(ctx, "Button 3")) { write_log("Pressed button 3"); }
if (mu_button(ctx, "Button 4")) { write_log("Pressed button 4"); }
if (mu_button(ctx, "Button 5")) { write_log("Pressed button 5"); }
if (mu_button(ctx, "Button 6")) { write_log("Pressed button 6"); }
mu_end_treenode(ctx);
}
if (mu_begin_treenode(ctx, "Test 3")) {
static int checks[3] = { 1, 0, 1 };
mu_checkbox(ctx, "Checkbox 1", &checks[0]);
mu_checkbox(ctx, "Checkbox 2", &checks[1]);
mu_checkbox(ctx, "Checkbox 3", &checks[2]);
mu_end_treenode(ctx);
}
mu_layout_end_column(ctx);
mu_layout_begin_column(ctx);
mu_layout_row(ctx, 1, (int[]) { -1 }, 0);
mu_text(ctx, "Lorem ipsum dolor sit amet, consectetur adipiscing "
"elit. Maecenas lacinia, sem eu lacinia molestie, mi risus faucibus "
"ipsum, eu varius magna felis a nulla.");
mu_layout_end_column(ctx);
}
/* background color sliders */
if (mu_header_ex(ctx, "Background Color", MU_OPT_EXPANDED)) {
mu_layout_row(ctx, 2, (int[]) { -78, -1 }, 74);
/* sliders */
mu_layout_begin_column(ctx);
mu_layout_row(ctx, 2, (int[]) { 46, -1 }, 0);
mu_label(ctx, "Red:"); mu_slider(ctx, &state.bg.r, 0, 255);
mu_label(ctx, "Green:"); mu_slider(ctx, &state.bg.g, 0, 255);
mu_label(ctx, "Blue:"); mu_slider(ctx, &state.bg.b, 0, 255);
mu_layout_end_column(ctx);
/* color preview */
mu_Rect r = mu_layout_next(ctx);
mu_draw_rect(ctx, r, mu_color(state.bg.r, state.bg.g, state.bg.b, 255));
char buf[32];
sprintf(buf, "#%02X%02X%02X", (int) state.bg.r, (int) state.bg.g, (int) state.bg.b);
mu_draw_control_text(ctx, buf, r, MU_COLOR_TEXT, MU_OPT_ALIGNCENTER);
}
mu_end_window(ctx);
}
}
static void log_window(mu_Context *ctx) {
if (mu_begin_window(ctx, "Log Window", mu_rect(350, 40, 300, 200))) {
/* output text panel */
mu_layout_row(ctx, 1, (int[]) { -1 }, -25);
mu_begin_panel(ctx, "Log Output");
mu_Container *panel = mu_get_current_container(ctx);
mu_layout_row(ctx, 1, (int[]) { -1 }, -1);
mu_text(ctx, state.logbuf);
mu_end_panel(ctx);
if (state.logbuf_updated) {
panel->scroll.y = panel->content_size.y;
state.logbuf_updated = 0;
}
/* input textbox + submit button */
static char buf[128];
int submitted = 0;
mu_layout_row(ctx, 2, (int[]) { -70, -1 }, 0);
if (mu_textbox(ctx, buf, sizeof(buf)) & MU_RES_SUBMIT) {
mu_set_focus(ctx, ctx->last_id);
submitted = 1;
}
if (mu_button(ctx, "Submit")) { submitted = 1; }
if (submitted) {
write_log(buf);
buf[0] = '\0';
}
mu_end_window(ctx);
}
}
static int uint8_slider(mu_Context *ctx, unsigned char *value, int low, int high) {
static float tmp;
mu_push_id(ctx, &value, sizeof(value));
tmp = *value;
int res = mu_slider_ex(ctx, (mu_Real*)&tmp, (mu_Real)low, (mu_Real)high, 0, "%.0f", MU_OPT_ALIGNCENTER);
*value = (unsigned char)tmp;
mu_pop_id(ctx);
return res;
}
static void style_window(mu_Context *ctx) {
static struct { const char *label; int idx; } colors[] = {
{ "text:", MU_COLOR_TEXT },
{ "border:", MU_COLOR_BORDER },
{ "windowbg:", MU_COLOR_WINDOWBG },
{ "titlebg:", MU_COLOR_TITLEBG },
{ "titletext:", MU_COLOR_TITLETEXT },
{ "panelbg:", MU_COLOR_PANELBG },
{ "button:", MU_COLOR_BUTTON },
{ "buttonhover:", MU_COLOR_BUTTONHOVER },
{ "buttonfocus:", MU_COLOR_BUTTONFOCUS },
{ "base:", MU_COLOR_BASE },
{ "basehover:", MU_COLOR_BASEHOVER },
{ "basefocus:", MU_COLOR_BASEFOCUS },
{ "scrollbase:", MU_COLOR_SCROLLBASE },
{ "scrollthumb:", MU_COLOR_SCROLLTHUMB },
{ NULL }
};
if (mu_begin_window(ctx, "Style Editor", mu_rect(350, 250, 300, 240))) {
int sw = mu_get_current_container(ctx)->body.w * 0.14;
mu_layout_row(ctx, 6, (int[]) { 80, sw, sw, sw, sw, -1 }, 0);
for (int i = 0; colors[i].label; i++) {
mu_label(ctx, colors[i].label);
uint8_slider(ctx, &ctx->style->colors[i].r, 0, 255);
uint8_slider(ctx, &ctx->style->colors[i].g, 0, 255);
uint8_slider(ctx, &ctx->style->colors[i].b, 0, 255);
uint8_slider(ctx, &ctx->style->colors[i].a, 0, 255);
mu_draw_rect(ctx, mu_layout_next(ctx), ctx->style->colors[i]);
}
mu_end_window(ctx);
}
}
sapp_desc sokol_main(int argc, char* argv[]) {
(void)argc; (void)argv;
return (sapp_desc){
.init_cb = init,
.frame_cb = frame,
.cleanup_cb = cleanup,
.event_cb = event,
.width = 720,
.height = 540,
.window_title = "microui+sokol_gl.h",
.icon.sokol_default = true,
.logger.func = slog_func,
};
}
//== micrui renderer ===========================================================
static sg_image atlas_img;
static sg_sampler atlas_smp;
static sgl_pipeline pip;
static void r_init(void) {
// atlas image data is in atlas.inl file, this only contains alpha
// values, need to expand this to RGBA8
uint32_t rgba8_size = ATLAS_WIDTH * ATLAS_HEIGHT * 4;
uint32_t* rgba8_pixels = (uint32_t*) malloc(rgba8_size);
for (int y = 0; y < ATLAS_HEIGHT; y++) {
for (int x = 0; x < ATLAS_WIDTH; x++) {
int index = y*ATLAS_WIDTH + x;
rgba8_pixels[index] = 0x00FFFFFF | ((uint32_t)atlas_texture[index]<<24);
}
}
atlas_img = sg_make_image(&(sg_image_desc){
.width = ATLAS_WIDTH,
.height = ATLAS_HEIGHT,
.data = {
.subimage[0][0] = {
.ptr = rgba8_pixels,
.size = rgba8_size
}
}
});
atlas_smp = sg_make_sampler(&(sg_sampler_desc){
// LINEAR would be better for text quality in HighDPI, but the
// atlas texture is "leaking" from neighbouring pixels unfortunately
.min_filter = SG_FILTER_NEAREST,
.mag_filter = SG_FILTER_NEAREST,
});
pip = sgl_make_pipeline(&(sg_pipeline_desc){
.colors[0].blend = {
.enabled = true,
.src_factor_rgb = SG_BLENDFACTOR_SRC_ALPHA,
.dst_factor_rgb = SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA
}
});
free(rgba8_pixels);
}
static void r_begin(int disp_width, int disp_height) {
sgl_defaults();
sgl_push_pipeline();
sgl_load_pipeline(pip);
sgl_enable_texture();
sgl_texture(atlas_img, atlas_smp);
sgl_matrix_mode_projection();
sgl_push_matrix();
sgl_ortho(0.0f, (float) disp_width, (float) disp_height, 0.0f, -1.0f, +1.0f);
sgl_begin_quads();
}
static void r_end(void) {
sgl_end();
sgl_pop_matrix();
sgl_pop_pipeline();
}
static void r_draw(void) {
sgl_draw();
}
static void r_push_quad(mu_Rect dst, mu_Rect src, mu_Color color) {
float u0 = (float) src.x / (float) ATLAS_WIDTH;
float v0 = (float) src.y / (float) ATLAS_HEIGHT;
float u1 = (float) (src.x + src.w) / (float) ATLAS_WIDTH;
float v1 = (float) (src.y + src.h) / (float) ATLAS_HEIGHT;
float x0 = (float) dst.x;
float y0 = (float) dst.y;
float x1 = (float) (dst.x + dst.w);
float y1 = (float) (dst.y + dst.h);
sgl_c4b(color.r, color.g, color.b, color.a);
sgl_v2f_t2f(x0, y0, u0, v0);
sgl_v2f_t2f(x1, y0, u1, v0);
sgl_v2f_t2f(x1, y1, u1, v1);
sgl_v2f_t2f(x0, y1, u0, v1);
}
static void r_draw_rect(mu_Rect rect, mu_Color color) {
r_push_quad(rect, atlas[ATLAS_WHITE], color);
}
static void r_draw_text(const char* text, mu_Vec2 pos, mu_Color color) {
mu_Rect dst = { pos.x, pos.y, 0, 0 };
for (const char* p = text; *p; p++) {
mu_Rect src = atlas[ATLAS_FONT + (unsigned char)*p];
dst.w = src.w;
dst.h = src.h;
r_push_quad(dst, src, color);
dst.x += dst.w;
}
}
static void r_draw_icon(int id, mu_Rect rect, mu_Color color) {
mu_Rect src = atlas[id];
int x = rect.x + (rect.w - src.w) / 2;
int y = rect.y + (rect.h - src.h) / 2;
r_push_quad(mu_rect(x, y, src.w, src.h), src, color);
}
static int r_get_text_width(const char* text, int len) {
int res = 0;
for (const char* p = text; *p && len--; p++) {
res += atlas[ATLAS_FONT + (unsigned char)*p].w;
}
return res;
}
static int r_get_text_height(void) {
return 18;
}
static void r_set_clip_rect(mu_Rect rect) {
sgl_end();
sgl_scissor_rect(rect.x, rect.y, rect.w, rect.h, true);
sgl_begin_quads();
}