Foundation classes for a basic GUI implementing simple widgets and events

Dependents:   TouchScreenGUIDemo

Widgets/TextWidget.cpp

Committer:
duncanFrance
Date:
2016-03-25
Revision:
2:bb9183379488
Parent:
1:48796b602c86
Child:
3:cb004f59b715

File content as of revision 2:bb9183379488:

#include "TextWidget.h"

/**
* A basic widget implementation which just draws some text.
* If the text does not fit in the bounding-box it will be clipped
**/

TextWidget::TextWidget(GraphicsDisplay* display) : Widget(display) {}

void TextWidget::setText(char* text)
{
    _text = text;
}

void TextWidget::setFont(Font* font)
{
    _font = font;
}

void TextWidget::draw()
{

    int right = _x + _width; // right side of clipping window
    int bottom = _y + _height; // bottom edge of clipping window

    int _cx = _x;
    // Start drawing characters at top left
    int _cy = _y;

    char c;
    char *p = _text;

    _display->foreground(_fg);
    _display->background(_bg);
    _display->set_font(_font->_font);


    while(*p != NULL) {
        c = *p;
        p++;
        if(c=='\n') {
            _cy = _cy + _font->_height;
            _cx = _x;
        } else {
            // Only draw the character if it is not clipped
            if( (_cx+_font->_width) < right && (_cy +_font->_height) < bottom) {
                _display->character(_cx, _cy, c);
                _cx += _font->widthOf(c);
            }
        }
    }
}

bool TextWidget::isEventTarget(Event e)
{
    return e.screenX < (_x+_width) && _x <= e.screenX && e.screenY < (_y+_height) && _y <= e.screenY;
}