-
Notifications
You must be signed in to change notification settings - Fork 1
/
ButtonDrawer.h
67 lines (61 loc) · 1.69 KB
/
ButtonDrawer.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
#ifndef _ButtonDrawer_H_
#define _ButtonDrawer_H_
#include <M5Stack.h>
#include "Rect.h"
class ButtonDrawer {
public:
static ButtonDrawer* getInstance() {
static ButtonDrawer singleton;
return &singleton;
}
ButtonDrawer(){};
ButtonDrawer(const String& btnA, const String& btnB, const String& btnC)
: titles{btnA,btnB,btnC}
{
};
void setTitle(const String& btnA, const String& btnB, const String& btnC) {
titles[0] = btnA;
titles[1] = btnB;
titles[2] = btnC;
}
void setTitle(uint8_t idx, const String& str) {
if (idx < 3) titles[idx] = str;
}
uint16_t colorFrame[2] = { 0xA514,0xffff };
uint16_t colorFill[2] = { 0,0 };
uint16_t colorFont[2] = { 0xffff,0xffff };
void draw()
{
Rect r(32, 226, 64, 14);
draw(r, M5.BtnA.isPressed(), titles[0]);
r.x += 96;
draw(r, M5.BtnB.isPressed(), titles[1]);
r.x += 96;
draw(r, M5.BtnC.isPressed(), titles[2]);
}
private:
String titles[3];
void draw(Rect r, bool pressed, const String& title)
{
uint16_t color = colorFrame[pressed];
M5.Lcd.drawRect(r.x+1,r.y ,r.w-2,r.h ,color);
M5.Lcd.drawRect(r.x ,r.y+1,r.w ,r.h-2 ,color);
r.Inflate(-2);
int h = (r.h - 8)/2;
int w = title.length() * 6;
int x = (r.w - w) / 2;
color = colorFill[pressed];
M5.Lcd.fillRect(r.x , r.y, x, r.h, color);
M5.Lcd.fillRect(r.x+x+w, r.y, r.w-w-x, r.h, color);
if (h > 0) {
M5.Lcd.fillRect(r.x+x, r.y, w, h, color);
M5.Lcd.fillRect(r.x+x, r.y+r.h-h, w, h, color);
}
M5.Lcd.setCursor(r.x+x, r.y + h);
M5.Lcd.setTextFont(0);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextColor(colorFont[pressed], color);
M5.Lcd.print(title);
}
};
#endif