Foundation classes for a basic GUI implementing simple widgets and events. (Fork for custom changes.)
Fork of SimpleGUI by
Widgets/SpinnerWidget.cpp@13:6714534e7974, 2016-05-21 (annotated)
- Committer:
- duncanFrance
- Date:
- Sat May 21 14:40:09 2016 +0000
- Revision:
- 13:6714534e7974
- Parent:
- 12:63db16fea709
- Child:
- 17:5184762fda6c
Cleaned up SpinnerWidget; Started on SpinnerDialog - will extract Dialog later
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 | 13:6714534e7974 | 3 | #include"resources/BlueButtonLeft_50x64_bmp.h" |
duncanFrance | 13:6714534e7974 | 4 | #include"resources/BlueButtonRight_50x64_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 | 13:6714534e7974 | 13 | setLayout(FIXED); |
duncanFrance | 13:6714534e7974 | 14 | setBorder(1, White); |
duncanFrance | 13:6714534e7974 | 15 | setPadding(1); |
duncanFrance | 13:6714534e7974 | 16 | _upArrow.setBitmap(BlueButtonLeft_50x64_bmp, 50, 64); |
duncanFrance | 13:6714534e7974 | 17 | _downArrow.setBitmap(BlueButtonRight_50x64_bmp, 50, 64); |
duncanFrance | 13:6714534e7974 | 18 | |
duncanFrance | 13:6714534e7974 | 19 | // Calculate these here to take account of border and margin settings |
duncanFrance | 13:6714534e7974 | 20 | // since BitmapWidgets resize themselves rather than clip (!) |
duncanFrance | 13:6714534e7974 | 21 | setSize( |
duncanFrance | 13:6714534e7974 | 22 | context->display()->width(), |
duncanFrance | 13:6714534e7974 | 23 | _upArrow.height() + 2*(_borderWidth+_padding) |
duncanFrance | 13:6714534e7974 | 24 | ); |
duncanFrance | 13:6714534e7974 | 25 | |
duncanFrance | 13:6714534e7974 | 26 | _upArrow.setLocation( |
duncanFrance | 13:6714534e7974 | 27 | _borderWidth + _padding, |
duncanFrance | 13:6714534e7974 | 28 | _borderWidth + _padding |
duncanFrance | 13:6714534e7974 | 29 | ); |
duncanFrance | 13:6714534e7974 | 30 | |
duncanFrance | 13:6714534e7974 | 31 | _downArrow.setLocation( |
duncanFrance | 13:6714534e7974 | 32 | width() - _downArrow.width() - (_borderWidth + _padding), |
duncanFrance | 13:6714534e7974 | 33 | _borderWidth + _padding |
duncanFrance | 13:6714534e7974 | 34 | ); |
duncanFrance | 13:6714534e7974 | 35 | |
duncanFrance | 12:63db16fea709 | 36 | |
duncanFrance | 13:6714534e7974 | 37 | int textWidth = width() |
duncanFrance | 13:6714534e7974 | 38 | - _upArrow.width() - 2*(_borderWidth + _padding) |
duncanFrance | 13:6714534e7974 | 39 | - _downArrow.width() - 2*(_borderWidth + _padding) |
duncanFrance | 13:6714534e7974 | 40 | ; |
duncanFrance | 13:6714534e7974 | 41 | |
duncanFrance | 13:6714534e7974 | 42 | int textHeight = height() - 2*(_borderWidth + _padding +1); |
duncanFrance | 12:63db16fea709 | 43 | |
duncanFrance | 13:6714534e7974 | 44 | _text.setSize(textWidth, textHeight); |
duncanFrance | 13:6714534e7974 | 45 | _text.setLocation(_downArrow.width() + _borderWidth + _padding, _borderWidth + _padding); |
duncanFrance | 12:63db16fea709 | 46 | _text.setForeground(White); |
duncanFrance | 12:63db16fea709 | 47 | _text.setBackground(Black); |
duncanFrance | 12:63db16fea709 | 48 | |
duncanFrance | 12:63db16fea709 | 49 | EventHandler* up = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onUpClick); |
duncanFrance | 12:63db16fea709 | 50 | EventHandler* down = new EventHandler(TOUCH_TAP, this, &SpinnerWidget::_onDownClick); |
duncanFrance | 12:63db16fea709 | 51 | |
duncanFrance | 12:63db16fea709 | 52 | attach(&_upArrow); |
duncanFrance | 12:63db16fea709 | 53 | attach(&_text); |
duncanFrance | 12:63db16fea709 | 54 | attach(&_downArrow); |
duncanFrance | 12:63db16fea709 | 55 | |
duncanFrance | 12:63db16fea709 | 56 | _upArrow.setEventHandler(up); |
duncanFrance | 12:63db16fea709 | 57 | _downArrow.setEventHandler(down); |
duncanFrance | 11:b485561aa112 | 58 | } |
duncanFrance | 11:b485561aa112 | 59 | |
duncanFrance | 11:b485561aa112 | 60 | void SpinnerWidget::setMin(float min) |
duncanFrance | 11:b485561aa112 | 61 | { |
duncanFrance | 12:63db16fea709 | 62 | if(_min != min) { |
duncanFrance | 12:63db16fea709 | 63 | _min = min; |
duncanFrance | 12:63db16fea709 | 64 | dirty(); |
duncanFrance | 12:63db16fea709 | 65 | } |
duncanFrance | 11:b485561aa112 | 66 | } |
duncanFrance | 11:b485561aa112 | 67 | |
duncanFrance | 11:b485561aa112 | 68 | void SpinnerWidget::setMax(float max) |
duncanFrance | 11:b485561aa112 | 69 | { |
duncanFrance | 12:63db16fea709 | 70 | if(_max != max) { |
duncanFrance | 12:63db16fea709 | 71 | _max = max; |
duncanFrance | 12:63db16fea709 | 72 | dirty(); |
duncanFrance | 12:63db16fea709 | 73 | } |
duncanFrance | 11:b485561aa112 | 74 | } |
duncanFrance | 11:b485561aa112 | 75 | |
duncanFrance | 11:b485561aa112 | 76 | void SpinnerWidget::setIncrement(float increment) |
duncanFrance | 11:b485561aa112 | 77 | { |
duncanFrance | 12:63db16fea709 | 78 | if(_increment != increment) { |
duncanFrance | 12:63db16fea709 | 79 | _increment = increment; |
duncanFrance | 12:63db16fea709 | 80 | dirty(); |
duncanFrance | 12:63db16fea709 | 81 | } |
duncanFrance | 11:b485561aa112 | 82 | } |
duncanFrance | 11:b485561aa112 | 83 | |
duncanFrance | 11:b485561aa112 | 84 | void SpinnerWidget::setValue(float value) |
duncanFrance | 11:b485561aa112 | 85 | { |
duncanFrance | 12:63db16fea709 | 86 | if(_value != value) { |
duncanFrance | 12:63db16fea709 | 87 | _value = value; |
duncanFrance | 12:63db16fea709 | 88 | dirty(); |
duncanFrance | 12:63db16fea709 | 89 | } |
duncanFrance | 12:63db16fea709 | 90 | } |
duncanFrance | 12:63db16fea709 | 91 | |
duncanFrance | 12:63db16fea709 | 92 | void SpinnerWidget::setFormat(const char* format) |
duncanFrance | 12:63db16fea709 | 93 | { |
duncanFrance | 12:63db16fea709 | 94 | _format = format; |
duncanFrance | 12:63db16fea709 | 95 | dirty(); |
duncanFrance | 12:63db16fea709 | 96 | } |
duncanFrance | 12:63db16fea709 | 97 | |
duncanFrance | 12:63db16fea709 | 98 | |
duncanFrance | 12:63db16fea709 | 99 | float SpinnerWidget::getMin() |
duncanFrance | 12:63db16fea709 | 100 | { |
duncanFrance | 12:63db16fea709 | 101 | return _min; |
duncanFrance | 11:b485561aa112 | 102 | } |
duncanFrance | 11:b485561aa112 | 103 | |
duncanFrance | 12:63db16fea709 | 104 | float SpinnerWidget::getMax() |
duncanFrance | 12:63db16fea709 | 105 | { |
duncanFrance | 12:63db16fea709 | 106 | return _max; |
duncanFrance | 12:63db16fea709 | 107 | } |
duncanFrance | 12:63db16fea709 | 108 | |
duncanFrance | 12:63db16fea709 | 109 | float SpinnerWidget::getIncrement() |
duncanFrance | 11:b485561aa112 | 110 | { |
duncanFrance | 12:63db16fea709 | 111 | return _increment; |
duncanFrance | 12:63db16fea709 | 112 | } |
duncanFrance | 12:63db16fea709 | 113 | |
duncanFrance | 12:63db16fea709 | 114 | float SpinnerWidget::getValue() |
duncanFrance | 12:63db16fea709 | 115 | { |
duncanFrance | 12:63db16fea709 | 116 | return _value; |
duncanFrance | 12:63db16fea709 | 117 | } |
duncanFrance | 12:63db16fea709 | 118 | |
duncanFrance | 12:63db16fea709 | 119 | const char* SpinnerWidget::getFormat() |
duncanFrance | 12:63db16fea709 | 120 | { |
duncanFrance | 12:63db16fea709 | 121 | return _format; |
duncanFrance | 11:b485561aa112 | 122 | } |
duncanFrance | 11:b485561aa112 | 123 | |
duncanFrance | 11:b485561aa112 | 124 | template<typename T> |
duncanFrance | 12:63db16fea709 | 125 | void SpinnerWidget::onChange(T* tptr, void (T::*mptr)(Event)) |
duncanFrance | 11:b485561aa112 | 126 | { |
duncanFrance | 11:b485561aa112 | 127 | _onChange.attach(tptr, mptr); |
duncanFrance | 11:b485561aa112 | 128 | } |
duncanFrance | 11:b485561aa112 | 129 | |
duncanFrance | 12:63db16fea709 | 130 | void SpinnerWidget::setSize(int width, int height) |
duncanFrance | 12:63db16fea709 | 131 | { |
duncanFrance | 12:63db16fea709 | 132 | ContainerWidget::setSize(width, height); |
duncanFrance | 12:63db16fea709 | 133 | } |
duncanFrance | 11:b485561aa112 | 134 | |
duncanFrance | 12:63db16fea709 | 135 | void SpinnerWidget::_dirty() |
duncanFrance | 12:63db16fea709 | 136 | { |
duncanFrance | 12:63db16fea709 | 137 | |
duncanFrance | 12:63db16fea709 | 138 | sprintf(_buf, _format, _value); |
duncanFrance | 12:63db16fea709 | 139 | _text.setText(_buf); |
duncanFrance | 12:63db16fea709 | 140 | |
duncanFrance | 12:63db16fea709 | 141 | ContainerWidget::_dirty(); |
duncanFrance | 11:b485561aa112 | 142 | } |
duncanFrance | 12:63db16fea709 | 143 | |
duncanFrance | 12:63db16fea709 | 144 | |
duncanFrance | 12:63db16fea709 | 145 | void SpinnerWidget::_onUpClick(Event e) |
duncanFrance | 12:63db16fea709 | 146 | { |
duncanFrance | 12:63db16fea709 | 147 | _value += _increment; |
duncanFrance | 12:63db16fea709 | 148 | if(_value > _max) { |
duncanFrance | 12:63db16fea709 | 149 | _value = _max; |
duncanFrance | 12:63db16fea709 | 150 | } |
duncanFrance | 12:63db16fea709 | 151 | dirty(); |
duncanFrance | 12:63db16fea709 | 152 | } |
duncanFrance | 12:63db16fea709 | 153 | |
duncanFrance | 12:63db16fea709 | 154 | void SpinnerWidget::_onDownClick(Event e) |
duncanFrance | 12:63db16fea709 | 155 | { |
duncanFrance | 12:63db16fea709 | 156 | _value -= _increment; |
duncanFrance | 12:63db16fea709 | 157 | if(_value < _min) { |
duncanFrance | 12:63db16fea709 | 158 | _value = _min; |
duncanFrance | 12:63db16fea709 | 159 | } |
duncanFrance | 12:63db16fea709 | 160 | dirty(); |
duncanFrance | 11:b485561aa112 | 161 | } |