Skip to content

Commit b8f2200

Browse files
Handle larger button dimensions, add initButtonUL() for buttons w/upper-left coord
1 parent dc40877 commit b8f2200

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

Adafruit_GFX.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -889,13 +889,25 @@ Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
889889
_gfx = 0;
890890
}
891891

892+
// Classic initButton() function: pass center & size
892893
void 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

910921
void 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

932945
boolean 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

938950
void 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.

Adafruit_GFX.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,13 @@ class Adafruit_GFX_Button {
108108

109109
public:
110110
Adafruit_GFX_Button(void);
111+
// "Classic" initButton() uses center & size
111112
void initButton(Adafruit_GFX *gfx, int16_t x, int16_t y,
112-
uint8_t w, uint8_t h, uint16_t outline, uint16_t fill,
113+
uint16_t w, uint16_t h, uint16_t outline, uint16_t fill,
114+
uint16_t textcolor, char *label, uint8_t textsize);
115+
// New/alt initButton() uses upper-left corner & size
116+
void initButtonUL(Adafruit_GFX *gfx, int16_t x1, int16_t y1,
117+
uint16_t w, uint16_t h, uint16_t outline, uint16_t fill,
113118
uint16_t textcolor, char *label, uint8_t textsize);
114119
void drawButton(boolean inverted = false);
115120
boolean contains(int16_t x, int16_t y);
@@ -121,11 +126,11 @@ class Adafruit_GFX_Button {
121126

122127
private:
123128
Adafruit_GFX *_gfx;
124-
int16_t _x, _y;
125-
uint16_t _w, _h;
126-
uint8_t _textsize;
127-
uint16_t _outlinecolor, _fillcolor, _textcolor;
128-
char _label[10];
129+
int16_t _x1, _y1; // Coordinates of top-left corner
130+
uint16_t _w, _h;
131+
uint8_t _textsize;
132+
uint16_t _outlinecolor, _fillcolor, _textcolor;
133+
char _label[10];
129134

130135
boolean currstate, laststate;
131136
};

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=Adafruit GFX Library
2-
version=1.1.5
2+
version=1.1.6
33
author=Adafruit
44
maintainer=Adafruit <[email protected]>
55
sentence=Adafruit GFX graphics core library, this is the 'core' class that all our other graphics libraries derive from.

0 commit comments

Comments
 (0)