@@ -889,13 +889,25 @@ Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
889889 _gfx = 0 ;
890890}
891891
892+ // Classic initButton() function: pass center & size
892893void Adafruit_GFX_Button::initButton (
893- Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h,
894+ Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
894895 uint16_t outline, uint16_t fill, uint16_t textcolor,
895896 char *label, uint8_t textsize)
896897{
897- _x = x;
898- _y = y;
898+ // Tweak arguments and pass to the newer initButtonUL() function...
899+ initButtonUL (gfx, x - (w / 2 ), y - (h / 2 ), w, h, outline, fill,
900+ textcolor, label, textsize);
901+ }
902+
903+ // Newer function instead accepts upper-left corner & size
904+ void Adafruit_GFX_Button::initButtonUL (
905+ Adafruit_GFX *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
906+ uint16_t outline, uint16_t fill, uint16_t textcolor,
907+ char *label, uint8_t textsize)
908+ {
909+ _x1 = x1;
910+ _y1 = y1;
899911 _w = w;
900912 _h = h;
901913 _outlinecolor = outline;
@@ -904,7 +916,6 @@ void Adafruit_GFX_Button::initButton(
904916 _textsize = textsize;
905917 _gfx = gfx;
906918 strncpy (_label, label, 9 );
907- _label[9 ] = 0 ;
908919}
909920
910921void Adafruit_GFX_Button::drawButton (boolean inverted) {
@@ -920,19 +931,20 @@ void Adafruit_GFX_Button::drawButton(boolean inverted) {
920931 text = _fillcolor;
921932 }
922933
923- _gfx->fillRoundRect (_x - (_w/2 ), _y - (_h/2 ), _w, _h, min (_w,_h)/4 , fill);
924- _gfx->drawRoundRect (_x - (_w/2 ), _y - (_h/2 ), _w, _h, min (_w,_h)/4 , outline);
934+ uint8_t r = min (_w, _h) / 4 ; // Corner radius
935+ _gfx->fillRoundRect (_x1, _y1, _w, _h, r, fill);
936+ _gfx->drawRoundRect (_x1, _y1, _w, _h, r, outline);
925937
926- _gfx->setCursor (_x - strlen (_label)*3 *_textsize, _y-4 *_textsize);
938+ _gfx->setCursor (_x1 + (_w/2 ) - (strlen (_label) * 3 * _textsize),
939+ _y1 + (_h/2 ) - (4 * _textsize));
927940 _gfx->setTextColor (text);
928941 _gfx->setTextSize (_textsize);
929942 _gfx->print (_label);
930943}
931944
932945boolean Adafruit_GFX_Button::contains (int16_t x, int16_t y) {
933- if ((x < (_x - _w/2 )) || (x > (_x + _w/2 ))) return false ;
934- if ((y < (_y - _h/2 )) || (y > (_y + _h/2 ))) return false ;
935- return true ;
946+ return ((x >= _x1) && (x < (_x1 + _w)) &&
947+ (y >= _y1) && (y < (_y1 + _h)));
936948}
937949
938950void Adafruit_GFX_Button::press (boolean p) {
@@ -956,7 +968,7 @@ boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate);
956968// the buffer is in MCU memory and not the display driver...GXFcanvas1 might
957969// be minimally useful on an Uno-class board, but this and GFXcanvas16 are
958970// much more likely to require at least a Mega or various recent ARM-type
959- // boards (recomment , as the text+bitmap draw can be pokey). GFXcanvas1
971+ // boards (recommended , as the text+bitmap draw can be pokey). GFXcanvas1
960972// requires 1 bit per pixel (rounded up to nearest byte per scanline),
961973// GFXcanvas16 requires 2 bytes per pixel (no scanline pad).
962974// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.
0 commit comments