-
Notifications
You must be signed in to change notification settings - Fork 85
/
MenuSystem.cpp
410 lines (324 loc) · 9.83 KB
/
MenuSystem.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
* Copyright (c) 2015, 2016 arduino-menusystem
* Licensed under the MIT license (see LICENSE)
*/
#include "MenuSystem.h"
#include <stdlib.h>
// *********************************************************
// MenuComponent
// *********************************************************
MenuComponent::MenuComponent(const char* name, SelectFnPtr select_fn)
: _name(name),
_has_focus(false),
_is_current(false),
_select_fn(select_fn) {
}
const char* MenuComponent::get_name() const {
return _name;
}
void MenuComponent::set_name(const char* name) {
_name = name;
}
bool MenuComponent::has_focus() const {
return _has_focus;
}
bool MenuComponent::is_current() const {
return _is_current;
}
void MenuComponent::set_current(bool is_current) {
_is_current = is_current;
}
Menu* MenuComponent::select() {
if (_select_fn != nullptr)
_select_fn(this);
return nullptr;
}
void MenuComponent::set_select_function(SelectFnPtr select_fn) {
_select_fn = select_fn;
}
// *********************************************************
// Menu
// *********************************************************
Menu::Menu(const char* name, SelectFnPtr select_fn)
: MenuComponent(name, select_fn),
_p_current_component(nullptr),
_menu_components(nullptr),
_p_parent(nullptr),
_num_components(0),
_current_component_num(0),
_previous_component_num(0) {
}
bool Menu::next(bool loop) {
_previous_component_num = _current_component_num;
if (!_num_components) {
return false;
} else if (_current_component_num != _num_components - 1) {
_current_component_num++;
_p_current_component = _menu_components[_current_component_num];
_p_current_component->set_current();
_menu_components[_previous_component_num]->set_current(false);
return true;
} else if (loop) {
_current_component_num = 0;
_p_current_component = _menu_components[_current_component_num];
_p_current_component->set_current();
_menu_components[_previous_component_num]->set_current(false);
return true;
}
return false;
}
bool Menu::prev(bool loop) {
_previous_component_num = _current_component_num;
if (!_num_components) {
return false;
} else if (_current_component_num != 0) {
_current_component_num--;
_p_current_component = _menu_components[_current_component_num];
_p_current_component->set_current();
_menu_components[_previous_component_num]->set_current(false);
return true;
} else if (loop) {
_current_component_num = _num_components - 1;
_p_current_component = _menu_components[_current_component_num];
_p_current_component->set_current();
_menu_components[_previous_component_num]->set_current(false);
return true;
}
return false;
}
Menu* Menu::activate() {
if (!_num_components)
return nullptr;
MenuComponent* pComponent = _menu_components[_current_component_num];
if (pComponent == nullptr)
return nullptr;
return pComponent->select();
}
Menu* Menu::select() {
MenuComponent::select();
return this;
}
void Menu::reset() {
for (int i = 0; i < _num_components; ++i)
_menu_components[i]->reset();
_p_current_component->set_current(false);
_previous_component_num = 0;
_current_component_num = 0;
_p_current_component = _num_components ? _menu_components[0] : nullptr;
_p_current_component->set_current();
}
void Menu::add_item(MenuItem* p_item) {
add_component((MenuComponent*) p_item);
}
void Menu::add_menu(Menu* p_menu) {
add_component((MenuComponent*) p_menu);
p_menu->set_parent(this);
}
void Menu::add_component(MenuComponent* p_component) {
// Resize menu component list, keeping existing items.
// If it fails, there the item is not added and the function returns.
_menu_components = (MenuComponent**) realloc(_menu_components,
(_num_components + 1)
* sizeof(MenuComponent*));
if (_menu_components == nullptr)
return;
_menu_components[_num_components] = p_component;
if (_num_components == 0) {
_p_current_component = p_component;
_p_current_component->set_current();
}
_num_components++;
}
Menu const* Menu::get_parent() const {
return _p_parent;
}
void Menu::set_parent(Menu* p_parent) {
_p_parent = p_parent;
}
MenuComponent const* Menu::get_menu_component(uint8_t index) const {
return _menu_components[index];
}
MenuComponent const* Menu::get_current_component() const {
return _p_current_component;
}
uint8_t Menu::get_num_components() const {
return _num_components;
}
uint8_t Menu::get_current_component_num() const {
return _current_component_num;
}
uint8_t Menu::get_previous_component_num() const {
return _previous_component_num;
}
void Menu::render(MenuComponentRenderer const& renderer) const {
renderer.render_menu(*this);
}
// *********************************************************
// BackMenuItem
// *********************************************************
BackMenuItem::BackMenuItem(const char* name, SelectFnPtr select_fn,
MenuSystem* ms)
: MenuItem(name, select_fn),
_menu_system(ms) {
}
Menu* BackMenuItem::select() {
if (_select_fn!=nullptr)
_select_fn(this);
if (_menu_system!=nullptr)
_menu_system->back();
return nullptr;
}
void BackMenuItem::render(MenuComponentRenderer const& renderer) const {
renderer.render_back_menu_item(*this);
}
// *********************************************************
// MenuItem
// *********************************************************
MenuItem::MenuItem(const char* name, SelectFnPtr select_fn)
: MenuComponent(name, select_fn) {
}
Menu* MenuItem::select() {
MenuComponent::select();
return nullptr;
}
void MenuItem::reset() {
// Do nothing.
}
void MenuItem::render(MenuComponentRenderer const& renderer) const {
renderer.render_menu_item(*this);
}
bool MenuItem::next(bool loop) {
return false;
}
bool MenuItem::prev(bool loop) {
return false;
}
// *********************************************************
// NumericMenuItem
// *********************************************************
NumericMenuItem::NumericMenuItem(const char* basename, SelectFnPtr select_fn,
float value, float min_value, float max_value,
float increment,
FormatValueFnPtr format_value_fn)
: MenuItem(basename, select_fn),
_value(value),
_min_value(min_value),
_max_value(max_value),
_increment(increment),
_format_value_fn(format_value_fn) {
if (_increment < 0.0) _increment = -_increment;
if (_min_value > _max_value) {
float tmp = _max_value;
_max_value = _min_value;
_min_value = tmp;
}
};
void NumericMenuItem::set_number_formatter(FormatValueFnPtr format_value_fn) {
_format_value_fn = format_value_fn;
}
Menu* NumericMenuItem::select() {
_has_focus = !_has_focus;
// Only run _select_fn when the user is done editing the value
if (!_has_focus && _select_fn != nullptr)
_select_fn(this);
return nullptr;
}
void NumericMenuItem::render(MenuComponentRenderer const& renderer) const {
renderer.render_numeric_menu_item(*this);
}
float NumericMenuItem::get_value() const {
return _value;
}
float NumericMenuItem::get_min_value() const {
return _min_value;
}
float NumericMenuItem::get_max_value() const {
return _max_value;
}
String NumericMenuItem::get_formatted_value() const {
String buffer;
if (_format_value_fn != nullptr)
buffer += _format_value_fn(_value);
else
buffer += _value;
return buffer;
}
void NumericMenuItem::set_value(float value) {
_value = value;
}
void NumericMenuItem::set_min_value(float value) {
_min_value = value;
}
void NumericMenuItem::set_max_value(float value) {
_max_value = value;
}
bool NumericMenuItem::next(bool loop) {
_value += _increment;
if (_value > _max_value) {
if (loop)
_value = _min_value;
else
_value = _max_value;
}
return true;
}
bool NumericMenuItem::prev(bool loop) {
_value -= _increment;
if (_value < _min_value) {
if (loop)
_value = _max_value;
else
_value = _min_value;
}
return true;
}
// *********************************************************
// MenuSystem
// *********************************************************
MenuSystem::MenuSystem(MenuComponentRenderer const& renderer)
: _p_root_menu(new Menu("", nullptr)),
_p_curr_menu(_p_root_menu),
_renderer(renderer) {
}
bool MenuSystem::next(bool loop) {
if (_p_curr_menu->_p_current_component->has_focus())
return _p_curr_menu->_p_current_component->next(loop);
else
return _p_curr_menu->next(loop);
}
bool MenuSystem::prev(bool loop) {
if (_p_curr_menu->_p_current_component->has_focus())
return _p_curr_menu->_p_current_component->prev(loop);
else
return _p_curr_menu->prev(loop);
}
void MenuSystem::reset() {
_p_curr_menu = _p_root_menu;
_p_root_menu->reset();
}
void MenuSystem::select(bool reset) {
Menu* pMenu = _p_curr_menu->activate();
if (pMenu != nullptr)
_p_curr_menu = pMenu;
else
if (reset)
this->reset();
}
bool MenuSystem::back() {
if (_p_curr_menu != _p_root_menu) {
_p_curr_menu = const_cast<Menu*>(_p_curr_menu->get_parent());
return true;
}
// We are already in the root menu
return false;
}
Menu& MenuSystem::get_root_menu() const {
return *_p_root_menu;
}
Menu const* MenuSystem::get_current_menu() const {
return _p_curr_menu;
}
void MenuSystem::display() const {
if (_p_curr_menu != nullptr)
_renderer.render(*_p_curr_menu);
}