discoverpixy
Data Structures | Macros | Typedefs | Functions
Button
Collaboration diagram for Button:

Data Structures

struct  BUTTON_STRUCT
 

Macros

#define AUTO   0
 Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height. More...
 

Typedefs

typedef void(* BUTTON_CALLBACK) (void *button)
 

Functions

bool gui_button_add (BUTTON_STRUCT *button)
 
void gui_button_remove (BUTTON_STRUCT *button)
 
void gui_button_redraw (BUTTON_STRUCT *button)
 

Detailed Description

The Button Gui-Element is a clickable, rectangular box with a label inside. When it is pressed and released you will be notified via the provided callback.

Macro Definition Documentation

#define AUTO   0

Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height.

Definition at line 65 of file button.h.

Typedef Documentation

typedef void(* BUTTON_CALLBACK) (void *button)

Prototype for Event Listeners (called when the button is pressed)

Note
You should NOT execute long running things in this callback nor should you update the gui. But you can call gui_screen_navigate() for instance.
Parameters
buttonThe pointer to the BUTTON_STRUCT where to corresponding Button was pressed

Definition at line 49 of file button.h.

Function Documentation

bool gui_button_add ( BUTTON_STRUCT button)

Adds a button. Your Callback will be called from now on, if the button was pressed

Parameters
buttonA Pointer to the preinitialized BUTTON_STRUCT
Returns
true on success

Definition at line 133 of file button.c.

134 {
135  if (touch_have_empty(1)) { //Check if the touch module can handle one additional area
136  //Calculate width and height of the button text
137  unsigned int strwidth = tft_font_width(button->font) * strlen(button->text);
138  unsigned char strheight = tft_font_height(button->font);
139 
140  button->base.hookedActions = PEN_DOWN; //At first we are interested in PEN_DOWN events
141  button->base.callback = buttons_cb; //Use our own callback for the touch area events
142 
143  if (button->base.x2 == AUTO) { //The user wants us to calculate the button width automatically
144  //Use string width + half of a character width as button width
145  button->base.x2 = button->base.x1 - 1 + strwidth + (tft_font_width(button->font) / 2);
146  } else if ((button->base.x2 - button->base.x1 + 1) < (strwidth + 2)) { //the provided width is too small to fit the entire text
147  return false; //report error
148  }
149 
150  if (button->base.y2 == AUTO) { //The user wants us to calculate the button height automatically
151  //Use one and a half character heights as button height
152  button->base.y2 = button->base.y1 - 1 + strheight + (strheight / 2);
153  } else if ((button->base.y2 - button->base.y1 + 1) < (strheight + 2)) { //the provided height is too small to fit the text
154  return false;
155  }
156 
157  gui_button_redraw(button); //call the redraw method, which will take care of drawing the entire button
158  return touch_register_area(&button->base); //Register the touch area and receive events for this button, from now on
159  }
160 
161  return false; //no more touch areas left
162 }
uint8_t tft_font_height(uint8_t fontnum)
Definition: tft.c:87
bool touch_register_area(TOUCH_AREA_STRUCT *area)
Definition: touch.c:181
const char * text
The label of the button.
Definition: button.h:61
uint8_t tft_font_width(uint8_t fontnum)
Definition: tft.c:92
void gui_button_redraw(BUTTON_STRUCT *button)
Definition: button.c:164
#define AUTO
Use this value instead of x2, y2 in the BUTTON_STRUCT to autocalculate the button width/height...
Definition: button.h:65
Receive an event when the pen goes down inside the region.
Definition: touch.h:54
uint16_t y1
Top Left Y-Coordinate of Area.
Definition: touch.h:75
uint16_t x1
Top Left X-Coordinate of Area.
Definition: touch.h:74
TOUCH_AREA_STRUCT base
Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct...
Definition: button.h:56
uint16_t y2
Bottom Right Y-Coordinate of Area.
Definition: touch.h:77
void buttons_cb(void *touchArea, TOUCH_ACTION triggeredAction)
Definition: button.c:92
TOUCH_CALLBACK callback
Callback which is executed when an event occurred in this Area.
Definition: touch.h:78
uint16_t x2
Bottom Right X-Coordinate of Area.
Definition: touch.h:76
TOUCH_ACTION hookedActions
Actions to listen to.
Definition: touch.h:73
bool touch_have_empty(unsigned char num)
Definition: touch.c:165
uint8_t font
The number of the font to use.
Definition: button.h:60

