-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuArea.cpp
More file actions
182 lines (133 loc) · 4.15 KB
/
Copy pathMenuArea.cpp
File metadata and controls
182 lines (133 loc) · 4.15 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
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
//
// Created by codexzier on 01.05.26.
//
#include "MenuArea.h"
#include "GC9A01_LTSM.hpp"
#include "fonts_LTSM/FontArialBold_LTSM.hpp" // 16x16 pixels
void MenuArea::drawUpdate() {
// draw only if menu items exists
if (!_menuItems) {
return;
}
drawInputValues();
drawMenu();
if (_timeup < 60) {
_ring1.setValue(_timeup);
_timeup++;
}
else {
_timeup = 0;
}
_ring1.drawGaugeUpdate();
}
// not sure if this is the best approach
void MenuArea::setMaxIndexPage() {
float maxIndexPage1 = static_cast<int>(_menuItems->size()) / 4.0f;
int maxIndexPage2 = static_cast<int>(_menuItems->size()) / 4;
if (maxIndexPage1 > (float)maxIndexPage2) {
maxIndexPage2++;
}
_menuIndexPageMax = maxIndexPage2;
}
void MenuArea::setIndexPage() {
// get the current index page based on the menu index
// example: is index 1, it is page 1 and it is index 4, it is page 2.
const float current = (static_cast<float>(_menuIndex) + 1.0f) / 4.0f;
int currentInt = (_menuIndex + 1) / 4;
if (current > static_cast<float>(currentInt)) {
currentInt++;
_tft->clearBuffer(_tft->C_BLACK);
}
_menuIndexPage = currentInt;
}
void MenuArea::addItem(const MenuItem& item) {
if (!_menuItems) {
_menuItems = std::make_unique<std::vector<MenuItem*>>();
}
// Erstelle eine Kopie des MenuItem-Objekts auf dem Heap
MenuItem* newItem = new MenuItem;
newItem->MenuName = item.MenuName; // std::string kopiert automatisch
newItem->MenuDescription = item.MenuDescription;
// Füge den Zeiger zum Vector hinzu
_menuItems->push_back(newItem);
_menuPaging.drawFrameAndPage(_menuIndexPage, _menuIndexPageMax);
setMaxIndexPage();
}
void MenuArea::drawMenu() {
drawClearByChangedPage();
const int maxEntries = 4;
const int yPositions[] = {_menuY1, _menuY2, _menuY3, _menuY4};
bool nothing = false;
for (int i = 0; i < maxEntries; ++i) {
int indexItem = i + ((_menuIndexPage - 1) * 4);
if (indexItem < 0 || indexItem >= static_cast<int>(_menuItems->size())) {
nothing = true;
}
if (nothing) {
continue;
}
bool isSelected = (indexItem == _menuIndex);
MenuItem* item = (*_menuItems)[indexItem];
if (_isInitializedDrawMenu && item->isSelected == isSelected) {
continue;
}
uint16_t color = isSelected ? _colorOn : _colorOff;
_tft->drawRoundRect(_menuX, yPositions[i], _width, _height, _cornerRadius, color);
item->isSelected = isSelected;
_tft->setFont(FontArialBold);
_tft->setTextColor(color, _tft->C_BLACK);
_tft->setCursor(_menuX + 10, yPositions[i] + 6);
_tft->print(item->MenuName.c_str());
}
drawMenuPaging();
_isInitializedDrawMenu = true;
}
void MenuArea::drawClearByChangedPage() {
if (_menuIndexPage != _menuIndexPageSelected) {
_isInitializedDrawMenu = false;
_tft->fillScreen(_tft->C_BLACK);
}
_menuIndexPageSelected = _menuIndexPage;
}
void MenuArea::drawMenuPaging() {
_menuPaging.drawFrameAndPage(_menuIndexPage, _menuIndexPageMax);
}
void MenuArea::setButton1() {
if (_menuIndex < 0 || _menuIndex >= static_cast<int>(_menuItems->size()))
{
_menuIndex = 0;
}
else {
_menuIndex++;
}
setIndexPage();
_isInitializedDrawMenu = false;
}
int MenuArea::getMenuSelectIndex() {
return _menuIndex;
}
void MenuArea::drawInputValues() {
_tft->setFont(FontDefault);
_tft->setTextColor(_tft->C_DGREY, _tft->C_BLACK);
_tft->setCursor(80, 30);
char buffer[12];
sprintf(buffer, "%d", _value1);
_tft->print(buffer);
_tft->setCursor(140, 30);
sprintf(buffer, "%d", _value2);
_tft->print(buffer);
}
void MenuArea::reset() {
_isInitializedDrawMenu = false;
_menuIndex = 0;
_menuIndexPage = 1;
//_menuPaging.drawFrameAndPage(_menuIndexPage, _menuIndexPageMax);
_tft->clearBuffer(_tft->C_BLACK);
}
MenuArea::~MenuArea() {
if (_menuItems) {
for (MenuItem* item : *_menuItems) {
delete item;
}
}
}