New IHM
Dependencies: Array_Matrix TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG
SeekBar.cpp@0:a2686ef737c2, 2016-03-31 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Thu Mar 31 07:28:42 2016 +0000
- Revision:
- 0:a2686ef737c2
- Child:
- 5:9c3ea4d4bc6b
1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikamiUitOpen | 0:a2686ef737c2 | 1 | //----------------------------------------------------------- |
MikamiUitOpen | 0:a2686ef737c2 | 2 | // SeekBar class |
MikamiUitOpen | 0:a2686ef737c2 | 3 | // |
MikamiUitOpen | 0:a2686ef737c2 | 4 | // 2016/03/29, Copyright (c) 2016 MIKAMI, Naoki |
MikamiUitOpen | 0:a2686ef737c2 | 5 | //----------------------------------------------------------- |
MikamiUitOpen | 0:a2686ef737c2 | 6 | |
MikamiUitOpen | 0:a2686ef737c2 | 7 | #include "SeekBar.hpp" |
MikamiUitOpen | 0:a2686ef737c2 | 8 | |
MikamiUitOpen | 0:a2686ef737c2 | 9 | namespace Mikami |
MikamiUitOpen | 0:a2686ef737c2 | 10 | { |
MikamiUitOpen | 0:a2686ef737c2 | 11 | // Slide thumb |
MikamiUitOpen | 0:a2686ef737c2 | 12 | // If the thumb is not touched, return false |
MikamiUitOpen | 0:a2686ef737c2 | 13 | bool SeekBar::Slide() |
MikamiUitOpen | 0:a2686ef737c2 | 14 | { |
MikamiUitOpen | 0:a2686ef737c2 | 15 | if (!PanelTouched()) |
MikamiUitOpen | 0:a2686ef737c2 | 16 | { |
MikamiUitOpen | 0:a2686ef737c2 | 17 | if (slided_) Draw(v_); |
MikamiUitOpen | 0:a2686ef737c2 | 18 | slided_ = false; |
MikamiUitOpen | 0:a2686ef737c2 | 19 | return false; |
MikamiUitOpen | 0:a2686ef737c2 | 20 | } |
MikamiUitOpen | 0:a2686ef737c2 | 21 | |
MikamiUitOpen | 0:a2686ef737c2 | 22 | uint16_t x, y; |
MikamiUitOpen | 0:a2686ef737c2 | 23 | bool rtn = IsOnThumb(x, y); |
MikamiUitOpen | 0:a2686ef737c2 | 24 | if (rtn) |
MikamiUitOpen | 0:a2686ef737c2 | 25 | { |
MikamiUitOpen | 0:a2686ef737c2 | 26 | v_ = ToValue(Point(x, y)); |
MikamiUitOpen | 0:a2686ef737c2 | 27 | Draw(v_, true); |
MikamiUitOpen | 0:a2686ef737c2 | 28 | slided_ = true; |
MikamiUitOpen | 0:a2686ef737c2 | 29 | } |
MikamiUitOpen | 0:a2686ef737c2 | 30 | |
MikamiUitOpen | 0:a2686ef737c2 | 31 | return rtn; |
MikamiUitOpen | 0:a2686ef737c2 | 32 | } |
MikamiUitOpen | 0:a2686ef737c2 | 33 | |
MikamiUitOpen | 0:a2686ef737c2 | 34 | // Draw seekbar |
MikamiUitOpen | 0:a2686ef737c2 | 35 | void SeekBar::Draw(float value, bool fill) |
MikamiUitOpen | 0:a2686ef737c2 | 36 | { |
MikamiUitOpen | 0:a2686ef737c2 | 37 | // Erase previous seekbar |
MikamiUitOpen | 0:a2686ef737c2 | 38 | lcd_.SetTextColor(BACK_COLOR_); |
MikamiUitOpen | 0:a2686ef737c2 | 39 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:a2686ef737c2 | 40 | lcd_.FillRect(X_-SIZE_/2, Y_-SIZE_/2, L_+SIZE_+1, SIZE_+1); |
MikamiUitOpen | 0:a2686ef737c2 | 41 | else |
MikamiUitOpen | 0:a2686ef737c2 | 42 | lcd_.FillRect(X_-SIZE_/2, Y_-SIZE_/2, SIZE_+1, L_+SIZE_+1); |
MikamiUitOpen | 0:a2686ef737c2 | 43 | |
MikamiUitOpen | 0:a2686ef737c2 | 44 | Point pt = ToPoint(Saturate(value)); // Position of thumb |
MikamiUitOpen | 0:a2686ef737c2 | 45 | |
MikamiUitOpen | 0:a2686ef737c2 | 46 | // Draw upper line |
MikamiUitOpen | 0:a2686ef737c2 | 47 | lcd_.SetTextColor(COLOR_H_); |
MikamiUitOpen | 0:a2686ef737c2 | 48 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:a2686ef737c2 | 49 | lcd_.FillRect(pt.x, Y_-W_/4, X_+L_-pt.x, W_/2); |
MikamiUitOpen | 0:a2686ef737c2 | 50 | else |
MikamiUitOpen | 0:a2686ef737c2 | 51 | lcd_.FillRect(X_-W_/4, Y_, W_/2, pt.y-Y_); |
MikamiUitOpen | 0:a2686ef737c2 | 52 | |
MikamiUitOpen | 0:a2686ef737c2 | 53 | // Draw lower line |
MikamiUitOpen | 0:a2686ef737c2 | 54 | lcd_.SetTextColor(COLOR_L_); |
MikamiUitOpen | 0:a2686ef737c2 | 55 | if ((ORIENT_ == Holizontal) && ((pt.x-X_) > 0)) |
MikamiUitOpen | 0:a2686ef737c2 | 56 | lcd_.FillRect(X_, Y_-W_/2, pt.x-X_, W_); |
MikamiUitOpen | 0:a2686ef737c2 | 57 | if ((ORIENT_ == Vertical) && ((Y_+L_-pt.y) > 0)) |
MikamiUitOpen | 0:a2686ef737c2 | 58 | lcd_.FillRect(X_-W_/2, pt.y, W_, Y_+L_-pt.y); |
MikamiUitOpen | 0:a2686ef737c2 | 59 | |
MikamiUitOpen | 0:a2686ef737c2 | 60 | // Draw thumb |
MikamiUitOpen | 0:a2686ef737c2 | 61 | lcd_.SetTextColor(CREATED_COLOR_); |
MikamiUitOpen | 0:a2686ef737c2 | 62 | if (fill) |
MikamiUitOpen | 0:a2686ef737c2 | 63 | lcd_.FillCircle(pt.x, pt.y, SIZE_/2); |
MikamiUitOpen | 0:a2686ef737c2 | 64 | else |
MikamiUitOpen | 0:a2686ef737c2 | 65 | lcd_.DrawCircle(pt.x, pt.y, SIZE_/2); |
MikamiUitOpen | 0:a2686ef737c2 | 66 | } |
MikamiUitOpen | 0:a2686ef737c2 | 67 | |
MikamiUitOpen | 0:a2686ef737c2 | 68 | // If touched position is on the button, return true |
MikamiUitOpen | 0:a2686ef737c2 | 69 | bool SeekBar::IsOnThumb(uint16_t &x, uint16_t &y) |
MikamiUitOpen | 0:a2686ef737c2 | 70 | { |
MikamiUitOpen | 0:a2686ef737c2 | 71 | x = state_.touchX[0]; |
MikamiUitOpen | 0:a2686ef737c2 | 72 | y = state_.touchY[0]; |
MikamiUitOpen | 0:a2686ef737c2 | 73 | |
MikamiUitOpen | 0:a2686ef737c2 | 74 | Point pt = ToPoint(v_); |
MikamiUitOpen | 0:a2686ef737c2 | 75 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:a2686ef737c2 | 76 | { |
MikamiUitOpen | 0:a2686ef737c2 | 77 | if ( (pt.x-SIZE_/5 <= x) && (x <= pt.x+SIZE_/5) && |
MikamiUitOpen | 0:a2686ef737c2 | 78 | (pt.y-SIZE_ <= y) && (y <= pt.y+SIZE_) ) return true; |
MikamiUitOpen | 0:a2686ef737c2 | 79 | } |
MikamiUitOpen | 0:a2686ef737c2 | 80 | else |
MikamiUitOpen | 0:a2686ef737c2 | 81 | { |
MikamiUitOpen | 0:a2686ef737c2 | 82 | if ( (pt.x-SIZE_ <= x) && (x <= pt.x+SIZE_) && |
MikamiUitOpen | 0:a2686ef737c2 | 83 | (pt.y-SIZE_/5 <= y) && (y <= pt.y+SIZE_/5) ) return true; |
MikamiUitOpen | 0:a2686ef737c2 | 84 | } |
MikamiUitOpen | 0:a2686ef737c2 | 85 | |
MikamiUitOpen | 0:a2686ef737c2 | 86 | return false; |
MikamiUitOpen | 0:a2686ef737c2 | 87 | } |
MikamiUitOpen | 0:a2686ef737c2 | 88 | |
MikamiUitOpen | 0:a2686ef737c2 | 89 | SeekBar::Point SeekBar::ToPoint(float value) |
MikamiUitOpen | 0:a2686ef737c2 | 90 | { |
MikamiUitOpen | 0:a2686ef737c2 | 91 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:a2686ef737c2 | 92 | return Point(X_ + L_*(value - MIN_)/(MAX_ - MIN_), Y_); |
MikamiUitOpen | 0:a2686ef737c2 | 93 | else |
MikamiUitOpen | 0:a2686ef737c2 | 94 | return Point(X_, Y_ + L_ - L_*(value - MIN_)/(MAX_ - MIN_)); |
MikamiUitOpen | 0:a2686ef737c2 | 95 | } |
MikamiUitOpen | 0:a2686ef737c2 | 96 | |
MikamiUitOpen | 0:a2686ef737c2 | 97 | float SeekBar::ToValue(Point pt) |
MikamiUitOpen | 0:a2686ef737c2 | 98 | { |
MikamiUitOpen | 0:a2686ef737c2 | 99 | float value; |
MikamiUitOpen | 0:a2686ef737c2 | 100 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:a2686ef737c2 | 101 | value = (pt.x - X_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
MikamiUitOpen | 0:a2686ef737c2 | 102 | else |
MikamiUitOpen | 0:a2686ef737c2 | 103 | value = -(pt.y - Y_ - L_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
MikamiUitOpen | 0:a2686ef737c2 | 104 | return Saturate(value); |
MikamiUitOpen | 0:a2686ef737c2 | 105 | } |
MikamiUitOpen | 0:a2686ef737c2 | 106 | |
MikamiUitOpen | 0:a2686ef737c2 | 107 | float SeekBar::Saturate(float value) |
MikamiUitOpen | 0:a2686ef737c2 | 108 | { |
MikamiUitOpen | 0:a2686ef737c2 | 109 | if (value < MIN_) value = MIN_; |
MikamiUitOpen | 0:a2686ef737c2 | 110 | if (value > MAX_) value = MAX_; |
MikamiUitOpen | 0:a2686ef737c2 | 111 | return value; |
MikamiUitOpen | 0:a2686ef737c2 | 112 | } |
MikamiUitOpen | 0:a2686ef737c2 | 113 | } |