Same as original
button.cpp@10:5ca60e724a76, 2015-12-31 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Dec 31 08:32:05 2015 +0000
- Revision:
- 10:5ca60e724a76
- Parent:
- 5:2cc388e91bde
- Child:
- 12:710078d50d9b
11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:d3038879fed6 | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 0:d3038879fed6 | 2 | // Button class |
MikamiUitOpen | 0:d3038879fed6 | 3 | // |
MikamiUitOpen | 10:5ca60e724a76 | 4 | // 2015/12/31, Copyright (c) 2015 MIKAMI, Naoki |
MikamiUitOpen | 0:d3038879fed6 | 5 | //----------------------------------------------------------- |
MikamiUitOpen | 0:d3038879fed6 | 6 | |
MikamiUitOpen | 0:d3038879fed6 | 7 | #include "button.hpp" |
MikamiUitOpen | 0:d3038879fed6 | 8 | |
MikamiUitOpen | 0:d3038879fed6 | 9 | namespace Mikami |
MikamiUitOpen | 0:d3038879fed6 | 10 | { |
MikamiUitOpen | 0:d3038879fed6 | 11 | // Draw button |
MikamiUitOpen | 0:d3038879fed6 | 12 | void Button::Draw(uint32_t color, uint32_t textColor) |
MikamiUitOpen | 0:d3038879fed6 | 13 | { |
MikamiUitOpen | 0:d3038879fed6 | 14 | lcd_.SetTextColor(color); |
MikamiUitOpen | 0:d3038879fed6 | 15 | lcd_.FillRect(X_, Y_, W_, H_); |
MikamiUitOpen | 0:d3038879fed6 | 16 | |
MikamiUitOpen | 0:d3038879fed6 | 17 | if (STR_.length() != 0) |
MikamiUitOpen | 0:d3038879fed6 | 18 | { |
MikamiUitOpen | 0:d3038879fed6 | 19 | lcd_.SetFont(FONTS_); |
MikamiUitOpen | 0:d3038879fed6 | 20 | lcd_.SetBackColor(color); |
MikamiUitOpen | 0:d3038879fed6 | 21 | lcd_.SetTextColor(textColor); |
MikamiUitOpen | 0:d3038879fed6 | 22 | |
MikamiUitOpen | 5:2cc388e91bde | 23 | uint16_t x0 = X_ + (W_ - FONT_WIDTH_*(STR_.length()))/2; |
MikamiUitOpen | 5:2cc388e91bde | 24 | uint16_t y0 = Y_ + (H_ - FONT_HEIGHT_)/2 + 1; |
MikamiUitOpen | 5:2cc388e91bde | 25 | lcd_.DisplayStringAt(x0, y0, (uint8_t *)STR_.c_str(), |
MikamiUitOpen | 5:2cc388e91bde | 26 | LEFT_MODE); |
MikamiUitOpen | 0:d3038879fed6 | 27 | lcd_.SetBackColor(BACK_COLOR_); // recover back color |
MikamiUitOpen | 0:d3038879fed6 | 28 | } |
MikamiUitOpen | 0:d3038879fed6 | 29 | } |
MikamiUitOpen | 0:d3038879fed6 | 30 | |
MikamiUitOpen | 0:d3038879fed6 | 31 | // Check touch detected |
MikamiUitOpen | 0:d3038879fed6 | 32 | bool Button::Touched() |
MikamiUitOpen | 0:d3038879fed6 | 33 | { |
MikamiUitOpen | 0:d3038879fed6 | 34 | TS_StateTypeDef state; |
MikamiUitOpen | 0:d3038879fed6 | 35 | ts_.GetState(&state); |
MikamiUitOpen | 0:d3038879fed6 | 36 | |
MikamiUitOpen | 0:d3038879fed6 | 37 | if (state.touchDetected) |
MikamiUitOpen | 0:d3038879fed6 | 38 | { |
MikamiUitOpen | 0:d3038879fed6 | 39 | uint16_t x = state.touchX[0]; |
MikamiUitOpen | 0:d3038879fed6 | 40 | uint16_t y = state.touchY[0]; |
MikamiUitOpen | 0:d3038879fed6 | 41 | |
MikamiUitOpen | 0:d3038879fed6 | 42 | return ( (X_ <= x) && (x <= X_+W_) && |
MikamiUitOpen | 0:d3038879fed6 | 43 | (Y_ <= y) && (y <= Y_+H_) ) ? true : false; |
MikamiUitOpen | 0:d3038879fed6 | 44 | } |
MikamiUitOpen | 0:d3038879fed6 | 45 | else |
MikamiUitOpen | 0:d3038879fed6 | 46 | return false; |
MikamiUitOpen | 0:d3038879fed6 | 47 | } |
MikamiUitOpen | 0:d3038879fed6 | 48 | |
MikamiUitOpen | 0:d3038879fed6 | 49 | // Check touch detected and redraw button |
MikamiUitOpen | 0:d3038879fed6 | 50 | bool Button::Touched(uint32_t color, uint32_t textColor) |
MikamiUitOpen | 0:d3038879fed6 | 51 | { |
MikamiUitOpen | 0:d3038879fed6 | 52 | bool rtn = Touched(); |
MikamiUitOpen | 0:d3038879fed6 | 53 | if (rtn) Draw(color, textColor); |
MikamiUitOpen | 0:d3038879fed6 | 54 | return rtn; |
MikamiUitOpen | 0:d3038879fed6 | 55 | } |
MikamiUitOpen | 0:d3038879fed6 | 56 | } |