-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.h
574 lines (494 loc) · 20.5 KB
/
Screen.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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
#pragma once
#ifdef ARDUINO
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#define ERRF(...) { /*Serial.print(__VA_ARGS__);*/ while(true); }
#else
#include <unistd.h>
#include <string>
#include <exception>
#include <chrono> // for std::chrono::milliseconds
#include <thread> // for std::this_thread::sleep_for
#include <iostream>
#include <cassert>
#include <SDL.h>
#include <SDL_ttf.h>
// using namespace std;
#define String std::string
#define ERRF(...) {printf(__VA_ARGS__); printf("\n"); throw exception(); }
class _Serial {
public:
void print(String s) {
printf("%s", s.c_str());
}
void println(String s) {
printf("%s\n", s.c_str());
}
void println() {
printf("\n");
}
} Serial;
#endif
// ---------------- COLORS -----------------------
//-------------------------------------
// EGA colors:
//-------------------------------------
//Color Name RGB Binary Decimal RGB565
// 0 black #000000 000000 0 0x0000
// 1 blue #0000AA 000001 1 0x0014
// 2 green #00AA00 000010 2 0x0540
// 3 cyan #00AAAA 000011 3 0x0554
// 4 red #AA0000 000100 4 0xA000
// 5 magenta #AA00AA 000101 5cd 0xA014
// 6 yellow / brown #AA5500 010100 20 0xA2A0
// 7 white / light gray #AAAAAA 000111 7 0xA554
// 8 dark gray / bright black #555555 111000 56 0x52AA
// 9 bright blue #5555FF 111001 57 0x52BF
// 10 bright green #55FF55 111010 58 0x57EA
// 11 bright cyan #55FFFF 111011 59 0x57FF
// 12 bright red #FF5555 111100 60 0xFAAA
// 13 bright magenta #FF55FF 111101 61 0xFABF
// 14 bright yellow #FFFF55 111110 62 0xFFEA
// 15 bright white #FFFFFF 111111 63 0xFFFF
#ifdef ARDUINO
#include "Arduino/COLOR.h"
#else
#include "SDL/COLOR.h"
#endif
enum EGA_Color: uint8_t {
EGA_BLACK = 0,
EGA_BLUE,
EGA_GREEN,
EGA_CYAN,
EGA_RED,
EGA_MAGENTA,
EGA_YELLOW,
EGA_GRAY,
EGA_DARK_GRAY,
EGA_BRIGHT_BLUE,
EGA_BRIGHT_GREEN,
EGA_BRIGHT_CYAN,
EGA_BRIGHT_RED,
EGA_BRIGHT_MAGENTA,
EGA_BRIGHT_YELLOW,
EGA_WHITE,
};
// ---------------------- TouchPad EVENTS -----------------------
#define EVENT_IDLE 0
#define EVENT_TOUCH 1
#define EVENT_MOVE 2
#define EVENT_RELEASE 3
// ------------------- SDL GRAPHICS ----------------------
#ifdef ARDUINO
#else
#include "SDL/SDLGraphics.h"
#endif
// ----------------- GFX/Touch -----------------------
#ifdef ARDUINO
#include "Arduino/Graphics.h"
#else
#include "SDL/Graphics.h"
#endif
#ifdef ARDUINO
#include "Arduino/Touch.h"
#else
#include "SDL/Touch.h"
#endif
// -------------------------------- SCREEN -------------------------------
enum Align {
ALIGN_TOP_LEFT = 0,
ALIGN_TOP_RIGHT,
ALIGN_TOP_CENTER,
ALIGN_BOTTOM_LEFT,
ALIGN_BOTTOM_RIGHT,
ALIGN_BOTTOM_CENTER,
ALIGN_CENTER_LEFT,
ALIGN_CENTER_RIGHT,
ALIGN_CENTER_CENTER,
};
#define ELEM_PROPS(color, fcolor, align, fsize) ((uint16_t)(color) | ((uint16_t)(fcolor) << 4) | ((uint16_t)(align) << 8) | ((uint16_t)(fsize) << 12))
#define ELEM_SET_COLOR(props, color) ((props) = ((props) & 0xFFF0U) | ((uint16_t)(color)))
#define ELEM_SET_FCOLOR(props, fcolor) ((props) = ((props) & 0xFF0FU) | ((uint16_t)(fcolor) << 4))
#define ELEM_SET_ALIGN(props, align) ((props) = ((props) & 0xF0FFU) | ((uint16_t)(align) << 8))
#define ELEM_SET_FSIZE(props, fsize) ((props) = ((props) & 0x0FFFU) | ((uint16_t)(fsize) << 12))
#define ELEM_GET_COLOR(props) ((uint16_t)((props) & 0x000FU))
#define ELEM_GET_FCOLOR(props) ((uint16_t)(((props) & 0x00F0U) >> 4))
#define ELEM_GET_ALIGN(props) ((uint16_t)(((props) & 0x0F00U) >> 8))
#define ELEM_GET_FSIZE(props) ((uint16_t)(((props) & 0xF000U) >> 12))
#define ELEM_STATE_PUSHED (uint8_t)0b00000001 // 1
#define ELEM_STATE_DISABLED (uint8_t)0b00000010 // 2
#define ELEM_STATE_HIDDEN (uint8_t)0b00000100 // 4
#define ELEM_STATE_STICKY (uint8_t)0b00001000 // 8
#define ELEM_STATE_STUCK (uint8_t)0b00010000 // 16
#define ELEM_STATE_BORDER (uint8_t)0b00100000 // 32
#define ELEM_STATE_VSLIDER (uint8_t)0b01000000 // 64
#define ELEM_STATE_HSLIDER (uint8_t)0b10000000 // 128
#define ELEM_SET_PUSH(state) ((state) |= ELEM_STATE_PUSHED)
#define ELEM_SET_RELEASE(state) ((state) &= ~ELEM_STATE_PUSHED)
#define ELEM_SET_DISABLE(state) ((state) |= ELEM_STATE_DISABLED)
#define ELEM_SET_ENABLE(state) ((state) &= ~ELEM_STATE_DISABLED)
#define ELEM_SET_SHOW(state) ((state) &= ~ELEM_STATE_HIDDEN)
#define ELEM_SET_HIDE(state) ((state) |= ELEM_STATE_HIDDEN)
#define ELEM_SET_STICKY(state) ((state) |= ELEM_STATE_STICKY)
#define ELEM_SET_SLIPPERY(state) ((state) &= ~ELEM_STATE_STICKY)
#define ELEM_SET_STUCK(state) ((state) |= ELEM_STATE_STUCK)
#define ELEM_SET_UNSTUCK(state) ((state) &= ~ELEM_STATE_STUCK)
#define ELEM_SET_BORDERED(state) ((state) |= ELEM_STATE_BORDER)
#define ELEM_SET_UNBORDERED(state) ((state) &= ~ELEM_STATE_BORDER)
#define ELEM_SET_VSLIDER(state) ((state) |= ELEM_STATE_VSLIDER)
#define ELEM_SET_UNVSLIDER(state) ((state) &= ~ELEM_STATE_VSLIDER)
#define ELEM_SET_HSLIDER(state) ((state) |= ELEM_STATE_HSLIDER)
#define ELEM_SET_UNHSLIDER(state) ((state) &= ~ELEM_STATE_HSLIDER)
#define IS_ELEM_PUSHED(state) ((state) & ELEM_STATE_PUSHED)
#define IS_ELEM_RELEASED(state) (!((state) & ELEM_STATE_PUSHED))
#define IS_ELEM_DISABLED(state) ((state) & ELEM_STATE_DISABLED)
#define IS_ELEM_ENABLED(state) (!((state) & ELEM_STATE_DISABLED))
#define IS_ELEM_HIDDEN(state) ((state) & ELEM_STATE_HIDDEN)
#define IS_ELEM_VISIBLE(state) (!((state) & ELEM_STATE_HIDDEN))
#define IS_ELEM_STICKY(state) ((state) & ELEM_STATE_STICKY)
#define IS_ELEM_SLIPPERY(state) (!((state) & ELEM_STATE_STICKY))
#define IS_ELEM_STUCK(state) ((state) & ELEM_STATE_STUCK)
#define IS_ELEM_UNSTUCK(state) (!((state) & ELEM_STATE_STUCK))
#define IS_ELEM_BORDERED(state) ((state) & ELEM_STATE_BORDER)
#define IS_ELEM_UNBORDERED(state) (!((state) & ELEM_STATE_BORDER))
#define IS_ELEM_VSLIDER(state) ((state) & ELEM_STATE_VSLIDER)
#define IS_ELEM_UNVSLIDER(state) (!((state) & ELEM_STATE_VSLIDER))
#define IS_ELEM_HSLIDER(state) ((state) & ELEM_STATE_HSLIDER)
#define IS_ELEM_UNHSLIDER(state) (!((state) & ELEM_STATE_HSLIDER))
template <const int SIZE>
class Screen {
private:
bool _refresh = false;
protected:
struct Elem {
uint16_t top = 0, left = 0, slider = 0, width = SCREEN_THEME_ELEM_WIDTH, height = SCREEN_THEME_ELEM_HEIGHT;
String text = "";
uint16_t props = ELEM_PROPS(SCREEN_THEME_ELEM_COLOR, SCREEN_THEME_FONT_COLOR, SCREEN_THEME_TEXT_ALIGN, SCREEN_THEME_FONT_SIZE);
uint8_t state = 0;
typedef void (*Event)(int x, int y);
Event touch = NULL, release = NULL, move = NULL;
bool operator==(const Elem& other) {
return
text == other.text &&
top == other.top &&
left == other.left &&
width == other.width &&
height == other.height &&
props == other.props &&
state == other.state &&
slider == other.slider;
}
bool operator!=(const Elem& other) {
return !(*this == other);
}
void getProps(Color& color, Color& fcolor, int& align, int& fsize) {
uint16_t index1 = props & 0x0F;
uint16_t index2 = (props >> 4) & 0x0F;
align = (props >> 8) & 0x0F;
fsize = (props >> 12) & 0x0F;
// state (pushed/disabled) can override the actual colors
if (IS_ELEM_PUSHED(state)) {
index1 = SCREEN_THEME_PUSHED_ELEM_COLOR;
index2 = SCREEN_THEME_PUSHED_FONT_COLOR;
}
if (IS_ELEM_DISABLED(state)) {
index1 = SCREEN_THEME_DISABLED_ELEM_COLOR;
index2 = SCREEN_THEME_DISABLED_FONT_COLOR;
}
color = COLOR(index1);
fcolor = COLOR(index2);
}
void clearAll(Graphics& graphics, Color color) {
graphics.fillRect(
left - SCREEN_THEME_BORDER_THICKNESS,
top - SCREEN_THEME_BORDER_THICKNESS,
width + SCREEN_THEME_BORDER_THICKNESS,
height + SCREEN_THEME_BORDER_THICKNESS,
color
);
}
void clearSurface(Graphics& graphics, Color color) {
graphics.fillRect(left, top, width, height, color);
}
void clearText(Graphics& graphics) {
Color color, fcolor;
int fsize, align;
getProps(color, fcolor, align, fsize);
drawText(graphics, fsize, align, color);
}
void drawBorder(Graphics& graphics) {
if (IS_ELEM_UNBORDERED(state)) return;
// state (pushed/disabled) can override the actual colors
int topColor = SCREEN_THEME_BORDER_TOP_COLOR, leftColor = SCREEN_THEME_BORDER_LEFT_COLOR, rightColor = SCREEN_THEME_BORDER_RIGHT_COLOR, bottomColor = SCREEN_THEME_BORDER_BOTTOM_COLOR;
if (IS_ELEM_PUSHED(state)) {
topColor = SCREEN_THEME_PUSHED_BORDER_TOP_COLOR;
leftColor = SCREEN_THEME_PUSHED_BORDER_LEFT_COLOR;
rightColor = SCREEN_THEME_PUSHED_BORDER_RIGHT_COLOR;
bottomColor = SCREEN_THEME_PUSHED_BORDER_BOTTOM_COLOR;
}
if (IS_ELEM_DISABLED(state)) {
topColor = SCREEN_THEME_DISABLED_BORDER_TOP_COLOR;
leftColor = SCREEN_THEME_DISABLED_BORDER_LEFT_COLOR;
rightColor = SCREEN_THEME_DISABLED_BORDER_RIGHT_COLOR;
bottomColor = SCREEN_THEME_DISABLED_BORDER_BOTTOM_COLOR;
}
for (int i=1; i<SCREEN_THEME_BORDER_THICKNESS; i++) {
graphics.hline(left - i, top - i, width + i * 2, COLOR(topColor));
graphics.vline(left - i, top - i, height + i * 2, COLOR(leftColor));
graphics.vline(left + width + i, top - i, height + i * 2, COLOR(rightColor));
graphics.hline(left - i, top + height + i, width + i * 2, COLOR(bottomColor));
}
}
void drawText(Graphics& graphics, int fsize, int align, Color fcolor) {
int x = left, y = top;
uint16_t w, h;
graphics.getTextBounds(text.c_str(), fsize, w, h);
switch (align) {
case ALIGN_TOP_RIGHT:
x += width - w; // right
break;
case ALIGN_TOP_CENTER:
x += (width - w) / 2; // hcenter
break;
case ALIGN_BOTTOM_LEFT:
y += height - h;
break;
case ALIGN_BOTTOM_RIGHT:
y += height - h;
x += width - w;
break;
case ALIGN_BOTTOM_CENTER:
y += height - h;
x += (width - w) / 2;
break;
case ALIGN_CENTER_LEFT:
y += (height - h) / 2;
break;
case ALIGN_CENTER_RIGHT:
y += (height - h) / 2;
x += width - w;
break;
case ALIGN_CENTER_CENTER:
y += (height - h) / 2;
x += (width - w) / 2;
break;
}
graphics.text(text.c_str(), x, y, fsize, fcolor);
}
void drawSurface(Graphics& graphics) {
Color color, fcolor;
int fsize, align;
getProps(color, fcolor, align, fsize);
graphics.fillRect(left, top, width+1, height+1, color);
bool elemIsVSlider = IS_ELEM_VSLIDER(state);
bool elemIsHSlider = IS_ELEM_HSLIDER(state);
if (elemIsVSlider || elemIsHSlider) {
int sx, sy, sw, sh;
if (elemIsVSlider) {
sx = left + 1;
sy = top + slider - SCREEN_THEME_SLIDER_THICKNESS;
sw = width -1;
sh = SCREEN_THEME_SLIDER_THICKNESS * 2;
}
if (elemIsHSlider) {
sx = left + slider - SCREEN_THEME_SLIDER_THICKNESS;
sy = top + 1;
sw = SCREEN_THEME_SLIDER_THICKNESS * 2;
sh = height -1;
}
if (sx < left + 1) sx = left + 1;
if (sy < top + 1) sy = top + 1;
if (sx > left + width - SCREEN_THEME_SLIDER_THICKNESS * 2) sx = left + width - SCREEN_THEME_SLIDER_THICKNESS * 2;
if (sy > top + height - SCREEN_THEME_SLIDER_THICKNESS * 2) sy = top + height - SCREEN_THEME_SLIDER_THICKNESS * 2;
graphics.fillRect(sx, sy, sw, sh, COLOR(SCREEN_THEME_SLIDER_COLOR));
}
drawText(graphics, fsize, align, fcolor);
}
void drawAll(Graphics& graphics) {
drawBorder(graphics);
drawSurface(graphics);
}
};
Graphics& graphics;
Touch& touch;
Elem prevs[SIZE];
Elem elems[SIZE];
size_t _size;
Color color;
public:
Screen(Graphics& graphics, Touch& touch, size_t size = SIZE, Color color = COLOR(SCREEN_THEME_COLOR)): graphics(graphics), touch(touch), _size(size), color(color) {
reset(size);
refresh();
}
Screen& reset() {
for (int i=0; i<SIZE; i++) {
elems[i].state = 0;
ELEM_SET_BORDERED(elems[i].state);
elems[i].props = ELEM_PROPS(SCREEN_THEME_ELEM_COLOR, SCREEN_THEME_FONT_COLOR, SCREEN_THEME_TEXT_ALIGN, SCREEN_THEME_FONT_SIZE);
elems[i].top = 0;
elems[i].left = 0;
elems[i].slider = 0;
elems[i].width = SCREEN_THEME_ELEM_WIDTH;
elems[i].height = SCREEN_THEME_ELEM_HEIGHT;
elems[i].text = "";
elems[i].touch = NULL;
elems[i].release = NULL;
elems[i].move = NULL;
}
return *this;
}
Screen& reset(size_t _size) {
reset();
size(_size);
return *this;
}
Screen& size(size_t size) {
_size = size;
return *this;
}
Screen& refresh() {
// if (!_inloop) graphics.fillScreen(color);
_refresh = true;
return *this;
}
Elem& operator[](int i) { return elems[i]; }
void loop() {
Touch::State state = touch.getState();
if (_refresh) {
_refresh = false;
graphics.fillScreen(color);
for (int i=0; i<SIZE; i++) if (IS_ELEM_VISIBLE(elems[i].state)) ELEM_SET_HIDE(prevs[i].state);
}
for (int i=0; i<_size; i++) {
Elem& elem = elems[i];
bool elemIsVisible = IS_ELEM_VISIBLE(elem.state);
bool elemIsSticky = IS_ELEM_STICKY(elem.state);
bool elemIsVSlider = IS_ELEM_VSLIDER(elem.state);
bool elemIsHSlider = IS_ELEM_HSLIDER(elem.state);
// propagate events (if elem covers the event point)
if (
elemIsVisible && IS_ELEM_ENABLED(elem.state) && (
(
elem.top <= state.y && elem.top + elem.height >= state.y &&
elem.left <= state.x && elem.left + elem.width >= state.x
) || (state.event == EVENT_RELEASE && !elemIsSticky)
)
) {
if (state.event != EVENT_IDLE && state.event != EVENT_RELEASE) {
if (elemIsVSlider) elem.slider = state.y-elem.top;
if (elemIsHSlider) elem.slider = state.x-elem.left;
}
switch (state.event) {
case EVENT_TOUCH:
ELEM_SET_PUSH(elem.state);
if (!elemIsSticky) {
if (elem.touch) elem.touch(state.x, state.y);
}
break;
case EVENT_RELEASE:
if (!elemIsSticky) {
ELEM_SET_RELEASE(elem.state);
if (elem.release) elem.release(state.x, state.y);
} else {
if (IS_ELEM_STUCK(elem.state)) {
ELEM_SET_UNSTUCK(elem.state);
if (elem.release) elem.release(state.x, state.y);
ELEM_SET_RELEASE(elem.state);
} else {
ELEM_SET_STUCK(elem.state);
if (elem.touch) elem.touch(state.x, state.y);
}
}
break;
case EVENT_MOVE:
if (elem.move) elem.move(state.x, state.y);
break;
}
}
// refresh elems
Elem& prev = prevs[i];
if (prev == elem) continue;
bool prevIsVisible = IS_ELEM_VISIBLE(prev.state);
// just disapeared in the moment?
if (prevIsVisible && !elemIsVisible) {
prev.clearAll(graphics, color);
prev = elem;
continue;
}
// just appeared in the moment?
if (!prevIsVisible && elemIsVisible) {
elem.drawAll(graphics);
prev = elem;
continue;
}
// go on please, nothing to see here..
if (!elemIsVisible) continue;
// bool elemIsPushed = IS_ELEM_PUSHED(elem.state);
// if (IS_ELEM_PUSHED(prev.state) != elemIsPushed) {
// elem.drawBorder(graphics); // (OR if state changed?)
// if (elemIsPushed) ELEM_SET_PUSH(prev.state); else ELEM_SET_RELEASE(prev.state);
// }
// if position changed? => clearAll and re-drawAll
// if size got smaller? => clearAll and re-drawAll
if (
prev.top != elem.top || prev.left != elem.left ||
prev.width > elem.width || prev.height > elem.height
) {
prev.clearAll(graphics, color);
if (elemIsVisible) elem.drawAll(graphics);
prev = elem;
continue;
}
// if size changed? (but not get smaller - see above) => re-drawAll
if (prev.width != elem.width || prev.height != elem.height) {
if (elemIsVisible) elem.drawAll(graphics);
prev = elem;
continue;
}
// if props or state are changed or slider moved? (color, fcolor, align, fsize)
bool sliderIsMoved = (elemIsVSlider || elemIsHSlider) && elem.slider != prev.slider;
if (prev.props != elem.props || prev.state != elem.state || sliderIsMoved) {
Color pcolor, pfcolor, ecolor, efcolor;
int pfsize, palign, efsize, ealign;
prev.getProps(pcolor, pfcolor, pfsize, palign);
elem.getProps(ecolor, efcolor, efsize, ealign);
// surface color changed? => re-draw surface
if (prev.state != elem.state) {
elem.drawBorder(graphics);
prev = elem;
}
if (pcolor != ecolor || sliderIsMoved) {
if (elemIsVisible) elem.drawSurface(graphics);
prev = elem;
continue;
}
// text align changed? => clear and re-draw text
// text size changed? => clear and re-draw text
if (palign != ealign || pfsize != efsize) {
prev.clearText(graphics);
if (elemIsVisible) elem.drawText(graphics, efsize, ealign, efcolor);
prev = elem;
continue;
}
// font color changed? => re-draw text
if (pfcolor != efcolor) {
if (elemIsVisible) elem.drawText(graphics, efsize, ealign, efcolor);
prev = elem;
continue;
}
}
// if text changed? => clear and re-draw text
if (prev.text != elem.text) {
prev.clearText(graphics);
if (elemIsVisible) {
Color color, fcolor;
int fsize, align;
elem.getProps(color, fcolor, align, fsize);
elem.drawText(graphics, fsize, align, fcolor);
}
prev = elem;
continue;
}
}
}
};