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

Dependents:   F746_SD_GraphicEqualizer_ren0620

Fork of F746_GUI by 不韋 呂

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SeekBar.cpp Source File

SeekBar.cpp

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