-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMenu.cpp
142 lines (124 loc) · 3.64 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
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
/*
Menu.cpp - Library for universal menu.
Created by Petr Vavrin, 2019
Released into the public domain.
*/
#include "Arduino.h"
#include "Menu.h"
Menu::Menu()
{
}
void Menu::init(Menu::ITEM * item) {
this->mainItem = item;
this->activeItem = item;
}
void Menu::reset() {
this->activeItem = this->mainItem;
}
void Menu::addItem (Menu::ITEM * item, char hotkey, char * text, Menu::ITEM * parent, bool (* cb)(), bool (* value_cb)(bool x)) {
Menu::ITEM * childItem;
if (parent == NULL) {
parent = &this->rootItem;
}
item->parent = parent;
item->prev = NULL;
item->next = NULL;
item->child = NULL;
item->text = text;
item->cb = cb;
item->value_cb = value_cb;
item->uid = this->itemCounter;
item->hotkey = hotkey;
item->disabled = false;
this->itemCounter += 1;
if (parent->child == NULL) {
parent->child = item;
} else {
childItem = parent->child;
while (childItem->next) {
childItem = childItem->next;
}
childItem->next = item;
item->prev = childItem;
}
}
void Menu::action(byte key) {
if (this->circular && key == this->KEY_UP && this->activeItem->prev == NULL) {
while (this->activeItem->next) {
this->activeItem = this->activeItem->next;
}
} else if (this->circular && key == this->KEY_DOWN && this->activeItem->next == NULL) {
while (this->activeItem->prev) {
this->activeItem = this->activeItem->prev;
}
} else if (key == this->KEY_UP && this->activeItem->prev != NULL) {
this->activeItem = this->activeItem->prev;
} else if (key == this->KEY_DOWN && this->activeItem->next != NULL) {
this->activeItem = this->activeItem->next;
} else if (key == this->KEY_LEFT && this->activeItem->parent != NULL && this->activeItem->parent->text != NULL) {
this->activeItem = this->activeItem->parent;
} else if (key == this->KEY_LEFT && (this->activeItem->parent == NULL || this->activeItem->parent->text == NULL) && this->menuExit != NULL) {
(this->menuExit)();
return;
} else if (this->activeItem->disabled == false) {
if ((key == this->KEY_RIGHT || key == this->KEY_RIGHT_ENTER) && this->activeItem->child != NULL) {
this->activeItem = this->activeItem->child;
} else if ((key == this->KEY_ENTER || key == this->KEY_RIGHT_ENTER) && this->activeItem->cb != NULL) {
(this->activeItem->cb)();
}
}
this->show();
}
void Menu::actionHotkey (char hotkey) {
Menu::ITEM * item = this->activeItem;
while (item->prev) {
item = item->prev;
}
do {
if (item->hotkey == hotkey) {
this->activeItem = item;
this->action(this->KEY_RIGHT_ENTER);
break;
} else if (item->next != NULL){
item = item->next;
} else {
break;
}
} while (true);
}
void Menu::show() {
if (this->preRender != NULL) {
(this->preRender)();
}
Menu::ITEM * showItem = this->activeItem;
int itemsCounter = this->itemsLimit;
bool iterate = true;
if (this->showTitle) {
if (this->renderTitle != NULL) {
(this->renderTitle)(this->activeItem->parent);
}
itemsCounter -= 1;
}
if (this->showPreviousitems) {
while (showItem->prev) {
showItem = showItem->prev;
}
}
do {
if (this->renderItem != NULL) {
(this->renderItem)(showItem, this->activeItem->uid, this->markActive, this->inlineValue);
}
itemsCounter -= 1;
if (itemsCounter > 0 && showItem->next != NULL){
showItem = showItem->next;
} else {
iterate = false;
}
} while (iterate);
if (this->showValue && this->activeItem->parent != NULL) {
showItem = this->activeItem->parent;
if (showItem->value_cb != NULL) {
(showItem->value_cb)(false);
}
}
}