New IHM
Dependencies: Array_Matrix TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG
SeekBar.cpp@16:b2762ec08863, 2016-07-12 (annotated)
- Committer:
- MikamiUitOpen
- Date:
- Tue Jul 12 02:23:26 2016 +0000
- Revision:
- 16:b2762ec08863
- Parent:
- 15:0511a08a3c09
- Child:
- 28:e2b7ea78b7a0
17
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 | 16:b2762ec08863 | 4 | // 2016/07/12, 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 | 6:b8f197b0012c | 11 | // Constructor with scale value (only horizontal) |
MikamiUitOpen | 6:b8f197b0012c | 12 | SeekBar::SeekBar(uint16_t x, uint16_t y, uint16_t length, |
MikamiUitOpen | 6:b8f197b0012c | 13 | float min, float max, float initialValue, |
MikamiUitOpen | 6:b8f197b0012c | 14 | string left, string center, string right, |
MikamiUitOpen | 6:b8f197b0012c | 15 | uint32_t thumbColor, |
MikamiUitOpen | 6:b8f197b0012c | 16 | uint16_t thumbSize, uint16_t width, |
MikamiUitOpen | 6:b8f197b0012c | 17 | uint32_t colorL, uint32_t colorH, |
MikamiUitOpen | 6:b8f197b0012c | 18 | uint32_t backColor) |
MikamiUitOpen | 6:b8f197b0012c | 19 | : GuiBase(x, y, Font12, GuiBase::ENUM_TEXT, backColor, thumbColor), |
MikamiUitOpen | 6:b8f197b0012c | 20 | L_(length), W_(width), |
MikamiUitOpen | 6:b8f197b0012c | 21 | SIZE_(thumbSize), COLOR_L_(colorL), COLOR_H_(colorH), |
MikamiUitOpen | 12:687ec6183385 | 22 | MIN_(min), MAX_(max), ORIENT_(Holizontal), v_(initialValue), |
MikamiUitOpen | 12:687ec6183385 | 23 | labelOn_(true), slided_(false), active_(true) |
MikamiUitOpen | 6:b8f197b0012c | 24 | { |
MikamiUitOpen | 6:b8f197b0012c | 25 | Draw(initialValue); |
MikamiUitOpen | 12:687ec6183385 | 26 | uint16_t y0 = y - thumbSize/2 - 13; |
MikamiUitOpen | 15:0511a08a3c09 | 27 | labelLCR_[0] = new Label(x, y0, left, Label::CENTER); |
MikamiUitOpen | 15:0511a08a3c09 | 28 | labelLCR_[1] = new Label(x+length/2, y0, center, Label::CENTER); |
MikamiUitOpen | 15:0511a08a3c09 | 29 | labelLCR_[2] = new Label(x+length, y0, right, Label::CENTER); |
MikamiUitOpen | 12:687ec6183385 | 30 | } |
MikamiUitOpen | 12:687ec6183385 | 31 | |
MikamiUitOpen | 12:687ec6183385 | 32 | SeekBar::~SeekBar() |
MikamiUitOpen | 12:687ec6183385 | 33 | { |
MikamiUitOpen | 12:687ec6183385 | 34 | if (labelOn_) |
MikamiUitOpen | 15:0511a08a3c09 | 35 | for (int n=0; n<3; n++) delete labelLCR_[n]; |
MikamiUitOpen | 6:b8f197b0012c | 36 | } |
MikamiUitOpen | 6:b8f197b0012c | 37 | |
MikamiUitOpen | 0:a2686ef737c2 | 38 | // Slide thumb |
MikamiUitOpen | 0:a2686ef737c2 | 39 | // If the thumb is not touched, return false |
MikamiUitOpen | 0:a2686ef737c2 | 40 | bool SeekBar::Slide() |
MikamiUitOpen | 0:a2686ef737c2 | 41 | { |
MikamiUitOpen | 5:9c3ea4d4bc6b | 42 | if (!active_) return false; |
MikamiUitOpen | 5:9c3ea4d4bc6b | 43 | |
MikamiUitOpen | 0:a2686ef737c2 | 44 | if (!PanelTouched()) |
MikamiUitOpen | 0:a2686ef737c2 | 45 | { |
MikamiUitOpen | 0:a2686ef737c2 | 46 | if (slided_) Draw(v_); |
MikamiUitOpen | 0:a2686ef737c2 | 47 | slided_ = false; |
MikamiUitOpen | 0:a2686ef737c2 | 48 | return false; |
MikamiUitOpen | 0:a2686ef737c2 | 49 | } |
MikamiUitOpen | 0:a2686ef737c2 | 50 | |
MikamiUitOpen | 0:a2686ef737c2 | 51 | uint16_t x, y; |
MikamiUitOpen | 0:a2686ef737c2 | 52 | bool rtn = IsOnThumb(x, y); |
MikamiUitOpen | 12:687ec6183385 | 53 | if (rtn || slided_) |
MikamiUitOpen | 0:a2686ef737c2 | 54 | { |
MikamiUitOpen | 12:687ec6183385 | 55 | if (rtn) v_ = ToValue(Point(x, y)); |
MikamiUitOpen | 12:687ec6183385 | 56 | Draw(v_, rtn); |
MikamiUitOpen | 12:687ec6183385 | 57 | slided_ = rtn; |
MikamiUitOpen | 0:a2686ef737c2 | 58 | } |
MikamiUitOpen | 0:a2686ef737c2 | 59 | return rtn; |
MikamiUitOpen | 0:a2686ef737c2 | 60 | } |
MikamiUitOpen | 6:b8f197b0012c | 61 | |
MikamiUitOpen | 5:9c3ea4d4bc6b | 62 | void SeekBar::Activate() |
MikamiUitOpen | 5:9c3ea4d4bc6b | 63 | { |
MikamiUitOpen | 5:9c3ea4d4bc6b | 64 | active_ = true; |
MikamiUitOpen | 5:9c3ea4d4bc6b | 65 | Draw(v_); |
MikamiUitOpen | 12:687ec6183385 | 66 | if (labelOn_) |
MikamiUitOpen | 15:0511a08a3c09 | 67 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_); |
MikamiUitOpen | 5:9c3ea4d4bc6b | 68 | } |
MikamiUitOpen | 6:b8f197b0012c | 69 | |
MikamiUitOpen | 5:9c3ea4d4bc6b | 70 | void SeekBar::Inactivate() |
MikamiUitOpen | 5:9c3ea4d4bc6b | 71 | { |
MikamiUitOpen | 5:9c3ea4d4bc6b | 72 | active_ = false; |
MikamiUitOpen | 5:9c3ea4d4bc6b | 73 | Draw(v_); |
MikamiUitOpen | 12:687ec6183385 | 74 | if (labelOn_) |
MikamiUitOpen | 15:0511a08a3c09 | 75 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(INACTIVE_TEXT_COLOR_); |
MikamiUitOpen | 5:9c3ea4d4bc6b | 76 | } |
MikamiUitOpen | 0:a2686ef737c2 | 77 | |
MikamiUitOpen | 9:c379410bda15 | 78 | // If touched position is on the thumb, return true |
MikamiUitOpen | 9:c379410bda15 | 79 | bool SeekBar::IsOnThumb(uint16_t &x, uint16_t &y) |
MikamiUitOpen | 9:c379410bda15 | 80 | { |
MikamiUitOpen | 9:c379410bda15 | 81 | x = state_.touchX[0]; |
MikamiUitOpen | 9:c379410bda15 | 82 | y = state_.touchY[0]; |
MikamiUitOpen | 9:c379410bda15 | 83 | |
MikamiUitOpen | 12:687ec6183385 | 84 | uint16_t th = SIZE_/2; |
MikamiUitOpen | 9:c379410bda15 | 85 | Point pt = ToPoint(v_); |
MikamiUitOpen | 9:c379410bda15 | 86 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 9:c379410bda15 | 87 | { |
MikamiUitOpen | 12:687ec6183385 | 88 | if ( (pt.x-th <= x) && (x <= pt.x+th) && |
MikamiUitOpen | 12:687ec6183385 | 89 | (pt.y-th <= y) && (y <= pt.y+th) ) return true; |
MikamiUitOpen | 9:c379410bda15 | 90 | } |
MikamiUitOpen | 9:c379410bda15 | 91 | else |
MikamiUitOpen | 9:c379410bda15 | 92 | { |
MikamiUitOpen | 12:687ec6183385 | 93 | if ( (pt.x-th <= x) && (x <= pt.x+th) && |
MikamiUitOpen | 12:687ec6183385 | 94 | (pt.y-th <= y) && (y <= pt.y+th) ) return true; |
MikamiUitOpen | 9:c379410bda15 | 95 | } |
MikamiUitOpen | 9:c379410bda15 | 96 | |
MikamiUitOpen | 9:c379410bda15 | 97 | return false; |
MikamiUitOpen | 9:c379410bda15 | 98 | } |
MikamiUitOpen | 9:c379410bda15 | 99 | |
MikamiUitOpen | 0:a2686ef737c2 | 100 | // Draw seekbar |
MikamiUitOpen | 0:a2686ef737c2 | 101 | void SeekBar::Draw(float value, bool fill) |
MikamiUitOpen | 0:a2686ef737c2 | 102 | { |
MikamiUitOpen | 10:5a2068884fd9 | 103 | uint16_t sizeS = (uint16_t)(SIZE_*0.6f); |
MikamiUitOpen | 0:a2686ef737c2 | 104 | // Erase previous seekbar |
MikamiUitOpen | 0:a2686ef737c2 | 105 | lcd_.SetTextColor(BACK_COLOR_); |
MikamiUitOpen | 0:a2686ef737c2 | 106 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 10:5a2068884fd9 | 107 | lcd_.FillRect(X_-sizeS/2, Y_-SIZE_/2, L_+sizeS+1, SIZE_+1); |
MikamiUitOpen | 0:a2686ef737c2 | 108 | else |
MikamiUitOpen | 10:5a2068884fd9 | 109 | lcd_.FillRect(X_-SIZE_/2, Y_-sizeS/2, SIZE_+1, L_+sizeS+1); |
MikamiUitOpen | 5:9c3ea4d4bc6b | 110 | |
MikamiUitOpen | 9:c379410bda15 | 111 | v_ = Saturate(value); // current value |
MikamiUitOpen | 9:c379410bda15 | 112 | Point pt = ToPoint(v_); // Position of thumb |
MikamiUitOpen | 0:a2686ef737c2 | 113 | |
MikamiUitOpen | 0:a2686ef737c2 | 114 | // Draw upper line |
MikamiUitOpen | 5:9c3ea4d4bc6b | 115 | if (active_) lcd_.SetTextColor(COLOR_H_); |
MikamiUitOpen | 5:9c3ea4d4bc6b | 116 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x404040); |
MikamiUitOpen | 12:687ec6183385 | 117 | if ((ORIENT_ == Holizontal) && ((X_+L_-pt.x) > 0)) |
MikamiUitOpen | 0:a2686ef737c2 | 118 | lcd_.FillRect(pt.x, Y_-W_/4, X_+L_-pt.x, W_/2); |
MikamiUitOpen | 12:687ec6183385 | 119 | if ((ORIENT_ == Vertical) && ((pt.y-Y_) > 0)) |
MikamiUitOpen | 0:a2686ef737c2 | 120 | lcd_.FillRect(X_-W_/4, Y_, W_/2, pt.y-Y_); |
MikamiUitOpen | 0:a2686ef737c2 | 121 | |
MikamiUitOpen | 0:a2686ef737c2 | 122 | // Draw lower line |
MikamiUitOpen | 5:9c3ea4d4bc6b | 123 | if (active_) lcd_.SetTextColor(COLOR_L_); |
MikamiUitOpen | 5:9c3ea4d4bc6b | 124 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x202020); |
MikamiUitOpen | 0:a2686ef737c2 | 125 | if ((ORIENT_ == Holizontal) && ((pt.x-X_) > 0)) |
MikamiUitOpen | 0:a2686ef737c2 | 126 | lcd_.FillRect(X_, Y_-W_/2, pt.x-X_, W_); |
MikamiUitOpen | 0:a2686ef737c2 | 127 | if ((ORIENT_ == Vertical) && ((Y_+L_-pt.y) > 0)) |
MikamiUitOpen | 0:a2686ef737c2 | 128 | lcd_.FillRect(X_-W_/2, pt.y, W_, Y_+L_-pt.y); |
MikamiUitOpen | 0:a2686ef737c2 | 129 | |
MikamiUitOpen | 0:a2686ef737c2 | 130 | // Draw thumb |
MikamiUitOpen | 5:9c3ea4d4bc6b | 131 | if (active_) lcd_.SetTextColor(CREATED_COLOR_); |
MikamiUitOpen | 5:9c3ea4d4bc6b | 132 | else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_); |
MikamiUitOpen | 10:5a2068884fd9 | 133 | uint16_t width = SIZE_; |
MikamiUitOpen | 10:5a2068884fd9 | 134 | uint16_t height = SIZE_; |
MikamiUitOpen | 10:5a2068884fd9 | 135 | if (ORIENT_ == Holizontal) width = sizeS; |
MikamiUitOpen | 10:5a2068884fd9 | 136 | else height = sizeS; |
MikamiUitOpen | 10:5a2068884fd9 | 137 | uint16_t xPos = pt.x - width/2; |
MikamiUitOpen | 10:5a2068884fd9 | 138 | uint16_t yPos = pt.y - height/2; |
MikamiUitOpen | 10:5a2068884fd9 | 139 | |
MikamiUitOpen | 0:a2686ef737c2 | 140 | if (fill) |
MikamiUitOpen | 10:5a2068884fd9 | 141 | lcd_.FillRect(xPos, yPos, width+1, height+1); |
MikamiUitOpen | 0:a2686ef737c2 | 142 | else |
MikamiUitOpen | 10:5a2068884fd9 | 143 | { |
MikamiUitOpen | 10:5a2068884fd9 | 144 | lcd_.DrawRect(xPos, yPos, width, height); |
MikamiUitOpen | 10:5a2068884fd9 | 145 | lcd_.DrawHLine(pt.x+width/2, pt.y+height/2, 1); |
MikamiUitOpen | 12:687ec6183385 | 146 | lcd_.DrawRect(xPos+1, yPos+1, width-2, height-2); |
MikamiUitOpen | 12:687ec6183385 | 147 | lcd_.DrawHLine(pt.x+width/2-1, pt.y+height/2-1, 1); |
MikamiUitOpen | 10:5a2068884fd9 | 148 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 12:687ec6183385 | 149 | lcd_.DrawVLine(pt.x, yPos+4, SIZE_-7); |
MikamiUitOpen | 10:5a2068884fd9 | 150 | else |
MikamiUitOpen | 12:687ec6183385 | 151 | lcd_.DrawHLine(xPos+4, pt.y, SIZE_-7); |
MikamiUitOpen | 10:5a2068884fd9 | 152 | } |
MikamiUitOpen | 0:a2686ef737c2 | 153 | } |
MikamiUitOpen | 0:a2686ef737c2 | 154 | |
MikamiUitOpen | 15:0511a08a3c09 | 155 | void SeekBar::Redraw(bool fill) |
MikamiUitOpen | 15:0511a08a3c09 | 156 | { |
MikamiUitOpen | 15:0511a08a3c09 | 157 | Draw(GetValue(), fill); |
MikamiUitOpen | 15:0511a08a3c09 | 158 | if (labelOn_) |
MikamiUitOpen | 15:0511a08a3c09 | 159 | for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_); |
MikamiUitOpen | 15:0511a08a3c09 | 160 | } |
MikamiUitOpen | 15:0511a08a3c09 | 161 | |
MikamiUitOpen | 0:a2686ef737c2 | 162 | SeekBar::Point SeekBar::ToPoint(float value) |
MikamiUitOpen | 0:a2686ef737c2 | 163 | { |
MikamiUitOpen | 0:a2686ef737c2 | 164 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 10:5a2068884fd9 | 165 | return Point(X_ + Round(L_*(value - MIN_)/(MAX_ - MIN_)), Y_); |
MikamiUitOpen | 0:a2686ef737c2 | 166 | else |
MikamiUitOpen | 10:5a2068884fd9 | 167 | return Point(X_, Y_ + L_ - Round(L_*(value - MIN_)/(MAX_ - MIN_))); |
MikamiUitOpen | 0:a2686ef737c2 | 168 | } |
MikamiUitOpen | 0:a2686ef737c2 | 169 | |
MikamiUitOpen | 0:a2686ef737c2 | 170 | float SeekBar::ToValue(Point pt) |
MikamiUitOpen | 0:a2686ef737c2 | 171 | { |
MikamiUitOpen | 0:a2686ef737c2 | 172 | float value; |
MikamiUitOpen | 0:a2686ef737c2 | 173 | if (ORIENT_ == Holizontal) |
MikamiUitOpen | 0:a2686ef737c2 | 174 | value = (pt.x - X_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
MikamiUitOpen | 0:a2686ef737c2 | 175 | else |
MikamiUitOpen | 0:a2686ef737c2 | 176 | value = -(pt.y - Y_ - L_)*(MAX_ - MIN_)/(float)L_ + MIN_; |
MikamiUitOpen | 0:a2686ef737c2 | 177 | return Saturate(value); |
MikamiUitOpen | 0:a2686ef737c2 | 178 | } |
MikamiUitOpen | 0:a2686ef737c2 | 179 | |
MikamiUitOpen | 0:a2686ef737c2 | 180 | float SeekBar::Saturate(float value) |
MikamiUitOpen | 0:a2686ef737c2 | 181 | { |
MikamiUitOpen | 0:a2686ef737c2 | 182 | if (value < MIN_) value = MIN_; |
MikamiUitOpen | 0:a2686ef737c2 | 183 | if (value > MAX_) value = MAX_; |
MikamiUitOpen | 0:a2686ef737c2 | 184 | return value; |
MikamiUitOpen | 0:a2686ef737c2 | 185 | } |
MikamiUitOpen | 0:a2686ef737c2 | 186 | } |
MikamiUitOpen | 16:b2762ec08863 | 187 |