acidportal- 😈 Worlds smallest Evil Portal on a LilyGo T-QT |
git clone git://git.acid.vegas/acidportal.git |
Log | Files | Refs | Archive | README | LICENSE |
Button.cpp (3183B)
1 /*************************************************************************************** 2 ** Code for the GFX button UI element 3 ** Grabbed from Adafruit_GFX library and enhanced to handle any label font 4 ***************************************************************************************/ 5 TFT_eSPI_Button::TFT_eSPI_Button(void) { 6 _gfx = nullptr; 7 _xd = 0; 8 _yd = 0; 9 _textdatum = MC_DATUM; 10 _label[9] = '\0'; 11 currstate = false; 12 laststate = false; 13 } 14 15 // Classic initButton() function: pass center & size 16 void TFT_eSPI_Button::initButton( 17 TFT_eSPI *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h, 18 uint16_t outline, uint16_t fill, uint16_t textcolor, 19 char *label, uint8_t textsize) 20 { 21 // Tweak arguments and pass to the newer initButtonUL() function... 22 initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill, 23 textcolor, label, textsize); 24 } 25 26 // Newer function instead accepts upper-left corner & size 27 void TFT_eSPI_Button::initButtonUL( 28 TFT_eSPI *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h, 29 uint16_t outline, uint16_t fill, uint16_t textcolor, 30 char *label, uint8_t textsize) 31 { 32 _x1 = x1; 33 _y1 = y1; 34 _w = w; 35 _h = h; 36 _outlinecolor = outline; 37 _fillcolor = fill; 38 _textcolor = textcolor; 39 _textsize = textsize; 40 _gfx = gfx; 41 strncpy(_label, label, 9); 42 } 43 44 // Adjust text datum and x, y deltas 45 void TFT_eSPI_Button::setLabelDatum(int16_t x_delta, int16_t y_delta, uint8_t datum) 46 { 47 _xd = x_delta; 48 _yd = y_delta; 49 _textdatum = datum; 50 } 51 52 void TFT_eSPI_Button::drawButton(bool inverted, String long_name) { 53 uint16_t fill, outline, text; 54 55 if(!inverted) { 56 fill = _fillcolor; 57 outline = _outlinecolor; 58 text = _textcolor; 59 } else { 60 fill = _textcolor; 61 outline = _outlinecolor; 62 text = _fillcolor; 63 } 64 65 uint8_t r = min(_w, _h) / 4; // Corner radius 66 _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill); 67 _gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline); 68 69 if (_gfx->textfont == 255) { 70 _gfx->setCursor(_x1 + (_w / 8), 71 _y1 + (_h / 4)); 72 _gfx->setTextColor(text); 73 _gfx->setTextSize(_textsize); 74 _gfx->print(_label); 75 } 76 else { 77 _gfx->setTextColor(text, fill); 78 _gfx->setTextSize(_textsize); 79 80 uint8_t tempdatum = _gfx->getTextDatum(); 81 _gfx->setTextDatum(_textdatum); 82 uint16_t tempPadding = _gfx->getTextPadding(); 83 _gfx->setTextPadding(0); 84 85 if (long_name == "") 86 _gfx->drawString(_label, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd); 87 else 88 _gfx->drawString(long_name, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd); 89 90 _gfx->setTextDatum(tempdatum); 91 _gfx->setTextPadding(tempPadding); 92 } 93 } 94 95 bool TFT_eSPI_Button::contains(int16_t x, int16_t y) { 96 return ((x >= _x1) && (x < (_x1 + _w)) && 97 (y >= _y1) && (y < (_y1 + _h))); 98 } 99 100 void TFT_eSPI_Button::press(bool p) { 101 laststate = currstate; 102 currstate = p; 103 } 104 105 bool TFT_eSPI_Button::isPressed() { return currstate; } 106 bool TFT_eSPI_Button::justPressed() { return (currstate && !laststate); } 107 bool TFT_eSPI_Button::justReleased() { return (!currstate && laststate); }