-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathMenuItemInterface.h
More file actions
200 lines (170 loc) · 6.62 KB
/
Copy pathMenuItemInterface.h
File metadata and controls
200 lines (170 loc) · 6.62 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
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
#ifndef __MENU_ITEM_INTERFACE_H__
#define __MENU_ITEM_INTERFACE_H__
#include "core/display.h"
#include <globals.h>
// Pixel span a menu icon covers when drawn at scale 1. Lower it to render the icons
// bigger inside the same box, raise it if the widest ones start touching their label.
#define ICON_SCALE_REFERENCE 80.0f
class MenuItemInterface {
public:
virtual ~MenuItemInterface() = default;
virtual void optionsMenu(void) = 0;
virtual void drawIcon(float scale = 1) = 0;
virtual void drawIconImg() {
drawImg(
*bruceConfig.themeFS(),
bruceConfig.getThemeItemImg(themePath()), // const String& - no heap alloc
0,
imgCenterY,
true,
bruceConfig.theme.gifDuration,
false
);
}
virtual bool hasTheme() = 0;
virtual const String& themePath() = 0;
bool checkTheme() { return hasTheme() && themePath().length() > 0; }
String getName() const { return String(_name); }
void draw(float scale = 1) {
if (rotation != bruceConfigPins.rotation) resetCoordinates();
if (!checkTheme()) {
tft.fillRect(0, 27, tftWidth, tftHeight - 27, bruceConfig.bgColor);
drawIcon(scale);
drawArrows(scale);
drawTitle(scale);
} else {
if (bruceConfig.theme.label)
drawTitle(scale); // If using .GIF, labels are draw after complete, which takes some time
drawIconImg();
if (bruceConfig.theme.label) drawTitle(scale); // Makes sure to draw over the image
}
drawStatusBar();
}
// Renders drawIcon() inside an arbitrary box instead of the full screen icon area,
// used by the grid main menu. The icons paint straight to `tft` using the member
// coordinates and read their colors from bruceConfig, so both are swapped for the
// call and put back afterwards.
void drawIconInBox(int centerX, int centerY, int box, uint16_t fgColor, uint16_t bgColor) {
int oldCenterX = iconCenterX, oldCenterY = iconCenterY;
int oldAreaX = iconAreaX, oldAreaY = iconAreaY;
int oldAreaW = iconAreaW, oldAreaH = iconAreaH;
uint16_t oldPriColor = bruceConfig.priColor;
uint16_t oldBgColor = bruceConfig.bgColor;
iconCenterX = centerX;
iconCenterY = centerY;
iconAreaW = box;
iconAreaH = box;
iconAreaX = centerX - box / 2;
iconAreaY = centerY - box / 2;
bruceConfig.priColor = fgColor;
bruceConfig.bgColor = bgColor;
// The widest icon spans about ICON_SCALE_REFERENCE px at scale 1, so dividing by it
// keeps every icon inside `box` and stops it bleeding into the neighbouring cells.
drawIcon((float)box / ICON_SCALE_REFERENCE);
bruceConfig.priColor = oldPriColor;
bruceConfig.bgColor = oldBgColor;
iconCenterX = oldCenterX;
iconCenterY = oldCenterY;
iconAreaX = oldAreaX;
iconAreaY = oldAreaY;
iconAreaW = oldAreaW;
iconAreaH = oldAreaH;
}
void drawArrows(float scale = 1) {
tft.fillRect(arrowAreaX, iconAreaY, arrowAreaW, iconAreaH, bruceConfig.bgColor);
tft.fillRect(
tftWidth - arrowAreaX - arrowAreaW, iconAreaY, arrowAreaW, iconAreaH, bruceConfig.bgColor
);
int arrowSize = scale * 10;
int lineWidth = scale * 3;
int arrowX = BORDER_PAD_X + 1.5 * arrowSize;
int arrowY = iconCenterY + 1.5 * arrowSize;
// Left Arrow
tft.drawWideLine(
arrowX,
arrowY,
arrowX + arrowSize,
arrowY + arrowSize,
lineWidth,
bruceConfig.priColor,
bruceConfig.bgColor
);
tft.drawWideLine(
arrowX,
arrowY,
arrowX + arrowSize,
arrowY - arrowSize,
lineWidth,
bruceConfig.priColor,
bruceConfig.bgColor
);
// Right Arrow
tft.drawWideLine(
tftWidth - arrowX,
arrowY,
tftWidth - arrowX - arrowSize,
arrowY + arrowSize,
lineWidth,
bruceConfig.priColor,
bruceConfig.bgColor
);
tft.drawWideLine(
tftWidth - arrowX,
arrowY,
tftWidth - arrowX - arrowSize,
arrowY - arrowSize,
lineWidth,
bruceConfig.priColor,
bruceConfig.bgColor
);
}
void drawTitle(float scale = 1) {
int titleY = iconCenterY + iconAreaH / 2 + FG;
tft.setTextSize(FM);
tft.drawPixel(0, 0, 0);
tft.fillRect(arrowAreaX, titleY, tftWidth - 2 * arrowAreaX, LH * FM, bruceConfig.bgColor);
int nchars = (tftWidth - 16) / (LW * FM);
tft.drawCentreString(getName().substring(0, nchars), iconCenterX, titleY, 1);
}
protected:
const char *_name = "";
uint8_t rotation = ROTATION;
int iconAreaH =
((tftHeight - 2 * BORDER_PAD_Y) % 2 == 0 ? tftHeight - 2 * BORDER_PAD_Y
: tftHeight - 2 * BORDER_PAD_Y + 1);
int iconAreaW = iconAreaH;
int iconCenterX = tftWidth / 2;
int iconCenterY = tftHeight / 2;
int imgCenterY = 13;
int iconAreaX = iconCenterX - iconAreaW / 2;
int iconAreaY = iconCenterY - iconAreaH / 2;
int arrowAreaX = BORDER_PAD_X;
int arrowAreaW = iconAreaX - arrowAreaX;
MenuItemInterface(const char *name) : _name(name) {}
void clearIconArea(void) {
tft.fillRect(iconAreaX, iconAreaY, iconAreaW, iconAreaH, bruceConfig.bgColor);
}
void clearImgArea(void) { tft.fillRect(7, 27, tftWidth - 14, tftHeight - 34, bruceConfig.bgColor); }
void resetCoordinates(void) {
// Recalculate Center and ared due to portrait/landscape changings
if (tftWidth > tftHeight) {
iconAreaH =
((tftHeight - 2 * BORDER_PAD_Y) % 2 == 0 ? tftHeight - 2 * BORDER_PAD_Y
: tftHeight - 2 * BORDER_PAD_Y + 1);
} else {
iconAreaH =
((tftWidth - 2 * BORDER_PAD_Y) % 2 == 0 ? tftWidth - 2 * BORDER_PAD_Y
: tftWidth - 2 * BORDER_PAD_Y + 1);
}
iconAreaW = iconAreaH;
iconCenterX = tftWidth / 2;
iconCenterY = tftHeight / 2;
iconAreaX = iconCenterX - iconAreaW / 2;
iconAreaY = iconCenterY - iconAreaH / 2;
arrowAreaX = BORDER_PAD_X;
arrowAreaW = iconAreaX - arrowAreaX;
rotation = bruceConfigPins.rotation;
}
private:
};
#endif // __MENU_ITEM_INTERFACE_H__