Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Widgets/SpinnerWidget.cpp@12:63db16fea709, 2016-05-08 (annotated)
- Committer:
- duncanFrance
- Date:
- Sun May 08 14:42:08 2016 +0000
- Revision:
- 12:63db16fea709
- Parent:
- 11:b485561aa112
- Child:
- 13:6714534e7974
Update to handle dirty pixels and geometric change/damage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 11:b485561aa112 | 1 | #include "SpinnerWidget.h" |
duncanFrance | 11:b485561aa112 | 2 | |
duncanFrance | 12:63db16fea709 | 3 | #include"resources/up_arrow_32x19_bmp.h" |
duncanFrance | 12:63db16fea709 | 4 | #include"resources/down_arrow_32x19_bmp.h" |
duncanFrance | 11:b485561aa112 | 5 | |
duncanFrance | 12:63db16fea709 | 6 | SpinnerWidget::SpinnerWidget(GraphicsContext *context) |
duncanFrance | 12:63db16fea709 | 7 | : ContainerWidget(context), |
duncanFrance | 12:63db16fea709 | 8 | _upArrow(context), _text(context), _downArrow(context), |
duncanFrance | 12:63db16fea709 | 9 | _min(0), _max(0), _increment(0.5), _value(0), |
duncanFrance | 12:63db16fea709 | 10 | _format(""), _buf("") |
duncanFrance | 11:b485561aa112 | 11 | { |
duncanFrance | 12:63db16fea709 | 12 | |
duncanFrance | 12:63db16fea709 | 13 | setLayout(VERTICAL_CENTER); |
duncanFrance | 12:63db16fea709 | 14 | |
duncanFrance | 12:63db16fea709 | 15 | _upArrow.setBitmap(up_arrow_32x19_bmp, 32, 19); |
duncanFrance | 12:63db16fea709 | 16 | _upArrow.setForeground(White); |
duncanFrance | 12:63db16fea709 | 17 | _upArrow.setBackground(Black); |
duncanFrance | 12:63db16fea709 | 18 | _upArrow.setMonochrome(true); |
duncanFrance | 12:63db16fea709 | 19 | |
duncanFrance | 12:63db16fea709 | 20 | _downArrow.setBitmap(down_arrow_32x19_bmp, 32, 19); |
duncanFrance | 12:63db16fea709 | 21 | _downArrow.setForeground(White); |
duncanFrance | 12:63db16fea709 | 22 | _downArrow.setBackground(Black); |
duncanFrance | 12:63db16fea709 | 23 | _downArrow.setMonochrome(true); |
duncanFrance | 12:63db16fea709 | 24 | |
duncanFrance | 12:63db16fea709 | 25 | _text.setSize(32,32); |
duncanFrance | 12:63db16fea709 | 26 | _text.setBorder(1,Green); |
duncanFrance | 12:63db16fea709 | 27 | _text.setForeground(White); |
duncanFrance | 12:63db16fea709 | 28 | _text.setBackground(Black); |
duncanFrance | 12:63db16fea709 | 29 | |
duncanFrance | 12:63db16fea709 | 30 | EventHandler* up = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onUpClick); |
duncanFrance | 12:63db16fea709 | 31 | EventHandler* down = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onDownClick); |
duncanFrance | 12:63db16fea709 | 32 | |
duncanFrance | 12:63db16fea709 | 33 | attach(&_upArrow); |
duncanFrance | 12:63db16fea709 | 34 | attach(&_text); |
duncanFrance | 12:63db16fea709 | 35 | attach(&_downArrow); |
duncanFrance | 12:63db16fea709 | 36 | |
duncanFrance | 12:63db16fea709 | 37 | _upArrow.setEventHandler(up); |
duncanFrance | 12:63db16fea709 | 38 | _downArrow.setEventHandler(down); |
duncanFrance | 11:b485561aa112 | 39 | } |
duncanFrance | 11:b485561aa112 | 40 | |
duncanFrance | 11:b485561aa112 | 41 | void SpinnerWidget::setMin(float min) |
duncanFrance | 11:b485561aa112 | 42 | { |
duncanFrance | 12:63db16fea709 | 43 | if(_min != min) { |
duncanFrance | 12:63db16fea709 | 44 | _min = min; |
duncanFrance | 12:63db16fea709 | 45 | dirty(); |
duncanFrance | 12:63db16fea709 | 46 | } |
duncanFrance | 11:b485561aa112 | 47 | } |
duncanFrance | 11:b485561aa112 | 48 | |
duncanFrance | 11:b485561aa112 | 49 | void SpinnerWidget::setMax(float max) |
duncanFrance | 11:b485561aa112 | 50 | { |
duncanFrance | 12:63db16fea709 | 51 | if(_max != max) { |
duncanFrance | 12:63db16fea709 | 52 | _max = max; |
duncanFrance | 12:63db16fea709 | 53 | dirty(); |
duncanFrance | 12:63db16fea709 | 54 | } |
duncanFrance | 11:b485561aa112 | 55 | } |
duncanFrance | 11:b485561aa112 | 56 | |
duncanFrance | 11:b485561aa112 | 57 | void SpinnerWidget::setIncrement(float increment) |
duncanFrance | 11:b485561aa112 | 58 | { |
duncanFrance | 12:63db16fea709 | 59 | if(_increment != increment) { |
duncanFrance | 12:63db16fea709 | 60 | _increment = increment; |
duncanFrance | 12:63db16fea709 | 61 | dirty(); |
duncanFrance | 12:63db16fea709 | 62 | } |
duncanFrance | 11:b485561aa112 | 63 | } |
duncanFrance | 11:b485561aa112 | 64 | |
duncanFrance | 11:b485561aa112 | 65 | void SpinnerWidget::setValue(float value) |
duncanFrance | 11:b485561aa112 | 66 | { |
duncanFrance | 12:63db16fea709 | 67 | if(_value != value) { |
duncanFrance | 12:63db16fea709 | 68 | _value = value; |
duncanFrance | 12:63db16fea709 | 69 | dirty(); |
duncanFrance | 12:63db16fea709 | 70 | } |
duncanFrance | 12:63db16fea709 | 71 | } |
duncanFrance | 12:63db16fea709 | 72 | |
duncanFrance | 12:63db16fea709 | 73 | void SpinnerWidget::setFormat(const char* format) |
duncanFrance | 12:63db16fea709 | 74 | { |
duncanFrance | 12:63db16fea709 | 75 | _format = format; |
duncanFrance | 12:63db16fea709 | 76 | dirty(); |
duncanFrance | 12:63db16fea709 | 77 | } |
duncanFrance | 12:63db16fea709 | 78 | |
duncanFrance | 12:63db16fea709 | 79 | |
duncanFrance | 12:63db16fea709 | 80 | float SpinnerWidget::getMin() |
duncanFrance | 12:63db16fea709 | 81 | { |
duncanFrance | 12:63db16fea709 | 82 | return _min; |
duncanFrance | 11:b485561aa112 | 83 | } |
duncanFrance | 11:b485561aa112 | 84 | |
duncanFrance | 12:63db16fea709 | 85 | float SpinnerWidget::getMax() |
duncanFrance | 12:63db16fea709 | 86 | { |
duncanFrance | 12:63db16fea709 | 87 | return _max; |
duncanFrance | 12:63db16fea709 | 88 | } |
duncanFrance | 12:63db16fea709 | 89 | |
duncanFrance | 12:63db16fea709 | 90 | float SpinnerWidget::getIncrement() |
duncanFrance | 11:b485561aa112 | 91 | { |
duncanFrance | 12:63db16fea709 | 92 | return _increment; |
duncanFrance | 12:63db16fea709 | 93 | } |
duncanFrance | 12:63db16fea709 | 94 | |
duncanFrance | 12:63db16fea709 | 95 | float SpinnerWidget::getValue() |
duncanFrance | 12:63db16fea709 | 96 | { |
duncanFrance | 12:63db16fea709 | 97 | return _value; |
duncanFrance | 12:63db16fea709 | 98 | } |
duncanFrance | 12:63db16fea709 | 99 | |
duncanFrance | 12:63db16fea709 | 100 | const char* SpinnerWidget::getFormat() |
duncanFrance | 12:63db16fea709 | 101 | { |
duncanFrance | 12:63db16fea709 | 102 | return _format; |
duncanFrance | 11:b485561aa112 | 103 | } |
duncanFrance | 11:b485561aa112 | 104 | |
duncanFrance | 11:b485561aa112 | 105 | template<typename T> |
duncanFrance | 12:63db16fea709 | 106 | void SpinnerWidget::onChange(T* tptr, void (T::*mptr)(Event)) |
duncanFrance | 11:b485561aa112 | 107 | { |
duncanFrance | 11:b485561aa112 | 108 | _onChange.attach(tptr, mptr); |
duncanFrance | 11:b485561aa112 | 109 | } |
duncanFrance | 11:b485561aa112 | 110 | |
duncanFrance | 12:63db16fea709 | 111 | void SpinnerWidget::setSize(int width, int height) |
duncanFrance | 12:63db16fea709 | 112 | { |
duncanFrance | 12:63db16fea709 | 113 | _text.setSize(width, _text.height()); |
duncanFrance | 12:63db16fea709 | 114 | ContainerWidget::setSize(width, height); |
duncanFrance | 12:63db16fea709 | 115 | } |
duncanFrance | 11:b485561aa112 | 116 | |
duncanFrance | 12:63db16fea709 | 117 | void SpinnerWidget::_dirty() |
duncanFrance | 12:63db16fea709 | 118 | { |
duncanFrance | 12:63db16fea709 | 119 | |
duncanFrance | 12:63db16fea709 | 120 | sprintf(_buf, _format, _value); |
duncanFrance | 12:63db16fea709 | 121 | _text.setText(_buf); |
duncanFrance | 12:63db16fea709 | 122 | |
duncanFrance | 12:63db16fea709 | 123 | ContainerWidget::_dirty(); |
duncanFrance | 11:b485561aa112 | 124 | } |
duncanFrance | 12:63db16fea709 | 125 | |
duncanFrance | 12:63db16fea709 | 126 | |
duncanFrance | 12:63db16fea709 | 127 | void SpinnerWidget::_onUpClick(Event e) |
duncanFrance | 12:63db16fea709 | 128 | { |
duncanFrance | 12:63db16fea709 | 129 | _value += _increment; |
duncanFrance | 12:63db16fea709 | 130 | if(_value > _max) { |
duncanFrance | 12:63db16fea709 | 131 | _value = _max; |
duncanFrance | 12:63db16fea709 | 132 | } |
duncanFrance | 12:63db16fea709 | 133 | dirty(); |
duncanFrance | 12:63db16fea709 | 134 | } |
duncanFrance | 12:63db16fea709 | 135 | |
duncanFrance | 12:63db16fea709 | 136 | void SpinnerWidget::_onDownClick(Event e) |
duncanFrance | 12:63db16fea709 | 137 | { |
duncanFrance | 12:63db16fea709 | 138 | _value -= _increment; |
duncanFrance | 12:63db16fea709 | 139 | if(_value < _min) { |
duncanFrance | 12:63db16fea709 | 140 | _value = _min; |
duncanFrance | 12:63db16fea709 | 141 | } |
duncanFrance | 12:63db16fea709 | 142 | dirty(); |
duncanFrance | 11:b485561aa112 | 143 | } |