el h / SimpleGUI

Fork of SimpleGUI by Duncan McIntyre

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SpinnerWidget.cpp Source File

SpinnerWidget.cpp

00001 #include "SpinnerWidget.h"
00002 
00003 #include"resources/BlueButtonLeft_50x64_bmp.h"
00004 #include"resources/BlueButtonRight_50x64_bmp.h"
00005 
00006 SpinnerWidget::SpinnerWidget(GraphicsContext *context)
00007     : ContainerWidget(context),
00008       _upArrow(context), _text(context), _downArrow(context),
00009       _min(0), _max(0), _increment(0.5), _value(0),
00010       _format(""), _buf("")
00011 {
00012 
00013     setLayout(FIXED);
00014     setBorder(1, White);
00015     setPadding(1);
00016     _upArrow.setBitmap(BlueButtonLeft_50x64_bmp, 50, 64);
00017     _downArrow.setBitmap(BlueButtonRight_50x64_bmp, 50, 64);
00018     
00019     // Calculate these here to take account of border and margin settings
00020     // since BitmapWidgets resize themselves rather than clip (!)
00021     setSize(
00022         context->display()->width(), 
00023         _upArrow.height() + 2*(_borderWidth+_padding)
00024     );
00025     
00026     _upArrow.setLocation(
00027         _borderWidth + _padding,
00028         _borderWidth + _padding
00029     );
00030     
00031     _downArrow.setLocation(
00032         width() - _downArrow.width() - (_borderWidth + _padding), 
00033         _borderWidth + _padding
00034     );
00035     
00036 
00037     int textWidth = width() 
00038         - _upArrow.width()  - 2*(_borderWidth + _padding)
00039         - _downArrow.width() - 2*(_borderWidth + _padding)
00040     ;
00041     
00042     int textHeight = height() - 2*(_borderWidth + _padding +1);
00043 
00044     _text.setSize(textWidth, textHeight);
00045     _text.setLocation(_downArrow.width() + _borderWidth + _padding, _borderWidth + _padding);
00046     _text.setForeground(White);
00047     _text.setBackground(Black);
00048     _text.setVAlign(TextWidget::MIDDLE);
00049 
00050     EventHandler* up = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onUpClick);
00051     EventHandler* down = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onDownClick);
00052 
00053     attach(&_upArrow);
00054     attach(&_text);
00055     attach(&_downArrow);
00056 
00057     _upArrow.setEventHandler(up);
00058     _downArrow.setEventHandler(down);
00059 }
00060 
00061 TextWidget *SpinnerWidget::getTextWidget() {
00062     return &_text;
00063 }
00064 
00065 void SpinnerWidget::setMin(float min)
00066 {
00067     if(_min != min) {
00068         _min = min;
00069         dirty();
00070     }
00071 }
00072 
00073 void SpinnerWidget::setMax(float max)
00074 {
00075     if(_max != max) {
00076         _max = max;
00077         dirty();
00078     }
00079 }
00080 
00081 void SpinnerWidget::setIncrement(float increment)
00082 {
00083     if(_increment != increment) {
00084         _increment = increment;
00085         dirty();
00086     }
00087 }
00088 
00089 void SpinnerWidget::setValue(float value)
00090 {
00091     if(_value != value) {
00092         _value = value;
00093         dirty();
00094     }
00095 }
00096 
00097 void SpinnerWidget::setFormat(const char* format)
00098 {
00099     _format = format;
00100     dirty();
00101 }
00102 
00103 
00104 float SpinnerWidget::getMin()
00105 {
00106     return _min;
00107 }
00108 
00109 float SpinnerWidget::getMax()
00110 {
00111     return _max;
00112 }
00113 
00114 float SpinnerWidget::getIncrement()
00115 {
00116     return _increment;
00117 }
00118 
00119 float SpinnerWidget::getValue()
00120 {
00121     return _value;
00122 }
00123 
00124 const char* SpinnerWidget::getFormat()
00125 {
00126     return _format;
00127 }
00128 
00129 template<typename T>
00130 void SpinnerWidget::onChange(T* tptr, void (T::*mptr)(Event))
00131 {
00132     _onChange.attach(tptr, mptr);
00133 }
00134 
00135 void SpinnerWidget::setSize(int width, int height)
00136 {
00137     ContainerWidget::setSize(width, height);
00138 }
00139 
00140 void SpinnerWidget::_dirty()
00141 {
00142 
00143     sprintf(_buf, _format, _value);
00144     _text.setText(_buf);
00145 
00146     ContainerWidget::_dirty();
00147 }
00148 
00149 
00150 void SpinnerWidget::_onUpClick(Event e)
00151 {
00152     _value += _increment;
00153     if(_value > _max) {
00154         _value = _max;
00155     }
00156     dirty();
00157 }
00158 
00159 void SpinnerWidget::_onDownClick(Event e)
00160 {
00161     _value -= _increment;
00162     if(_value < _min) {
00163         _value = _min;
00164     }
00165     dirty();
00166 }