Here is the call graph for this function:

Here is the caller graph for this function:

void gui_button_redraw ( BUTTON_STRUCT button)

Redraws the button. Call this method if you have to redraw the entire screen or if you want to draw a button on top of an image.

Parameters
buttonA Pointer to the BUTTON_STRUCT

Definition at line 164 of file button.c.

165 {
166  //Calculate text dimensions and shadow colors
167  unsigned int strwidth = tft_font_width(button->font) * strlen(button->text);
168  unsigned char strheight = tft_font_height(button->font);
169  uint16_t c_light, c_dark;
170  calculate_shadows(button->bgcolor, &c_light, &c_dark);
171 
172  //Draw the background and the 4 lines (shadow colors)
173  tft_fill_rectangle(button->base.x1 + 1, button->base.y1 + 1, button->base.x2 - 1, button->base.y2 - 1, button->bgcolor);
174  tft_draw_line(button->base.x1 + 1, button->base.y1, button->base.x2 - 1, button->base.y1, c_light); //North
175  tft_draw_line(button->base.x1, button->base.y1 + 1, button->base.x1, button->base.y2 - 1, c_light); //West
176  tft_draw_line(button->base.x1 + 1, button->base.y2, button->base.x2 - 1, button->base.y2, c_dark); //South
177  tft_draw_line(button->base.x2, button->base.y1 + 1, button->base.x2, button->base.y2 - 1, c_dark); //East
178 
179  //Draw the text
180  tft_print_line(button->base.x1 + (button->base.x2 - button->base.x1 + 1 - strwidth) / 2, button->base.y1 + (button->base.y2 - button->base.y1 + 1 - strheight) / 2, button->txtcolor, button->bgcolor, button->font, button->text);
181 
182 }
uint8_t tft_font_height(uint8_t fontnum)
Definition: tft.c:87
const char * text
The label of the button.
Definition: button.h:61
uint8_t tft_font_width(uint8_t fontnum)
Definition: tft.c:92
uint16_t txtcolor
The 16-bit text color.
Definition: button.h:59
void tft_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: tft.c:50
uint16_t y1
Top Left Y-Coordinate of Area.
Definition: touch.h:75
void tft_print_line(uint16_t x, uint16_t y, uint16_t color, uint16_t bgcolor, uint8_t font, const char *text)
Definition: tft.c:98
uint16_t x1
Top Left X-Coordinate of Area.
Definition: touch.h:74
uint16_t bgcolor
The 16-bit background color of the button.
Definition: button.h:57
TOUCH_AREA_STRUCT base
Basic geometry of the button. You only need to set the x1, y1, x2, y2 members of this struct...
Definition: button.h:56
uint16_t y2
Bottom Right Y-Coordinate of Area.
Definition: touch.h:77
uint16_t x2
Bottom Right X-Coordinate of Area.
Definition: touch.h:76
void calculate_shadows(uint16_t bgcolor, uint16_t *light_shadow, uint16_t *dark_shadow)
Definition: button.c:34
void tft_fill_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
Definition: tft.c:67
uint8_t font
The number of the font to use.
Definition: button.h:60

Here is the call graph for this function:

Here is the caller graph for this function:

void gui_button_remove ( BUTTON_STRUCT button)

Removes the button. You will no longer receive events for this button. This function will not overdraw the region where the button was located.

Parameters
buttonA Pointer to the BUTTON_STRUCT

Definition at line 184 of file button.c.

185 {
186  //We only need to unregister the touch area, as we have not allocated anything else
188 }
void touch_unregister_area(TOUCH_AREA_STRUCT *area)
Definition: touch.c:195

Here is the call graph for this function:

Here is the caller graph for this function: