-
Notifications
You must be signed in to change notification settings - Fork 0
/
abcdgui.h
493 lines (382 loc) · 8.45 KB
/
abcdgui.h
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
/*
* Copyright (c) 2021 Alessandro De Santis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#include <string>
#include <functional>
#include "abcddraw.h"
namespace abcd {
class theme
{
std::vector<color> m_colors;
std::string m_font_family;
uint32_t m_font_size;
public:
theme()
{
dark();
}
color bg() {return m_colors[0];}
color back() {return m_colors[1];}
color fore() {return m_colors[2];}
color text() {return m_colors[3];}
const char * font_family() {return m_font_family.c_str();}
uint32_t font_size() {return m_font_size;}
void set_colors(std::vector<color> colors)
{
m_colors = colors;
}
void set_font(const char * family, uint32_t size)
{
m_font_family = family;
m_font_size = size;
}
void dark()
{
m_colors =
{
{51, 63, 85, 255},
{20, 27, 43, 255},
{49, 125, 250, 255},
{225, 225, 225, 255},
};
m_font_family = "Roboto";
m_font_size = 20;
}
};
class grid;
// ---------------------------------------------------------
// HBOX
// ---------------------------------------------------------
class hbox
{
friend class grid;
rect next;
std::vector<int> xpos;
int x1;
public:
void create(rect bounds, uint32_t columns)
{
std::vector<uint32_t> weight;
weight.resize(columns);
std::fill(weight.begin(), weight.end(), 1);
create(bounds, weight);
}
void create(rect bounds, const std::vector<uint32_t> weight)
{
xpos.clear();
xpos.reserve(weight.size() + 1);
double sum = 0;
x1 = bounds.x1;
double x = 0;
xpos.push_back(x);
for (auto w = weight.begin(); w != weight.end(); ++w) sum += *w;
for (auto w = weight.begin(); w != weight.end(); ++w)
{
x = x + bounds.width() * *w / sum;
xpos.push_back(int(0.5 + x));
}
next.y1 = bounds.y1;
next.y2 = bounds.y2;
}
void create_fixed(rect bounds, const std::vector<int> extent)
{
xpos.clear();
xpos.reserve(extent.size() + 1);
double x = 0;
x1 = bounds.x1;
xpos.push_back(x);
for (auto e = extent.begin(); e != extent.end(); ++e)
{
x = x + *e;
xpos.push_back(x);
}
next.y1 = bounds.y1;
next.y2 = bounds.y2;
}
rect cell(uint32_t i)
{
next.x1 = x1 + xpos[i];
next.x2 = x1 + xpos[i + 1];
return next;
}
};
// ---------------------------------------------------------
// VBOX
// ---------------------------------------------------------
class vbox
{
friend class grid;
rect next;
std::vector<int> ypos;
int y1;
public:
void create(rect bounds, uint32_t rows)
{
std::vector<uint32_t> weight;
weight.resize(rows);
std::fill(weight.begin(), weight.end(), 1);
create(bounds, weight);
}
void create(rect bounds, const std::vector<uint32_t> weight)
{
ypos.clear();
ypos.reserve(weight.size() + 1);
double sum = 0;
double y = 0;
y1 = bounds.y1;
ypos.push_back(y);
for (auto w = weight.begin(); w != weight.end(); ++w) sum += *w;
for (auto w = weight.begin(); w != weight.end(); ++w)
{
y = y + bounds.height() * *w / sum;
ypos.push_back(int(0.5 + y));
}
next.x1 = bounds.x1;
next.x2 = bounds.x2;
}
void create_fixed(rect bounds, const std::vector<int> extent)
{
ypos.clear();
ypos.reserve(extent.size() + 1);
double y = 0;
y1 = bounds.y1;
ypos.push_back(y);
for (auto e = extent.begin(); e != extent.end(); ++e)
{
y = y + *e;
ypos.push_back(y);
}
next.y1 = bounds.y1;
next.y2 = bounds.y2;
}
rect cell(uint32_t i)
{
next.y1 = y1 + ypos[i];
next.y2 = y1 + ypos[i + 1];
return next;
}
};
// ---------------------------------------------------------
// GRID
// ---------------------------------------------------------
class grid
{
hbox hb;
vbox vb;
public:
void create(rect bounds, int rows, int columns)
{
hb.create(bounds, columns);
vb.create(bounds, rows);
}
void create(rect bounds,
const std::vector<uint32_t> rweight, const std::vector<uint32_t> cweight)
{
hb.create(bounds, cweight);
vb.create(bounds, rweight);
}
void create_fixed(rect bounds,
const std::vector<int> rextent, const std::vector<int> cextent)
{
hb.create_fixed(bounds, cextent);
vb.create_fixed(bounds, rextent);
}
rect cell(uint32_t r, uint32_t c)
{
rect next;
next.x1 = hb.x1 + hb.xpos[c];
next.x2 = hb.x1 + hb.xpos[c + 1];
next.y1 = vb.y1 + vb.ypos[r];
next.y2 = vb.y1 + vb.ypos[r + 1];
return next;
}
};
// ---------------------------------------------------------
// GRID
// ---------------------------------------------------------
struct span
{
int padding1;
int length;
int padding2;
int size() {return padding1 + length + padding2;}
};
// ---------------------------------------------------------
// GRID
// ---------------------------------------------------------
class guide
{
int pos {0};
public:
guide(int position = 0)
{
pos = position;
}
int position()
{
return pos;
}
void move(int position)
{
pos = position;
}
void shift(int delta)
{
pos = pos + delta;
}
void left(rect &r)
{
auto w = r.width();
r.x1 = pos;
r.x2 = pos + w;
}
void right(rect &r)
{
auto w = r.width();
r.x1 = pos - w;
r.x2 = pos;
}
void xcenter(rect &r)
{
auto w = r.width();
r.x1 = pos - w / 2;
r.x2 = r.x1 + w;
}
void top(rect &r)
{
auto h = r.height();
r.y1 = pos;
r.y2 = pos + h;
}
void bottom(rect &r)
{
auto h = r.height();
r.y1 = pos - h;
r.y2 = pos;
}
void ycenter(rect &r)
{
auto h = r.height();
r.y1 = pos - h / 2;
r.y2 = r.y1 + h;
}
};
struct widget
{
std::string name;
};
struct window
{
theme m_theme;
widget background;
Draw *draw;
bool mouse_down {false};
uint32_t mouse_button {0};
int mouse_x {-1};
int mouse_y {-1};
widget *mouse_widget {nullptr};
widget *focus_widget {nullptr};
bool key_down {false};
std::string key_utf8;
window();
void begin(Draw *draw);
void end();
void begin_widget(rect &r);
void end_widget();
};
struct slider_widget : public widget
{
int delta;
};
struct knob_widget : public widget
{
float x1, y1, angle {0};
};
struct list_widget : public widget
{
int yref;
float yvalue {0};
bool scrolling {false};
};
struct panel_widget : public widget
{
rect r;
};
void move(rect &r, int x, int y);
bool contains(const rect &r, point pt);
void inflate(rect &r, int dx, int dy);
rect split(rect &r, int side, int size);
/**
* creates and returns a scaled and aligned version of r
*/
rect adjust(rect &r, double sx, double sy, int xa = 0, int ya = 0);
/**
* creates and returns a fixed size rectangle aligned with r
*/
rect adjust(rect &r, int w, int h, int xa = 0, int ya = 0);
/**
*
*/
rect pad(rect r, span h, span v);
////////////////////////////////////////////////////////////////
// WIDGETS
/**
*
*/
void label(window *, widget *, abcd::rect, std::string text, int xa = 0, int ya = 0);
/**
*
*/
bool button(window *win, widget *id, abcd::rect r, std::string text);
/**
*
*/
bool checkbutton(window *win, widget *id, abcd::rect r, bool *value);
/**
*
*/
bool radiobutton(window *win, widget *id, abcd::rect r, int index, int *value);
/**
*
*/
bool slider(window *win, slider_widget *id, abcd::rect r, int thumbsize, float *value, bool horz);
/**
*
*/
bool knob(window *win, knob_widget *id, abcd::rect r, float *value);
/**
*
*/
bool input(window *win, widget *id, abcd::rect r, std::string& value);
/**
*
*/
bool list(window *win, list_widget *id, abcd::rect r, const std::vector<std::string> &items, int &value);
/**
*
*/
abcd::rect begin_panel(window *win, panel_widget *id, abcd::rect);
/**
*
*/
abcd::rect end_panel(window *win, panel_widget *id);
} // abcd