Handle larger button dimensions, add initButtonUL() for buttons w/upper-left coord

This commit is contained in:
Phillip Burgess 2017-03-07 10:24:06 -08:00
parent dc40877a22
commit b8f2200766
3 changed files with 35 additions and 18 deletions

View File

@ -889,13 +889,25 @@ Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
_gfx = 0; _gfx = 0;
} }
// Classic initButton() function: pass center & size
void Adafruit_GFX_Button::initButton( void Adafruit_GFX_Button::initButton(
Adafruit_GFX *gfx, int16_t x, int16_t y, uint8_t w, uint8_t h, Adafruit_GFX *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
uint16_t outline, uint16_t fill, uint16_t textcolor, uint16_t outline, uint16_t fill, uint16_t textcolor,
char *label, uint8_t textsize) char *label, uint8_t textsize)
{ {
_x = x; // Tweak arguments and pass to the newer initButtonUL() function...
_y = y; initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
textcolor, label, textsize);
}
// Newer function instead accepts upper-left corner & size
void Adafruit_GFX_Button::initButtonUL(
Adafruit_GFX *gfx, 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)
{
_x1 = x1;
_y1 = y1;
_w = w; _w = w;
_h = h; _h = h;
_outlinecolor = outline; _outlinecolor = outline;
@ -904,7 +916,6 @@ void Adafruit_GFX_Button::initButton(
_textsize = textsize; _textsize = textsize;
_gfx = gfx; _gfx = gfx;
strncpy(_label, label, 9); strncpy(_label, label, 9);
_label[9] = 0;
} }
void Adafruit_GFX_Button::drawButton(boolean inverted) { void Adafruit_GFX_Button::drawButton(boolean inverted) {
@ -920,19 +931,20 @@ void Adafruit_GFX_Button::drawButton(boolean inverted) {
text = _fillcolor; text = _fillcolor;
} }
_gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill); uint8_t r = min(_w, _h) / 4; // Corner radius
_gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline); _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
_gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
_gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize); _gfx->setCursor(_x1 + (_w/2) - (strlen(_label) * 3 * _textsize),
_y1 + (_h/2) - (4 * _textsize));
_gfx->setTextColor(text); _gfx->setTextColor(text);
_gfx->setTextSize(_textsize); _gfx->setTextSize(_textsize);
_gfx->print(_label); _gfx->print(_label);
} }
boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) { boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false; return ((x >= _x1) && (x < (_x1 + _w)) &&
if ((y < (_y - _h/2)) || (y > (_y + _h/2))) return false; (y >= _y1) && (y < (_y1 + _h)));
return true;
} }
void Adafruit_GFX_Button::press(boolean p) { void Adafruit_GFX_Button::press(boolean p) {
@ -956,7 +968,7 @@ boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate);
// the buffer is in MCU memory and not the display driver...GXFcanvas1 might // the buffer is in MCU memory and not the display driver...GXFcanvas1 might
// be minimally useful on an Uno-class board, but this and GFXcanvas16 are // be minimally useful on an Uno-class board, but this and GFXcanvas16 are
// much more likely to require at least a Mega or various recent ARM-type // much more likely to require at least a Mega or various recent ARM-type
// boards (recomment, as the text+bitmap draw can be pokey). GFXcanvas1 // boards (recommended, as the text+bitmap draw can be pokey). GFXcanvas1
// requires 1 bit per pixel (rounded up to nearest byte per scanline), // requires 1 bit per pixel (rounded up to nearest byte per scanline),
// GFXcanvas16 requires 2 bytes per pixel (no scanline pad). // GFXcanvas16 requires 2 bytes per pixel (no scanline pad).
// NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND. // NOT EXTENSIVELY TESTED YET. MAY CONTAIN WORST BUGS KNOWN TO HUMANKIND.

View File

@ -108,8 +108,13 @@ class Adafruit_GFX_Button {
public: public:
Adafruit_GFX_Button(void); Adafruit_GFX_Button(void);
// "Classic" initButton() uses center & size
void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y, void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y,
uint8_t w, uint8_t h, uint16_t outline, uint16_t fill, uint16_t w, uint16_t h, uint16_t outline, uint16_t fill,
uint16_t textcolor, char *label, uint8_t textsize);
// New/alt initButton() uses upper-left corner & size
void initButtonUL(Adafruit_GFX *gfx, 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); uint16_t textcolor, char *label, uint8_t textsize);
void drawButton(boolean inverted = false); void drawButton(boolean inverted = false);
boolean contains(int16_t x, int16_t y); boolean contains(int16_t x, int16_t y);
@ -121,7 +126,7 @@ class Adafruit_GFX_Button {
private: private:
Adafruit_GFX *_gfx; Adafruit_GFX *_gfx;
int16_t _x, _y; int16_t _x1, _y1; // Coordinates of top-left corner
uint16_t _w, _h; uint16_t _w, _h;
uint8_t _textsize; uint8_t _textsize;
uint16_t _outlinecolor, _fillcolor, _textcolor; uint16_t _outlinecolor, _fillcolor, _textcolor;

View File

@ -1,5 +1,5 @@
name=Adafruit GFX Library name=Adafruit GFX Library
version=1.1.5 version=1.1.6
author=Adafruit author=Adafruit
maintainer=Adafruit <info@adafruit.com> maintainer=Adafruit <info@adafruit.com>
sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from. sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from.