GUI parts for DISCO-F746NG. GuiBase, Button, ButtonGroup, ResetButton, Label, BlinkLabel, NumericLabel, SeekBar, SeekbarGroup, NumericUpDown

Dependencies:   Array_Matrix BSP_DISCO_F746NG LCD_DISCO_F746NG TS_DISCO_F746NG

Dependents:   F746_AudioOutQSPI F746_AudioPlayerSD DISCO-F746NG_test001 F746_SD_WavPlayer ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SeekBar.cpp Source File

SeekBar.cpp

00001 //-----------------------------------------------------------
00002 //  SeekBar class
00003 //
00004 //  2017/01/25, Copyright (c) 2017 MIKAMI, Naoki
00005 //-----------------------------------------------------------
00006 
00007 #include "SeekBar.hpp"
00008 
00009 namespace Mikami
00010 {
00011     // Constructor with scale value (only horizontal)
00012     SeekBar::SeekBar(uint16_t x, uint16_t y, uint16_t length,
00013                      float min, float max, float initialValue,
00014                      string left, string center, string right,
00015                      uint32_t thumbColor,
00016                      uint16_t thumbSize, uint16_t width,
00017                      uint32_t colorL, uint32_t colorH,
00018                      uint32_t backColor)
00019         : GuiBase(x, y, Font12, GuiBase::ENUM_TEXT, backColor, thumbColor),
00020           L_(length), W_(width),
00021           SIZE_(thumbSize), COLOR_L_(colorL), COLOR_H_(colorH),
00022           MIN_(min), MAX_(max), ORIENT_(Holizontal), v_(initialValue),
00023           labelOn_(true), slided_(false), active_(true)
00024     {
00025         Draw(initialValue);
00026         uint16_t y0 = y - thumbSize/2 - 13;
00027         labelLCR_[0] = new Label(x, y0, left, Label::CENTER);
00028         labelLCR_[1] = new Label(x+length/2, y0, center, Label::CENTER);
00029         labelLCR_[2] = new Label(x+length, y0, right, Label::CENTER);
00030     }
00031 
00032     // Slide thumb
00033     //      If the thumb is not touched, return false
00034     bool SeekBar::Slide()
00035     {
00036         if (!active_) return false;
00037 
00038         if (!PanelTouched())
00039         {
00040             if (slided_) Draw(v_);
00041             slided_ = false;
00042             return false;
00043         }
00044 
00045         uint16_t x, y;
00046         bool rtn = IsOnThumb(x, y);
00047         if (rtn || slided_)
00048         {
00049             if (rtn) v_ = ToValue(Point(x, y));
00050             Draw(v_, rtn);
00051             slided_ = rtn;
00052         }
00053         return rtn;
00054     }
00055 
00056     void SeekBar::Activate()
00057     {
00058         active_ = true;
00059         Draw(v_);
00060         if (labelOn_)
00061             for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_);
00062     }
00063 
00064     void SeekBar::Inactivate()
00065     {
00066         active_ = false;
00067         Draw(v_);
00068         if (labelOn_)
00069             for (int n=0; n<3; n++) labelLCR_[n]->Draw(INACTIVE_TEXT_COLOR_);
00070     }
00071 
00072     // If touched position is on the thumb, return true
00073     bool SeekBar::IsOnThumb(uint16_t &x, uint16_t &y)
00074     {
00075         x = state_.touchX[0];
00076         y = state_.touchY[0];
00077 
00078         uint16_t th = SIZE_/2;
00079         Point pt = ToPoint(v_);
00080         if (ORIENT_ == Holizontal)
00081         {
00082             if ( (pt.x-th <= x) && (x <= pt.x+th) &&
00083                  (pt.y-th <= y) && (y <= pt.y+th) ) return true;
00084         }
00085         else
00086         {
00087             if ( (pt.x-th <= x) && (x <= pt.x+th) &&
00088                  (pt.y-th <= y) && (y <= pt.y+th) ) return true;
00089         }
00090 
00091         return false;
00092     }
00093 
00094     // Draw seekbar
00095     void SeekBar::Draw(float value, bool fill)
00096     {
00097         uint16_t sizeS = (uint16_t)(SIZE_*0.6f);
00098         // Erase previous seekbar
00099         lcd_.SetTextColor(BACK_COLOR_);
00100         if (ORIENT_ == Holizontal)
00101             lcd_.FillRect(X_-sizeS/2, Y_-SIZE_/2, L_+sizeS+1, SIZE_+1);
00102         else
00103             lcd_.FillRect(X_-SIZE_/2, Y_-sizeS/2, SIZE_+1, L_+sizeS+1);
00104 
00105         v_ = Saturate(value);       // current value
00106         Point pt = ToPoint(v_);     // Position of thumb
00107 
00108         // Draw upper line
00109         if (active_) lcd_.SetTextColor(COLOR_H_);
00110         else         lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x404040);
00111         if ((ORIENT_ == Holizontal) && ((X_+L_-pt.x) > 0))
00112             lcd_.FillRect(pt.x, Y_-W_/4, X_+L_-pt.x, W_/2);
00113         if ((ORIENT_ == Vertical) && ((pt.y-Y_) > 0))
00114             lcd_.FillRect(X_-W_/4, Y_, W_/2, pt.y-Y_);
00115 
00116         // Draw lower line
00117         if (active_) lcd_.SetTextColor(COLOR_L_);
00118         else         lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x202020);
00119         if ((ORIENT_ == Holizontal) && ((pt.x-X_) > 0))
00120             lcd_.FillRect(X_, Y_-W_/2, pt.x-X_, W_);
00121         if ((ORIENT_ == Vertical) && ((Y_+L_-pt.y) > 0))
00122             lcd_.FillRect(X_-W_/2, pt.y, W_, Y_+L_-pt.y);
00123 
00124         // Draw thumb
00125         if (active_) lcd_.SetTextColor(CREATED_COLOR_);
00126         else         lcd_.SetTextColor(INACTIVE_TEXT_COLOR_);
00127         uint16_t width = SIZE_;
00128         uint16_t height = SIZE_;
00129         if (ORIENT_ == Holizontal) width = sizeS;
00130         else                       height = sizeS;
00131         uint16_t xPos = pt.x - width/2;
00132         uint16_t yPos = pt.y - height/2;
00133         
00134         if (fill)
00135             lcd_.FillRect(xPos, yPos, width+1, height+1);
00136         else
00137         {
00138             lcd_.DrawRect(xPos, yPos, width, height);
00139             lcd_.DrawHLine(pt.x+width/2, pt.y+height/2, 1);
00140             lcd_.DrawRect(xPos+1, yPos+1, width-2, height-2);
00141             lcd_.DrawHLine(pt.x+width/2-1, pt.y+height/2-1, 1);
00142             if (ORIENT_ == Holizontal)
00143                 lcd_.DrawVLine(pt.x, yPos+4, SIZE_-7);
00144             else
00145                 lcd_.DrawHLine(xPos+4, pt.y, SIZE_-7);
00146         }
00147     }
00148 
00149     void SeekBar::Redraw(bool fill)
00150     {
00151         Draw(GetValue(), fill);
00152         if (labelOn_)
00153             for (int n=0; n<3; n++) labelLCR_[n]->Draw(TEXT_COLOR_);
00154     }
00155 
00156     SeekBar::Point SeekBar::ToPoint(float value)
00157     {
00158         if (ORIENT_ == Holizontal)
00159             return Point(X_ + Round(L_*(value - MIN_)/(MAX_ - MIN_)), Y_);
00160         else
00161             return Point(X_, Y_ + L_ - Round(L_*(value - MIN_)/(MAX_ - MIN_)));
00162     }
00163 
00164     float SeekBar::ToValue(Point pt)
00165     {
00166         float value;
00167         if (ORIENT_ == Holizontal)
00168             value = (pt.x - X_)*(MAX_ - MIN_)/(float)L_ + MIN_;
00169         else
00170             value = -(pt.y - Y_ - L_)*(MAX_ - MIN_)/(float)L_ + MIN_;
00171         return Saturate(value);
00172     }
00173 
00174     float SeekBar::Saturate(float value)
00175     {
00176         if (value < MIN_) value = MIN_;
00177         if (value > MAX_) value = MAX_;
00178         return value;
00179     }
00180 }
00181 
00182