-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu.cpp
145 lines (130 loc) · 3.18 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
143
144
145
/*
* author : Shuichi TAKANO
* since : Sat Mar 09 2024 05:30:23
*/
#include "menu.h"
#include "pad_state.h"
#include "font.h"
void Menu::setOpenCloseFunc(OpenCloseFunc f)
{
onOpenClose_ = f;
if (f)
{
f(open_);
}
}
void Menu::refresh()
{
textScreen_->clearLayer(layer_);
if (onOpenClose_)
{
onOpenClose_(open_);
}
if (open_)
{
textScreen_->setFont(0, getLeftRightFont());
textScreen_->setFont(1, getUpDownFont());
}
}
void Menu::update(int dclk,
uint32_t pad,
bool menuButtonRelease, bool menuButtonLongPress)
{
blinkCounter_ += dclk;
if (blinkCounter_ >= blinkInterval_)
{
blinkCounter_ -= blinkInterval_;
blinkPhase_ ^= true;
}
pad_ = pad;
auto testButton = [&](PadStateButton b)
{
return ((pad ^ padPrev_) & pad) & (1u << static_cast<int>(b));
};
if (menuButtonRelease)
{
open_ = !open_;
currentItem_ = 0;
refresh();
}
else if (open_)
{
auto &item = items_[currentItem_];
if (testButton(PadStateButton::UP))
{
currentItem_ = (currentItem_ + items_.size() - 1) % items_.size();
}
else if (testButton(PadStateButton::DOWN))
{
currentItem_ = (currentItem_ + 1) % items_.size();
}
else if (testButton(PadStateButton::LEFT))
{
if (item.value)
{
*item.value = std::max(item.range.first, *item.value - 1);
if (item.onValueChange)
{
item.onValueChange(*this);
}
changed_ = true;
}
}
else if (testButton(PadStateButton::RIGHT))
{
if (item.value)
{
*item.value = std::min(item.range.second, *item.value + 1);
if (item.onValueChange)
{
item.onValueChange(*this);
}
changed_ = true;
}
}
else
{
if (item.onButton &&
((item.menuButton && menuButtonLongPress) ||
(!item.menuButton && testButton(PadStateButton::A))))
{
item.onButton(*this);
changed_ = true;
}
}
}
padPrev_ = pad;
}
void Menu::render() const
{
if (!open_)
{
return;
}
textScreen_->clearLayer(layer_);
auto &item = items_[currentItem_];
textScreen_->print(0, 0, layer_, item.name);
if (item.valueTexts)
{
textScreen_->print(0, 1, layer_, item.valueTexts[*item.value]);
}
else if (item.valueFormat)
{
char buf[16];
// snprintf(buf, sizeof(buf), item.valueFormat, *item.value);
item.valueFormat(buf, sizeof(buf), *item.value);
textScreen_->print(0, 1, layer_, buf);
}
else if (item.valueText)
{
textScreen_->print(0, 1, layer_, item.valueText);
}
if (blinkPhase_)
{
if (item.value)
{
textScreen_->print(7, 1, layer_, "\240");
}
textScreen_->print(7, 0, layer_, "\1");
}
}