Foundation classes for a basic GUI implementing simple widgets and events. (Fork for custom changes.)

Fork of SimpleGUI by Duncan McIntyre

Revision:
12:63db16fea709
Parent:
11:b485561aa112
Child:
13:6714534e7974
--- a/Widgets/SpinnerWidget.cpp	Fri Apr 22 16:12:42 2016 +0000
+++ b/Widgets/SpinnerWidget.cpp	Sun May 08 14:42:08 2016 +0000
@@ -1,54 +1,143 @@
 #include "SpinnerWidget.h"
 
-#include"resources/spinner-upArrow.bmp"
-#include"resources/spinner-downArrow.bmp"
+#include"resources/up_arrow_32x19_bmp.h"
+#include"resources/down_arrow_32x19_bmp.h"
 
-SpinnerWidget::SpinnerWidget(GUI* gui) : ContainerWidget(gui),
-  _upArrow(gui), _downArrow(gui), _text(gui), _min(0), _max(0), _increment(1), _value(0)
+SpinnerWidget::SpinnerWidget(GraphicsContext *context)
+    : ContainerWidget(context),
+      _upArrow(context), _text(context), _downArrow(context),
+      _min(0), _max(0), _increment(0.5), _value(0),
+      _format(""), _buf("")
 {
-    _upArrow.setBitmap(spinner_upArrow_bmp);   
-    _upArrow.setSize(10,10);
-    
-    _downArrow.setBitmap(spinner_downArrow_bmp);
-    _downArrow.setSize(10,10);
-    
-    _text.setSize(gui->defaultFont()->width() * 4, gui->defaultFont()->height());
-    
-    append(_upArrow);
-    append(_downArrow);
-    append(_text);
-    
+
+    setLayout(VERTICAL_CENTER);
+
+    _upArrow.setBitmap(up_arrow_32x19_bmp, 32, 19);
+    _upArrow.setForeground(White);
+    _upArrow.setBackground(Black);
+    _upArrow.setMonochrome(true);
+
+    _downArrow.setBitmap(down_arrow_32x19_bmp, 32, 19);
+    _downArrow.setForeground(White);
+    _downArrow.setBackground(Black);
+    _downArrow.setMonochrome(true);
+
+    _text.setSize(32,32);
+    _text.setBorder(1,Green);
+    _text.setForeground(White);
+    _text.setBackground(Black);
+
+    EventHandler* up = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onUpClick);
+    EventHandler* down = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onDownClick);
+
+    attach(&_upArrow);
+    attach(&_text);
+    attach(&_downArrow);
+
+    _upArrow.setEventHandler(up);
+    _downArrow.setEventHandler(down);
 }
 
 void SpinnerWidget::setMin(float min)
 {
+    if(_min != min) {
+        _min = min;
+        dirty();
+    }
 }
 
 void SpinnerWidget::setMax(float max)
 {
+    if(_max != max) {
+        _max = max;
+        dirty();
+    }
 }
 
 void SpinnerWidget::setIncrement(float increment)
 {
+    if(_increment != increment) {
+        _increment = increment;
+        dirty();
+    }
 }
 
 void SpinnerWidget::setValue(float value)
 {
+    if(_value != value) {
+        _value = value;
+        dirty();
+    }
+}
+
+void SpinnerWidget::setFormat(const char* format)
+{
+    _format = format;
+    dirty();
+}
+
+
+float SpinnerWidget::getMin()
+{
+    return _min;
 }
 
-void SpinnerWidget::getValue()
+float SpinnerWidget::getMax()
+{
+    return _max;
+}
+
+float SpinnerWidget::getIncrement()
 {
+    return _increment;
+}
+
+float SpinnerWidget::getValue()
+{
+    return _value;
+}
+
+const char* SpinnerWidget::getFormat()
+{
+    return _format;
 }
 
 template<typename T>
-void SpinnerWidget::onChange(T* tptr, void (T::*mptr)(SpinnerWidget*))
+void SpinnerWidget::onChange(T* tptr, void (T::*mptr)(Event))
 {
     _onChange.attach(tptr, mptr);
 }
 
+void SpinnerWidget::setSize(int width, int height)
+{
+    _text.setSize(width, _text.height());
+    ContainerWidget::setSize(width, height);
+}
 
-void SpinnerWidget::_onUpClick() {
+void SpinnerWidget::_dirty()
+{
+
+    sprintf(_buf, _format, _value);
+    _text.setText(_buf);
+
+    ContainerWidget::_dirty();
 }
-    
-void SpinnerWidget::_onDownClick() {
+
+
+void SpinnerWidget::_onUpClick(Event e)
+{
+    _value += _increment;
+    if(_value > _max) {
+        _value = _max;
+    }
+    dirty();
+}
+
+void SpinnerWidget::_onDownClick(Event e)
+{
+    _value -= _increment;
+    if(_value < _min) {
+        _value = _min;
+    }
+    dirty();
 }
\ No newline at end of file