-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMenuScreen.h
139 lines (132 loc) · 3.81 KB
/
MenuScreen.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#pragma once
#include "LcdMenu.h"
#include "renderer/MenuRenderer.h"
#include "utils/constants.h"
#include <MenuItem.h>
#include <utils/utils.h>
/**
* @class MenuScreen
* @brief Represents single screen with number of menu items.
* Contains logic of navigating between items. Stores current cursor and view.
*/
class MenuScreen {
friend LcdMenu;
private:
/**
* @brief Previous screen.
* When `BACK` command received this screen will be shown.
*/
MenuScreen* parent = NULL;
/**
* @brief The menu items to be displayed on screen.
* These items will be drawn on renderer.
*/
MenuItem** items = NULL;
/**
* @brief Cursor position.
*
* ```
* X X
* X X X X X
* ┌───────────────┐
* │ X X X │
* │ X X X X │
* │ > X X X X X X │ <── cursor
* │ X X X X X │
* └───────────────┘
* X X X X
* X X X X X X
* ```
*
* When `up` or `down` then this position will be moved over the items accordingly.
* Always in range [`view`, `view` + `renderer.getMaxRows()` - 1].
*/
uint8_t cursor = 0;
/**
* @brief First visible item's position in the menu array.
*
* ```
* X X
* X X X X X
* ┌───────────────┐
* │ X X X │ <── view
* │ X X X X │
* │ > X X X X X X │
* │ X X X X X │
* └───────────────┘
* X X X X
* X X X X X X
* ```
*
* Is moved when `cursor` crosses the borders of visible area.
* When number of items < `renderer.getMaxRows()` this index should be 0.
* The size of the view is always the same and equals to `renderer.getMaxRows()`.
*/
uint8_t view = 0;
uint8_t itemCount = 0;
public:
/**
* Constructor
*/
MenuScreen(MenuItem** items);
/**
* @brief Set new parent screen.
*/
void setParent(MenuScreen* parent);
/**
* @brief Get current cursor position.
*/
uint8_t getCursor();
/**
* @brief Get a `MenuItem` at position.
* @return `MenuItem` - item at `position`
*/
MenuItem* getItemAt(uint8_t position);
/**
* @brief Get a `MenuItem` at position.
* @return `MenuItem` - item at `position`
*/
MenuItem* operator[](const uint8_t position);
protected:
/**
* @brief Move cursor to specified position.
*/
void setCursor(MenuRenderer* renderer, uint8_t position);
/**
* @brief Draw the screen on screen.
* @param renderer The renderer to use for drawing.
*/
void draw(MenuRenderer* renderer);
/**
* @brief Sync indicators with the renderer.
*/
void syncIndicators(uint8_t index, MenuRenderer* renderer);
/**
* @brief Process the command.
* @return `true` if the command was processed, `false` otherwise.
*/
bool process(LcdMenu* menu, const unsigned char command);
/**
* @brief Move cursor up.
*/
void up(MenuRenderer* renderer);
/**
* @brief Move cursor down.
*/
void down(MenuRenderer* renderer);
/**
* @brief Reset the screen to initial state.
*/
void reset(MenuRenderer* renderer);
/**
* @brief Poll the screen for changes.
* @param renderer The renderer to use for drawing.
* @param pollInterval The interval to poll the screen.
*/
void poll(MenuRenderer* renderer, uint16_t pollInterval);
};
#define MENU_SCREEN(screen, items, ...) \
extern MenuItem* items[]; \
extern MenuScreen* screen; \
MenuItem* items[] = {__VA_ARGS__, nullptr}; \
MenuScreen* screen = new MenuScreen(items)