-
Notifications
You must be signed in to change notification settings - Fork 1
/
widgets.cpp
391 lines (343 loc) · 11.8 KB
/
widgets.cpp
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
#include "widgets.hpp"
#include <math.h>
#include <stddef.h>
extern bool g_focus;
#define APP_WIDGET_HEIGHT 20
#define APP_WIDGET_FONT_SIZE 16.0f // use 14.0f for Inter
#define APP_WIDGET_FILL_COLOR nvgRGBA(212, 208, 200, 255)
#define APP_WIDGET_FONT_COLOR (g_focus ? nvgRGBA(0, 0, 5, 255) : nvgRGBA(0x40, 0x40, 0x45, 255))
#define APP_WIDGET_HOT_COLOR nvgRGBA(0, 220, 80, 255)
#define APP_WIDGET_BUTTON_ROUNDING 0
#define APP_WIDGET_BUTTON_SHADOW_OX 0
#define APP_WIDGET_BUTTON_SHADOW_OY 1
#define APP_WIDGET_BUTTON_SHADOW_INSET 0
#define APP_WIDGET_BUTTON_SHADOW_SPREAD 2
#define APP_WIDGET_BUTTON_TEXT_NORMAL APP_WIDGET_FONT_COLOR
#define APP_WIDGET_BUTTON_TEXT_DISABLED nvgRGBA(0x44, 0x44, 0x44, 255)
#define APP_WIDGET_BUTTON_BG_DISABLED nvgRGBA(192, 188, 180, 255)
#define APP_WIDGET_BUTTON_BG_ACTIVE nvgRGBA(232, 218, 210, 255)
#define APP_WIDGET_BUTTON_BG_INACTIVE nvgRGBA(212, 208, 200, 255)
#define APP_WIDGET_BUTTON_BG_HOT nvgRGBA(222, 218, 210, 255)
#define APP_WIDGET_BUTTON_HIGHLIGHT_LOW nvgRGBA(0, 0, 0, 0)
#define APP_WIDGET_BUTTON_HIGHLIGHT_HIGH nvgRGBA(255, 255, 255, 255)
#define APP_WIDGET_BUTTON_SHADOW_COLOR nvgRGBA(0, 0, 0, 0x60)
struct ItemRounding {
float tl;
float tr;
float bl;
float br;
};
static ItemRounding round_item_corners(int item, bool _first, bool _last, float radius) {
ItemRounding r;
r.tl = 0.0f;
r.tr = 0.0f;
r.bl = 0.0f;
r.br = 0.0f;
unsigned box = uiGetBox(item);
if ((_first && _last) || !(box & (UI_ROW | UI_COLUMN))) {
r.tl = radius;
r.tr = radius;
r.bl = radius;
r.br = radius;
}
else if ((box & UI_ROW) == UI_ROW) {
if (_first) {
r.tl = radius;
r.bl = radius;
}
else {
r.tr = radius;
r.br = radius;
}
}
else {
if (_first) {
r.tl = radius;
r.tr = radius;
}
else {
r.bl = radius;
r.br = radius;
}
}
return r;
}
static void auto_rect(NVGcontext *vg, NVGpaint col, float x, float y, float w, float h, ItemRounding *r = NULL, float pad = 0.0f, float round = 0.0f) {
nvgBeginPath(vg);;
if (r) {
nvgRoundedRectVarying(vg, x + pad, y + pad, w - pad * 2, h - pad * 2, r->tl, r->tr, r->br, r->bl);
}
else {
nvgRoundedRect(vg, x + pad, y + pad, w - pad * 2, h - pad * 2, round);
}
nvgFillPaint(vg, col);
nvgFill(vg);
}
static void auto_rect(NVGcontext *vg, NVGcolor col, float x, float y, float w, float h, ItemRounding *r = NULL, float pad = 0.0f, float round = 0.0f) {
nvgBeginPath(vg);
if (r) {
nvgRoundedRectVarying(vg, x + pad, y + pad, w - pad * 2, h - pad * 2, r->tl, r->tr, r->br, r->bl);
}
else {
nvgRoundedRect(vg, x + pad, y + pad, w - pad * 2, h - pad * 2, round);
}
nvgFillColor(vg, col);
nvgFill(vg);
}
struct BoxShading {
NVGcolor bg;
NVGcolor high;
NVGcolor low;
float grad_start;
float grad_end;
};
static void shaded_box(NVGcontext *vg, float x, float y, float w, float h, BoxShading col, ItemRounding *corners){
float r = APP_WIDGET_BUTTON_ROUNDING;
float r2 = r;
float ox = APP_WIDGET_BUTTON_SHADOW_OX;
float oy = APP_WIDGET_BUTTON_SHADOW_OY;
float inset = APP_WIDGET_BUTTON_SHADOW_INSET;
float spread = APP_WIDGET_BUTTON_SHADOW_SPREAD;
auto shadow = nvgBoxGradient(vg,
x + inset + ox, y + inset + oy,
w - inset * 2, h - inset * 2,
r2, spread,
APP_WIDGET_BUTTON_SHADOW_COLOR, nvgRGBA(0, 0, 0, 0)
);
auto highlight = nvgLinearGradient(vg, x, y + col.grad_start, x, y + col.grad_end, col.high, col.low);
auto_rect(vg, shadow, x - spread + ox, y - spread + oy, w + 2 * spread, h + 2 * spread, corners, 0, 0);
auto_rect(vg, col.bg, x, y, w, h, corners, 0, r);
auto_rect(vg, highlight, x, y, w, h, corners, 0, r);
}
// bug: on press, there's a frame where this doesn't register as hot
static void draw_hot(NVGcontext *vg, UIrect rect, int item) {
unsigned char heat = 0;
unsigned state = uiGetState(item);
if (state == UI_HOT || state == UI_BUTTON0_DOWN) {
heat = 100;
}
else if (state == UI_BUTTON0_CAPTURE || state == UI_ACTIVE) {
heat = 150;
}
nvgBeginPath(vg);
nvgRect(vg, 0, 0, rect.w, rect.h);
nvgFillColor(vg, nvgRGBA(255, 255, 255, heat));
nvgFill(vg);
}
static void draw_window(NVGcontext *vg, UIrect rect) {
nvgBeginPath(vg);
float spread = 10.0f;
float ox = 0.0f;
float oy = 2.0f;
float r = 0.0f;
nvgFillPaint(vg, nvgBoxGradient(vg, ox, oy, rect.w, rect.h, r, spread, nvgRGBA(0, 0, 0, 50), nvgRGBA(0, 0, 0, 0)));
nvgRoundedRect(vg, ox-spread, oy-spread, rect.w + spread * 2, rect.h + spread * 2, r);
nvgFill(vg);
nvgBeginPath(vg);
nvgRoundedRect(vg, 0, 0, rect.w, rect.h, r);
nvgFillColor(vg, APP_WIDGET_FILL_COLOR);
nvgFill(vg);
}
static void draw_label(NVGcontext *vg, UIrect rect, ButtonData *data) {
nvgFontSize(vg, APP_WIDGET_FONT_SIZE);
nvgFontFace(vg, data->head.type == WT_HEADING ? "heading" : "regular");
nvgFillColor(vg, APP_WIDGET_FONT_COLOR);
nvgTextAlign(vg, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE);
nvgText(vg, 0, (int)(APP_WIDGET_HEIGHT / 2), data->label, NULL);
}
static void draw_checkbox(NVGcontext *vg, UIrect rect, CheckBoxData *data, int item) {
float pad = 3.0f;
float size = rect.h - 5.0f;
float cx = rect.w - size - pad;
float cy = (rect.h - size) * 0.5f;
auto high = APP_WIDGET_BUTTON_HIGHLIGHT_HIGH;
auto low = APP_WIDGET_BUTTON_HIGHLIGHT_LOW;
if (*data->checked) {
auto tmp = high;
high = low;
low = tmp;
}
BoxShading col;
col.bg = APP_WIDGET_BUTTON_BG_INACTIVE;
col.high = high;
col.low = low;
col.grad_start = -10.0f;
col.grad_end = 25.0f;
shaded_box(vg, cx, cy, size, size, col, NULL);
if (*data->checked) {
float line_width = 3.0f;
float check_size = size - 3.0f;
float check_offset = 0.5f * (size - check_size);
check_size = check_size - line_width;
float cxo = cx + check_offset + line_width * 0.5f;
float cyo = cy + check_offset + line_width * 0.5f;
nvgBeginPath(vg);
nvgStrokeColor(vg, nvgRGB(0, 0xbb, 0x99));
nvgStrokeWidth(vg, line_width);
nvgLineCap(vg, NVG_ROUND);
nvgMoveTo(vg, cxo, cyo);
nvgLineTo(vg, cxo + check_size, cyo + check_size);
nvgMoveTo(vg, cxo, cyo + check_size);
nvgLineTo(vg, cxo + check_size, cyo);
nvgStroke(vg);
}
float bounds[4];
nvgTextBounds(vg, pad, rect.h / 2, data->label, NULL, bounds);
nvgBeginPath(vg);
nvgRect(vg, bounds[2] + 5, rect.h / 2, rect.w - bounds[2] - size - pad - 10, 1);
nvgFillColor(vg, nvgRGB(0xB0, 0xB0, 0xB0));
nvgFill(vg);
nvgFontFace(vg, "regular");
nvgFontSize(vg, APP_WIDGET_FONT_SIZE);
nvgFillColor(vg, APP_WIDGET_FONT_COLOR);
nvgTextAlign(vg, NVG_ALIGN_MIDDLE | NVG_ALIGN_LEFT);
nvgText(vg, pad, rect.h / 2, data->label, NULL);
}
static void draw_checkbox_float(NVGcontext *vg, UIrect rect, CheckBoxFloatData *data, int item) {
float pad = 3.0f;
float size = rect.h - 5.0f;
float cx = rect.w - size - pad;
float cy = (rect.h - size) * 0.5f;
auto high = APP_WIDGET_BUTTON_HIGHLIGHT_HIGH;
auto low = APP_WIDGET_BUTTON_HIGHLIGHT_LOW;
if (*data->checked) {
auto tmp = high;
high = low;
low = tmp;
}
BoxShading col;
col.bg = APP_WIDGET_BUTTON_BG_INACTIVE;
col.high = high;
col.low = low;
col.grad_start = -10.0f;
col.grad_end = 25.0f;
shaded_box(vg, cx, cy, size, size, col, NULL);
if (*data->checked > data->low) {
float line_width = 3.0f;
float check_size = size - 3.0f;
float check_offset = 0.5f * (size - check_size);
check_size = check_size - line_width;
float cxo = cx + check_offset + line_width * 0.5f;
float cyo = cy + check_offset + line_width * 0.5f;
nvgBeginPath(vg);
nvgStrokeColor(vg, nvgRGB(0, 0xbb, 0x99));
nvgStrokeWidth(vg, line_width);
nvgLineCap(vg, NVG_ROUND);
nvgMoveTo(vg, cxo, cyo);
nvgLineTo(vg, cxo + check_size, cyo + check_size);
nvgMoveTo(vg, cxo, cyo + check_size);
nvgLineTo(vg, cxo + check_size, cyo);
nvgStroke(vg);
}
float bounds[4];
nvgTextBounds(vg, pad, rect.h / 2, data->label, NULL, bounds);
nvgBeginPath(vg);
nvgRect(vg, bounds[2] + 5, rect.h / 2, rect.w - bounds[2] - size - pad - 10, 1);
nvgFillColor(vg, nvgRGB(0xB0, 0xB0, 0xB0));
nvgFill(vg);
nvgFontFace(vg, "regular");
nvgFontSize(vg, APP_WIDGET_FONT_SIZE);
nvgFillColor(vg, APP_WIDGET_FONT_COLOR);
nvgTextAlign(vg, NVG_ALIGN_MIDDLE | NVG_ALIGN_LEFT);
nvgText(vg, pad, rect.h / 2, data->label, NULL);
}
static BoxShading hot_colors() {
BoxShading col;
col.high = APP_WIDGET_BUTTON_HIGHLIGHT_HIGH;
col.low = APP_WIDGET_BUTTON_HIGHLIGHT_LOW;
col.bg = APP_WIDGET_BUTTON_BG_HOT;
col.grad_start = -10;
col.grad_end = 25;
return col;
}
static BoxShading active_colors(float h) {
BoxShading col;
col.high = APP_WIDGET_BUTTON_HIGHLIGHT_LOW;
col.low = nvgRGBA(255, 255, 255, 0x30);
col.bg = APP_WIDGET_BUTTON_BG_ACTIVE;
col.grad_start = h - 30;
col.grad_end = h;
return col;
}
static BoxShading disabled_colors() {
BoxShading col;
col.high = nvgRGBA(0xBB, 0xBB, 0xBB, 255);
col.low = APP_WIDGET_BUTTON_HIGHLIGHT_LOW;
col.bg = APP_WIDGET_BUTTON_BG_DISABLED;
col.grad_start = -10;
col.grad_end = 15;
return col;
}
static void draw_button(NVGcontext *vg, UIrect rect, ButtonData *data, int item, bool _first, bool _last) {
BoxShading col;
col.bg = APP_WIDGET_BUTTON_BG_INACTIVE;
col.high = APP_WIDGET_BUTTON_HIGHLIGHT_HIGH;
col.low = APP_WIDGET_BUTTON_HIGHLIGHT_LOW;
col.grad_start = -10;
col.grad_end = 25;
unsigned state = uiGetState(item);
bool disabled = data->head.handler == NULL;
if (disabled) {
col = disabled_colors();
}
else if (state == UI_HOT) {
col = hot_colors();
}
else if (state == UI_BUTTON0_CAPTURE || state == UI_ACTIVE) {
col = active_colors(rect.h);
}
ItemRounding corners = round_item_corners(item, _first, _last, APP_WIDGET_BUTTON_ROUNDING);
shaded_box(vg, 0, 0, rect.w, rect.h, col, &corners);
nvgFontFace(vg, "regular");
nvgFontSize(vg, APP_WIDGET_FONT_SIZE);
nvgFillColor(vg, disabled ? APP_WIDGET_BUTTON_TEXT_DISABLED : APP_WIDGET_BUTTON_TEXT_NORMAL);
nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_MIDDLE);
nvgText(vg, (int)(rect.w * 0.5), (int)(APP_WIDGET_HEIGHT * 0.5), data->label, NULL);
}
static void draw_hbox(NVGcontext *vg, UIrect rect) {
nvgBeginPath(vg);
nvgRect(vg, 0, 0, rect.w, rect.h);
nvgFillColor(vg, nvgRGBAf(0.0f, 1.0f, 1.0f, 0.5f));
nvgFill(vg);
}
static void draw_vbox(NVGcontext *vg, UIrect rect) {
nvgBeginPath(vg);
nvgRect(vg, 0, 0, rect.w, rect.h);
nvgFillColor(vg, nvgRGBAf(1.0f, 1.0f, 0.0f, 0.5f));
nvgFill(vg);
}
static void draw_separator(NVGcontext *vg, UIrect rect) {
float margin = 0.0f;
nvgBeginPath(vg);
nvgRect(vg, margin, floorf(rect.h / 2 - 0.5), rect.w - margin * 2, 1);
nvgFillColor(vg, nvgRGBA(0xa0, 0xa0, 0xa0, 255));
nvgFill(vg);
nvgBeginPath(vg);
nvgRect(vg, margin, ceilf(rect.h / 2), rect.w - margin * 2, 1);
nvgFillColor(vg, nvgRGBA(0xf0, 0xf0, 0xf0, 255));
nvgFill(vg);
}
void draw_widget(NVGcontext *vg, Data *head, int item, bool _first, bool _last) {
// get the widgets absolute rectangle
UIrect rect = uiGetRect(item);
nvgSave(vg);
nvgFontFace(vg, "regular");
nvgFontSize(vg, APP_WIDGET_FONT_SIZE);
nvgTranslate(vg, rect.x, rect.y);
//nvgScissor(vg, 0, 0, rect.w, rect.h);
// this affects anything that responds to mouse
draw_hot(vg, rect, item);
switch (head->type) {
default: break;
case WT_WINDOW: draw_window(vg, rect); break;
case WT_HEADING:
case WT_LABEL: draw_label(vg, rect, (ButtonData *)head); break;
case WT_HBOX: draw_hbox(vg, rect); break;
case WT_VBOX: draw_vbox(vg, rect); break;
case WT_BUTTON: draw_button(vg, rect, (ButtonData *)head, item, _first, _last); break;
case WT_CHECKBOX: draw_checkbox(vg, rect, (CheckBoxData *)head, item); break;
case WT_CHECKBOX_FLOAT: draw_checkbox_float(vg, rect, (CheckBoxFloatData *)head, item); break;
case WT_SEPARATOR: draw_separator(vg, rect); break;
}
nvgRestore(vg);
}