Add "actions" to buttons, allowing custom callbacks to be attached to buttons
This commit is contained in:
parent
1be1b4b05b
commit
4e7fb37a14
|
|
@ -8,6 +8,13 @@ TFT_eSPI_Button::TFT_eSPI_Button(void) {
|
||||||
_yd = 0;
|
_yd = 0;
|
||||||
_textdatum = MC_DATUM;
|
_textdatum = MC_DATUM;
|
||||||
_label[9] = '\0';
|
_label[9] = '\0';
|
||||||
|
_actionFunction = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set custom callback allowing arbitrary effects
|
||||||
|
void TFT_eSPI_Button::setButtonAction(ButtonActionFunction buttonActionFunction)
|
||||||
|
{
|
||||||
|
_actionFunction = buttonActionFunction;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Classic initButton() function: pass center & size
|
// Classic initButton() function: pass center & size
|
||||||
|
|
@ -103,3 +110,10 @@ void TFT_eSPI_Button::press(bool p) {
|
||||||
bool TFT_eSPI_Button::isPressed() { return currstate; }
|
bool TFT_eSPI_Button::isPressed() { return currstate; }
|
||||||
bool TFT_eSPI_Button::justPressed() { return (currstate && !laststate); }
|
bool TFT_eSPI_Button::justPressed() { return (currstate && !laststate); }
|
||||||
bool TFT_eSPI_Button::justReleased() { return (!currstate && laststate); }
|
bool TFT_eSPI_Button::justReleased() { return (!currstate && laststate); }
|
||||||
|
|
||||||
|
|
||||||
|
void TFT_eSPI_Button::action() {
|
||||||
|
if (_actionFunction != NULL) {
|
||||||
|
_actionFunction();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,8 @@
|
||||||
// within button
|
// within button
|
||||||
***************************************************************************************/
|
***************************************************************************************/
|
||||||
|
|
||||||
|
typedef void (*ButtonActionFunction)(void);
|
||||||
|
|
||||||
class TFT_eSPI_Button : public TFT_eSPI {
|
class TFT_eSPI_Button : public TFT_eSPI {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -26,6 +28,10 @@ class TFT_eSPI_Button : public TFT_eSPI {
|
||||||
void drawButton(bool inverted = false, String long_name = "");
|
void drawButton(bool inverted = false, String long_name = "");
|
||||||
bool contains(int16_t x, int16_t y);
|
bool contains(int16_t x, int16_t y);
|
||||||
|
|
||||||
|
// Set custom callback allowing arbitrary effects
|
||||||
|
void setButtonAction(ButtonActionFunction actionFunction);
|
||||||
|
void action(void);
|
||||||
|
|
||||||
void press(bool p);
|
void press(bool p);
|
||||||
bool isPressed();
|
bool isPressed();
|
||||||
bool justPressed();
|
bool justPressed();
|
||||||
|
|
@ -41,4 +47,6 @@ class TFT_eSPI_Button : public TFT_eSPI {
|
||||||
char _label[10]; // Button text is 9 chars maximum unless long_name used
|
char _label[10]; // Button text is 9 chars maximum unless long_name used
|
||||||
|
|
||||||
bool currstate, laststate; // Button states
|
bool currstate, laststate; // Button states
|
||||||
|
|
||||||
|
ButtonActionFunction _actionFunction; // Action callback
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue