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.h@41:91b0c73b9e02, 2019-05-09 (annotated)
- Committer:
- el17ajf
- Date:
- Thu May 09 12:10:29 2019 +0000
- Revision:
- 41:91b0c73b9e02
- Parent:
- 37:8da316cf4549
Commented remaining parts, added unit tests
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17ajf | 21:62d2b5b73160 | 1 | #ifndef UI_H |
| el17ajf | 21:62d2b5b73160 | 2 | #define UI_H |
| el17ajf | 16:3f84f2d7b910 | 3 | |
| el17ajf | 21:62d2b5b73160 | 4 | namespace Menus { |
| el17ajf | 37:8da316cf4549 | 5 | /** |
| el17ajf | 37:8da316cf4549 | 6 | * UI class |
| el17ajf | 37:8da316cf4549 | 7 | * @brief Provides a user-friendly abstraction layer above the graphics |
| el17ajf | 37:8da316cf4549 | 8 | * namespace, allowing the programmer to add labels, buttons and other |
| el17ajf | 37:8da316cf4549 | 9 | * UI elements to the 'canvas', without worrying about positioning or |
| el17ajf | 37:8da316cf4549 | 10 | * input management. |
| el17ajf | 37:8da316cf4549 | 11 | * @see Graphics::UI |
| el17ajf | 37:8da316cf4549 | 12 | */ |
| el17ajf | 21:62d2b5b73160 | 13 | class UI { |
| el17ajf | 21:62d2b5b73160 | 14 | public: |
| el17ajf | 37:8da316cf4549 | 15 | /** |
| el17ajf | 37:8da316cf4549 | 16 | * @brief Creates a new UI object ready to be used |
| el17ajf | 37:8da316cf4549 | 17 | */ |
| el17ajf | 21:62d2b5b73160 | 18 | UI(); |
| el17ajf | 37:8da316cf4549 | 19 | |
| el17ajf | 37:8da316cf4549 | 20 | /** |
| el17ajf | 37:8da316cf4549 | 21 | * @brief Destructor |
| el17ajf | 37:8da316cf4549 | 22 | */ |
| el17ajf | 21:62d2b5b73160 | 23 | ~UI(); |
| el17ajf | 21:62d2b5b73160 | 24 | |
| el17ajf | 37:8da316cf4549 | 25 | /** |
| el17ajf | 37:8da316cf4549 | 26 | * @brief Should be called before a new Menu is loaded, to reset state |
| el17ajf | 37:8da316cf4549 | 27 | * such as the cursor position. |
| el17ajf | 37:8da316cf4549 | 28 | */ |
| el17ajf | 21:62d2b5b73160 | 29 | void reset(); |
| el17ajf | 37:8da316cf4549 | 30 | |
| el17ajf | 37:8da316cf4549 | 31 | /** |
| el17ajf | 37:8da316cf4549 | 32 | * @brief Should be called every frame, prepares the 'canvas' for a new |
| el17ajf | 37:8da316cf4549 | 33 | * set of 'draw' calls. |
| el17ajf | 37:8da316cf4549 | 34 | */ |
| el17ajf | 21:62d2b5b73160 | 35 | void clear(); |
| el17ajf | 21:62d2b5b73160 | 36 | |
| el17ajf | 37:8da316cf4549 | 37 | /** |
| el17ajf | 37:8da316cf4549 | 38 | * @brief Draws a 'title' style label to the screen, essentially the |
| el17ajf | 37:8da316cf4549 | 39 | * same as drawLabel. |
| el17ajf | 37:8da316cf4549 | 40 | * @param text The text of the title, must be <= 8 characters |
| el17ajf | 37:8da316cf4549 | 41 | * @see drawLabel |
| el17ajf | 37:8da316cf4549 | 42 | */ |
| el17ajf | 21:62d2b5b73160 | 43 | void drawTitle(const char * text); |
| el17ajf | 37:8da316cf4549 | 44 | |
| el17ajf | 37:8da316cf4549 | 45 | /** |
| el17ajf | 37:8da316cf4549 | 46 | * @brief Draws a label to the canvas and advance the 'canvas pointer'. |
| el17ajf | 37:8da316cf4549 | 47 | * @param text The text of the label, must be <= 8 characters |
| el17ajf | 37:8da316cf4549 | 48 | */ |
| el17ajf | 21:62d2b5b73160 | 49 | void drawLabel(const char * text); |
| el17ajf | 37:8da316cf4549 | 50 | |
| el17ajf | 37:8da316cf4549 | 51 | /** |
| el17ajf | 37:8da316cf4549 | 52 | * @brief |
| el17ajf | 37:8da316cf4549 | 53 | * @param text The text of the flashing label, must be <= 8 characters |
| el17ajf | 37:8da316cf4549 | 54 | * @see drawLabel |
| el17ajf | 37:8da316cf4549 | 55 | */ |
| el17ajf | 27:2ed9e3c9f4e9 | 56 | void drawFlashingLabel(const char * text); |
| el17ajf | 37:8da316cf4549 | 57 | |
| el17ajf | 37:8da316cf4549 | 58 | /** |
| el17ajf | 37:8da316cf4549 | 59 | * @brief |
| el17ajf | 37:8da316cf4549 | 60 | * @param text The text of the button, must be <= 8 characters |
| el17ajf | 37:8da316cf4549 | 61 | * @returns true if the button is pressed this frame, else false. |
| el17ajf | 37:8da316cf4549 | 62 | */ |
| el17ajf | 21:62d2b5b73160 | 63 | bool drawAndCheckButton(const char * text); |
| el17ajf | 37:8da316cf4549 | 64 | |
| el17ajf | 37:8da316cf4549 | 65 | /** |
| el17ajf | 37:8da316cf4549 | 66 | * @brief Draws the big inverted 'TETRIS' logo and advances the canvas |
| el17ajf | 37:8da316cf4549 | 67 | * pointer. |
| el17ajf | 37:8da316cf4549 | 68 | */ |
| el17ajf | 30:11838cb6979f | 69 | void drawLogo(); |
| el17ajf | 21:62d2b5b73160 | 70 | |
| el17ajf | 37:8da316cf4549 | 71 | /** |
| el17ajf | 37:8da316cf4549 | 72 | * @brief Called to move the 'cursor' (highlighted button) down to |
| el17ajf | 37:8da316cf4549 | 73 | * the next one vertically beneath. |
| el17ajf | 37:8da316cf4549 | 74 | */ |
| el17ajf | 21:62d2b5b73160 | 75 | void selectNextButton(); |
| el17ajf | 37:8da316cf4549 | 76 | |
| el17ajf | 37:8da316cf4549 | 77 | /** |
| el17ajf | 37:8da316cf4549 | 78 | * @brief Called to move the 'cursor' (highlighted button) up to |
| el17ajf | 37:8da316cf4549 | 79 | * the next one vertically above. |
| el17ajf | 37:8da316cf4549 | 80 | */ |
| el17ajf | 21:62d2b5b73160 | 81 | void selectPreviousButton(); |
| el17ajf | 37:8da316cf4549 | 82 | |
| el17ajf | 37:8da316cf4549 | 83 | /** |
| el17ajf | 37:8da316cf4549 | 84 | * @brief "Presses" the current button, so it's draw function |
| el17ajf | 37:8da316cf4549 | 85 | * will return true. |
| el17ajf | 37:8da316cf4549 | 86 | */ |
| el17ajf | 21:62d2b5b73160 | 87 | void pressButton(); |
| el17ajf | 37:8da316cf4549 | 88 | |
| el17ajf | 37:8da316cf4549 | 89 | /** |
| el17ajf | 37:8da316cf4549 | 90 | * @brief Simply adds vertical whitespace to the canvas. |
| el17ajf | 37:8da316cf4549 | 91 | */ |
| el17ajf | 21:62d2b5b73160 | 92 | void newLine(); |
| el17ajf | 21:62d2b5b73160 | 93 | |
| el17ajf | 21:62d2b5b73160 | 94 | private: |
| el17ajf | 41:91b0c73b9e02 | 95 | /** |
| el17ajf | 41:91b0c73b9e02 | 96 | * The amount of X padding between the top of the screen and UI elements |
| el17ajf | 41:91b0c73b9e02 | 97 | */ |
| el17ajf | 25:bf47fe41883a | 98 | static const int PADDING_X = 3; |
| el17ajf | 41:91b0c73b9e02 | 99 | |
| el17ajf | 41:91b0c73b9e02 | 100 | /** |
| el17ajf | 41:91b0c73b9e02 | 101 | * The padding between the edge of the screen and UI elements |
| el17ajf | 41:91b0c73b9e02 | 102 | */ |
| el17ajf | 30:11838cb6979f | 103 | static const int PADDING_Y = 4; |
| el17ajf | 41:91b0c73b9e02 | 104 | |
| el17ajf | 41:91b0c73b9e02 | 105 | /** |
| el17ajf | 41:91b0c73b9e02 | 106 | * The padding between buttons |
| el17ajf | 41:91b0c73b9e02 | 107 | */ |
| el17ajf | 25:bf47fe41883a | 108 | static const int LINE_PADDING = 3; |
| el17ajf | 41:91b0c73b9e02 | 109 | |
| el17ajf | 41:91b0c73b9e02 | 110 | /** |
| el17ajf | 41:91b0c73b9e02 | 111 | * Intefaced with nextY() |
| el17ajf | 41:91b0c73b9e02 | 112 | */ |
| el17ajf | 32:7b5a864b9234 | 113 | int next_y_pos; |
| el17ajf | 41:91b0c73b9e02 | 114 | |
| el17ajf | 41:91b0c73b9e02 | 115 | /** |
| el17ajf | 41:91b0c73b9e02 | 116 | * @brief Increments next_y_pos |
| el17ajf | 41:91b0c73b9e02 | 117 | * @returns The next y position to put a button at |
| el17ajf | 41:91b0c73b9e02 | 118 | */ |
| el17ajf | 21:62d2b5b73160 | 119 | int nextY(); |
| el17ajf | 41:91b0c73b9e02 | 120 | |
| el17ajf | 41:91b0c73b9e02 | 121 | /** |
| el17ajf | 41:91b0c73b9e02 | 122 | * The button currently selected in the list. |
| el17ajf | 41:91b0c73b9e02 | 123 | */ |
| el17ajf | 32:7b5a864b9234 | 124 | int button_currently_selected; |
| el17ajf | 41:91b0c73b9e02 | 125 | |
| el17ajf | 41:91b0c73b9e02 | 126 | /** |
| el17ajf | 41:91b0c73b9e02 | 127 | * The number being drawn currently, starting at 0, resets to zero |
| el17ajf | 41:91b0c73b9e02 | 128 | * every frame. |
| el17ajf | 41:91b0c73b9e02 | 129 | */ |
| el17ajf | 32:7b5a864b9234 | 130 | int button_currently_drawing; |
| el17ajf | 41:91b0c73b9e02 | 131 | |
| el17ajf | 41:91b0c73b9e02 | 132 | /** |
| el17ajf | 41:91b0c73b9e02 | 133 | * True if the 'OK button' has been clicked this frame |
| el17ajf | 41:91b0c73b9e02 | 134 | */ |
| el17ajf | 32:7b5a864b9234 | 135 | bool button_pressed_frame; |
| el17ajf | 41:91b0c73b9e02 | 136 | |
| el17ajf | 41:91b0c73b9e02 | 137 | /** |
| el17ajf | 41:91b0c73b9e02 | 138 | * The current frame counter, used for flashing text |
| el17ajf | 41:91b0c73b9e02 | 139 | */ |
| el17ajf | 27:2ed9e3c9f4e9 | 140 | int frame; |
| el17ajf | 21:62d2b5b73160 | 141 | }; |
| el17ajf | 21:62d2b5b73160 | 142 | }; |
| el17ajf | 21:62d2b5b73160 | 143 | #endif |
