aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_ui.c
blob: a0bb5e38781f6b996feba681f89365062c0a9397 (plain)
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include "teensy_ui.h"

#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>
 
#include "teensy_list.h"

#define TEXT_HEIGHT (8)
#define MAX_LAYOUTS (16)
#define MAX_WINDOWS (255)

#define window() (uictx.active)
#define layout() (window()->layout - 1)
#define push_layout() (window()->layout++)
#define pop_layout() (--window()->layout)
#define padding() (uictx.style.padding)
#define control_padding() (uictx.style.control_padding)
#define title_bar_height() (uictx.style.title_bar_height)

enum {
    CMD_RECT,
    CMD_TEXT,
    CMD_CLIP,
    CMD_IMAGE,
};
typedef uint8_t Cmd_Kind;

typedef struct {
    const char *text;
    ty_Recti rect;
    ty_Color color;
    ty_Image img;
    Cmd_Kind kind;
    tyui_Align align;
} Cmd;

typedef struct {
    int idx;
    int columns;
    float column_widths[TYUI_MAX_LAYOUT_SIZE];
    int max_height;
    int total_height;
    int width;
} Layout;

typedef struct {
    const char *title;
    Cmd *cmds;
    Layout *layout;
    Layout layout_stack[MAX_LAYOUTS];
    ty_Recti rect;
    uint32_t flags;
    bool focused;
    bool has_lmb;
    bool has_rmb;
} Window;

typedef struct {
    Window *active;
    Window root;
    Window windows[MAX_WINDOWS];
    int window_order[MAX_WINDOWS];
    int window_count;
    int layout_idx;
    int layout_size;
    tyui_Style style;
    const ty_Font *font;
    const float *layout;

    ty_Vec2i prev_mouse_pos;
} Ctx;

static
tyui_Style default_style = {
    .fg_normal        = ty_color(200, 200, 200),
    .bg_normal        = ty_color(20, 20, 20),
    .fg_hover         = ty_color(255, 255, 255),
    .bg_hover         = ty_color(100, 100, 100),
    .fg_pressed       = ty_color(150, 150, 150),
    .bg_pressed       = ty_color(10, 10, 10),
    .win_bg           = ty_color(48, 48, 48),
    .win_border       = ty_color(24, 24, 24),
    .win_title        = ty_color(64, 128, 128),
    .frame            = ty_color(10, 10, 10),
    .title_bar_height = 12,
    .padding          = 1,
    .control_padding  = 2,
    .frame_size       = 1,
};

static
Ctx uictx;

static
void rect_cmd(ty_Recti rect, ty_Color color)
{
    Cmd cmd = {};
    cmd.kind = CMD_RECT;
    cmd.rect = rect;
    cmd.color = color;

    ty_list_append(window()->cmds, cmd);
}

static
void text_cmd(const char *text, ty_Recti rect, tyui_Align align)
{
    Cmd cmd = {};
    cmd.kind = CMD_TEXT;
    cmd.text = text;
    cmd.rect = rect;
    cmd.align = align;

    ty_list_append(window()->cmds, cmd);
}

static
void image_cmd(ty_Image img, ty_Recti rect)
{
    Cmd cmd = {};
    cmd.kind = CMD_IMAGE;
    cmd.img = img;
    cmd.rect = rect;
    ty_list_append(window()->cmds, cmd);
}

static
void clip_cmd(ty_Recti rect)
{
    Cmd cmd = {};
    cmd.kind = CMD_CLIP;
    if (rect.w < 0 || rect.h < 0)
        rect = window()->rect; // This is a full screen clip

    cmd.rect = ty_recti_clamp(rect, window()->rect);
    ty_list_append(window()->cmds, cmd);
}

static
void draw_frame(ty_Recti rect, ty_Color bg_col)
{
    clip_cmd(TY_CLIP_NONE);
    rect_cmd(rect, uictx.style.frame);
    ty_Recti content_rect = ty_recti_shrink(rect, uictx.style.frame_size);
    rect_cmd(content_rect, bg_col);
    clip_cmd(content_rect);
}

static
void advance_layout(void)
{
    Layout *l = layout();

    if (++l->idx < l->columns)
        return;

    l->total_height += l->max_height + padding();
    l->idx = 0;
    l->max_height = 0;
}

static
int get_column_width(const Layout *l, int x, float width)
{
    if (width < 0)
        return (l->width - padding() * 2) - width - x;
    if (width < 1)
        return (l->width - padding() * 2) * width;
    return width;
}

static
int get_column_x(const Layout *l)
{
    int x = 0;
    for (int i = 0; i < l->idx; i++)
        x += get_column_width(l, x, l->column_widths[i]) + padding();
    return x;
}

