Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Widgets/TextWidget.cpp@3:cb004f59b715, 2016-03-27 (annotated)
- Committer:
- duncanFrance
- Date:
- Sun Mar 27 14:41:31 2016 +0000
- Revision:
- 3:cb004f59b715
- Parent:
- 2:bb9183379488
- Child:
- 4:27546fb8b670
Added FontRenderer
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 | 0:0a590815d51c | 7 | |
duncanFrance | 3:cb004f59b715 | 8 | TextWidget::TextWidget(GraphicsDisplay* display) : Widget(display), _renderer(display) {} |
duncanFrance | 0:0a590815d51c | 9 | |
duncanFrance | 1:48796b602c86 | 10 | void TextWidget::setText(char* text) |
duncanFrance | 1:48796b602c86 | 11 | { |
duncanFrance | 0:0a590815d51c | 12 | _text = text; |
duncanFrance | 0:0a590815d51c | 13 | } |
duncanFrance | 0:0a590815d51c | 14 | |
duncanFrance | 2:bb9183379488 | 15 | void TextWidget::setFont(Font* font) |
duncanFrance | 1:48796b602c86 | 16 | { |
duncanFrance | 2:bb9183379488 | 17 | _font = font; |
duncanFrance | 0:0a590815d51c | 18 | } |
duncanFrance | 0:0a590815d51c | 19 | |
duncanFrance | 3:cb004f59b715 | 20 | |
duncanFrance | 3:cb004f59b715 | 21 | void TextWidget::draw() |
duncanFrance | 3:cb004f59b715 | 22 | { |
duncanFrance | 3:cb004f59b715 | 23 | _renderer.clip(_x, _y, _width, _height); |
duncanFrance | 3:cb004f59b715 | 24 | |
duncanFrance | 3:cb004f59b715 | 25 | char c; |
duncanFrance | 3:cb004f59b715 | 26 | char *p = _text; |
duncanFrance | 3:cb004f59b715 | 27 | |
duncanFrance | 3:cb004f59b715 | 28 | while(*p != NULL) { |
duncanFrance | 3:cb004f59b715 | 29 | c = *p; |
duncanFrance | 3:cb004f59b715 | 30 | p++; |
duncanFrance | 3:cb004f59b715 | 31 | _renderer.putc(c, _font, _fg, _bg); |
duncanFrance | 3:cb004f59b715 | 32 | } |
duncanFrance | 3:cb004f59b715 | 33 | |
duncanFrance | 3:cb004f59b715 | 34 | _display->copy_to_lcd(); |
duncanFrance | 3:cb004f59b715 | 35 | } |
duncanFrance | 3:cb004f59b715 | 36 | |
duncanFrance | 3:cb004f59b715 | 37 | /** |
duncanFrance | 1:48796b602c86 | 38 | void TextWidget::draw() |
duncanFrance | 1:48796b602c86 | 39 | { |
duncanFrance | 0:0a590815d51c | 40 | |
duncanFrance | 0:0a590815d51c | 41 | int right = _x + _width; // right side of clipping window |
duncanFrance | 1:48796b602c86 | 42 | int bottom = _y + _height; // bottom edge of clipping window |
duncanFrance | 0:0a590815d51c | 43 | |
duncanFrance | 0:0a590815d51c | 44 | int _cx = _x; |
duncanFrance | 0:0a590815d51c | 45 | // Start drawing characters at top left |
duncanFrance | 1:48796b602c86 | 46 | int _cy = _y; |
duncanFrance | 0:0a590815d51c | 47 | |
duncanFrance | 0:0a590815d51c | 48 | char c; |
duncanFrance | 0:0a590815d51c | 49 | char *p = _text; |
duncanFrance | 1:48796b602c86 | 50 | |
duncanFrance | 0:0a590815d51c | 51 | _display->foreground(_fg); |
duncanFrance | 0:0a590815d51c | 52 | _display->background(_bg); |
duncanFrance | 2:bb9183379488 | 53 | _display->set_font(_font->_font); |
duncanFrance | 1:48796b602c86 | 54 | |
duncanFrance | 1:48796b602c86 | 55 | |
duncanFrance | 1:48796b602c86 | 56 | while(*p != NULL) { |
duncanFrance | 0:0a590815d51c | 57 | c = *p; |
duncanFrance | 0:0a590815d51c | 58 | p++; |
duncanFrance | 0:0a590815d51c | 59 | if(c=='\n') { |
duncanFrance | 2:bb9183379488 | 60 | _cy = _cy + _font->_height; |
duncanFrance | 0:0a590815d51c | 61 | _cx = _x; |
duncanFrance | 0:0a590815d51c | 62 | } else { |
duncanFrance | 0:0a590815d51c | 63 | // Only draw the character if it is not clipped |
duncanFrance | 2:bb9183379488 | 64 | if( (_cx+_font->_width) < right && (_cy +_font->_height) < bottom) { |
duncanFrance | 0:0a590815d51c | 65 | _display->character(_cx, _cy, c); |
duncanFrance | 2:bb9183379488 | 66 | _cx += _font->widthOf(c); |
duncanFrance | 0:0a590815d51c | 67 | } |
duncanFrance | 0:0a590815d51c | 68 | } |
duncanFrance | 0:0a590815d51c | 69 | } |
duncanFrance | 0:0a590815d51c | 70 | } |
duncanFrance | 3:cb004f59b715 | 71 | **/ |
duncanFrance | 1:48796b602c86 | 72 | bool TextWidget::isEventTarget(Event e) |
duncanFrance | 1:48796b602c86 | 73 | { |
duncanFrance | 0:0a590815d51c | 74 | return e.screenX < (_x+_width) && _x <= e.screenX && e.screenY < (_y+_height) && _y <= e.screenY; |
duncanFrance | 0:0a590815d51c | 75 | } |