Foundation classes for a basic GUI implementing simple widgets and events

Dependents:   TouchScreenGUIDemo

Widgets/SpinnerWidget.cpp

Committer:
duncanFrance
Date:
2016-05-08
Revision:
12:63db16fea709
Parent:
11:b485561aa112
Child:
13:6714534e7974

File content as of revision 12:63db16fea709:

#include "SpinnerWidget.h"

#include"resources/up_arrow_32x19_bmp.h"
#include"resources/down_arrow_32x19_bmp.h"

SpinnerWidget::SpinnerWidget(GraphicsContext *context)
    : ContainerWidget(context),
      _upArrow(context), _text(context), _downArrow(context),
      _min(0), _max(0), _increment(0.5), _value(0),
      _format(""), _buf("")
{

    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;
}

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)(Event))
{
    _onChange.attach(tptr, mptr);
}

void SpinnerWidget::setSize(int width, int height)
{
    _text.setSize(width, _text.height());
    ContainerWidget::setSize(width, height);
}

void SpinnerWidget::_dirty()
{

    sprintf(_buf, _format, _value);
    _text.setText(_buf);

    ContainerWidget::_dirty();
}


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();
}