-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.cpp
More file actions
391 lines (315 loc) · 8.23 KB
/
Menu.cpp
File metadata and controls
391 lines (315 loc) · 8.23 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
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
#include "Menu.hpp"
#include "plugin.hpp"
void MenuEntry::setParentMenu(ParentMenu *parent)
{
m_parentMenu = parent;
}
//////////////////////////////////// ParentMenu
void ParentMenu::leaveSubMenu() {
m_subMenu = nullptr;
}
void ParentMenu::enterSubMenu(Menu *menu) {
m_subMenu = menu;
}
//////////////////////////////////// SimpleMenu
SimpleMenu::SimpleMenu(MenuEntry **entries, int32_t count)
: m_entryList(entries)
, m_entryCount(count)
{
for(int32_t i = 0; i < count; i++)
m_entryList[i]->setParentMenu(this);
}
bool SimpleMenu::process()
{
if (m_subMenu != nullptr) {
bool ret = m_subMenu->process();
if (!ret)
return false;
} else {
if (patch.encoder.RisingEdge()) {
m_entryList[m_idx]->onClick();
}
int32_t inc = patch.encoder.Increment();
if (inc > 0) {
if (m_idx < m_entryCount - 1)
m_idx++;
} else if (inc < 0) {
if (m_idx > 0)
m_idx--;
}
}
patch.display.Fill(false);
if (m_idx < m_showIdx)
m_showIdx = m_idx;
else if (m_idx >= m_showIdx + 6)
m_showIdx = m_idx - 5;
int line = 0;
for (int32_t i = m_showIdx; i < std::min(m_showIdx + 6, m_entryCount); i++) {
if (i == m_idx)
patch.display.DrawRect(0, line*10, patch.display.Width() - 1, line*10 + 9, true);
m_entryList[i]->print( line, i != m_idx );
line++;
}
return false;
}
//////////////////////////////////// KVMenuEntry
KVMenuEntry::KVMenuEntry(const char *name)
: m_name(name)
{
m_paramPos = strlen(m_name) + 1;
}
void KVMenuEntry::print(int line, bool on)
{
patch.display.SetCursor(0, line * 10);
patch.display.WriteString((char*)m_name, Font_7x10, on);
patch.display.SetCursor(m_paramPos * 7, line * 10);
patch.display.WriteString((char*)repr().c_str(), Font_7x10, on);
}
const std::string KVMenuEntry::label() const
{
return m_name;
}
//////////////////////////////////// KVEditMenuEntry
KVEditMenuEntry::KVEditMenuEntry(const char *name)
: KVMenuEntry(name)
{
}
bool KVEditMenuEntry::process()
{
if (patch.encoder.RisingEdge()) {
m_parentMenu->leaveSubMenu();
return true;
}
int32_t inc = patch.encoder.Increment();
increment(inc);
return true;
}
void KVEditMenuEntry::onClick()
{
m_parentMenu->enterSubMenu(this);
}
//////////////////////////////////// LabelMenuEntry
LabelEntry::LabelEntry(const char* name)
: m_name(name)
{
}
void LabelEntry::print(int line, bool on)
{
patch.display.SetCursor(0, line * 10);
patch.display.WriteString((char*)m_name, Font_7x10, on);
}
void LabelEntry::onClick()
{
//N/A
}
//////////////////////////////////// BackMenuEntry
void BackMenuEntry::print(int line, bool on) {
patch.display.SetCursor(0, line * 10);
patch.display.WriteString((char*)"..", Font_7x10, on);
}
void BackMenuEntry::onClick() {
m_parentMenu->leaveSubMenu();
}
//////////////////////////////////// SubMenuEntry
SubMenuEntry::SubMenuEntry(const char *name, MenuEntry**entries, int32_t count)
: SimpleMenu(entries, count)
, m_name(name)
{}
void SubMenuEntry::print(int line, bool on) {
patch.display.SetCursor(0, line * 10);
patch.display.WriteString((char*)m_name, Font_7x10, on);
}
void SubMenuEntry::onClick() {
m_parentMenu->enterSubMenu(this);
}
////////////////////////////////// BoolMenuEntry
BoolMenuEntry::BoolMenuEntry(const char *name, bool defaultValue)
: KVMenuEntry(name)
, m_value(defaultValue)
{
}
std::string BoolMenuEntry::repr() const
{
return m_value ? "Y" : "N";
}
void BoolMenuEntry::onClick()
{
m_value = !m_value;
}
////////////////////////////////// SetMenuEntry
SetMenuEntry::SetMenuEntry(const char *name, const char **labels, int32_t maxValue)
: KVEditMenuEntry(name)
, m_labels(labels)
, m_maxValue(maxValue)
{
}
void SetMenuEntry::setSet(const char **labels, int32_t maxValue) {
m_labels = labels;
m_maxValue = maxValue;
m_value = 0;
}
void SetMenuEntry::increment(int val)
{
m_value = (m_value + val + m_maxValue) % m_maxValue;
}
std::string SetMenuEntry::repr() const
{
if (!m_labels || m_maxValue == 0)
return "N/A";
return m_labels[m_value];
}
int32_t SetMenuEntry::value() const {
if (m_maxValue == 0)
return -1;
return m_value;
}
ShowParamEntry::ShowParamEntry(const char *name)
: KVMenuEntry(name)
{
}
std::string ShowParamEntry::repr() const
{
if (!m_param) {
return "N/A";
} else {
return std::to_string((int)(m_param->Value() * 1000.f));
}
}
void ShowParamEntry::onClick()
{
//N/A
}
void ShowParamEntry::setParam(Parameter *param)
{
m_param = param;
}
///////////////////////////////// ShowROStringEntry
ShowROStringEntry::ShowROStringEntry(const char* name)
: KVMenuEntry(name)
{
}
std::string ShowROStringEntry::repr() const
{
return m_value;
}
void ShowROStringEntry::onClick() {}
void ShowROStringEntry::setValue(const std::string& value)
{
m_value = value;
}
////////////////////////////////// RangeParamEntry
RangeParamEntry::RangeParamEntry(const char *name, float min, float max, int initial, int steps, Parameter::Curve scale)
: KVEditMenuEntry(name)
, m_min(min)
, m_max(max)
, m_intValue(initial)
, m_steps(steps)
, m_scale(scale)
{
m_lmin = logf(min < 0.0000001f ? 0.0000001f : min);
m_lmax = logf(max);
increment(0);
}
std::string RangeParamEntry::repr() const
{
return std::to_string((int)(m_value * 1000.f));
}
void RangeParamEntry::increment(int inc)
{
if (m_bind != -1)
return;
m_intValue += inc;
if (m_intValue < 0)
m_intValue = 0;
if (m_intValue > m_steps)
m_intValue = m_steps;
float val = (float)m_intValue / (float)m_steps;
switch(m_scale)
{
case Parameter::LINEAR:
m_value = (val * (m_max - m_min)) + m_min;
break;
case Parameter::EXPONENTIAL:
m_value = ((val * val) * (m_max -m_min)) + m_min;
break;
case Parameter::LOGARITHMIC:
m_value = expf((val * (m_lmax - m_lmax)) + m_lmin);
break;
case Parameter::CUBE:
m_value = ((val * (val * val)) * (m_max - m_min)) + m_min;
break;
default: break;
}
}
void RangeParamEntry::bind(int b)
{
m_bind = b;
if (b != -1)
m_param.Init(patch.controls[b], m_min, m_max, m_scale);
else
increment(0);
}
void RangeParamEntry::processParam()
{
if (m_bind == -1)
return;
m_value = m_param.Process() * 0.05 + m_value * 0.95;
}
BindParamEntry::BindParamEntry(const char* name, int hwCtrl, RangeParamEntry** entries, BindParamEntry** bindedEntries, size_t count)
: KVEditMenuEntry(name)
, m_hwCtrl(hwCtrl)
, m_entries(entries)
, m_bindedEntries(bindedEntries)
, m_count(count)
{
}
std::string BindParamEntry::repr() const
{
if (m_index == -1) {
return "NONE";
} else {
return m_entries[m_index]->label();
}
}
void BindParamEntry::increment(int inc)
{
if (inc == 0)
return;
int m_oldIndex = m_index;
do {
m_index += inc;
if (m_index <= -1) {
m_index = -1;
break;
}
if (m_index >= (int)m_count) {
m_index = m_oldIndex;
break;
}
} while (m_index != -1 && m_bindedEntries[m_index] != nullptr);
if (m_oldIndex == m_index)
return;
if (m_oldIndex != -1) {
m_entries[m_oldIndex]->bind(-1);
m_bindedEntries[m_oldIndex] = nullptr;
}
if (m_index != -1) {
m_entries[m_index]->bind(m_hwCtrl);
m_bindedEntries[m_index] = this;
}
}
BindAllParamEntry::BindAllParamEntry(RangeParamEntry** entries, size_t count)
: m_bindedEntries(new class BindParamEntry*[count] )
, p1("P1", DaisyPatch::CTRL_1, entries, m_bindedEntries, count)
, p2("P2", DaisyPatch::CTRL_2, entries, m_bindedEntries, count)
, p3("P3", DaisyPatch::CTRL_3, entries, m_bindedEntries, count)
, p4("P4", DaisyPatch::CTRL_4, entries, m_bindedEntries, count)
{
for (size_t i =0; i < count; i++) {
m_bindedEntries = nullptr;
}
}
BindAllParamEntry::~BindAllParamEntry()
{
delete[] m_bindedEntries;
}