-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.cpp
More file actions
122 lines (104 loc) · 2.48 KB
/
Button.cpp
File metadata and controls
122 lines (104 loc) · 2.48 KB
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
#include "Button.h"
#include "UI.h"
#define BUTTON_RADIUS 3
Button::Button() {
}
void Button::drawButton() {
int x1, y1, w, h, hw, hh;
x1 = x;
y1 = y;
w = width;
h = height;
hw = w / 2;
hh = h / 2;
if (!visible) {
gfx->fillRect(x1 + 1, y1 + 1, w - 2, h - 2, BLACK); // clear previous drawing
return;
}
// draw frame
int frameColor = WHITE;
if (active) {
frameColor = GREEN;
}
if (id != UP_BUTTON && id != DOWN_BUTTON) {
gfx->drawRoundRect(x1, y1, w, h, BUTTON_RADIUS, frameColor);
}
if (id == REC_BUTTON) {
gfx->fillCircle(x1 + hw, y1 + hh, 12, RED);
return;
}
else if (id == REC_BUTTON_ACTIVE) {
gfx->fillCircle(x1 + hw, y1 + hh, 12, YELLOW);
return;
}
else if (id == PAUSE_BUTTON) {
gfx->fillRect(x1 + hw - 12, y1 + 7, 10, h - 16, GREEN);
gfx->fillRect(x1 + hw + 2, y1 + 7, 10, h - 16, GREEN);
return;
}
else if (id == PLAY_BUTTON) {
gfx->fillTriangle(x1 + 30, y1 + 7, x1 + 30, y1 + h - 7, x1 + w - 30, y1 + hh, GREEN);
return;
}
else if (id == DOWN_BUTTON) {
gfx->setCursor(x1, gfx->height() / 2 + 5);
gfx->print("-");
return;
}
else if (id == UP_BUTTON) {
gfx->setCursor(gfx->width() - 20, gfx->height() / 2 + 5);
gfx->print("+");
return;
}
String label;
if (id == MANUAL_BUTTON) {
label = "m";
}
else if (id == TIMER1_BUTTON) {
label = "1";
}
else if (id == TIMER2_BUTTON) {
label = "2";
}
gfx->setTextColor(frameColor);
gfx->setCursor(x1 + 35, y1 + 35);
gfx->print(label);
gfx->setTextColor(WHITE);
if (!active) {
frameColor = BLACK;
}
// emphasise frame
gfx->drawRoundRect(x1 + 1, y1 + 1, w - 2, h - 2, BUTTON_RADIUS, frameColor);
gfx->drawRoundRect(x1 + 2, y1 + 2, w - 4, h - 4, BUTTON_RADIUS, frameColor);
}
void Button::animateButtonClick() {
if (!visible) {
return;
}
int x1, y1, w, h;
// preserve button frame
x1 = x + 1;
y1 = y + 1;
w = width - 2;
h = height - 2;
gfx->fillRect(x1, y1, w, h, GRAY);
delay(150);
gfx->fillRect(x1, y1, w, h, BLACK);
drawButton();
}
void Button::showButtonAndDraw(bool isVisible) {
visible = isVisible;
drawButton();
}
bool Button::hit(uint16_t xpos, uint16_t ypos) {
bool hitTest= visible && enabled && (xpos > x) && (xpos < (x + width)) && (ypos > y) && (ypos < (y + height));
if (hitTest) {
animateButtonClick();
callback();
}
return hitTest;
}
void Button::activateButtonAndDraw(bool activate) {
active = activate;
drawButton();
}