Button.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  12. // Classic initButton() function: pass center & size
  13. void TFT_eSPI_Button::initButton(
  14. TFT_eSPI *gfx, int16_t x, int16_t y, uint16_t w, uint16_t h,
  15. uint16_t outline, uint16_t fill, uint16_t textcolor,
  16. char *label, uint8_t textsize)
  17. {
  18. // Tweak arguments and pass to the newer initButtonUL() function...
  19. initButtonUL(gfx, x - (w / 2), y - (h / 2), w, h, outline, fill,
  20. textcolor, label, textsize);
  21. }
  22. // Newer function instead accepts upper-left corner & size
  23. void TFT_eSPI_Button::initButtonUL(
  24. TFT_eSPI *gfx, int16_t x1, int16_t y1, uint16_t w, uint16_t h,
  25. uint16_t outline, uint16_t fill, uint16_t textcolor,
  26. char *label, uint8_t textsize)
  27. {
  28. _x1 = x1;
  29. _y1 = y1;
  30. _w = w;
  31. _h = h;
  32. _outlinecolor = outline;
  33. _fillcolor = fill;
  34. _textcolor = textcolor;
  35. _textsize = textsize;
  36. _gfx = gfx;
  37. strncpy(_label, label, 9);
  38. }
  39. // Adjust text datum and x, y deltas
  40. void TFT_eSPI_Button::setLabelDatum(int16_t x_delta, int16_t y_delta, uint8_t datum)
  41. {
  42. _xd = x_delta;
  43. _yd = y_delta;
  44. _textdatum = datum;
  45. }
  46. void TFT_eSPI_Button::drawButton(bool inverted, String long_name) {
  47. uint16_t fill, outline, text;
  48. if(!inverted) {
  49. fill = _fillcolor;
  50. outline = _outlinecolor;
  51. text = _textcolor;
  52. } else {
  53. fill = _textcolor;
  54. outline = _outlinecolor;
  55. text = _fillcolor;
  56. }
  57. uint8_t r = min(_w, _h) / 4; // Corner radius
  58. _gfx->fillRoundRect(_x1, _y1, _w, _h, r, fill);
  59. _gfx->drawRoundRect(_x1, _y1, _w, _h, r, outline);
  60. _gfx->setTextColor(text, fill);
  61. _gfx->setTextSize(_textsize);
  62. uint8_t tempdatum = _gfx->getTextDatum();
  63. _gfx->setTextDatum(_textdatum);
  64. uint16_t tempPadding = _gfx->getTextPadding();
  65. _gfx->setTextPadding(0);
  66. if (long_name == "")
  67. _gfx->drawString(_label, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
  68. else
  69. _gfx->drawString(long_name, _x1 + (_w/2) + _xd, _y1 + (_h/2) - 4 + _yd);
  70. _gfx->setTextDatum(tempdatum);
  71. _gfx->setTextPadding(tempPadding);
  72. }
  73. bool TFT_eSPI_Button::contains(int16_t x, int16_t y) {
  74. return ((x >= _x1) && (x < (_x1 + _w)) &&
  75. (y >= _y1) && (y < (_y1 + _h)));
  76. }
  77. void TFT_eSPI_Button::press(bool p) {
  78. laststate = currstate;
  79. currstate = p;
  80. }
  81. bool TFT_eSPI_Button::isPressed() { return currstate; }
  82. bool TFT_eSPI_Button::justPressed() { return (currstate && !laststate); }
  83. bool TFT_eSPI_Button::justReleased() { return (!currstate && laststate); }