-
Notifications
You must be signed in to change notification settings - Fork 4
/
Menu.cpp
executable file
·85 lines (60 loc) · 1.81 KB
/
Menu.cpp
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
#include "Menu.h"
Menu::Menu(MenuItem* parent, const FlashString* text, MenuEnterCallback enter_cb) : MenuItem(parent, text) {
this->firstEntry = NULL;
this->lastEntry = NULL;
this->enter_cb = enter_cb;
};
MenuItem* Menu::addItem(MenuItem* item) {
ListEntry* e = new ListEntry(item);
if(firstEntry) {
lastEntry->next = e;
e->prev = lastEntry;
} else
firstEntry = e;
lastEntry = e;
selectedItem = firstEntry;
return item;
}
void Menu::clearItems() {
ListEntry* e = this->firstEntry;
ListEntry* next;
while(e) {
next = e->next;
delete e;
e = next;
}
this->firstEntry = NULL;
this->lastEntry = NULL;
}
bool Menu::activate() {
if (this->enter_cb)
this->enter_cb(this);
// Select the first item when entering the menu
selectedItem = lastEntry;
// And jump to the next. If firstElement is enabled, it will stop there,
// otherwise it will find the first enabled item.
doNext();
}
void Menu::doNext() {
// TODO: infinite loop if all entries are disabled??
do {
selectedItem = selectedItem->next;
if(!selectedItem) selectedItem = firstEntry;
} while(!selectedItem->item->isEnabled());
}
void Menu::doPrev() {
// TODO: infinite loop if all entries are disabled??
do {
selectedItem = selectedItem->prev;
if(!selectedItem) selectedItem = lastEntry;
} while(!selectedItem->item->isEnabled());
}
MenuItem* Menu::action() {
// Let's the Item do something to start
int takeControl = selectedItem->item->activate();
// Then let it take control, or maintain it.
if(takeControl)
return selectedItem->item;
else
return NULL;
}