Kerry Martin
/
Liz_Test_Code
KSM edits
Button.cpp@19:fee3f71fab2d, 2019-07-10 (annotated)
- Committer:
- lizard753
- Date:
- Wed Jul 10 13:30:27 2019 +0000
- Revision:
- 19:fee3f71fab2d
- Child:
- 20:d25fb9c55781
Adding button library and main code for button
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lizard753 | 19:fee3f71fab2d | 1 | |
lizard753 | 19:fee3f71fab2d | 2 | #include "Button.h" |
lizard753 | 19:fee3f71fab2d | 3 | /** Button Constructor |
lizard753 | 19:fee3f71fab2d | 4 | * creates an object to emulate a button on the screen |
lizard753 | 19:fee3f71fab2d | 5 | * arguments: |
lizard753 | 19:fee3f71fab2d | 6 | * |
lizard753 | 19:fee3f71fab2d | 7 | * |
lizard753 | 19:fee3f71fab2d | 8 | */ |
lizard753 | 19:fee3f71fab2d | 9 | Button::Button(loc_t arg_corner1_x, loc_t arg_corner1_y, loc_t arg_corner2_x, loc_t arg_corner2_y, color_t arg_color, RA8875 *arg_display, const char * arg_name) |
lizard753 | 19:fee3f71fab2d | 10 | : corner1_x(arg_corner1_x), |
lizard753 | 19:fee3f71fab2d | 11 | corner1_y(arg_corner1_y), |
lizard753 | 19:fee3f71fab2d | 12 | corner2_x(arg_corner2_x), |
lizard753 | 19:fee3f71fab2d | 13 | corner2_y(arg_corner2_y), |
lizard753 | 19:fee3f71fab2d | 14 | //button_name(arg_name), |
lizard753 | 19:fee3f71fab2d | 15 | button_color(arg_color), |
lizard753 | 19:fee3f71fab2d | 16 | lcd(arg_display) |
lizard753 | 19:fee3f71fab2d | 17 | { |
lizard753 | 19:fee3f71fab2d | 18 | int len = strlen(arg_name) + 1; |
lizard753 | 19:fee3f71fab2d | 19 | button_name = new char[len]; |
lizard753 | 19:fee3f71fab2d | 20 | snprintf(button_name, len, "%s", arg_name); |
lizard753 | 19:fee3f71fab2d | 21 | } |
lizard753 | 19:fee3f71fab2d | 22 | |
lizard753 | 19:fee3f71fab2d | 23 | //Button::Button() |
lizard753 | 19:fee3f71fab2d | 24 | //: Button(0, 0, 0, 0, RGB(0,0,0), RA8875(), string("noname")) |
lizard753 | 19:fee3f71fab2d | 25 | //{ |
lizard753 | 19:fee3f71fab2d | 26 | //} |
lizard753 | 19:fee3f71fab2d | 27 | /** isPressed |
lizard753 | 19:fee3f71fab2d | 28 | * given the current coordinates of the press, is it within the bounds of the button |
lizard753 | 19:fee3f71fab2d | 29 | * returns true/false |
lizard753 | 19:fee3f71fab2d | 30 | */ |
lizard753 | 19:fee3f71fab2d | 31 | bool Button::isPressed(loc_t cur_x, loc_t cur_y) |
lizard753 | 19:fee3f71fab2d | 32 | { |
lizard753 | 19:fee3f71fab2d | 33 | if(corner1_x < cur_x && corner2_x > cur_x && corner1_y < cur_y && corner2_y > cur_y) |
lizard753 | 19:fee3f71fab2d | 34 | { |
lizard753 | 19:fee3f71fab2d | 35 | return true; |
lizard753 | 19:fee3f71fab2d | 36 | } |
lizard753 | 19:fee3f71fab2d | 37 | return false; |
lizard753 | 19:fee3f71fab2d | 38 | } |
lizard753 | 19:fee3f71fab2d | 39 | |
lizard753 | 19:fee3f71fab2d | 40 | /** draw |
lizard753 | 19:fee3f71fab2d | 41 | * will use the screen given in the constructor to draw out a rectangle and text to symbolize the button |
lizard753 | 19:fee3f71fab2d | 42 | * returns: void |
lizard753 | 19:fee3f71fab2d | 43 | */ |
lizard753 | 19:fee3f71fab2d | 44 | void Button::draw() |
lizard753 | 19:fee3f71fab2d | 45 | { |
lizard753 | 19:fee3f71fab2d | 46 | lcd->rect(corner1_x, corner1_y, corner2_x, corner2_y, button_color); |
lizard753 | 19:fee3f71fab2d | 47 | lcd->puts(corner1_x, corner2_y, button_name); |
lizard753 | 19:fee3f71fab2d | 48 | } |
lizard753 | 19:fee3f71fab2d | 49 | |
lizard753 | 19:fee3f71fab2d | 50 | /** toString |
lizard753 | 19:fee3f71fab2d | 51 | * returns a string with the values of the button |
lizard753 | 19:fee3f71fab2d | 52 | * used mainly for debugging |
lizard753 | 19:fee3f71fab2d | 53 | * returns: string with values of button |
lizard753 | 19:fee3f71fab2d | 54 | */ |
lizard753 | 19:fee3f71fab2d | 55 | string Button::toString() |
lizard753 | 19:fee3f71fab2d | 56 | { |
lizard753 | 19:fee3f71fab2d | 57 | string return_string = "Liz is awesome"; |
lizard753 | 19:fee3f71fab2d | 58 | //sprintf(return_string, "Button Name: %s\nLocation: %d,%d:%d,%d\n", button_name, corner1_x, corner1_y, corner2_x, corner2_y); |
lizard753 | 19:fee3f71fab2d | 59 | return return_string; |
lizard753 | 19:fee3f71fab2d | 60 | } |