SPKT
Button.cpp@0:4e245f4bc8ac, 2019-06-07 (annotated)
- Committer:
- phungductung
- Date:
- Fri Jun 07 05:06:42 2019 +0000
- Revision:
- 0:4e245f4bc8ac
spkt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
phungductung | 0:4e245f4bc8ac | 1 | //----------------------------------------------------------- |
phungductung | 0:4e245f4bc8ac | 2 | // Button class handling multi-touch |
phungductung | 0:4e245f4bc8ac | 3 | // Multi-touch: Enabled (default) |
phungductung | 0:4e245f4bc8ac | 4 | // |
phungductung | 0:4e245f4bc8ac | 5 | // 2016/04/08, Copyright (c) 2016 MIKAMI, Naoki |
phungductung | 0:4e245f4bc8ac | 6 | //----------------------------------------------------------- |
phungductung | 0:4e245f4bc8ac | 7 | |
phungductung | 0:4e245f4bc8ac | 8 | #include "Button.hpp" |
phungductung | 0:4e245f4bc8ac | 9 | |
phungductung | 0:4e245f4bc8ac | 10 | namespace Mikami |
phungductung | 0:4e245f4bc8ac | 11 | { |
phungductung | 0:4e245f4bc8ac | 12 | // Draw button |
phungductung | 0:4e245f4bc8ac | 13 | void Button::Draw(uint32_t color, uint32_t textColor) |
phungductung | 0:4e245f4bc8ac | 14 | { |
phungductung | 0:4e245f4bc8ac | 15 | if (color == BACK_COLOR_) active_ = true; |
phungductung | 0:4e245f4bc8ac | 16 | if (!active_) return; |
phungductung | 0:4e245f4bc8ac | 17 | lcd_.SetTextColor(color); |
phungductung | 0:4e245f4bc8ac | 18 | lcd_.FillRect(X_, Y_, W_, H_); |
phungductung | 0:4e245f4bc8ac | 19 | |
phungductung | 0:4e245f4bc8ac | 20 | if (STR_.length() != 0) |
phungductung | 0:4e245f4bc8ac | 21 | { |
phungductung | 0:4e245f4bc8ac | 22 | lcd_.SetFont(FONTS_); |
phungductung | 0:4e245f4bc8ac | 23 | lcd_.SetBackColor(color); |
phungductung | 0:4e245f4bc8ac | 24 | lcd_.SetTextColor(textColor); |
phungductung | 0:4e245f4bc8ac | 25 | uint16_t x0 = X_ + (W_ - FONTS_->Width*(STR_.length()))/2; |
phungductung | 0:4e245f4bc8ac | 26 | uint16_t y0 = Y_ + (H_ - FONTS_->Height)/2 + 1; |
phungductung | 0:4e245f4bc8ac | 27 | DrawString(x0, y0, STR_); |
phungductung | 0:4e245f4bc8ac | 28 | lcd_.SetBackColor(BACK_COLOR_); // recover back color |
phungductung | 0:4e245f4bc8ac | 29 | } |
phungductung | 0:4e245f4bc8ac | 30 | } |
phungductung | 0:4e245f4bc8ac | 31 | |
phungductung | 0:4e245f4bc8ac | 32 | // Check touch detected |
phungductung | 0:4e245f4bc8ac | 33 | bool Button::Touched() |
phungductung | 0:4e245f4bc8ac | 34 | { |
phungductung | 0:4e245f4bc8ac | 35 | if (!active_) return false; |
phungductung | 0:4e245f4bc8ac | 36 | if (!PanelTouched()) return false; |
phungductung | 0:4e245f4bc8ac | 37 | if (!IsOnButton()) return false; |
phungductung | 0:4e245f4bc8ac | 38 | Draw(TOUCHED_COLOR_, TEXT_COLOR_); |
phungductung | 0:4e245f4bc8ac | 39 | return true; |
phungductung | 0:4e245f4bc8ac | 40 | } |
phungductung | 0:4e245f4bc8ac | 41 | |
phungductung | 0:4e245f4bc8ac | 42 | // If touched position is on the button, return true |
phungductung | 0:4e245f4bc8ac | 43 | bool Button::IsOnButton() |
phungductung | 0:4e245f4bc8ac | 44 | { |
phungductung | 0:4e245f4bc8ac | 45 | int nTouch = multiTouch_ ? state_.touchDetected : 1; |
phungductung | 0:4e245f4bc8ac | 46 | for (int n=0; n<nTouch; n++) |
phungductung | 0:4e245f4bc8ac | 47 | { |
phungductung | 0:4e245f4bc8ac | 48 | uint16_t x = state_.touchX[n]; |
phungductung | 0:4e245f4bc8ac | 49 | uint16_t y = state_.touchY[n]; |
phungductung | 0:4e245f4bc8ac | 50 | if ( (X_ <= x) && (x <= X_+W_) && |
phungductung | 0:4e245f4bc8ac | 51 | (Y_ <= y) && (y <= Y_+H_) ) return true; |
phungductung | 0:4e245f4bc8ac | 52 | } |
phungductung | 0:4e245f4bc8ac | 53 | return false; |
phungductung | 0:4e245f4bc8ac | 54 | } |
phungductung | 0:4e245f4bc8ac | 55 | |
phungductung | 0:4e245f4bc8ac | 56 | void Button::Activate() |
phungductung | 0:4e245f4bc8ac | 57 | { |
phungductung | 0:4e245f4bc8ac | 58 | active_ = true; |
phungductung | 0:4e245f4bc8ac | 59 | Draw(); |
phungductung | 0:4e245f4bc8ac | 60 | } |
phungductung | 0:4e245f4bc8ac | 61 | |
phungductung | 0:4e245f4bc8ac | 62 | void Button::Inactivate() |
phungductung | 0:4e245f4bc8ac | 63 | { |
phungductung | 0:4e245f4bc8ac | 64 | Draw(INACTIVE_COLOR_, INACTIVE_TEXT_COLOR_); |
phungductung | 0:4e245f4bc8ac | 65 | active_ = false; |
phungductung | 0:4e245f4bc8ac | 66 | } |
phungductung | 0:4e245f4bc8ac | 67 | } |