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
Diff: SeekBar.cpp
- Revision:
- 0:a2686ef737c2
- Child:
- 5:9c3ea4d4bc6b
diff -r 000000000000 -r a2686ef737c2 SeekBar.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SeekBar.cpp Thu Mar 31 07:28:42 2016 +0000 @@ -0,0 +1,113 @@ +//----------------------------------------------------------- +// SeekBar class +// +// 2016/03/29, Copyright (c) 2016 MIKAMI, Naoki +//----------------------------------------------------------- + +#include "SeekBar.hpp" + +namespace Mikami +{ + // Slide thumb + // If the thumb is not touched, return false + bool SeekBar::Slide() + { + if (!PanelTouched()) + { + if (slided_) Draw(v_); + slided_ = false; + return false; + } + + uint16_t x, y; + bool rtn = IsOnThumb(x, y); + if (rtn) + { + v_ = ToValue(Point(x, y)); + Draw(v_, true); + slided_ = true; + } + + return rtn; + } + + // Draw seekbar + void SeekBar::Draw(float value, bool fill) + { + // Erase previous seekbar + lcd_.SetTextColor(BACK_COLOR_); + if (ORIENT_ == Holizontal) + lcd_.FillRect(X_-SIZE_/2, Y_-SIZE_/2, L_+SIZE_+1, SIZE_+1); + else + lcd_.FillRect(X_-SIZE_/2, Y_-SIZE_/2, SIZE_+1, L_+SIZE_+1); + + Point pt = ToPoint(Saturate(value)); // Position of thumb + + // Draw upper line + lcd_.SetTextColor(COLOR_H_); + if (ORIENT_ == Holizontal) + lcd_.FillRect(pt.x, Y_-W_/4, X_+L_-pt.x, W_/2); + else + lcd_.FillRect(X_-W_/4, Y_, W_/2, pt.y-Y_); + + // Draw lower line + lcd_.SetTextColor(COLOR_L_); + if ((ORIENT_ == Holizontal) && ((pt.x-X_) > 0)) + lcd_.FillRect(X_, Y_-W_/2, pt.x-X_, W_); + if ((ORIENT_ == Vertical) && ((Y_+L_-pt.y) > 0)) + lcd_.FillRect(X_-W_/2, pt.y, W_, Y_+L_-pt.y); + + // Draw thumb + lcd_.SetTextColor(CREATED_COLOR_); + if (fill) + lcd_.FillCircle(pt.x, pt.y, SIZE_/2); + else + lcd_.DrawCircle(pt.x, pt.y, SIZE_/2); + } + + // If touched position is on the button, return true + bool SeekBar::IsOnThumb(uint16_t &x, uint16_t &y) + { + x = state_.touchX[0]; + y = state_.touchY[0]; + + Point pt = ToPoint(v_); + if (ORIENT_ == Holizontal) + { + if ( (pt.x-SIZE_/5 <= x) && (x <= pt.x+SIZE_/5) && + (pt.y-SIZE_ <= y) && (y <= pt.y+SIZE_) ) return true; + } + else + { + if ( (pt.x-SIZE_ <= x) && (x <= pt.x+SIZE_) && + (pt.y-SIZE_/5 <= y) && (y <= pt.y+SIZE_/5) ) return true; + } + + return false; + } + + SeekBar::Point SeekBar::ToPoint(float value) + { + if (ORIENT_ == Holizontal) + return Point(X_ + L_*(value - MIN_)/(MAX_ - MIN_), Y_); + else + return Point(X_, Y_ + L_ - L_*(value - MIN_)/(MAX_ - MIN_)); + } + + float SeekBar::ToValue(Point pt) + { + float value; + if (ORIENT_ == Holizontal) + value = (pt.x - X_)*(MAX_ - MIN_)/(float)L_ + MIN_; + else + value = -(pt.y - Y_ - L_)*(MAX_ - MIN_)/(float)L_ + MIN_; + return Saturate(value); + } + + float SeekBar::Saturate(float value) + { + if (value < MIN_) value = MIN_; + if (value > MAX_) value = MAX_; + return value; + } +}