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