GUI parts for DISCO-F746NG. GuiBase, Button, ButtonGroup, Label, BlinkLabel, NumericLabel, SeekBar, SeekbarGroup
Dependents: F746_SD_GraphicEqualizer_ren0620
Fork of F746_GUI by
SeekBar.cpp
- Committer:
- MikamiUitOpen
- Date:
- 2016-03-31
- Revision:
- 0:a2686ef737c2
- Child:
- 5:9c3ea4d4bc6b
File content as of revision 0:a2686ef737c2:
//-----------------------------------------------------------
// 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;
}
}
