Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Widgets/TextWidget.cpp@16:e9a771ecfdbe, 2016-05-22 (annotated)
- Committer:
- duncanFrance
- Date:
- Sun May 22 14:40:29 2016 +0000
- Revision:
- 16:e9a771ecfdbe
- Parent:
- 13:6714534e7974
- Child:
- 17:5184762fda6c
Added vertical alignment control to TextWidget; pumpEvents no longer blocks
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 0:0a590815d51c | 1 | #include "TextWidget.h" |
duncanFrance | 0:0a590815d51c | 2 | |
duncanFrance | 0:0a590815d51c | 3 | /** |
duncanFrance | 0:0a590815d51c | 4 | * A basic widget implementation which just draws some text. |
duncanFrance | 0:0a590815d51c | 5 | * If the text does not fit in the bounding-box it will be clipped |
duncanFrance | 0:0a590815d51c | 6 | **/ |
duncanFrance | 12:63db16fea709 | 7 | TextWidget::TextWidget(GraphicsContext *context) : |
duncanFrance | 12:63db16fea709 | 8 | Widget(context), |
duncanFrance | 12:63db16fea709 | 9 | _renderer(context->fontRenderer()), |
duncanFrance | 16:e9a771ecfdbe | 10 | _font(context->defaultFont()), |
duncanFrance | 16:e9a771ecfdbe | 11 | _halign(LEFT), _valign(TOP) |
duncanFrance | 8:a460cabc85ac | 12 | { |
duncanFrance | 8:a460cabc85ac | 13 | } |
duncanFrance | 8:a460cabc85ac | 14 | |
duncanFrance | 12:63db16fea709 | 15 | TextWidget::TextWidget(GraphicsContext *context, FontRenderer* renderer) : |
duncanFrance | 12:63db16fea709 | 16 | Widget(context), |
duncanFrance | 8:a460cabc85ac | 17 | _renderer(renderer), |
duncanFrance | 16:e9a771ecfdbe | 18 | _font(context->defaultFont()), |
duncanFrance | 16:e9a771ecfdbe | 19 | _halign(LEFT), _valign(TOP) |
duncanFrance | 8:a460cabc85ac | 20 | { |
duncanFrance | 8:a460cabc85ac | 21 | } |
duncanFrance | 8:a460cabc85ac | 22 | |
duncanFrance | 12:63db16fea709 | 23 | TextWidget::TextWidget(GraphicsContext *context, FontRenderer* renderer, Font* font) : |
duncanFrance | 12:63db16fea709 | 24 | Widget(context), |
duncanFrance | 8:a460cabc85ac | 25 | _renderer(renderer), |
duncanFrance | 16:e9a771ecfdbe | 26 | _font(font), |
duncanFrance | 16:e9a771ecfdbe | 27 | _halign(LEFT), _valign(TOP) |
duncanFrance | 7:303850a4b30c | 28 | { |
duncanFrance | 7:303850a4b30c | 29 | } |
duncanFrance | 0:0a590815d51c | 30 | |
duncanFrance | 12:63db16fea709 | 31 | void TextWidget::setText(char* text) |
duncanFrance | 1:48796b602c86 | 32 | { |
duncanFrance | 11:b485561aa112 | 33 | _text = text; |
duncanFrance | 12:63db16fea709 | 34 | dirty(); |
duncanFrance | 0:0a590815d51c | 35 | } |
duncanFrance | 0:0a590815d51c | 36 | |
duncanFrance | 2:bb9183379488 | 37 | void TextWidget::setFont(Font* font) |
duncanFrance | 1:48796b602c86 | 38 | { |
duncanFrance | 2:bb9183379488 | 39 | _font = font; |
duncanFrance | 12:63db16fea709 | 40 | dirty(); |
duncanFrance | 0:0a590815d51c | 41 | } |
duncanFrance | 0:0a590815d51c | 42 | |
duncanFrance | 16:e9a771ecfdbe | 43 | void TextWidget::setHAlign(HAlign alignment) { |
duncanFrance | 16:e9a771ecfdbe | 44 | _halign = alignment; |
duncanFrance | 16:e9a771ecfdbe | 45 | dirty(); |
duncanFrance | 16:e9a771ecfdbe | 46 | } |
duncanFrance | 16:e9a771ecfdbe | 47 | |
duncanFrance | 16:e9a771ecfdbe | 48 | void TextWidget::setVAlign(VAlign alignment) { |
duncanFrance | 16:e9a771ecfdbe | 49 | _valign = alignment; |
duncanFrance | 16:e9a771ecfdbe | 50 | dirty(); |
duncanFrance | 16:e9a771ecfdbe | 51 | } |
duncanFrance | 3:cb004f59b715 | 52 | |
duncanFrance | 8:a460cabc85ac | 53 | void TextWidget::_draw() |
duncanFrance | 3:cb004f59b715 | 54 | { |
duncanFrance | 12:63db16fea709 | 55 | Widget::_draw(); |
duncanFrance | 12:63db16fea709 | 56 | |
duncanFrance | 16:e9a771ecfdbe | 57 | /******************************************************************/ |
duncanFrance | 16:e9a771ecfdbe | 58 | /* --------------------------------- ^ */ |
duncanFrance | 16:e9a771ecfdbe | 59 | /* | | | */ |
duncanFrance | 16:e9a771ecfdbe | 60 | /* | | | */ |
duncanFrance | 16:e9a771ecfdbe | 61 | /* | | inner.height */ |
duncanFrance | 16:e9a771ecfdbe | 62 | /* | | | */ |
duncanFrance | 16:e9a771ecfdbe | 63 | /* | | | */ |
duncanFrance | 16:e9a771ecfdbe | 64 | /* | | | */ |
duncanFrance | 16:e9a771ecfdbe | 65 | /* | | | */ |
duncanFrance | 16:e9a771ecfdbe | 66 | /* --------------------------------- | */ |
duncanFrance | 16:e9a771ecfdbe | 67 | /******************************************************************/ |
duncanFrance | 16:e9a771ecfdbe | 68 | /** |
duncanFrance | 16:e9a771ecfdbe | 69 | * We need a window as high as the font with it's origin: |
duncanFrance | 16:e9a771ecfdbe | 70 | * VALIGN=TOP : (0, 0) |
duncanFrance | 16:e9a771ecfdbe | 71 | * VALIGN=MIDDLE : (0, inner.height/2 - font.height/2) |
duncanFrance | 16:e9a771ecfdbe | 72 | * VALIGN=BOTTOM : (0, inner.height - font.height) |
duncanFrance | 16:e9a771ecfdbe | 73 | **/ |
duncanFrance | 16:e9a771ecfdbe | 74 | int t=0; |
duncanFrance | 16:e9a771ecfdbe | 75 | switch(_valign) { |
duncanFrance | 16:e9a771ecfdbe | 76 | case TOP: t = 0; break; |
duncanFrance | 16:e9a771ecfdbe | 77 | case MIDDLE: t = (_inner.height - _font->zoomedHeight())/2; break; |
duncanFrance | 16:e9a771ecfdbe | 78 | case BOTTOM: t = (_inner.height - _font->zoomedHeight()); break; |
duncanFrance | 16:e9a771ecfdbe | 79 | } |
duncanFrance | 13:6714534e7974 | 80 | |
duncanFrance | 16:e9a771ecfdbe | 81 | _renderer->window(_inner.x, _inner.y + t, _inner.width, _font->zoomedHeight(), false); |
duncanFrance | 7:303850a4b30c | 82 | _renderer->setForeground(_fg); |
duncanFrance | 7:303850a4b30c | 83 | _renderer->setBackground(_bg); |
duncanFrance | 12:63db16fea709 | 84 | display()->fillrect(_inner.x, _inner.y, _inner.x+_inner.width, _inner.y+_inner.height, _bg); |
duncanFrance | 8:a460cabc85ac | 85 | |
duncanFrance | 12:63db16fea709 | 86 | _renderer->puts(_text, display(), _font); |
duncanFrance | 12:63db16fea709 | 87 | display()->copy_to_lcd(); |
duncanFrance | 8:a460cabc85ac | 88 | } |