-
Notifications
You must be signed in to change notification settings - Fork 9
/
MenuBackend.h
executable file
·264 lines (234 loc) · 6.32 KB
/
MenuBackend.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
||
|| @file MenuBackend.h
|| @version 1.4
|| @author Alexander Brevig
|| @contact [email protected]
|| @contribution Adrian Brzezinski [email protected], http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=viewprofile;username=vzhang
||
|| @description
|| | Provide an easy way of making menus
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/
#ifndef MenuBackend_h
#define MenuBackend_h
/*
A menu item will be a container for an item that is a part of a menu
Each such item has a logical position in the hierarchy as well as a text and maybe a mnemonic shortkey
*/
class MenuItem {
public:
MenuItem(const char* itemName, char shortKey='\0' ) : name(itemName), shortkey(shortKey) {
before = right = after = left = 0;
}
//void use(){} //update some internal data / statistics
inline const char* getName() const { return name; }
inline const char getShortkey() const { return shortkey; }
inline const bool hasShortkey() const { return (shortkey!='\0'); }
inline void setBack(MenuItem *b) { back = b; }
inline MenuItem* getBack() const { return back; }
inline MenuItem* getBefore() const { return before; }
inline MenuItem* getRight() const { return right; }
inline MenuItem* getAfter() const { return after; }
inline MenuItem* getLeft() const { return left; }
MenuItem *moveBack() { return back; }
MenuItem *moveUp() {
if (before) { before->back = this; }
return before;
}
MenuItem *moveDown() {
if (after) { after->back = this; }
return after;
}
MenuItem *moveLeft() {
if (left) { left->back = this; }
return left;
}
MenuItem *moveRight() {
if (right) { right->back = this; }
return right;
}
//default vertical menu
MenuItem &add(MenuItem &mi) { return addAfter(mi); }
MenuItem &addBefore(MenuItem &mi) {
mi.after = this;
before = &mi;
if ( !mi.back ) mi.back = back;
return mi;
}
MenuItem &addRight(MenuItem &mi) {
mi.left = this;
right = &mi;
if ( !mi.back ) mi.back = back;
return mi;
}
MenuItem &addAfter(MenuItem &mi) {
mi.before = this;
after = &mi;
if ( !mi.back ) mi.back = back;
return mi;
}
MenuItem &addLeft(MenuItem &mi) {
mi.right = this;
left = &mi;
if ( !mi.back ) mi.back = back;
return mi;
}
protected:
const char* name;
const char shortkey;
MenuItem *before;
MenuItem *right;
MenuItem *after;
MenuItem *left;
MenuItem *back;
};
//no dependant inclusion of string or cstring
bool menuTestStrings(const char *a, const char *b) {
while (*a) { if (*a != *b) { return false; } b++; a++; }
return true;
}
bool operator==(MenuItem &lhs, char* test) {
return menuTestStrings(lhs.getName(),test);
}
bool operator==(const MenuItem &lhs, char* test) {
return menuTestStrings(lhs.getName(),test);
}
bool operator==(MenuItem &lhs, MenuItem &rhs) {
return menuTestStrings(lhs.getName(),rhs.getName());
}
bool operator==(const MenuItem &lhs, MenuItem &rhs) {
return menuTestStrings(lhs.getName(),rhs.getName());
}
struct MenuChangeEvent {
const MenuItem &from;
const MenuItem &to;
};
struct MenuUseEvent {
const MenuItem &item;
};
typedef void (*cb_change)(MenuChangeEvent);
typedef void (*cb_use)(MenuUseEvent);
class MenuBackend {
public:
MenuBackend(cb_use menuUse, cb_change menuChange = 0) : root("MenuRoot") {
current = &root;
cb_menuChange = menuChange;
cb_menuUse = menuUse;
}
MenuItem &getRoot() {
return root;
}
MenuItem &getCurrent() {
return *current;
}
void moveBack() {
setCurrent(current->getBack());
}
void moveUp() {
setCurrent(current->moveUp());
}
void moveDown() {
setCurrent(current->moveDown());
}
void moveLeft() {
setCurrent(current->moveLeft());
}
void moveRight() {
setCurrent(current->moveRight());
}
void use(char shortkey)
{
recursiveSearch(shortkey,&root);
use();
}
void use() {
//current->use();
if (cb_menuUse) {
MenuUseEvent mue = { *current };
cb_menuUse(mue);
}
}
private:
void setCurrent( MenuItem *next ) {
if (next) {
if (cb_menuChange) {
MenuChangeEvent mce = { *current, *next };
(*cb_menuChange)(mce);
}
current = next;
}
}
void foundShortkeyItem(MenuItem *mi) {
mi->setBack(current);
current = mi;
}
char canSearch(const char shortkey, MenuItem *m) {
if (m==0) { return 0; }
else {
if (m->getShortkey()==shortkey) {
foundShortkeyItem(m);
return 1;
}
return -1;
}
}
void rSAfter(const char shortkey, MenuItem *m) {
if (canSearch(shortkey,m)!=1) {
rSAfter(shortkey, m->getAfter());
rSRight(shortkey, m->getRight());
rSLeft(shortkey, m->getLeft());
}
}
void rSRight(const char shortkey, MenuItem *m) {
if (canSearch(shortkey,m)!=1) {
rSAfter(shortkey, m->getAfter());
rSRight(shortkey, m->getRight());
rSBefore(shortkey, m->getBefore());
}
}
void rSLeft(const char shortkey, MenuItem *m) {
if (canSearch(shortkey,m)!=1) {
rSAfter(shortkey, m->getAfter());
rSLeft(shortkey, m->getLeft());
rSBefore(shortkey, m->getBefore());
}
}
void rSBefore(const char shortkey, MenuItem *m) {
if (canSearch(shortkey,m)!=1) {
rSRight(shortkey, m->getRight());
rSLeft(shortkey, m->getLeft());
rSBefore(shortkey, m->getBefore());
}
}
void recursiveSearch(const char shortkey, MenuItem *m) {
if (canSearch(shortkey,m)!=1) {
rSAfter(shortkey, m->getAfter());
rSRight(shortkey, m->getRight());
rSLeft(shortkey, m->getLeft());
rSBefore(shortkey, m->getBefore());
}
}
MenuItem root;
MenuItem *current;
cb_change cb_menuChange;
cb_use cb_menuUse;
};
#endif