@@ -970,68 +970,70 @@ void Adafruit_GFX::invertDisplay(boolean i) {
970970// code for the GFX button UI element
971971
972972Adafruit_GFX_Button::Adafruit_GFX_Button (void ) {
973- _gfx = 0 ;
974- laststate = 0 ;
975- _fillcolor = 0 ;
976- _textsize = 1 ;
977- _w = 0 ;
978- _x = 0 ;
979- currstate = 0 ;
980- _textcolor = 0 ;
981- _outlinecolor = 0 ;
982- _h = 0 ;
983- _y = 0 ;
973+ _gfx = 0 ;
984974}
985975
976+ // Classic initButton() function: pass center & size
986977void Adafruit_GFX_Button::initButton (
987- Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h,
988- uint16_t outline, uint16_t fill, uint16_t textcolor,
989- char *label, uint8_t textsize)
978+ Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
979+ uint16_t outline, uint16_t fill, uint16_t textcolor,
980+ char *label, uint8_t textsize)
990981{
991- _x = x;
992- _y = y;
993- _w = w;
994- _h = h;
995- _outlinecolor = outline;
996- _fillcolor = fill;
997- _textcolor = textcolor;
998- _textsize = textsize;
999- _gfx = gfx;
1000- strncpy (_label, label, 9 );
1001- _label[9 ] = 0 ;
982+ // Tweak arguments and pass to the newer initButtonUL() function...
983+ initButtonUL (gfx, x - (w / 2 ), y - (h / 2 ), w, h, outline, fill,
984+ textcolor, label, textsize);
1002985}
1003986
1004- void Adafruit_GFX_Button::drawButton (boolean inverted) {
1005- uint16_t fill, outline, text;
1006-
1007- if (!inverted) {
1008- fill = _fillcolor;
1009- outline = _outlinecolor;
1010- text = _textcolor;
1011- } else {
1012- fill = _textcolor;
1013- outline = _outlinecolor;
1014- text = _fillcolor;
1015- }
1016-
1017- _gfx->fillRoundRect (_x - (_w/2 ), _y - (_h/2 ), _w, _h, min (_w,_h)/4 , fill);
1018- _gfx->drawRoundRect (_x - (_w/2 ), _y - (_h/2 ), _w, _h, min (_w,_h)/4 , outline);
987+ // Newer function instead accepts upper-left corner & size
988+ void Adafruit_GFX_Button::initButtonUL (
989+ Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
990+ uint16_t outline, uint16_t fill, uint16_t textcolor,
991+ char *label, uint8_t textsize)
992+ {
993+ _x1 = x1;
994+ _y1 = y1;
995+ _w = w;
996+ _h = h;
997+ _outlinecolor = outline;
998+ _fillcolor = fill;
999+ _textcolor = textcolor;
1000+ _textsize = textsize;
1001+ _gfx = gfx;
1002+ strncpy (_label, label, 9 );
1003+ }
10191004
1020- _gfx->setCursor (_x - strlen (_label)*3 *_textsize, _y-4 *_textsize);
1021- _gfx->setTextColor (text);
1022- _gfx->setTextSize (_textsize);
1023- _gfx->print (_label);
1005+ void Adafruit_GFX_Button::drawButton (boolean inverted) {
1006+ uint16_t fill, outline, text;
1007+
1008+ if (!inverted) {
1009+ fill = _fillcolor;
1010+ outline = _outlinecolor;
1011+ text = _textcolor;
1012+ } else {
1013+ fill = _textcolor;
1014+ outline = _outlinecolor;
1015+ text = _fillcolor;
1016+ }
1017+
1018+ uint8_t r = min (_w, _h) / 4 ; // Corner radius
1019+ _gfx->fillRoundRect (_x1, _y1, _w, _h, r, fill);
1020+ _gfx->drawRoundRect (_x1, _y1, _w, _h, r, outline);
1021+
1022+ _gfx->setCursor (_x1 + (_w/2 ) - (strlen (_label) * 3 * _textsize),
1023+ _y1 + (_h/2 ) - (4 * _textsize));
1024+ _gfx->setTextColor (text);
1025+ _gfx->setTextSize (_textsize);
1026+ _gfx->print (_label);
10241027}
10251028
10261029boolean Adafruit_GFX_Button::contains (int16_t x, int16_t y) {
1027- if ((x < (_x - _w/2 )) || (x > (_x + _w/2 ))) return false ;
1028- if ((y < (_y - _h/2 )) || (y > (_y + _h/2 ))) return false ;
1029- return true ;
1030+ return ((x >= _x1) && (x < (_x1 + _w)) &&
1031+ (y >= _y1) && (y < (_y1 + _h)));
10301032}
10311033
10321034void Adafruit_GFX_Button::press (boolean p) {
1033- laststate = currstate;
1034- currstate = p;
1035+ laststate = currstate;
1036+ currstate = p;
10351037}
10361038
10371039boolean Adafruit_GFX_Button::isPressed () { return currstate; }
@@ -1050,7 +1052,7 @@ boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate);
10501052// the buffer is in MCU memory and not the display driver...GXFcanvas1 might
10511053// be minimally useful on an Uno-class board, but this and GFXcanvas16 are
10521054// much more likely to require at least a Mega or various recent ARM-type
1053- // boards (recomment , as the text+bitmap draw can be pokey). GFXcanvas1
1055+ // boards (recommended , as the text+bitmap draw can be pokey). GFXcanvas1
10541056// requires 1 bit per pixel (rounded up to nearest byte per scanline),
10551057// GFXcanvas16 requires 2 bytes per pixel (no scanline pad).
10561058// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
0 commit comments