Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Widgets/TextWidget.cpp@0:0a590815d51c, 2016-03-25 (annotated)
- Committer:
- duncanFrance
- Date:
- Fri Mar 25 13:47:04 2016 +0000
- Revision:
- 0:0a590815d51c
- Child:
- 1:48796b602c86
Added EventSource interface
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 | 0:0a590815d51c | 8 | TextWidget::TextWidget(GraphicsDisplay* display) : Widget(display) {} |
duncanFrance | 0:0a590815d51c | 9 | |
duncanFrance | 0:0a590815d51c | 10 | void TextWidget::setText(char* text) { |
duncanFrance | 0:0a590815d51c | 11 | _text = text; |
duncanFrance | 0:0a590815d51c | 12 | } |
duncanFrance | 0:0a590815d51c | 13 | |
duncanFrance | 0:0a590815d51c | 14 | void TextWidget::setFont(unsigned char *font) { |
duncanFrance | 0:0a590815d51c | 15 | _font = font; |
duncanFrance | 0:0a590815d51c | 16 | _fw = _font[1]; |
duncanFrance | 0:0a590815d51c | 17 | _fh = _font[2]; |
duncanFrance | 0:0a590815d51c | 18 | } |
duncanFrance | 0:0a590815d51c | 19 | |
duncanFrance | 0:0a590815d51c | 20 | void TextWidget::draw() { |
duncanFrance | 0:0a590815d51c | 21 | |
duncanFrance | 0:0a590815d51c | 22 | int right = _x + _width; // right side of clipping window |
duncanFrance | 0:0a590815d51c | 23 | |
duncanFrance | 0:0a590815d51c | 24 | int _cx = _x; |
duncanFrance | 0:0a590815d51c | 25 | // Start drawing characters at top left |
duncanFrance | 0:0a590815d51c | 26 | int _cy = _y + _height - _fh; |
duncanFrance | 0:0a590815d51c | 27 | |
duncanFrance | 0:0a590815d51c | 28 | char c; |
duncanFrance | 0:0a590815d51c | 29 | char *p = _text; |
duncanFrance | 0:0a590815d51c | 30 | |
duncanFrance | 0:0a590815d51c | 31 | _display->foreground(_fg); |
duncanFrance | 0:0a590815d51c | 32 | _display->background(_bg); |
duncanFrance | 0:0a590815d51c | 33 | |
duncanFrance | 0:0a590815d51c | 34 | while(p != NULL) { |
duncanFrance | 0:0a590815d51c | 35 | c = *p; |
duncanFrance | 0:0a590815d51c | 36 | p++; |
duncanFrance | 0:0a590815d51c | 37 | if(c=='\n') { |
duncanFrance | 0:0a590815d51c | 38 | _cy = _cy - _fh; |
duncanFrance | 0:0a590815d51c | 39 | _cx = _x; |
duncanFrance | 0:0a590815d51c | 40 | } else { |
duncanFrance | 0:0a590815d51c | 41 | // Only draw the character if it is not clipped |
duncanFrance | 0:0a590815d51c | 42 | if( (_cx+_fw) < right && (_cy -_fh) > _y) { |
duncanFrance | 0:0a590815d51c | 43 | _display->character(_cx, _cy, c); |
duncanFrance | 0:0a590815d51c | 44 | _cx += _fw; |
duncanFrance | 0:0a590815d51c | 45 | _cy -= _fh; |
duncanFrance | 0:0a590815d51c | 46 | } |
duncanFrance | 0:0a590815d51c | 47 | } |
duncanFrance | 0:0a590815d51c | 48 | } |
duncanFrance | 0:0a590815d51c | 49 | } |
duncanFrance | 0:0a590815d51c | 50 | |
duncanFrance | 0:0a590815d51c | 51 | bool TextWidget::isEventTarget(Event e) { |
duncanFrance | 0:0a590815d51c | 52 | return e.screenX < (_x+_width) && _x <= e.screenX && e.screenY < (_y+_height) && _y <= e.screenY; |
duncanFrance | 0:0a590815d51c | 53 | } |