Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of el17ajf by
Menus/UI/UI.cpp@18:24ce897024d0, 2019-04-03 (annotated)
- Committer:
- el17ajf
- Date:
- Wed Apr 03 14:31:49 2019 +0000
- Revision:
- 18:24ce897024d0
- Parent:
- 17:cc448ab7266f
- Child:
- 19:370d83a8dc33
Added adaptable grid sizes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
el17ajf | 17:cc448ab7266f | 1 | #include "UI.h" |
el17ajf | 17:cc448ab7266f | 2 | #include "Graphics.h" |
el17ajf | 17:cc448ab7266f | 3 | |
el17ajf | 17:cc448ab7266f | 4 | UI::UI() { |
el17ajf | 17:cc448ab7266f | 5 | reset(); |
el17ajf | 17:cc448ab7266f | 6 | } |
el17ajf | 17:cc448ab7266f | 7 | |
el17ajf | 17:cc448ab7266f | 8 | void UI::reset() { |
el17ajf | 17:cc448ab7266f | 9 | buttonCurrentlySelected = 0; |
el17ajf | 17:cc448ab7266f | 10 | } |
el17ajf | 17:cc448ab7266f | 11 | |
el17ajf | 17:cc448ab7266f | 12 | UI::~UI() { |
el17ajf | 16:3f84f2d7b910 | 13 | |
el17ajf | 17:cc448ab7266f | 14 | } |
el17ajf | 17:cc448ab7266f | 15 | |
el17ajf | 17:cc448ab7266f | 16 | void UI::clear() { |
el17ajf | 17:cc448ab7266f | 17 | buttonPressedFrame = false; |
el17ajf | 17:cc448ab7266f | 18 | nextYPos = PADDING_Y; |
el17ajf | 17:cc448ab7266f | 19 | buttonCurrentlyDrawing = -1; |
el17ajf | 17:cc448ab7266f | 20 | } |
el17ajf | 17:cc448ab7266f | 21 | |
el17ajf | 17:cc448ab7266f | 22 | void UI::drawTitle(const char * text) { |
el17ajf | 17:cc448ab7266f | 23 | drawLabel(text); |
el17ajf | 17:cc448ab7266f | 24 | } |
el17ajf | 17:cc448ab7266f | 25 | |
el17ajf | 17:cc448ab7266f | 26 | void UI::drawLabel(const char * text) { |
el17ajf | 17:cc448ab7266f | 27 | Graphics::UI::drawText(PADDING_X, nextY(), text); |
el17ajf | 17:cc448ab7266f | 28 | } |
el17ajf | 17:cc448ab7266f | 29 | |
el17ajf | 17:cc448ab7266f | 30 | void UI::newLine() { |
el17ajf | 17:cc448ab7266f | 31 | nextY(); |
el17ajf | 17:cc448ab7266f | 32 | } |
el17ajf | 16:3f84f2d7b910 | 33 | |
el17ajf | 17:cc448ab7266f | 34 | bool UI::drawAndCheckButton(const char * text) { |
el17ajf | 17:cc448ab7266f | 35 | buttonCurrentlyDrawing++; |
el17ajf | 17:cc448ab7266f | 36 | |
el17ajf | 17:cc448ab7266f | 37 | int y = nextY(); |
el17ajf | 17:cc448ab7266f | 38 | |
el17ajf | 17:cc448ab7266f | 39 | Graphics::UI::drawText(PADDING_X, y, text); |
el17ajf | 17:cc448ab7266f | 40 | |
el17ajf | 17:cc448ab7266f | 41 | bool selected = buttonCurrentlyDrawing == buttonCurrentlySelected; |
el17ajf | 17:cc448ab7266f | 42 | |
el17ajf | 18:24ce897024d0 | 43 | int wordWidth = strlen(text) * (Graphics::UI::CHAR_WIDTH + Graphics::UI::CHAR_SPACE); |
el17ajf | 18:24ce897024d0 | 44 | |
el17ajf | 17:cc448ab7266f | 45 | if (selected) { |
el17ajf | 18:24ce897024d0 | 46 | Graphics::UI::drawBorder( |
el17ajf | 18:24ce897024d0 | 47 | PADDING_X - 2, |
el17ajf | 18:24ce897024d0 | 48 | y - 2, |
el17ajf | 18:24ce897024d0 | 49 | PADDING_X + wordWidth + 2, |
el17ajf | 18:24ce897024d0 | 50 | y + Graphics::UI::CHAR_HEIGHT + 1 |
el17ajf | 18:24ce897024d0 | 51 | ); |
el17ajf | 17:cc448ab7266f | 52 | } |
el17ajf | 17:cc448ab7266f | 53 | |
el17ajf | 17:cc448ab7266f | 54 | return buttonPressedFrame && selected; |
el17ajf | 17:cc448ab7266f | 55 | } |
el17ajf | 17:cc448ab7266f | 56 | |
el17ajf | 17:cc448ab7266f | 57 | void UI::selectNextButton() { |
el17ajf | 17:cc448ab7266f | 58 | if (buttonCurrentlySelected < buttonCurrentlyDrawing) { |
el17ajf | 17:cc448ab7266f | 59 | buttonCurrentlySelected++; |
el17ajf | 17:cc448ab7266f | 60 | } |
el17ajf | 17:cc448ab7266f | 61 | } |
el17ajf | 17:cc448ab7266f | 62 | |
el17ajf | 17:cc448ab7266f | 63 | void UI::selectPreviousButton() { |
el17ajf | 17:cc448ab7266f | 64 | if (buttonCurrentlySelected > 0) { |
el17ajf | 17:cc448ab7266f | 65 | buttonCurrentlySelected--; |
el17ajf | 17:cc448ab7266f | 66 | } |
el17ajf | 17:cc448ab7266f | 67 | } |
el17ajf | 17:cc448ab7266f | 68 | |
el17ajf | 17:cc448ab7266f | 69 | void UI::pressButton() { |
el17ajf | 17:cc448ab7266f | 70 | buttonPressedFrame = true; |
el17ajf | 17:cc448ab7266f | 71 | } |
el17ajf | 17:cc448ab7266f | 72 | |
el17ajf | 17:cc448ab7266f | 73 | int UI::nextY() { |
el17ajf | 17:cc448ab7266f | 74 | int currentYPos = nextYPos; |
el17ajf | 17:cc448ab7266f | 75 | nextYPos += Graphics::UI::CHAR_HEIGHT + LINE_PADDING; |
el17ajf | 17:cc448ab7266f | 76 | return currentYPos; |
el17ajf | 17:cc448ab7266f | 77 | } |