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