static
ty_Recti next_rect(int height)
{
    Layout *l = layout();

    l->max_height = ty_max(l->max_height, height);

    int x = get_column_x(l) + padding();
    float width = get_column_width(l, x, l->column_widths[l->idx]);

    int y = 0;
    for (Layout *outer = window()->layout_stack; outer <= l; outer++) {
        x += get_column_x(outer);
        y += outer->total_height;
    }

    x += window()->rect.x;
    y += window()->rect.y;

    advance_layout();

    return ty_recti(x, y, width, height);
}

static
ty_Vec2i mouse_delta(void)
{
    ty_Vec2i cur = ty_mouse_pos();
    return ty_vec2i(
        cur.x - uictx.prev_mouse_pos.x,
        cur.y - uictx.prev_mouse_pos.y
    );
}

static
bool is_hovered(ty_Recti rect)
{
    return ty_pointi_in_recti(ty_mouse_pos(), rect);
}

static
Window create_window(const char *title, ty_Recti rect, uint32_t flags)
{
    Window win = {};
    win.title = title;
    win.rect = rect;
    win.flags = flags;
    win.cmds = ty_list_create();
    ty_list_reserve(win.cmds, 64);
    return win;
}

void tyui_init(const ty_Font *font)
{
    assert(font);

    uint32_t root_flags = 0;
    root_flags |= TYUI_WIN_NORESIZE;
    root_flags |= TYUI_WIN_NOCLOSE;
    root_flags |= TYUI_WIN_NOMOVE;
    root_flags |= TYUI_WIN_INVISIBLE;

    uictx.root = create_window(
        "__ROOT__",
        ty_recti(0, 0, 256, 256),
        root_flags
    );

    uictx.style = default_style;
    uictx.font = font;

    uictx.active = &uictx.root;
}

void tyui_deinit(void)
{
    for (int i = 0; i < uictx.window_count; i++)
        ty_list_free(uictx.windows[i].cmds);
}

void tyui_push_layout(const float *column_widths)
{
    assert(column_widths);

    Layout *outer = layout();

    Layout *new = push_layout();
    memset(new, 0, sizeof(*new));

    for (const float *width = column_widths; *width != 0; width++)
        new->columns++;

    memcpy(
        new->column_widths,
        column_widths,
        sizeof(*column_widths) * new->columns
    );

    new->total_height = 0;

    if (new != window()->layout_stack) {
        int x = get_column_x(outer);
        new->width =
            get_column_width(outer, x, outer->column_widths[outer->idx]);
    } else {
        new->width = window()->rect.w;
    }
}

void tyui_pop_layout(void)
{
    Layout *old = pop_layout();
    Layout *top = layout();
    top->max_height = ty_max(top->max_height, old->total_height - padding());
    advance_layout();
}

bool tyui_begin_window_ex(
    const char *title,
    ty_Recti rect,
    tyui_Id *id,
    uint32_t flags,
    bool *closed_ptr
) {
    assert(id);

    bool closed = false;

    if (closed_ptr)
        closed = *closed_ptr;

    if (window() != &uictx.root) {
        ty_log_err("cannot create window within another window");
        return false;
    }

    if (closed)
        return !closed;

    if (*id == 0) {
        *id = ++uictx.window_count;
        uictx.windows[*id - 1] = create_window(title, rect, flags);
        uictx.window_order[uictx.window_count - 1] = *id;
    }

    Window *win = &uictx.windows[*id - 1];
    uictx.active = win;
    win->layout = win->layout_stack;

    if (win->flags & TYUI_WIN_INVISIBLE)
        goto done;

    ty_Vec2i mouse_pos = ty_mouse_pos();
    ty_Vec2i md = mouse_delta();
    mouse_pos.x -= md.x;
    mouse_pos.y -= md.y;

    bool hovering_resizer = false;

    // Grabbing resizer?
    {
        ty_Vec2i win_end = ty_recti_end(win->rect);
        ty_Recti resize_rect = ty_recti(
            win_end.x - 20, win_end.y - 20 + TEXT_HEIGHT,
            20, 20
        );

        hovering_resizer =
            ty_pointi_in_recti(mouse_pos, resize_rect) ||
            ty_pointi_in_recti(ty_mouse_pos(), resize_rect);

        if (win->has_lmb && hovering_resizer) {
            win->rect.w += md.x;
            win->rect.h += md.y;
        }
    }

    ty_Recti title_rect = win->rect;
    title_rect.h = title_bar_height();

    // Grabbing title bar?
    {
        if (
            win->has_lmb && 
            (ty_pointi_in_recti(mouse_pos, title_rect) ||
            ty_pointi_in_recti(ty_mouse_pos(), title_rect))
        ) {
            win->rect.x += md.x;
            win->rect.y += md.y;

            title_rect = win->rect;
            title_rect.h = title_bar_height();
        }
    }

    clip_cmd(TY_CLIP_NONE);

    rect_cmd(title_rect, uictx.style.win_title);
    text_cmd(win->title, title_rect, TYUI_ALIGN_LEFT);

    ty_Recti body_rect = win->rect;
    body_rect.y += title_bar_height();
    body_rect.h -= title_bar_height();
    rect_cmd(body_rect, uictx.style.win_border);
    rect_cmd(ty_recti_shrink(body_rect, 1), uictx.style.win_bg);

    if (hovering_resizer) {
        ty_Recti right_bar = ty_recti(
            win->rect.x + win->rect.w - 1, 0,
            1, win->rect.y + win->rect.h
        );
        rect_cmd(right_bar, uictx.style.win_title);
        ty_Recti bot_bar = ty_recti(
            0, win->rect.y + win->rect.h - 1,
            win->rect.x + win->rect.w, 1
        );
        rect_cmd(bot_bar, uictx.style.win_title);
    }

    tyui_push_layout((float[]){-1, 0});
    layout()->total_height = title_bar_height() + padding();

done:
    return !closed;
}

