Button.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /***************************************************************************************
  2. // The following button class has been ported over from the Adafruit_GFX library so
  3. // should be compatible.
  4. // A slightly different implementation in this TFT_eSPI library allows the button
  5. // legends to be in any font, allow longer labels and to adjust text positioning
  6. // within button
  7. ***************************************************************************************/
  8. class TFT_eSPI_Button {
  9. public:
  10. TFT_eSPI_Button(void);
  11. // "Classic" initButton() uses centre & size
  12. void initButton(TFT_eSPI *gfx, int16_t x, int16_t y,
  13. uint16_t w, uint16_t h, uint16_t outline, uint16_t fill,
  14. uint16_t textcolor, char *label, uint8_t textsize);
  15. // New/alt initButton() uses upper-left corner & size
  16. void initButtonUL(TFT_eSPI *gfx, int16_t x1, int16_t y1,
  17. uint16_t w, uint16_t h, uint16_t outline, uint16_t fill,
  18. uint16_t textcolor, char *label, uint8_t textsize);
  19. // Adjust text datum and x, y deltas
  20. void setLabelDatum(int16_t x_delta, int16_t y_delta, uint8_t datum = MC_DATUM);
  21. void drawButton(bool inverted = false, String long_name = "");
  22. bool contains(int16_t x, int16_t y);
  23. void press(bool p);
  24. bool isPressed();
  25. bool justPressed();
  26. bool justReleased();
  27. private:
  28. TFT_eSPI *_gfx;
  29. int16_t _x1, _y1; // Coordinates of top-left corner of button
  30. int16_t _xd, _yd; // Button text datum offsets (wrt centre of button)
  31. uint16_t _w, _h; // Width and height of button
  32. uint8_t _textsize, _textdatum; // Text size multiplier and text datum for button
  33. uint16_t _outlinecolor, _fillcolor, _textcolor;
  34. char _label[10]; // Button text is 9 chars maximum unless long_name used
  35. bool currstate, laststate; // Button states
  36. };