-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
140 lines (99 loc) · 2.49 KB
/
Menu.cpp
File metadata and controls
140 lines (99 loc) · 2.49 KB
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
#include "Menu.h"
#define MENU_ITEM_SIZE 15
Menu::Menu()
{
translateY = 0;
activeIndex = 0;
hasBack = false;
title = "";
items = new SimpleList<BaseMenuItem *>;
}
int Menu::getOffsetTop(int itemIndex)
{
return (MENU_ITEM_SIZE * itemIndex) + MENU_ITEM_SIZE + translateY;
}
void Menu::addItem(String title, bool selectable)
{
MenuItem *item = new MenuItem(title, selectable);
items->add(item);
}
void Menu::addBack()
{
MenuItemBack *item = new MenuItemBack();
items->add(item);
hasBack = true;
}
void Menu::setTitle(String _title)
{
title = _title;
}
void Menu::updateItem(int index, String title, bool selectable)
{
MenuItem *item = new MenuItem(title, selectable);
items->replace(index, item);
}
void Menu::addLoading(bool loading)
{
MenuItemLoading *item = new MenuItemLoading(loading);
items->add(item);
}
void Menu::startLoading(int index)
{
MenuItemLoading *item = new MenuItemLoading(true);
items->replace(index, item);
}
void Menu::stopLoading(int index)
{
MenuItemLoading *item = new MenuItemLoading(false);
items->replace(index, item);
}
void Menu::setDisplay(U8G2 _dsp)
{
dsp = _dsp;
}
void Menu::render()
{
dsp.clearBuffer();
dsp.setFont(u8g2_font_mercutio_basic_nbp_t_all);
if( title != "" ){
int titleWidth = dsp.getStrWidth(title.c_str());
dsp.setCursor(dsp.getDisplayWidth() - titleWidth, MENU_ITEM_SIZE - 5);
dsp.print(title.substring(0,15));
}
int minTopPosition = hasBack ? MENU_ITEM_SIZE + 5 : 0;
for( int i = 0; i < items->size(); i++ ){
int posY = (MENU_ITEM_SIZE * i) + MENU_ITEM_SIZE + translateY;
BaseMenuItem *item = items->get(i);
if( ( posY < minTopPosition || posY > ( dsp.getDisplayHeight() ) ) && ! item->isFixed() ){
continue;
}
item->setSelected(activeIndex == i);
item->render(dsp, posY);
}
dsp.sendBuffer();
}
void Menu::selectNextItem()
{
if( activeIndex + 1 >= items->size() ){
return;
}
activeIndex++;
if( getOffsetTop(activeIndex) > dsp.getDisplayHeight() ){
translateY -= MENU_ITEM_SIZE;
}
}
void Menu::selectPreviousItem()
{
activeIndex--;
if( activeIndex < 0 ){
activeIndex = 0;
}
int threshold = hasBack ? MENU_ITEM_SIZE + 5 : MENU_ITEM_SIZE;
if( activeIndex > 0 && getOffsetTop(activeIndex) < threshold ){
translateY += MENU_ITEM_SIZE;
}
}
int Menu::getActiveIndex()
{
return activeIndex;
}