a
F746_BUTTON.cpp@0:a7fb0dba4c8a, 2019-10-09 (annotated)
- Committer:
- fundokukiri
- Date:
- Wed Oct 09 12:55:47 2019 +0000
- Revision:
- 0:a7fb0dba4c8a
a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fundokukiri | 0:a7fb0dba4c8a | 1 | //----------------------------------------------------------- |
fundokukiri | 0:a7fb0dba4c8a | 2 | // |
fundokukiri | 0:a7fb0dba4c8a | 3 | // F746_BUTTON class |
fundokukiri | 0:a7fb0dba4c8a | 4 | // |
fundokukiri | 0:a7fb0dba4c8a | 5 | //----------------------------------------------------------- |
fundokukiri | 0:a7fb0dba4c8a | 6 | |
fundokukiri | 0:a7fb0dba4c8a | 7 | #include "F746_BUTTON.hpp" |
fundokukiri | 0:a7fb0dba4c8a | 8 | |
fundokukiri | 0:a7fb0dba4c8a | 9 | // Draw button |
fundokukiri | 0:a7fb0dba4c8a | 10 | void Button::Render() |
fundokukiri | 0:a7fb0dba4c8a | 11 | { |
fundokukiri | 0:a7fb0dba4c8a | 12 | active_ = true; |
fundokukiri | 0:a7fb0dba4c8a | 13 | |
fundokukiri | 0:a7fb0dba4c8a | 14 | switch(STYLE_) |
fundokukiri | 0:a7fb0dba4c8a | 15 | { |
fundokukiri | 0:a7fb0dba4c8a | 16 | case 1: // Style 1 - Normal button |
fundokukiri | 0:a7fb0dba4c8a | 17 | lcd_.SetTextColor(BUTTON_COLOUR_); // Set Textcolour to original |
fundokukiri | 0:a7fb0dba4c8a | 18 | lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw button rectangle |
fundokukiri | 0:a7fb0dba4c8a | 19 | break; |
fundokukiri | 0:a7fb0dba4c8a | 20 | |
fundokukiri | 0:a7fb0dba4c8a | 21 | case 2: // Style 2 - 3D button |
fundokukiri | 0:a7fb0dba4c8a | 22 | lcd_.SetTextColor(LCD_COLOR_DARKGRAY); // Set Textcolour to shadow colour |
fundokukiri | 0:a7fb0dba4c8a | 23 | lcd_.FillRect(ButtonX_+1, ButtonY_+2, (ButtonW_+3), ButtonH_+3);// Draw shadow |
fundokukiri | 0:a7fb0dba4c8a | 24 | |
fundokukiri | 0:a7fb0dba4c8a | 25 | lcd_.SetTextColor(BUTTON_COLOUR_); // Set Textcolour to original |
fundokukiri | 0:a7fb0dba4c8a | 26 | lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw button |
fundokukiri | 0:a7fb0dba4c8a | 27 | break; |
fundokukiri | 0:a7fb0dba4c8a | 28 | } |
fundokukiri | 0:a7fb0dba4c8a | 29 | if (LABEL_.length() != 0) // If the label is larger than 0 |
fundokukiri | 0:a7fb0dba4c8a | 30 | { |
fundokukiri | 0:a7fb0dba4c8a | 31 | lcd_.SetFont(FONTS_); |
fundokukiri | 0:a7fb0dba4c8a | 32 | lcd_.SetBackColor(BUTTON_COLOUR_); // Set background colour |
fundokukiri | 0:a7fb0dba4c8a | 33 | lcd_.SetTextColor(LCD_COLOR_WHITE); // Set textcolour |
fundokukiri | 0:a7fb0dba4c8a | 34 | |
fundokukiri | 0:a7fb0dba4c8a | 35 | uint16_t x0 = ButtonX_ + (ButtonW_ - FONT_WIDTH_*(LABEL_.length()))/2; // Calcukate x value for Button name |
fundokukiri | 0:a7fb0dba4c8a | 36 | uint16_t y0 = ButtonY_ + (ButtonH_ - FONT_HEIGHT_)/2 + 1; // Calcukate y value for Button name |
fundokukiri | 0:a7fb0dba4c8a | 37 | lcd_.DisplayStringAt(x0, y0, (uint8_t *)LABEL_.c_str(), |
fundokukiri | 0:a7fb0dba4c8a | 38 | LEFT_MODE); // Write button name |
fundokukiri | 0:a7fb0dba4c8a | 39 | lcd_.SetBackColor(LCD_COLOR_WHITE); // Set back colour |
fundokukiri | 0:a7fb0dba4c8a | 40 | } |
fundokukiri | 0:a7fb0dba4c8a | 41 | } |
fundokukiri | 0:a7fb0dba4c8a | 42 | |
fundokukiri | 0:a7fb0dba4c8a | 43 | |
fundokukiri | 0:a7fb0dba4c8a | 44 | void Button::Change() // Change colour of button when pressed |
fundokukiri | 0:a7fb0dba4c8a | 45 | { |
fundokukiri | 0:a7fb0dba4c8a | 46 | switch(STYLE_) |
fundokukiri | 0:a7fb0dba4c8a | 47 | { |
fundokukiri | 0:a7fb0dba4c8a | 48 | case 1: // Style 1 - Normal button |
fundokukiri | 0:a7fb0dba4c8a | 49 | lcd_.SetTextColor(CHANGE_COLOUR_); // Set Textcolour to the change colour |
fundokukiri | 0:a7fb0dba4c8a | 50 | lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw button rectangle |
fundokukiri | 0:a7fb0dba4c8a | 51 | break; |
fundokukiri | 0:a7fb0dba4c8a | 52 | |
fundokukiri | 0:a7fb0dba4c8a | 53 | case 2: // Style 2 - 3D button |
fundokukiri | 0:a7fb0dba4c8a | 54 | lcd_.SetTextColor(LCD_COLOR_WHITE); // Set Textcolour to background colour |
fundokukiri | 0:a7fb0dba4c8a | 55 | lcd_.FillRect(ButtonX_, ButtonY_, (ButtonW_+3), ButtonH_+3); // Clears background |
fundokukiri | 0:a7fb0dba4c8a | 56 | |
fundokukiri | 0:a7fb0dba4c8a | 57 | lcd_.SetTextColor(LCD_COLOR_GRAY); // Set Textcolour to shadow colour |
fundokukiri | 0:a7fb0dba4c8a | 58 | lcd_.FillRect(ButtonX_+1, ButtonY_+2, (ButtonW_+3), ButtonH_+3);// Draws shadow |
fundokukiri | 0:a7fb0dba4c8a | 59 | |
fundokukiri | 0:a7fb0dba4c8a | 60 | lcd_.SetTextColor(CHANGE_COLOUR_); // Indent button |
fundokukiri | 0:a7fb0dba4c8a | 61 | lcd_.FillRect((ButtonX_+6), (ButtonY_+6), (ButtonW_-3), (ButtonH_-2)); // Draw indented button |
fundokukiri | 0:a7fb0dba4c8a | 62 | break; |
fundokukiri | 0:a7fb0dba4c8a | 63 | } |
fundokukiri | 0:a7fb0dba4c8a | 64 | |
fundokukiri | 0:a7fb0dba4c8a | 65 | if (LABEL_.length() != 0) |
fundokukiri | 0:a7fb0dba4c8a | 66 | { |
fundokukiri | 0:a7fb0dba4c8a | 67 | lcd_.SetFont(FONTS_); |
fundokukiri | 0:a7fb0dba4c8a | 68 | lcd_.SetBackColor(CHANGE_COLOUR_); |
fundokukiri | 0:a7fb0dba4c8a | 69 | lcd_.SetTextColor(LCD_COLOR_BLACK); |
fundokukiri | 0:a7fb0dba4c8a | 70 | uint16_t x0 = ButtonX_ + (ButtonW_ - FONT_WIDTH_*(LABEL_.length()))/2; // Calcukate x value for Button name |
fundokukiri | 0:a7fb0dba4c8a | 71 | uint16_t y0 = ButtonY_ + (ButtonH_ - FONT_HEIGHT_)/2 + 1; // Calcukate y value for Button name |
fundokukiri | 0:a7fb0dba4c8a | 72 | lcd_.DisplayStringAt(x0, y0, (uint8_t *)LABEL_.c_str(), |
fundokukiri | 0:a7fb0dba4c8a | 73 | LEFT_MODE); // Write button name |
fundokukiri | 0:a7fb0dba4c8a | 74 | lcd_.SetBackColor(LCD_COLOR_WHITE); // Set back colour |
fundokukiri | 0:a7fb0dba4c8a | 75 | } |
fundokukiri | 0:a7fb0dba4c8a | 76 | } |
fundokukiri | 0:a7fb0dba4c8a | 77 | |
fundokukiri | 0:a7fb0dba4c8a | 78 | |
fundokukiri | 0:a7fb0dba4c8a | 79 | |
fundokukiri | 0:a7fb0dba4c8a | 80 | void Button::Hide() // Hide button |
fundokukiri | 0:a7fb0dba4c8a | 81 | { |
fundokukiri | 0:a7fb0dba4c8a | 82 | lcd_.SetTextColor(LCD_COLOR_WHITE); // Set textcolour to background colour |
fundokukiri | 0:a7fb0dba4c8a | 83 | lcd_.FillRect(ButtonX_, ButtonY_, ButtonW_, ButtonH_); // Draw rectangle in background colour |
fundokukiri | 0:a7fb0dba4c8a | 84 | active_ = false; // Set active boolean to false |
fundokukiri | 0:a7fb0dba4c8a | 85 | } |
fundokukiri | 0:a7fb0dba4c8a | 86 | |
fundokukiri | 0:a7fb0dba4c8a | 87 | |
fundokukiri | 0:a7fb0dba4c8a | 88 | bool Button::Press() // Check if touch detected |
fundokukiri | 0:a7fb0dba4c8a | 89 | { |
fundokukiri | 0:a7fb0dba4c8a | 90 | ts_.GetState(&state_); |
fundokukiri | 0:a7fb0dba4c8a | 91 | if (!state_.touchDetected) return false; |
fundokukiri | 0:a7fb0dba4c8a | 92 | if (!active_) return false; |
fundokukiri | 0:a7fb0dba4c8a | 93 | if (!ButtonBoundaryCheck()) return false; |
fundokukiri | 0:a7fb0dba4c8a | 94 | Change(); // Run the Change routine to change the colour of the button |
fundokukiri | 0:a7fb0dba4c8a | 95 | wait(0.2); // Wait to allow the colour change to be visible |
fundokukiri | 0:a7fb0dba4c8a | 96 | Render(); // Draw the original button |
fundokukiri | 0:a7fb0dba4c8a | 97 | return true; |
fundokukiri | 0:a7fb0dba4c8a | 98 | } |
fundokukiri | 0:a7fb0dba4c8a | 99 | |
fundokukiri | 0:a7fb0dba4c8a | 100 | bool Button::ButtonBoundaryCheck() // Check if touch is within button boundaries |
fundokukiri | 0:a7fb0dba4c8a | 101 | { |
fundokukiri | 0:a7fb0dba4c8a | 102 | int nTouch = multiTouch ? state_.touchDetected : 1; |
fundokukiri | 0:a7fb0dba4c8a | 103 | for (int n=0; n<nTouch; n++) |
fundokukiri | 0:a7fb0dba4c8a | 104 | { |
fundokukiri | 0:a7fb0dba4c8a | 105 | uint16_t x = state_.touchX[n]; // Assign touch x pos to x |
fundokukiri | 0:a7fb0dba4c8a | 106 | uint16_t y = state_.touchY[n]; // Assign touch y pos to y |
fundokukiri | 0:a7fb0dba4c8a | 107 | |
fundokukiri | 0:a7fb0dba4c8a | 108 | if ( (ButtonX_ <= x) && (x <= ButtonX_+ButtonW_) && // Check x value inside boundary |
fundokukiri | 0:a7fb0dba4c8a | 109 | (ButtonY_ <= y) && (y <= ButtonY_+ButtonH_) ) return true; // Check y value inside boundary |
fundokukiri | 0:a7fb0dba4c8a | 110 | } |
fundokukiri | 0:a7fb0dba4c8a | 111 | return false; |
fundokukiri | 0:a7fb0dba4c8a | 112 | } |
fundokukiri | 0:a7fb0dba4c8a | 113 | |
fundokukiri | 0:a7fb0dba4c8a | 114 | TS_StateTypeDef Button::state_; |
fundokukiri | 0:a7fb0dba4c8a | 115 | bool Button::multiTouch = false; |
fundokukiri | 0:a7fb0dba4c8a | 116 |