0% found this document useful (0 votes)
3 views3 pages

False False False: // Classic Initbutton Function: Pass Center & Size

The document contains the implementation of a ButtonWidget class for a graphical user interface, which is built on top of the Adafruit_tft library. It includes methods for initializing the button, setting actions for press and release events, drawing the button, and checking its state. The class allows customization of button appearance and behavior, including text labeling and color settings.

Uploaded by

phuocloc1019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

False False False: // Classic Initbutton Function: Pass Center & Size

The document contains the implementation of a ButtonWidget class for a graphical user interface, which is built on top of the Adafruit_tft library. It includes methods for initializing the button, setting actions for press and release events, drawing the button, and checking its state. The class allows customization of button appearance and behavior, including text labeling and color settings.

Uploaded by

phuocloc1019
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1 #include "ButtonWidget.

h"
2
3 /***************************************************************************************
4 ** Code for the GFX button UI element
5 ** Grabbed from Adafruit_tft library and enhanced to handle any label font
6 ***************************************************************************************/
7 ButtonWidget::ButtonWidget(TFT_eSPI *tft) {
8 _tft = tft;
9 _xd = 0;
10 _yd = 0;
11 _textdatum = MC_DATUM;
12 _label[9] = '\0';
13 _currstate = false;
14 _laststate = false;
15 _inverted = false;
16 }
17
18 void ButtonWidget::setPressAction(actionCallback action)
19 {
20 pressAction = action;
21 }
22
23 void ButtonWidget::setReleaseAction(actionCallback action)
24 {
25 releaseAction = action;
26 }
27
28 // Classic initButton() function: pass center & size
29 void ButtonWidget::initButton(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t
outline, uint16_t fill, uint16_t textcolor, char *label, uint8_t textsize)
30 {
31 // Tweak arguments and pass to the newer initButtonUL() function...
32 initButtonUL(x - (w / 2), y - (h / 2), w, h, outline, fill, textcolor, label, textsize
);
33 }
34
35 // Newer function instead accepts upper-left corner & size
36 void ButtonWidget::initButtonUL(int16_t x1, int16_t y1, uint16_t w, uint16_t h, uint16_t
outline, uint16_t fill, uint16_t textcolor, char *label, uint8_t textsize)
37 {
38 _x1 = x1;
39 _y1 = y1;
40 _w = w;
41 _h = h;
42 _outlinecolor = outline;
43 _outlinewidth = 2;
44 _fillcolor = fill;
45 _textcolor = textcolor;
46 _textsize = textsize;
47 strncpy(_label, label, 9);
48 _pressTime = 0xFFFFFFFF;
49 _releaseTime = 0xFFFFFFFF;
50 }
51
52 // Adjust text datum and x, y deltas
53 void ButtonWidget::setLabelDatum(int16_t x_delta, int16_t y_delta, uint8_t datum)
54 {
55 _xd = x_delta;
56 _yd = y_delta;
57 _textdatum = datum;
58 }
59
60 void ButtonWidget::drawButton(bool inverted, String long_name) {
61 uint16_t fill, outline, text;
62
63 _inverted = inverted;
64
65 if(!inverted) {
66 fill = _fillcolor;
67 outline = _outlinecolor;
68 text = _textcolor;
69 } else {
70 fill = _textcolor;
71 outline = _outlinecolor;
72 text = _fillcolor;
73 }
74
75 uint8_t r = min(_w, _h) / 4; // Corner radius
76 _tft->fillRoundRect(_x1, _y1, _w, _h, r, fill);
77 _tft->drawRoundRect(_x1, _y1, _w, _h, r, outline);
78
79 if (_tft->textfont == 255) {
80 _tft->setCursor(_x1 + (_w / 8),
81 _y1 + (_h / 4));
82 _tft->setTextColor(text);
83 _tft->setTextSize(_textsize);
84 _tft->print(_label);
85 }
86 else {
87 _tft->setTextColor(text, fill);
88 _tft->setTextSize(_textsize);
89
90 uint8_t tempdatum = _tft->getTextDatum();
91 _tft->setTextDatum(_textdatum);
92 uint16_t tempPadding = _tft->getTextPadding();
93 _tft->setTextPadding(0);
94
95 if (long_name == "")
96 _tft->drawString(_label, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
97 else
98 _tft->drawString(long_name, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
99
100 _tft->setTextDatum(tempdatum);
101 _tft->setTextPadding(tempPadding);
102 }
103 }
104
105 void ButtonWidget::drawSmoothButton(bool inverted, int16_t outlinewidth, uint32_t bgcolor
, String long_name) {
106 uint16_t fill, outline, text;
107 if (bgcolor != 0x00FFFFFF) _bgcolor = bgcolor;
108 if (outlinewidth >=0) _outlinewidth = outlinewidth;
109 _inverted = inverted;
110
111 if(!inverted) {
112 fill = _fillcolor;
113 outline = _outlinecolor;
114 text = _textcolor;
115 } else {
116 fill = _textcolor;
117 outline = _outlinecolor;
118 text = _fillcolor;
119 }
120
121 uint8_t r = min(_w, _h) / 4; // Corner radius
122 if (outlinewidth > 0) _tft->fillSmoothRoundRect(_x1, _y1, _w, _h, r, outline, _bgcolor
);
123 _tft->fillSmoothRoundRect(_x1+_outlinewidth, _y1+_outlinewidth, _w-(2*_outlinewidth),
_h-(2*_outlinewidth), r-_outlinewidth, fill, outline);
124
125 if (_tft->textfont == 255) {
126 _tft->setCursor(_x1 + (_w / 8),
127 _y1 + (_h / 4));
128 _tft->setTextColor(text);
129 _tft->setTextSize(_textsize);
130 _tft->print(_label);
131 }
132 else {
133 _tft->setTextColor(text, fill);
134 _tft->setTextSize(_textsize);
135
136 uint8_t tempdatum = _tft->getTextDatum();
137 _tft->setTextDatum(_textdatum);
138 uint16_t tempPadding = _tft->getTextPadding();
139 _tft->setTextPadding(0);
140
141 if (long_name == "")
142 _tft->drawString(_label, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
143 else
144 _tft->drawString(long_name, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
145
146 _tft->setTextDatum(tempdatum);
147 _tft->setTextPadding(tempPadding);
148 }
149 }
150
151 bool ButtonWidget::contains(int16_t x, int16_t y) {
152 return ((x >= _x1) && (x < (_x1 + _w)) &&
153 (y >= _y1) && (y < (_y1 + _h)));
154 }
155
156 void ButtonWidget::press(bool p) {
157 _laststate = _currstate;
158 _currstate = p;
159 }
160
161 bool ButtonWidget::isPressed() { return _currstate; }
162 bool ButtonWidget::justPressed() { return (_currstate && !_laststate); }
163 bool ButtonWidget::justReleased() { return (!_currstate && _laststate); }
164

You might also like