-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_editor.cpp
More file actions
355 lines (307 loc) · 6.75 KB
/
state_editor.cpp
File metadata and controls
355 lines (307 loc) · 6.75 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
/*
* state_editor.cpp
*
* Created on: 29 dec. 2017
* Author: MisterCavespider
*/
#include "state_editor.h"
void EditorState::setup()
{
u8g2->setFont(u8g2_font_4x6_tr);
WRITEP(encc1, 0);
WRITEP(encc2, 0);
WRITEP(encc3, 0);
mdd = new drawdata_t[getSize()];
selI.curr = -1;
selI.last = -1;
selO.curr = 0;
selO.last = 0;
m = getModule(0);
onZoomedOut();
}
void EditorState::loop()
{
// Go out
if(PRESSEDP(encc1) > 0 || PRESSEDP(encc2) > 0)
{
u8g2->clearBuffer();
stateManager->setCurrentState(0);
return;
}
// Actual loop
zoomed ? whileZoomedIn() : whileZoomedOut();
}
void EditorState::onZoomedIn()
{
// Update currently selected module (in zoomOut)
// Also 'locks' it
// m = getModule(selO.curr);
uint8_t n = 0; // cursor position
uint8_t i = 0; // iterated value
u8g2->enableUTF8Print();
u8g2->clearBuffer();
u8g2->setCursor(1, n+=5);
u8g2->print(m->title());
u8g2->setCursor(1, n+=5);
u8g2->print("----");
for(i = 0; i < m->valueCount(); i++)
{
u8g2->setCursor(1, n+=6); // Give it an extra pixel for spacing
u8g2->print(m->names()[i]);
u8g2->setCursor(99, n); //28 px should be enough
u8g2->print(m->values()[i].value());
}
encc3->r.write(0);
// Make sure it's (il)legal
selI.curr = -1;
selI.curr = -1;
}
/*
* Order:
* - read curr
* - validate
* - update highlight
* - update value
*/
void EditorState::whileZoomedIn()
{
if(PRESSEDP(encc3) > 0)
{
zoomed = !zoomed;
if(zoomed) onZoomedIn();
else onZoomedOut();
return;
}
selI.curr = READP(encc3) / 4;
if(selI.curr >= m->valueCount())
{
selI.curr = m->valueCount();
}
/*
* If there's a change in selection,
* deselect the last one and select
* the current one
*/
if(selI.curr != selI.last)
{
u8g2->setDrawColor(2);
// Remove last select
if(selI.last >= 0 && selI.last < m->valueCount())
{
u8g2->drawBox(0, 10 + 6*selI.last, 128, 7);
}
// Set new select
if(selI.curr >= 0 && selI.curr < m->valueCount())
{
u8g2->drawBox(0, 10 + 6*selI.curr, 128, 7);
}
u8g2->setDrawColor(1);
}
/*
* If the value is to be changed, erase
* letters and draw them again.
*/
/*
* The first button does 1 step
*/
if(READP(encc1)/4 != 0)
{
// Set value
m->values()[selI.curr] += READP(encc1)/4;
encc1->r.write(0);
// Erase area
// u8g2->setDrawColor(1);
u8g2->drawBox(99, 10 + 6*selI.curr, 28, 7);
//Draw text in
u8g2->setDrawColor(0);
u8g2->setCursor(99, 16 + 6*selI.curr); //28 px should be enough
u8g2->print(m->values()[selI.curr].value());
u8g2->setDrawColor(1);
}
/*
* The second button does 10 steps
*/
if(READP(encc2)/4 != 0)
{
// Set value
m->values()[selI.curr] += READP(encc2)/4 * 10;
encc2->r.write(0);
// Erase area
// u8g2->setDrawColor(1);
u8g2->drawBox(99, 10 + 6*selI.curr, 28, 7);
//Draw text in
u8g2->setDrawColor(0);
u8g2->setCursor(99, 16 + 6*selI.curr); //28 px should be enough
u8g2->print(m->values()[selI.curr].value());
u8g2->setDrawColor(1);
}
// Only clear a small area for debug
u8g2->setDrawColor(0);
u8g2->drawBox(0, 40, 128, 24);
u8g2->setDrawColor(1);
char buf[40];
sprintf(buf, "c:%d, l:%d", selI.curr, selI.last);
u8g2->drawStr(0, 58, buf);
selI.last = selI.curr;
}
void EditorState::onZoomedOut()
{
u8g2->clearBuffer(); // TODO: check if necessary
// m = getModule(0);
drawModule(m);
highlightModule(m);
}
void EditorState::whileZoomedOut()
{
selO.curr = encc3->r.read()/4;
if(PRESSEDP(encc3) > 0)
{
if(selO.curr < 0)
{
u8g2->clearBuffer();
m = getModule(m->inputs()[-selO.curr-1]);
drawModule(m);
highlightModule(m);
selO.curr = 0;
selO.last = 0;
WRITEP(encc3, 0);
}
else if(selO.curr > 0)
{
u8g2->clearBuffer();
m = getModule(m->outputs()[selO.curr-1]);
drawModule(m);
highlightModule(m);
selO.curr = 0;
selO.last = 0;
WRITEP(encc3, 0);
}
else if(selO.curr == 0) {
zoomed = !zoomed;
if(zoomed) onZoomedIn();
else onZoomedOut();
return;
}
}
if(selO.curr != selO.last)
{
// Remove last select
if(selO.last < 0)
{
highlightModule(getModule(m->inputs()[-selO.last-1]));
}
else if(selO.last == 0)
{
highlightModule(m);
}
else if(selO.last > 0)
{
highlightModule(getModule(m->outputs()[selO.last-1]));
}
// Set new select
if(selO.curr < 0)
{
highlightModule(getModule(m->inputs()[-selO.curr-1]));
}
else if(selO.curr == 0)
{
highlightModule(m);
}
else if(selO.curr > 0)
{
highlightModule(getModule(m->outputs()[selO.curr-1]));
}
}
char buf[40];
sprintf(buf, "c:%d, l:%d", selO.curr, selO.last);
u8g2->drawStr(32, 58, buf);
selO.last = selO.curr;
}
void EditorState::highlightModule(Module *m)
{
drawdata_t dd = mdd[m->id()];
u8g2->setDrawColor(2);
u8g2->drawBox(dd.x-1, dd.y-3, dd.w+2, 7);
u8g2->setDrawColor(1);
}
uint8_t used(uint8_t *arr, uint8_t size)
{
uint8_t c = 0;
for(uint8_t i = 0; i < size; i++)
{
if(arr[i] < getSize())
{
c++; // Nice
}
}
return c;
}
void EditorState::drawModule(Module *m)
{
// Draw the name in the middle
drawdata_t ddm = mdd[m->id()];
ddm.w = u8g2->getStrWidth(m->title());
ddm.x = (128-ddm.w)/2;
ddm.y = 32;
mdd[m->id()] = ddm;
uint8_t i = 0;
/* USES BRACKETS SETTING */
// *
u8g2->drawStr(ddm.x, ddm.y+3, m->title());
// [
u8g2->drawHLine(ddm.x-2, ddm.y-4, 4);
u8g2->drawVLine(ddm.x-2, ddm.y-3, 7);
u8g2->drawHLine(ddm.x-2, ddm.y+4, 4);
// ]
u8g2->drawHLine(ddm.x+ddm.w-2, ddm.y-4, 4);
u8g2->drawVLine(ddm.x+ddm.w+1, ddm.y-3, 7);
u8g2->drawHLine(ddm.x+ddm.w-2, ddm.y+4, 4);
// Draw the inputs
uint8_t drawn = 0;
uint8_t toDraw = used(m->inputs(), m->inputCount());
for(i = 0; i < m->inputCount(); i++)
{
uint8_t tid = m->inputs()[i];
if(tid < getSize())
{
Module *t = getModule(tid);
drawdata_t ddt = mdd[tid];
ddt.w = u8g2->getStrWidth(t->title());
ddt.x = (128-ddm.w)/2 - (7+ddt.w);
ddt.y = 32 + (toDraw*9+toDraw-1)/2-4 -10*drawn;
mdd[tid] = ddt;
/* USES BRACKETS SETTING */
// *
u8g2->drawStr(ddt.x, ddt.y+3, t->title());
// ]
u8g2->drawHLine(ddt.x+ddt.w-2, ddt.y-4, 4);
u8g2->drawVLine(ddt.x+ddt.w+1, ddt.y-3, 7);
u8g2->drawHLine(ddt.x+ddt.w-2, ddt.y+4, 4);
drawn++;
}
}
// Draw the outputs
drawn = 0;
toDraw = used(m->outputs(), m->outputCount() * m->paralsCount());
for(i = 0; i < m->outputCount() * m->paralsCount(); i++)
{
uint8_t tid = m->outputs()[i];
if(tid < getSize())
{
Module *t = getModule(tid);
drawdata_t ddt = mdd[tid];
ddt.w = u8g2->getStrWidth(t->title());
ddt.x = (128-ddm.w)/2 + (7+ddm.w);
ddt.y = 32 + (toDraw*9+toDraw-1)/2-4 -10*drawn;
mdd[tid] = ddt;
/* USES BRACKETS SETTING */
// *
u8g2->drawStr(ddt.x, ddt.y+3, t->title());
// [
u8g2->drawHLine(ddt.x-2, ddt.y-4, 4);
u8g2->drawVLine(ddt.x-2, ddt.y-3, 7);
u8g2->drawHLine(ddt.x-2, ddt.y+4, 4);
drawn++;
}
}
}