void tyui_end_window(void)
{
    if (window() == &uictx.root) {
        ty_log_err("ending window with no window open");
        return;
    }

    clip_cmd(TY_CLIP_NONE);

    uictx.active = &uictx.root;
}

void tyui_text_ex(tyui_Align align, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    char *text = ty_format_args(fmt, args);
    va_end(args);

    ty_Recti rect = next_rect(TEXT_HEIGHT);

    clip_cmd(rect);
    text_cmd(text, rect, align);
}

bool tyui_button(const char *text)
{
    ty_Recti rect = next_rect(TEXT_HEIGHT + control_padding() * 2);
    ty_Color bg_col = uictx.style.bg_normal;

    bool clicked = false;
    if (is_hovered(rect)) {
        clicked = window()->has_lmb;
        bg_col = ty_button_down(TY_BTN_DB_LMB)
            ? uictx.style.bg_pressed
            : uictx.style.bg_hover;
    }

    draw_frame(rect, bg_col);
    text_cmd(text, ty_recti_shrink(rect, control_padding()), TYUI_ALIGN_CENTER);

    return clicked;
}

static
void draw_window(Window *win)
{
    for (size_t i = 0; i < ty_list_len(win->cmds); i++) {
        Cmd cmd = win->cmds[i];
        switch (cmd.kind) {
        case CMD_RECT:
            ty_draw_rect(cmd.rect, cmd.color);
            break;
        case CMD_TEXT: {
            int text_width = ty_font_width(uictx.font, cmd.text);
            ty_Vec2i pos = ty_recti_start(cmd.rect);
            switch (cmd.align) {
            case TYUI_ALIGN_LEFT:
                break;
            case TYUI_ALIGN_RIGHT:
                pos.x += cmd.rect.w - text_width;
                break;
            case TYUI_ALIGN_CENTER:
                pos.x += (cmd.rect.w - text_width) / 2;
                break;
            }
            pos.y += (cmd.rect.h - ty_font_height(uictx.font)) / 2;
            ty_draw_text(uictx.font, pos, cmd.text);
            break;
        }
        case CMD_IMAGE:
            ty_draw_image(cmd.img, ty_recti_start(cmd.rect));
            break;
        case CMD_CLIP:
            ty_draw_set_clip(cmd.rect);
            break;
        }
    }
    ty_draw_set_clip(TY_CLIP_NONE);
    ty_list_clear(win->cmds);
}

static
void focus_window(tyui_Id win_id)
{
    int idx = uictx.window_count - 1;
    for (; idx >= 0; idx--) {
        if (uictx.window_order[idx] == win_id)
            break;
    }

    if (idx < 0)
        ty_log_err("uh oh");

    for (int i = idx; i < uictx.window_count - 1; i++)
        uictx.window_order[i] = uictx.window_order[i + 1];
    uictx.window_order[uictx.window_count - 1] = win_id;

    uictx.windows[win_id - 1].focused = true;
}

static
void update_windows(void)
{
    for (int i = 0; i < uictx.window_count; i++) {
        Window *win = &uictx.windows[i];
        win->has_lmb = false;
        win->has_rmb = false;
        win->focused = false;
    }

    for (int i = uictx.window_count - 1; i >= 0; i--) {
        tyui_Id win_id = uictx.window_order[i];
        Window *win = &uictx.windows[win_id - 1];
        if (!is_hovered(win->rect))
            continue;

        win->has_lmb = ty_button_down(TY_BTN_DB_LMB);
        win->has_rmb = ty_button_down(TY_BTN_DB_RMB);

        if (win->has_lmb || win->has_rmb)
            focus_window(win_id);

        break;
    }
}

void tyui_draw(void)
{
    draw_window(&uictx.root);

    for (int i = 0; i < uictx.window_count; i++) {
        tyui_Id win_id = uictx.window_order[i];
        draw_window(&uictx.windows[win_id - 1]);
    }

    uictx.prev_mouse_pos = ty_mouse_pos();
    update_windows();
}