mirror of
https://github.com/adafruit/Adafruit-GFX-Library.git
synced 2026-10-27 06:21:36 +00:00
Add UI Button element
This commit is contained in:
1 parent
b346ad0df5
commit
734b107c39
2 files changed
+94
No files matched your search
@@ -518,3 +518,70 @@ void Adafruit_GFX::invertDisplay(boolean i) {
|
||||
// Do nothing, must be subclassed if supported
|
||||
}
|
||||
|
||||
/***************************************************************************/
|
||||
// code for the GFX button UI element
|
||||
|
||||
Adafruit_GFX_Button::Adafruit_GFX_Button(void) {
|
||||
_gfx = 0;
|
||||
}
|
||||
|
||||
void Adafruit_GFX_Button::initButton(Adafruit_GFX *gfx,
|
||||
int16_t x, int16_t y,
|
||||
uint8_t w, uint8_t h,
|
||||
uint16_t outline, uint16_t fill,
|
||||
uint16_t textcolor,
|
||||
char *label, uint8_t textsize)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
_w = w;
|
||||
_h = h;
|
||||
_outlinecolor = outline;
|
||||
_fillcolor = fill;
|
||||
_textcolor = textcolor;
|
||||
_textsize = textsize;
|
||||
_gfx = gfx;
|
||||
strncpy(_label, label, 9);
|
||||
_label[9] = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Adafruit_GFX_Button::drawButton(boolean inverted) {
|
||||
uint16_t fill, outline, text;
|
||||
|
||||
if (! inverted) {
|
||||
fill = _fillcolor;
|
||||
outline = _outlinecolor;
|
||||
text = _textcolor;
|
||||
} else {
|
||||
fill = _textcolor;
|
||||
outline = _outlinecolor;
|
||||
text = _fillcolor;
|
||||
}
|
||||
|
||||
_gfx->fillRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, fill);
|
||||
_gfx->drawRoundRect(_x - (_w/2), _y - (_h/2), _w, _h, min(_w,_h)/4, outline);
|
||||
|
||||
|
||||
_gfx->setCursor(_x - strlen(_label)*3*_textsize, _y-4*_textsize);
|
||||
_gfx->setTextColor(text);
|
||||
_gfx->setTextSize(_textsize);
|
||||
_gfx->print(_label);
|
||||
}
|
||||
|
||||
boolean Adafruit_GFX_Button::contains(int16_t x, int16_t y) {
|
||||
if ((x < (_x - _w/2)) || (x > (_x + _w/2))) return false;
|
||||
if ((y < (_y - _h)) || (y > (_y + _h/2))) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
void Adafruit_GFX_Button::press(boolean p) {
|
||||
laststate = currstate;
|
||||
currstate = p;
|
||||
}
|
||||
|
||||
boolean Adafruit_GFX_Button::isPressed() { return currstate; }
|
||||
boolean Adafruit_GFX_Button::justPressed() { return (currstate && !laststate); }
|
||||
boolean Adafruit_GFX_Button::justReleased() { return (!currstate && laststate); }
|
||||
@@ -87,4 +87,31 @@ class Adafruit_GFX : public Print {
|
||||
wrap; // If set, 'wrap' text at right edge of display
|
||||
};
|
||||
|
||||
class Adafruit_GFX_Button {
|
||||
|
||||
public:
|
||||
Adafruit_GFX_Button(void);
|
||||
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 textcolor,
|
||||
char *label, uint8_t textsize);
|
||||
void drawButton(boolean inverted = false);
|
||||
boolean contains(int16_t x, int16_t y);
|
||||
|
||||
void press(boolean p);
|
||||
boolean isPressed();
|
||||
boolean justPressed();
|
||||
boolean justReleased();
|
||||
|
||||
private:
|
||||
Adafruit_GFX *_gfx;
|
||||
int16_t _x, _y;
|
||||
uint16_t _w, _h;
|
||||
uint8_t _textsize;
|
||||
uint16_t _outlinecolor, _fillcolor, _textcolor;
|
||||
char _label[10];
|
||||
|
||||
boolean currstate, laststate;
|
||||
};
|
||||
|
||||
#endif // _ADAFRUIT_GFX_H
|
||||
Reference in new issue
Block a user