New IHM
Dependencies: Array_Matrix TS_DISCO_F746NG LCD_DISCO_F746NG BSP_DISCO_F746NG
SeekBar.cpp
- Committer:
- MikamiUitOpen
- Date:
- 2016-04-12
- Revision:
- 6:b8f197b0012c
- Parent:
- 5:9c3ea4d4bc6b
- Child:
- 9:c379410bda15
File content as of revision 6:b8f197b0012c:
//----------------------------------------------------------- // SeekBar class // // 2016/04/12, Copyright (c) 2016 MIKAMI, Naoki //----------------------------------------------------------- #include "SeekBar.hpp" namespace Mikami { // Constructor with scale value (only horizontal) SeekBar::SeekBar(uint16_t x, uint16_t y, uint16_t length, float min, float max, float initialValue, string left, string center, string right, uint32_t thumbColor, uint16_t thumbSize, uint16_t width, uint32_t colorL, uint32_t colorH, uint32_t backColor) : GuiBase(x, y, Font12, GuiBase::ENUM_TEXT, backColor, thumbColor), L_(length), W_(width), SIZE_(thumbSize), COLOR_L_(colorL), COLOR_H_(colorH), MIN_(min), MAX_(max), ORIENT_(Holizontal), v_(initialValue), active_(true) { Draw(initialValue); labelL = new Label(x, y-28, left, Label::CENTER); labelC = new Label(x+length/2, y-28, center, Label::CENTER); labelR = new Label(x+length, y-28, right, Label::CENTER); } // Slide thumb // If the thumb is not touched, return false bool SeekBar::Slide() { if (!active_) return false; 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; } void SeekBar::Activate() { active_ = true; Draw(v_); if (labelL != NULL) { labelL->Draw(TEXT_COLOR_); labelC->Draw(TEXT_COLOR_); labelR->Draw(TEXT_COLOR_); } } void SeekBar::Inactivate() { active_ = false; Draw(v_); if (labelL != NULL) { labelL->Draw(INACTIVE_TEXT_COLOR_); labelC->Draw(INACTIVE_TEXT_COLOR_); labelR->Draw(INACTIVE_TEXT_COLOR_); } } // 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 if (active_) lcd_.SetTextColor(COLOR_H_); else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x404040); 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 if (active_) lcd_.SetTextColor(COLOR_L_); else lcd_.SetTextColor(INACTIVE_TEXT_COLOR_-0x202020); 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 if (active_) lcd_.SetTextColor(CREATED_COLOR_); else lcd_.SetTextColor(INACTIVE_TEXT_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; } }