aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_ui.c
blob: af3e78f94808da0f4ae9ef18876c8df280f2a35e (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
#include "teensy_ui.h"

#include <assert.h>
 
#include "teensy_list.h"

#define TITLE_BAR_HEIGHT (8)
#define TEXT_HEIGHT (8)

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

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

typedef struct {
    const char *title;
    ty_Recti rect;
    ty_Image content;
    uint32_t flags;

    ty_Vec2i next_pos;
} Window;

typedef struct {
    Window *active;
    Window root;
    Window windows[255];
    int window_count;
    tyui_Style style;
    const ty_Font *font;
    Cmd *cmds;

    ty_Vec2i prev_mouse_pos;
} Ctx;

static
tyui_Style default_style = {
    .fg_normal = ty_color(200, 200, 200),
    .bg_normal = ty_color(64, 64, 64),
    .fg_hover  = ty_color(255, 255, 255),
    .bg_hover  = ty_color(100, 100, 100),
    .win_bg    = ty_color(48, 48, 48),
    .win_title = ty_color(64, 128, 128),
};

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(uictx.cmds, cmd);
}

static
void text_cmd(const char *text, ty_Vec2i pos)
{
    Cmd cmd = {};
    cmd.kind = CMD_TEXT;
    cmd.text = text;
    cmd.rect.x = pos.x;
    cmd.rect.y = pos.y;

    ty_list_append(uictx.cmds, cmd);
}

static
void image_cmd(const ty_Image *img, ty_Vec2i pos)
{
    Cmd cmd = {};
    cmd.kind = CMD_IMAGE;
    cmd.img = img;
    cmd.rect.x = pos.x;
    cmd.rect.y = pos.y;
    ty_list_append(uictx.cmds, cmd);
}

static
void target_cmd(const ty_Image *target)
{
    Cmd cmd = {};
    cmd.kind = CMD_TARGET;
    cmd.img = target;
    ty_list_append(uictx.cmds, cmd);
}

static
ty_Vec2i next_pos(int height)
{
    ty_Vec2i next = uictx.active->next_pos;
    uictx.active->next_pos.y += height;
    return next;
}

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
Window create_window(const char *title, ty_Recti rect, uint32_t flags)
{
    Window win = {};
    win.title = title;
    win.rect = rect;
    win.flags = flags;
    win.content = ty_create_image(rect.w, rect.h, NULL);
    return win;
}

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

    uictx.cmds = ty_list_create();
    ty_list_reserve(uictx.cmds, 256);

    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)
{
    ty_list_free(uictx.cmds);

    ty_free_image(uictx.root.content);
    for (int i = 0; i < uictx.window_count; i++) {
        ty_free_image(uictx.windows[i].content);
    }
}

void tyui_draw(void)
{
    for (size_t i = 0; i < ty_list_len(uictx.cmds); i++) {
        Cmd cmd = uictx.cmds[i];
        switch (cmd.kind) {
        case CMD_RECT:
            ty_draw_rect(cmd.rect, cmd.color);
            break;
        case CMD_TEXT:
            ty_draw_text(uictx.font, ty_recti_start(cmd.rect), cmd.text);
            break;
        case CMD_IMAGE:
            ty_draw_image(*cmd.img, ty_recti_start(cmd.rect));
            break;
        case CMD_TARGET:
            ty_draw_set_target(cmd.img);
            if (cmd.img != NULL) ty_draw_clear(TY_COLOR_MAGENTA);
            break;
        }
    }
    ty_list_clear(uictx.cmds);

    uictx.prev_mouse_pos = ty_mouse_pos();
}

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 (uictx.active != &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);
    }

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

    win->next_pos = ty_vec2i(0, 0);

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

    ty_Recti title_rect = win->rect;
    title_rect.h = TEXT_HEIGHT;

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

    if (ty_button_down(TY_BTN_DB_LMB) && 
        ty_pointi_in_recti(mouse_pos, title_rect)
    ) {
        ty_Vec2i md = mouse_delta();
        win->rect.x += md.x;
        win->rect.y += md.y;

        title_rect = win->rect;
        title_rect.h = TEXT_HEIGHT;
    }

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

    ty_Recti body_rect = win->rect;
    body_rect.y += TEXT_HEIGHT;
    rect_cmd(body_rect, uictx.style.win_bg);

    target_cmd(&win->content);

done:
    return !closed;
}

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

    target_cmd(NULL);
    ty_Vec2i content_pos = ty_recti_start(uictx.active->rect);
    content_pos.y += TITLE_BAR_HEIGHT;
    image_cmd(&uictx.active->content, content_pos);

    uictx.active = &uictx.root;
}

void tyui_text(const char *text)
{
    text_cmd(text, next_pos(TEXT_HEIGHT));
}