-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_renderer.h
More file actions
491 lines (460 loc) · 16.2 KB
/
Copy pathui_renderer.h
File metadata and controls
491 lines (460 loc) · 16.2 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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
#ifndef UI_RENDERER_H
#define UI_RENDERER_H
StudyStats calculateStudyStats();
TimeStatus currentTimeStatus();
const char *timeStatusLabel();
bool richModeEnabled();
uint16_t uiTextColor()
{
return TEXT_COLOR;
}
uint16_t uiMutedColor()
{
return currentColorMode == COLOR_MODE_MONOCHROME ? 0x8410 : MUTED_COLOR;
}
uint16_t uiDoneColor()
{
return currentColorMode == COLOR_MODE_MONOCHROME ? TEXT_COLOR : DONE_COLOR;
}
uint16_t uiHotkeyColor()
{
return currentColorMode == COLOR_MODE_MONOCHROME ? TEXT_COLOR : 0xFD20;
}
uint16_t uiSegmentOffColor()
{
return currentColorMode == COLOR_MODE_MONOCHROME ? 0x2104 : SEGMENT_OFF_COLOR;
}
uint16_t uiSegmentDimColor()
{
return currentColorMode == COLOR_MODE_MONOCHROME ? 0x4208 : SEGMENT_DIM_COLOR;
}
void drawCenteredText(const String &text, int y, int textSize, uint16_t color)
{
screenCanvas.setTextSize(textSize);
screenCanvas.setTextColor(color, BACKGROUND_COLOR);
int textWidth = screenCanvas.textWidth(text);
int x = (screenCanvas.width() - textWidth) / 2;
screenCanvas.setCursor(max(0, x), y);
screenCanvas.print(text);
}
void drawCenteredLabel(const char *text, int y, uint16_t color)
{
screenCanvas.setFont(&fonts::lgfxJapanGothic_16);
screenCanvas.setTextSize(1);
screenCanvas.setTextColor(color, BACKGROUND_COLOR);
int textWidth = screenCanvas.textWidth(text);
int x = (screenCanvas.width() - textWidth) / 2;
screenCanvas.setCursor(max(0, x), y);
screenCanvas.print(text);
screenCanvas.setTextFont(1);
}
void drawLabelAt(const String &text, int x, int y, uint16_t color)
{
screenCanvas.setFont(&fonts::lgfxJapanGothic_16);
screenCanvas.setTextSize(1);
screenCanvas.setTextColor(color, BACKGROUND_COLOR);
screenCanvas.setCursor(x, y);
screenCanvas.print(text);
screenCanvas.setTextFont(1);
}
void drawCenteredHotkeyLabel(const char *text, int y)
{
String fullText = String(text);
screenCanvas.setFont(&fonts::lgfxJapanGothic_16);
screenCanvas.setTextSize(1);
screenCanvas.setTextColor(uiMutedColor(), BACKGROUND_COLOR);
int totalWidth = screenCanvas.textWidth(fullText);
int startX = static_cast<int>((screenCanvas.width() - totalWidth) / 2);
if (startX < 0)
{
startX = 0;
}
screenCanvas.setCursor(startX, y);
screenCanvas.print(fullText);
for (int i = 0; text[i] != '\0'; i++)
{
char c = text[i];
bool isHotkeyChar = (c == 'f' || c == 'n' ||
c == ';' || c == ',' ||
c == '.' || c == '/');
if (!isHotkeyChar)
{
continue;
}
String prefix = fullText.substring(0, i);
char token[2] = {c, '\0'};
int tokenX = startX + screenCanvas.textWidth(prefix);
screenCanvas.setTextColor(uiHotkeyColor(), BACKGROUND_COLOR);
screenCanvas.setCursor(tokenX, y);
screenCanvas.print(token);
}
screenCanvas.setTextFont(1);
}
void drawTimerMeta(int y, uint16_t color)
{
drawCenteredLabel(timerMetaText().c_str(), y, color);
}
void drawDeviceStatus(int y, uint16_t color)
{
drawCenteredLabel(deviceStatusText().c_str(), y, color);
}
uint16_t rgb565(uint8_t red, uint8_t green, uint8_t blue)
{
return ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | (blue >> 3);
}
uint8_t blendChannel(uint8_t from, uint8_t to, float amount)
{
return from + (to - from) * amount;
}
uint16_t blendRgb(uint8_t r1, uint8_t g1, uint8_t b1,
uint8_t r2, uint8_t g2, uint8_t b2,
float amount)
{
return rgb565(blendChannel(r1, r2, amount),
blendChannel(g1, g2, amount),
blendChannel(b1, b2, amount));
}
uint16_t segmentColorForIndex(uint8_t index, bool dimmed)
{
if (currentColorMode == COLOR_MODE_MONOCHROME)
{
return dimmed ? uiSegmentDimColor() : uiTextColor();
}
if (dimmed)
{
return uiSegmentDimColor();
}
float position = static_cast<float>(index) / static_cast<float>(TOTAL_SEGMENTS - 1);
if (position < 0.25f)
{
return blendRgb(0, 120, 255, 0, 220, 160, position / 0.25f);
}
if (position < 0.45f)
{
return blendRgb(0, 220, 160, 40, 220, 40, (position - 0.25f) / 0.20f);
}
if (position < 0.62f)
{
return blendRgb(40, 220, 40, 255, 230, 0, (position - 0.45f) / 0.17f);
}
if (position < 0.80f)
{
return blendRgb(255, 230, 0, 255, 120, 0, (position - 0.62f) / 0.18f);
}
return blendRgb(255, 120, 0, 255, 30, 30, (position - 0.80f) / 0.20f);
}
uint8_t segmentDrawIndex(uint8_t progressIndex)
{
uint8_t topCenter = TOP_SEGMENTS / 2;
if (progressIndex <= TOP_SEGMENTS - topCenter - 1)
{
return topCenter + progressIndex;
}
progressIndex -= TOP_SEGMENTS - topCenter;
if (progressIndex < SIDE_SEGMENTS)
{
return TOP_SEGMENTS + progressIndex;
}
progressIndex -= SIDE_SEGMENTS;
if (progressIndex < TOP_SEGMENTS)
{
return TOP_SEGMENTS + SIDE_SEGMENTS + progressIndex;
}
progressIndex -= TOP_SEGMENTS;
if (progressIndex < SIDE_SEGMENTS)
{
return TOP_SEGMENTS + SIDE_SEGMENTS + TOP_SEGMENTS + progressIndex;
}
progressIndex -= SIDE_SEGMENTS;
return progressIndex;
}
void segmentBounds(uint8_t index, int &x, int &y, int &w, int &h)
{
w = SEGMENT_HORIZONTAL_W;
h = SEGMENT_HORIZONTAL_H;
int horizontalStep = SEGMENT_HORIZONTAL_W + SEGMENT_GAP;
int topStartX = (screenCanvas.width() - (TOP_SEGMENTS * SEGMENT_HORIZONTAL_W) - ((TOP_SEGMENTS - 1) * SEGMENT_GAP)) / 2;
int verticalStep = SEGMENT_VERTICAL_H + SEGMENT_GAP;
int sideStartY = SEGMENT_MARGIN_Y + SEGMENT_HORIZONTAL_H + SEGMENT_GAP;
if (index < TOP_SEGMENTS)
{
x = topStartX + index * horizontalStep;
y = SEGMENT_TOP_Y;
return;
}
if (index < TOP_SEGMENTS + SIDE_SEGMENTS)
{
uint8_t sideIndex = index - TOP_SEGMENTS;
x = SEGMENT_SIDE_X_RIGHT;
y = sideStartY + sideIndex * verticalStep;
w = SEGMENT_VERTICAL_W;
h = SEGMENT_VERTICAL_H;
return;
}
if (index < TOP_SEGMENTS + SIDE_SEGMENTS + TOP_SEGMENTS)
{
uint8_t bottomIndex = index - TOP_SEGMENTS - SIDE_SEGMENTS;
x = topStartX + (TOP_SEGMENTS - 1 - bottomIndex) * horizontalStep;
y = SEGMENT_BOTTOM_Y;
return;
}
uint8_t sideIndex = index - TOP_SEGMENTS - SIDE_SEGMENTS - TOP_SEGMENTS;
x = SEGMENT_SIDE_X_LEFT;
y = sideStartY + (SIDE_SEGMENTS - 1 - sideIndex) * verticalStep;
w = SEGMENT_VERTICAL_W;
h = SEGMENT_VERTICAL_H;
}
void drawSegment(uint8_t index, bool lit, uint16_t color)
{
int x = 0, y = 0, w = 0, h = 0;
segmentBounds(index, x, y, w, h);
screenCanvas.fillRoundRect(x, y, w, h, 2, lit ? color : uiSegmentOffColor());
}
void drawSegments(float ratio, bool dimmed)
{
uint8_t elapsedSegments = floorf((1.0f - ratio) * TOTAL_SEGMENTS);
if (elapsedSegments > TOTAL_SEGMENTS)
{
elapsedSegments = TOTAL_SEGMENTS;
}
bool blinkEnabled = appState == STATE_RUNNING && elapsedSegments < TOTAL_SEGMENTS;
for (uint8_t progressIndex = 0; progressIndex < TOTAL_SEGMENTS; progressIndex++)
{
uint8_t drawIndex = segmentDrawIndex(progressIndex);
bool lit = progressIndex >= elapsedSegments;
bool blinkingNext = blinkEnabled && progressIndex == elapsedSegments;
if (blinkingNext && !segmentBlinkOn)
{
lit = false;
}
drawSegment(drawIndex, lit, segmentColorForIndex(progressIndex, dimmed));
}
}
String settingsLabel(SettingsItem item, const char *en, const char *ja)
{
String label = selectedSettingsItem == item ? ">" : " ";
label += tr(en, ja);
return label;
}
uint16_t settingsItemColor(SettingsItem item, bool active)
{
if (selectedSettingsItem == item || active)
{
return uiTextColor();
}
return uiMutedColor();
}
void drawStatsBars(const StudyStats &stats)
{
uint16_t maxMinutes = 1;
for (uint8_t index = 0; index < 7; index++)
{
if (stats.dayMinutes[index] > maxMinutes)
{
maxMinutes = stats.dayMinutes[index];
}
}
const int barWidth = 20;
const int barGap = 6;
const int baseX = 30;
const int baseY = 112;
const int maxHeight = 32;
for (uint8_t index = 0; index < 7; index++)
{
int barHeight = (stats.dayMinutes[index] * maxHeight) / maxMinutes;
if (stats.dayMinutes[index] > 0 && barHeight < 2)
{
barHeight = 2;
}
int x = baseX + index * (barWidth + barGap);
screenCanvas.drawRect(x, baseY - maxHeight, barWidth, maxHeight, uiMutedColor());
screenCanvas.fillRect(x + 2, baseY - barHeight, barWidth - 4, barHeight, uiTextColor());
}
}
void drawStatsScreen()
{
StudyStats stats = calculateStudyStats();
drawCenteredLabel(tr("LOG", "記録"), 4, uiTextColor());
if (!stats.sdReady)
{
drawCenteredLabel("NO LOG", 58, uiMutedColor());
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
return;
}
if (!stats.timeReady)
{
TimeStatus status = currentTimeStatus();
const char *statusLabel = status == TIME_STATUS_SYNCED ? tr("NO DATE", "日付なし") : timeStatusLabel();
drawCenteredLabel(statusLabel, 48, uiMutedColor());
if (status != TIME_STATUS_SYNCED)
{
drawCenteredLabel(tr("SYNC NEEDED", "同期が必要"), 76, uiMutedColor());
}
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
return;
}
if (currentTimeStatus() == TIME_STATUS_OFFLINE_DATE)
{
drawCenteredLabel(timeStatusLabel(), 12, uiMutedColor());
}
drawLabelAt(tr("TODAY", "今日"), 12, 28, uiMutedColor());
drawLabelAt(String(stats.todayMinutes) + "m", 88, 28, uiTextColor());
drawLabelAt(tr("7 DAYS", "7日間"), 12, 50, uiMutedColor());
drawLabelAt(String(stats.lastSevenDaysMinutes) + "m", 88, 50, uiTextColor());
drawLabelAt(tr("STREAK", "連続"), 150, 28, uiMutedColor());
drawLabelAt(String(stats.streakDays) + "d", 150, 50, uiTextColor());
drawStatsBars(stats);
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
}
void drawVoiceMemoScreen()
{
drawCenteredLabel(tr("VOICE MEMO", "ボイスメモ"), 4, uiTextColor());
if (!sdAvailable)
{
drawCenteredLabel("NO SD", 58, uiMutedColor());
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
return;
}
if (voiceMemoCount == 0)
{
drawCenteredLabel(tr("NO MEMO", "メモなし"), 58, uiMutedColor());
drawCenteredLabel(tr("M HOLD REC", "M長押し録音"), 88, uiMutedColor());
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
return;
}
String countLine = String(selectedVoiceMemoIndex + 1) + "/" + String(voiceMemoCount);
drawCenteredLabel(countLine.c_str(), 28, uiMutedColor());
drawCenteredLabel(voiceMemoEntries[selectedVoiceMemoIndex].label.c_str(), 52, uiTextColor());
uint32_t audioBytes = voiceMemoEntries[selectedVoiceMemoIndex].sizeBytes > sizeof(WavHeader)
? voiceMemoEntries[selectedVoiceMemoIndex].sizeBytes - sizeof(WavHeader)
: 0;
uint32_t seconds = audioBytes / (VOICE_SAMPLE_RATE * 2);
if (audioBytes > 0 && seconds == 0)
{
seconds = 1;
}
drawCenteredLabel((String(seconds) + " sec").c_str(), 76, uiMutedColor());
drawCenteredLabel(voicePlaying ? tr("ENTER STOP", "ENTER 停止") : tr("ENTER PLAY", "ENTER 再生"), 100, uiMutedColor());
drawCenteredLabel(tr("fn+; / fn+/ DEL", "fn+; / fn+/ DEL"), 120, uiMutedColor());
}
void drawVoiceOverlay()
{
if (voiceRecording)
{
screenCanvas.fillRect(196, 0, 44, 16, BACKGROUND_COLOR);
drawLabelAt("REC", 202, 0, uiDoneColor());
return;
}
if (voiceStatusText.length() > 0 && millis() < voiceStatusUntilMs)
{
screenCanvas.fillRect(174, 0, 66, 16, BACKGROUND_COLOR);
drawLabelAt(voiceStatusText, 176, 0, uiMutedColor());
}
}
void drawScreen()
{
if (!needsRedraw)
{
return;
}
needsRedraw = false;
screenCanvas.fillScreen(BACKGROUND_COLOR);
switch (appState)
{
case STATE_READY:
drawSegments(1.0f, false);
drawCenteredText(formatTime(remainingSeconds), 34, 5, uiTextColor());
drawDeviceStatus(78, deviceStatusColor());
drawCenteredLabel(richModeEnabled() ? tr("START 1-9 0 S L V", "開始 1-9 0 S L V") : tr("START 1-9 0 S", "開始 1-9 0 S"), 102, uiMutedColor());
if (!sdAvailable)
{
drawCenteredText("LOG OFF", 126, 1, uiMutedColor());
}
break;
case STATE_CUSTOM_INPUT:
drawCenteredLabel(tr("MIN", "分"), 20, uiMutedColor());
if (customInputMinutes == 0)
{
drawCenteredText("_", 52, 4, uiTextColor());
}
else
{
drawCenteredText(String(customInputMinutes), 52, 4, uiTextColor());
}
drawCenteredLabel(tr("ENTER", "開始"), 108, uiMutedColor());
break;
case STATE_RUNNING:
drawSegments(remainingRatio(), false);
drawCenteredText(formatTime(remainingSeconds), 34, 5, uiTextColor());
drawDeviceStatus(78, deviceStatusColor());
drawTimerMeta(102, uiMutedColor());
break;
case STATE_PAUSED:
drawSegments(remainingRatio(), true);
drawCenteredText(formatTime(remainingSeconds), 34, 5, uiMutedColor());
drawTimerMeta(88, uiMutedColor());
drawCenteredLabel(tr("PAUSE", "一時停止"), 110, uiTextColor());
break;
case STATE_DONE:
drawSegments(0.0f, false);
drawCenteredLabel(tr("DONE", "完了"), 42, uiDoneColor());
if (lastLogSaved)
{
StudyStats stats = calculateStudyStats();
drawCenteredLabel(tr("SAVED", "保存"), 88, uiMutedColor());
if (stats.timeReady)
{
String todayLine = String(tr("Today ", "今日 ")) + String(stats.todayMinutes) + "m";
drawCenteredLabel(todayLine.c_str(), 108, uiMutedColor());
}
}
else
{
drawCenteredLabel("LOG OFF", 98, uiMutedColor());
}
break;
case STATE_STATS:
drawStatsScreen();
break;
case STATE_VOICE_MEMOS:
drawVoiceMemoScreen();
break;
case STATE_LANGUAGE_SETTINGS:
drawCenteredLabel(tr("Language", "言語の設定"), 10, uiTextColor());
drawLabelAt(settingsLabel(SETTINGS_LANG_JA, "1 Japanese", "1 日本語"), 62, 44, settingsItemColor(SETTINGS_LANG_JA, currentLanguage == LANG_JA));
drawLabelAt(settingsLabel(SETTINGS_LANG_EN, "2 English", "2 English"), 62, 72, settingsItemColor(SETTINGS_LANG_EN, currentLanguage == LANG_EN));
drawCenteredHotkeyLabel(tr("NEXT: fn+/", "次: fn+/"), 96);
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
break;
case STATE_VOLUME_SETTINGS:
drawCenteredLabel(tr("Volume", "音量の設定"), 4, uiTextColor());
drawCenteredHotkeyLabel(tr("PREV fn+, NEXT fn+/", "前 fn+, 次 fn+/"), 24);
drawLabelAt(settingsLabel(SETTINGS_SOUND_QUIET, "1 Quiet", "1 消音"), 62, 44, settingsItemColor(SETTINGS_SOUND_QUIET, currentSoundMode == SOUND_QUIET));
drawLabelAt(settingsLabel(SETTINGS_SOUND_NORMAL, "2 Normal", "2 普通の音"), 62, 68, settingsItemColor(SETTINGS_SOUND_NORMAL, currentSoundMode == SOUND_NORMAL));
drawLabelAt(settingsLabel(SETTINGS_SOUND_LOUD, "3 Loud", "3 うるさい"), 62, 96, settingsItemColor(SETTINGS_SOUND_LOUD, currentSoundMode == SOUND_LOUD));
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
break;
case STATE_MODE_SETTINGS:
drawCenteredLabel(tr("Mode", "モード"), 10, uiTextColor());
drawCenteredHotkeyLabel(tr("PREV fn+, NEXT fn+/", "前 fn+, 次 fn+/"), 28);
drawLabelAt(settingsLabel(SETTINGS_MODE_SIMPLE, "1 Simple", "1 シンプル"), 62, 52, settingsItemColor(SETTINGS_MODE_SIMPLE, currentFeatureMode == MODE_SIMPLE));
drawLabelAt(settingsLabel(SETTINGS_MODE_RICH, "2 Rich", "2 リッチ"), 62, 80, settingsItemColor(SETTINGS_MODE_RICH, currentFeatureMode == MODE_RICH));
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
break;
case STATE_COLOR_SETTINGS:
drawCenteredLabel(tr("Color", "色"), 10, uiTextColor());
drawCenteredHotkeyLabel(tr("PREV: fn+,", "前: fn+,"), 28);
drawLabelAt(settingsLabel(SETTINGS_COLOR_COLOR, "1 Color", "1 カラー"), 62, 52, settingsItemColor(SETTINGS_COLOR_COLOR, currentColorMode == COLOR_MODE_COLOR));
drawLabelAt(settingsLabel(SETTINGS_COLOR_MONOCHROME, "2 Monochrome", "2 モノクロ"), 62, 80, settingsItemColor(SETTINGS_COLOR_MONOCHROME, currentColorMode == COLOR_MODE_MONOCHROME));
drawCenteredLabel(tr("DEL BACK", "DEL 戻る"), 120, uiMutedColor());
break;
case STATE_CONFIRM_RESET:
drawCenteredLabel(tr("RESET?", "リセット?"), 34, uiTextColor());
drawCenteredLabel(tr("ENTER YES", "ENTER はい"), 70, uiMutedColor());
drawCenteredLabel(tr("DEL NO", "DEL いいえ"), 96, uiMutedColor());
break;
}
drawVoiceOverlay();
screenCanvas.pushSprite(&M5Cardputer.Display, 0, 0);
}
#endif