-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui_screen.cpp
More file actions
1399 lines (1210 loc) · 52.4 KB
/
Copy pathui_screen.cpp
File metadata and controls
1399 lines (1210 loc) · 52.4 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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file ui_screen.cpp
* @brief TACTICAL TIMER 2 — 螢幕顯示原語實作
*
* 封裝所有 M5Stack Core2 LCD 繪圖操作,提供各模式畫面的繪製函式。
* 使用 M5.Lcd(ILI9342C 320×240)和 TFT_eSPI 圖形函式庫。
* 字體使用 FreeFont FSS9(9pt sans-serif)。
*
* 所有函式在 UI Task(Core 1)執行,不需要額外 mutex 保護。
*
* @version 2.0.0
*/
#include "ui_screen.h"
#include <M5Core2.h>
#include "preset_mgr.h"
// ============================================================
// [UI-helpers] 內部繪圖輔助函式
// ============================================================
namespace {
/**
* @brief 繪製圓角矩形徽章(badge)
* @param x 左上角 X
* @param y 左上角 Y
* @param w 寬度
* @param h 高度
* @param text 顯示文字
* @param bgColor 背景顏色(RGB565)
* @param textColor 文字顏色(RGB565)
*/
void drawBadge(int16_t x, int16_t y, int16_t w, int16_t h,
const char* text, uint16_t bgColor, uint16_t textColor) {
M5.Lcd.fillRoundRect(x, y, w, h, 3, bgColor);
M5.Lcd.setTextColor(textColor, bgColor);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.drawString(text, x + w/2, y + h/2, GFXFF);
}
/**
* @brief 格式化時間 ms → "X.XX"(截斷不四捨五入)
* @param ms 時間(ms)
* @param buf 輸出緩衝區
* @param len 緩衝區長度
*/
void fmtTime(unsigned long ms, char* buf, size_t len) {
snprintf(buf, len, "%lu.%02lu", ms / 1000, (ms % 1000) / 10);
}
} // anonymous namespace
// ============================================================
// [UI1-impl] 系統層
// ============================================================
// ── Shot log 分頁狀態(file-level,供翻頁控制)────────────────
static uint32_t s_pageBase = 1; ///< 目前頁第一筆 hitIdx(1-based)
static uint32_t s_totalShotUI = 0; ///< 目前局總發數(由 drawShotRow 更新)
// ── Shot Row 佈局常數(全域共用)─────────────────────────────
// START_Y = GAME_AREA_Y(20) + MODE_HDR_H(18) + 2 = 40
// STATS_H = 50(底部統計區,drawResultScreen 使用)
// SHOT_END_Y = IP_Y(204) - STATS_H(50) - DIVIDER(2) = 152
// SHOT_AREA = 152 - 40 = 112px
// ROW_H = 16px → 7筆/頁(112/16=7)
static constexpr int16_t SHOT_START_Y = Layout::GAME_AREA_Y + Layout::MODE_HDR_H + 2; // 40
static constexpr int16_t SHOT_STATS_H = 50;
static constexpr int16_t SHOT_END_Y_VAL = 204 - SHOT_STATS_H - 2; // 152
static constexpr int16_t SHOT_ROW_H = 16;
static constexpr uint8_t ROWS_PER_PAGE_UI = (SHOT_END_Y_VAL - SHOT_START_Y) / SHOT_ROW_H; // 7
/**
* @brief 開機 Splash 畫面(顯示約 3 秒)
*
* 顯示應用程式名稱、版本、作者,以及 BM8563 RTC 目前日期時間。
* 在所有模組初始化完成前呼叫,讓使用者看到啟動進度。
*/
void UIScreen::drawSplash() {
M5.Lcd.clear(TFT_BLACK);
// 頂部裝飾線
M5.Lcd.fillRect(0, 0, HW::LCD_W, 3, TFT_CYAN);
// 程式名稱
M5.Lcd.setFreeFont(FSB12);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_CYAN, TFT_BLACK);
M5.Lcd.drawString("TACTICAL", HW::LCD_W / 2, 55, GFXFF);
M5.Lcd.drawString("TIMER 2", HW::LCD_W / 2, 82, GFXFF);
// 副標題
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("AIR GUN SHOT TIMER SYSTEM", HW::LCD_W / 2, 108, GFXFF);
// 分隔線
M5.Lcd.drawFastHLine(40, 122, HW::LCD_W - 80, UIColor::CARD_BORD);
// 日期(從 RTC 讀取)
HalRtc::DateTime dt;
HalRtc::readDateTime(dt);
char dateBuf[24];
HalRtc::formatDate(dt, dateBuf, sizeof(dateBuf), "Syncing via NTP...");
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(dateBuf, HW::LCD_W / 2, 140, GFXFF);
// 作者
M5.Lcd.setTextColor(0x6B4D, TFT_BLACK);
M5.Lcd.drawString("Louis Chou", HW::LCD_W / 2, 160, GFXFF);
// Copyright
M5.Lcd.setTextColor(TFT_DARKGREY, TFT_BLACK);
M5.Lcd.drawString("(c) 2026 All Rights Reserved", HW::LCD_W / 2, 178, GFXFF);
// Version
char verBuf[16];
snprintf(verBuf, sizeof(verBuf), "v%s", TT2Version::TT2_APP_VER);
M5.Lcd.setTextColor(UIColor::CARD_BORD, TFT_BLACK);
M5.Lcd.drawString(verBuf, HW::LCD_W / 2, 196, GFXFF);
// 底部裝飾線
M5.Lcd.fillRect(0, HW::LCD_H - 3, HW::LCD_W, 3, TFT_CYAN);
delay(3000);
}
/**
* @brief 繪製頂部狀態列(電池電量 + WiFi RSSI + 時間)
*
* 每 INFO_INTERVAL_MS(2 秒)由 GameFSM::update() 定時呼叫。
* 高度 = STATUS_H(20px),Y = 0。
*/
void UIScreen::drawStatusBar() {
M5.Lcd.fillRect(0, 0, HW::LCD_W, Layout::STATUS_H, TFT_BLACK);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextDatum(BL_DATUM);
// WiFi RSSI
int32_t rssi = NetworkMgr::getRSSI();
M5.Lcd.setTextColor(rssi < -70 ? TFT_RED : TFT_GREEN, TFT_BLACK);
char wifiBuf[16];
snprintf(wifiBuf, sizeof(wifiBuf), "WiFi:%d", (int)rssi);
M5.Lcd.drawString(wifiBuf, 0, Layout::STATUS_H, GFXFF);
// 電池
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
char batBuf[12];
snprintf(batBuf, sizeof(batBuf), "%.2fV", M5.Axp.GetBatVoltage());
M5.Lcd.drawString(batBuf, 120, Layout::STATUS_H, GFXFF);
// 日期時間(RTC)
HalRtc::DateTime dt;
HalRtc::readDateTime(dt);
if (dt.isValid()) {
char timeBuf[12];
HalRtc::formatTime(dt, timeBuf, sizeof(timeBuf));
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString(timeBuf, 200, Layout::STATUS_H, GFXFF);
}
// Active Preset 名稱
const Preset& p = PresetMgr::getActive();
M5.Lcd.setTextColor(TFT_CYAN, TFT_BLACK);
M5.Lcd.setTextDatum(BR_DATUM);
M5.Lcd.drawString(p.name[0] ? p.name : "MIC",
HW::LCD_W, Layout::STATUS_H, GFXFF);
// IP bar 只在 HOME 畫面顯示(由 drawHomeScreen 呼叫)
}
/**
* @brief 繪製 IP 位址列
*
* STA 模式:顯示 Router 分配的 IP(藍綠色)
* AP 模式:顯示 "[AP] 192.168.4.1"(橙色)
* 未連線:灰色
*
* 高度 = IP_H(16px),Y = IP_Y(204)。
*/
void UIScreen::drawIPBar() {
M5.Lcd.fillRect(0, Layout::IP_Y, HW::LCD_W, Layout::IP_H, UIColor::BLUE_DARK);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextDatum(BC_DATUM);
String ip = NetworkMgr::getIPString();
uint16_t col;
if (NetworkMgr::isWiFiConnected()) {
col = TFT_CYAN; // STA 模式:藍綠色
} else if (NetworkMgr::isAPMode()) {
// AP 模式:橙色 + 顯示熱點 SSID 提示
col = 0xFD20; // 橙色
String apStr = String("[AP] ") + ip;
ip = apStr;
} else {
col = TFT_DARKGREY; // 未連線
}
M5.Lcd.setTextColor(col, UIColor::BLUE_DARK);
M5.Lcd.drawString(ip.c_str(),
HW::LCD_W / 2,
Layout::IP_Y + Layout::IP_H - 2, GFXFF);
}
/**
* @brief 繪製底部按鍵標籤列(BtnA / BtnB / BtnC)
*
* 高度 20px,Y = BTN_Y(220)。
* nullptr 標籤 → 對應位置留空(不繪製)。
*
* @param labelA BtnA 標籤(左)
* @param labelB BtnB 標籤(中)
* @param labelC BtnC 標籤(右)
*/
void UIScreen::drawBtnBar(const char* labelA, const char* labelB, const char* labelC) {
M5.Lcd.fillRect(0, Layout::BTN_Y, HW::LCD_W, 20, TFT_YELLOW);
M5.Lcd.setTextColor(TFT_BLACK, TFT_YELLOW);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextDatum(MC_DATUM); // 垂直居中
M5.Lcd.setFreeFont(FSS9); // FSB12 → FSS9(縮小字型)
int16_t midY = Layout::BTN_Y + 10; // 20px 列的垂直中心
if (labelA) M5.Lcd.drawString(labelA, 55, midY, GFXFF);
if (labelB) M5.Lcd.drawString(labelB, HW::LCD_W / 2, midY, GFXFF);
if (labelC) M5.Lcd.drawString(labelC, 270, midY, GFXFF);
}
/**
* @brief 繪製左下角 HOME 觸控區 overlay(供部分模式使用)
*
* 在遊戲區左下角繪製小型 [HOME] 標籤,
* 觸控熱區對應 Layout::HOME_BTN_* 常數。
*/
void UIScreen::drawHomeBtnOverlay() {
// 左下角半透明 [HOME] 觸控區
M5.Lcd.fillRoundRect(Layout::HOME_BTN_X, Layout::HOME_BTN_Y,
Layout::HOME_BTN_W, Layout::HOME_BTN_H,
3, UIColor::CARD_BG);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, UIColor::CARD_BG);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString("HOME",
Layout::HOME_BTN_X + Layout::HOME_BTN_W / 2,
Layout::HOME_BTN_Y + Layout::HOME_BTN_H / 2, GFXFF);
}
// ============================================================
// [UI2-impl] 模式共用元件
// ============================================================
/**
* @brief 清除遊戲區(STATUS_H 到 IP_Y,高度 = GAME_AREA_H = 184px)
*/
void UIScreen::clearGameArea() {
M5.Lcd.fillRect(0, Layout::GAME_AREA_Y,
HW::LCD_W, Layout::GAME_AREA_H, TFT_BLACK);
// 同時清除 IP bar 區域(避免 HOME 的 IP bar 在其他頁面留殘影)
M5.Lcd.fillRect(0, Layout::IP_Y, HW::LCD_W, Layout::IP_H, TFT_BLACK);
}
void UIScreen::resetShotPage() {
s_pageBase = 1;
s_totalShotUI = 0;
// 清除 shot row 區域,避免新局開始時顯示舊成績
M5.Lcd.fillRect(0, SHOT_START_Y, HW::LCD_W,
SHOT_END_Y_VAL - SHOT_START_Y + SHOT_ROW_H, TFT_BLACK);
}
bool UIScreen::shotPageHasPrev() {
return s_pageBase > 1;
}
bool UIScreen::shotPageHasNext() {
// 下一頁起點
uint32_t nextBase = s_pageBase + ROWS_PER_PAGE_UI;
return nextBase <= s_totalShotUI;
}
void UIScreen::shotPagePrev() {
if (!shotPageHasPrev()) return;
s_pageBase = (s_pageBase > ROWS_PER_PAGE_UI)
? s_pageBase - ROWS_PER_PAGE_UI : 1;
}
void UIScreen::shotPageNext() {
if (!shotPageHasNext()) return;
s_pageBase += ROWS_PER_PAGE_UI;
}
uint32_t UIScreen::getPageBase() { return s_pageBase; }
uint8_t UIScreen::getRowsPerPage() { return ROWS_PER_PAGE_UI; }
void UIScreen::setTotalShotUI(uint32_t total) { s_totalShotUI = total; }
/**
* @brief 繪製指定 hitIdx 在已知 pageBase 的列位置(不自動推頁)
* 供翻頁重繪(redrawShotPage)使用,避免自動推頁覆蓋 s_pageBase。
*/
void UIScreen::drawShotRowAt(uint32_t hitIdx, uint32_t pageBase,
uint8_t stationId,
unsigned long elapsed, unsigned long split,
bool pass) {
if (hitIdx < pageBase || hitIdx >= pageBase + ROWS_PER_PAGE_UI) return;
constexpr int16_t START_Y = SHOT_START_Y;
constexpr int16_t SHOT_END_Y = SHOT_END_Y_VAL;
constexpr int16_t ROW_H = SHOT_ROW_H;
uint8_t localRow = (uint8_t)(hitIdx - pageBase);
int16_t y0 = START_Y + localRow * ROW_H;
int16_t yC = y0 + ROW_H / 2;
M5.Lcd.fillRect(0, y0, HW::LCD_W, ROW_H - 1, TFT_BLACK);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
bool isOverflow = (hitIdx > Limits::HIT_MAX);
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.setTextColor(isOverflow ? 0xFD20 : UIColor::TEXT_DIM, TFT_BLACK);
char numBuf[5]; snprintf(numBuf, sizeof(numBuf), "#%02u", (unsigned)hitIdx);
M5.Lcd.drawString(numBuf, 38, yC, GFXFF);
bool isMic = (stationId == 0);
uint16_t srcBg = isMic ? 0x1A12 : 0x0A20;
uint16_t srcCol = isMic ? 0xFFC0 : 0x40B8;
char srcBuf[6];
if (isMic) strlcpy(srcBuf, "MIC", sizeof(srcBuf));
else snprintf(srcBuf, sizeof(srcBuf), "S%u", stationId);
M5.Lcd.fillRoundRect(40, y0 + 1, 30, ROW_H - 3, 2, srcBg);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(srcCol, srcBg);
M5.Lcd.drawString(srcBuf, 55, yC, GFXFF);
char elBuf[10]; fmtTime(elapsed, elBuf, sizeof(elBuf));
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(elBuf, 220, yC, GFXFF);
char spBuf[10]; fmtTime(split, spBuf, sizeof(spBuf));
bool fast = (split < 600);
uint16_t spColor = pass
? (fast ? UIColor::SPLIT_FAST : UIColor::SPLIT_SLOW)
: UIColor::FAIL_RED;
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.setTextColor(spColor, TFT_BLACK);
M5.Lcd.drawString(spBuf, 316, yC, GFXFF);
}
/**
* @brief 繪製模式標題列(模式名稱 + 狀態 badge)
*
* 高度 = MODE_HDR_H(18px),Y = GAME_AREA_Y(20)。
*
* @param title 模式名稱(左側,如 "FREE SHOOTING")
* @param badge 狀態文字(右側,如 "GOING"、"STANDBY")
* @param badgeColor badge 背景色(RGB565)
*/
void UIScreen::drawModeHeader(const char* title, const char* badge, uint16_t badgeColor) {
// 多清 2px 確保字體 descender 殘影也被清除
constexpr int16_t CLEAR_H = Layout::MODE_HDR_H + 2;
M5.Lcd.fillRect(0, Layout::GAME_AREA_Y, HW::LCD_W, CLEAR_H, UIColor::CARD_BG);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextDatum(ML_DATUM);
// title 不帶背景色,避免 bounding box 溢出
M5.Lcd.setTextColor(TFT_CYAN);
M5.Lcd.drawString(title, 4, Layout::GAME_AREA_Y + Layout::MODE_HDR_H / 2, GFXFF);
// 狀態徽章(右側)
if (badge) {
constexpr int16_t BW_MAX = 90;
constexpr int16_t bh = 14;
int16_t by = Layout::GAME_AREA_Y + 2;
// 先清右側整塊區域(比 MODE_HDR_H 多 2px)
M5.Lcd.fillRect(HW::LCD_W - BW_MAX - 6, Layout::GAME_AREA_Y,
BW_MAX + 6, CLEAR_H, UIColor::CARD_BG);
// 動態計算 badge 寬度
M5.Lcd.setFreeFont(FSS9);
int16_t textW = M5.Lcd.textWidth(badge, GFXFF);
int16_t bw = constrain((int16_t)(textW + 14), (int16_t)48, BW_MAX);
int16_t bx = HW::LCD_W - bw - 4;
// badge 背景(用 fillRect 替代 fillRoundRect,徹底清角落)
M5.Lcd.fillRect(bx, by, bw, bh, badgeColor);
// 文字不帶背景色,避免 drawString bounding box 超出 badge 範圍留殘影
M5.Lcd.setTextColor(TFT_BLACK);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(badge, bx + bw / 2, by + bh / 2, GFXFF);
}
}
/**
* @brief 繪製單筆命中記錄列
*
* 支援雙欄排版(1–5 左欄,6–10 右欄),
* 每列高度 SHOT_ROW_H(20px)。
* pass=false 時 split 顯示為紅色警告。
*
* @param hitIdx 命中序號(1-based)
* @param stationId 來源(0=MIC, 1+=ESP-NOW)
* @param elapsed 累積時間(ms)
* @param split 分段時間(ms)
* @param pass 是否在 par time 內(預設 true)
*/
void UIScreen::drawShotRow(uint32_t hitIdx, uint8_t stationId,
unsigned long elapsed, unsigned long split,
bool pass) {
// hitIdx = 真實第幾發(1-based,可超過 HIT_MAX)
if (hitIdx == 0) return;
// ── 佈局常數(使用 file-level 共用常數)──────────────────────
constexpr int16_t START_Y = SHOT_START_Y;
constexpr int16_t SHOT_END_Y = SHOT_END_Y_VAL;
constexpr int16_t ROW_H = SHOT_ROW_H;
// 更新總發數記錄
if (hitIdx > s_totalShotUI) s_totalShotUI = hitIdx;
// 整頁翻頁:hitIdx 超出目前頁尾才推進(整頁對齊)
// 頁 1:hitIdx 1-7,頁 2:8-14,頁 3:15-21 ...
// s_pageBase = ((hitIdx-1) / ROWS_PER_PAGE_UI) * ROWS_PER_PAGE_UI + 1
uint32_t correctBase = ((hitIdx - 1) / ROWS_PER_PAGE_UI) * ROWS_PER_PAGE_UI + 1;
if (correctBase != s_pageBase) {
s_pageBase = correctBase;
// 多清一個 ROW_H 確保最後一行的行距也清除
M5.Lcd.fillRect(0, START_Y, HW::LCD_W,
SHOT_END_Y - START_Y + ROW_H, TFT_BLACK);
}
uint8_t localRow = (uint8_t)(hitIdx - s_pageBase);
if (localRow >= ROWS_PER_PAGE_UI) return;
int16_t y0 = START_Y + localRow * ROW_H;
int16_t yC = y0 + ROW_H / 2;
M5.Lcd.fillRect(0, y0, HW::LCD_W, ROW_H - 1, TFT_BLACK);
// ── 繪製各欄位(全寬 320px)────────────────────────────────
// 序號 [#XX] 來源badge elapsed split
// x: 2 22 180 280
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
// 序號(顯示真實發數,超過 HIT_MAX 用橙色提示環形覆寫)
bool isOverflow = (hitIdx > Limits::HIT_MAX);
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.setTextColor(isOverflow ? 0xFD20 : UIColor::TEXT_DIM, TFT_BLACK);
char numBuf[5]; snprintf(numBuf, sizeof(numBuf), "#%02u", hitIdx);
M5.Lcd.drawString(numBuf, 38, yC, GFXFF);
// 來源徽章
bool isMic = (stationId == 0);
uint16_t srcBg = isMic ? 0x1A12 : 0x0A20;
uint16_t srcCol = isMic ? 0xFFC0 : 0x40B8;
char srcBuf[6];
if (isMic) strlcpy(srcBuf, "MIC", sizeof(srcBuf));
else snprintf(srcBuf, sizeof(srcBuf), "S%u", stationId);
M5.Lcd.fillRoundRect(40, y0 + 1, 30, ROW_H - 3, 2, srcBg);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(srcCol, srcBg);
M5.Lcd.drawString(srcBuf, 55, yC, GFXFF);
// 累計時間(右對齊到 240px)
char elBuf[10]; fmtTime(elapsed, elBuf, sizeof(elBuf));
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(elBuf, 220, yC, GFXFF);
// 分段時間(右對齊到 316px)
char spBuf[10]; fmtTime(split, spBuf, sizeof(spBuf));
bool fast = (split < 600);
uint16_t spColor = pass
? (fast ? UIColor::SPLIT_FAST : UIColor::SPLIT_SLOW)
: UIColor::FAIL_RED;
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.setTextColor(spColor, TFT_BLACK);
M5.Lcd.drawString(spBuf, 316, yC, GFXFF);
}
/**
* @brief 繪製 FREE 模式結果畫面(成績統計 + 最佳紀錄)
*
* @param totalMs 本局總時間(ms)
* @param hitCount 本局命中數
* @param bestTotal 歷史最佳總時間(秒)
* @param bestAvg 歷史最佳平均 split(秒)
*/
void UIScreen::drawResultScreen(unsigned long totalMs, uint8_t hitCount,
float bestTotal, float bestAvg) {
// ── Stats 區:2 行 × 24px + 2px 分隔線 = 50px ─────────────
// STATS_Y = 204 - 50 = 154,行1 中心 Y=166,行2 中心 Y=190
// FSS9 字高約 11px,行高 24px → 上下各留 ~6.5px,完全不超出
constexpr int16_t STATS_H = 50;
constexpr int16_t DIVIDER = 2;
constexpr int16_t STATS_Y = Layout::IP_Y - STATS_H; // 154
constexpr int16_t ROW_H = 24;
constexpr int16_t COL_W = HW::LCD_W / 3; // 106px
constexpr uint16_t BG = 0x0821;
// 分隔線 + 背景
M5.Lcd.fillRect(0, STATS_Y - DIVIDER, HW::LCD_W, DIVIDER, UIColor::CARD_BORD);
M5.Lcd.fillRect(0, STATS_Y, HW::LCD_W, STATS_H, BG);
// 三欄垂直分隔線
M5.Lcd.drawFastVLine(COL_W, STATS_Y, STATS_H, UIColor::CARD_BORD);
M5.Lcd.drawFastVLine(COL_W * 2, STATS_Y, STATS_H, UIColor::CARD_BORD);
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
unsigned long avg = (hitCount > 0 && totalMs > 0) ? (totalMs / hitCount) : 0;
char totBuf[10]; fmtTime(totalMs, totBuf, sizeof(totBuf));
char avgBuf[10]; fmtTime(avg, avgBuf, sizeof(avgBuf));
char buf[14];
// 各欄水平分界(label 左對齊,值右對齊)
// 欄0: x=0~105 欄1: x=106~211 欄2: x=212~319
int16_t lx0 = 3, rx0 = COL_W - 3;
int16_t lx1 = COL_W + 3, rx1 = COL_W * 2 - 3;
int16_t lx2 = COL_W * 2 + 3, rx2 = HW::LCD_W - 3;
// 行1 / 行2 垂直中心
int16_t y1 = STATS_Y + ROW_H / 2; // 154 + 12 = 166
int16_t y2 = STATS_Y + ROW_H + ROW_H / 2; // 154 + 36 = 190
// ── 行 1:SHOTS X/10 | TOTAL X.XXs | AVG X.XXs ────────────
// 欄0: SHOTS
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, BG);
M5.Lcd.drawString("SH", lx0, y1, GFXFF);
snprintf(buf, sizeof(buf), "%u/%u", hitCount, (unsigned)Limits::HIT_MAX);
M5.Lcd.setTextColor(TFT_WHITE, BG);
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.drawString(buf, rx0, y1, GFXFF);
// 欄1: TOTAL
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, BG);
M5.Lcd.drawString("TOT", lx1, y1, GFXFF);
snprintf(buf, sizeof(buf), "%ss", totBuf);
M5.Lcd.setTextColor(TFT_WHITE, BG);
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.drawString(buf, rx1, y1, GFXFF);
// 欄2: AVG
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, BG);
M5.Lcd.drawString("AVG", lx2, y1, GFXFF);
if (avg > 0) {
snprintf(buf, sizeof(buf), "%ss", avgBuf);
M5.Lcd.setTextColor(TFT_WHITE, BG);
} else {
strlcpy(buf, "--", sizeof(buf));
M5.Lcd.setTextColor(UIColor::TEXT_DIM, BG);
}
M5.Lcd.setTextDatum(MR_DATUM);
M5.Lcd.drawString(buf, rx2, y1, GFXFF);
// ── 行 2:* BEST * | TOTAL best | AVG best ─────────────────
// 欄0: ★ BEST 標籤
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(0x39C7, BG);
M5.Lcd.drawString("* BEST *", COL_W / 2, y2, GFXFF);
// 欄1: BEST TOTAL
if (bestTotal > 0.0f) {
snprintf(buf, sizeof(buf), "%.2fs", bestTotal);
M5.Lcd.setTextColor(UIColor::PASS_GREEN, BG);
} else {
strlcpy(buf, "--", sizeof(buf));
M5.Lcd.setTextColor(UIColor::TEXT_DIM, BG);
}
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(buf, COL_W + COL_W / 2, y2, GFXFF);
// 欄2: BEST AVG
if (bestAvg > 0.0f) {
snprintf(buf, sizeof(buf), "%.2fs", bestAvg);
M5.Lcd.setTextColor(UIColor::PASS_GREEN, BG);
} else {
strlcpy(buf, "--", sizeof(buf));
M5.Lcd.setTextColor(UIColor::TEXT_DIM, BG);
}
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(buf, COL_W * 2 + COL_W / 2, y2, GFXFF);
}
// ─── 廢棄注解已移除 ───
// ============================================================
// [UI3-impl] 模式專用畫面
// ============================================================
/**
* @brief 繪製 Home 模式的 6-card grid 畫面
*
* 3×2 卡片排列(FREE / DRILL / DRY FIRE / SPY / RO / HISTORY),
* 每張卡片顯示圖示和模式名稱,使用 CARD_* Layout 常數計算位置。
*/
void UIScreen::drawHomeScreen() {
M5.Lcd.clear(TFT_BLACK);
drawStatusBar();
// 6-card grid
constexpr int16_t GY = Layout::HOME_GRID_Y;
constexpr int16_t P = Layout::CARD_PAD;
constexpr int16_t G = Layout::CARD_GAP;
constexpr int16_t CW = (HW::LCD_W - 2*P - 2*G) / 3;
constexpr int16_t CH = (Layout::HOME_GRID_H - 2*P - 1*G) / 2;
struct CardInfo {
const char* title;
const char* sub;
uint16_t accentColor;
};
static const CardInfo cards[6] = {
{ "FREE", "Shooting", TFT_CYAN },
{ "DRILLS", "& Scoring", 0xFFC0 },
{ "DRY", "Fire", 0xA000 },
{ "SPY", "Mode", TFT_GREEN },
{ "RO", "Mode", 0xF800 },
{ "HISTORY", "SD Card", 0xFF80 },
};
for (uint8_t i = 0; i < 6; i++) {
uint8_t col = i % 3;
uint8_t row = i / 3;
int16_t x = P + col * (CW + G);
int16_t y = GY + P + row * (CH + G);
// 卡片背景與邊框
M5.Lcd.fillRoundRect(x, y, CW, CH, 4, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(x, y, CW, CH, 4, UIColor::CARD_BORD);
// 頂部色條(accent)
M5.Lcd.fillRect(x + 1, y + 1, CW - 2, 3, cards[i].accentColor);
// 標題(縮小:FSB12 → FSS9,避免溢出卡片)
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString(cards[i].title, x + CW/2, y + CH/2 - 7, GFXFF);
// 副標題(使用內建小字型更清晰)
M5.Lcd.setFreeFont(nullptr); // 回到內建 bitmap font
M5.Lcd.setTextSize(1); // 6x8 像素,最小清晰字型
M5.Lcd.setTextColor(UIColor::TEXT_DIM, UIColor::CARD_BG);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString(cards[i].sub, x + CW/2, y + CH/2 + 9, GFXFF);
}
drawIPBar();
drawBtnBar("STOP", "SETTINGS", "START");
}
/**
* @brief 繪製 SETTINGS 模式畫面(4 個設定列)
*
* 列佈局(ROW_H = 41px,Y0 = GAME_AREA_Y + MODE_HDR_H + 2 = 40):
* - Row 0:PRESET 槍型(◀ / 名稱 / ▶)
* - Row 1:SOURCE 三個按鈕(ESP-NOW / MIC / BOTH)
* - Row 2:THRESH RMS 門檻(− / 數值 / +)
* - Row 3:RAND DELAY 開關(OFF / ON)
*
* @param s 目前 AppSettings(_edit 暫存值)
*/
void UIScreen::drawSettingsScreen(const AppSettings& s) {
// ── 清除 + Header ────────────────────────────────────────────
M5.Lcd.fillRect(0, Layout::GAME_AREA_Y, HW::LCD_W, Layout::GAME_AREA_H, TFT_BLACK);
drawModeHeader("SETTINGS", nullptr, 0);
// 可用高度 = 184 - 18 = 166px,5行 × 33px = 165px
constexpr int16_t Y0 = Layout::GAME_AREA_Y + Layout::MODE_HDR_H + 2;
constexpr int16_t ROW_H = 33;
constexpr int16_t LBL_X = 4;
constexpr int16_t VAL_X = 110;
constexpr int16_t CX = HW::LCD_W / 2;
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
// 分隔線輔助 lambda
auto drawDiv = [&](uint8_t row) {
M5.Lcd.drawFastHLine(0, Y0 + row * ROW_H, HW::LCD_W, UIColor::CARD_BORD);
};
// ── Row 0:PRESET ────────────────────────────────────────────
{
constexpr int16_t RY = Y0;
constexpr int16_t RCY = RY + ROW_H / 2;
constexpr int16_t AW = 24, AH = 22;
constexpr int16_t LX = VAL_X;
constexpr int16_t RX = HW::LCD_W - AW - 4;
constexpr int16_t NCX = (LX + AW + RX) / 2; // 名稱中心 = 兩箭頭之間中點
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("PRESET", LBL_X, RCY, GFXFF);
// ◀ 按鈕
M5.Lcd.fillRoundRect(LX, RY + 6, AW, AH, 3, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(LX, RY + 6, AW, AH, 3, UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("<", LX + AW/2, RCY, GFXFF);
// Preset 名稱(置中於兩箭頭之間)
Preset p; PresetMgr::get(s.activePreset, p);
char nameBuf[24];
snprintf(nameBuf, sizeof(nameBuf), "%u:%s", s.activePreset + 1, p.name);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_CYAN, TFT_BLACK);
M5.Lcd.drawString(nameBuf, NCX, RCY, GFXFF);
// ▶ 按鈕
M5.Lcd.fillRoundRect(RX, RY + 6, AW, AH, 3, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(RX, RY + 6, AW, AH, 3, UIColor::CARD_BORD);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString(">", RX + AW/2, RCY, GFXFF);
drawDiv(1);
}
// ── Row 1:SOURCE ────────────────────────────────────────────
{
constexpr int16_t RY = Y0 + ROW_H;
constexpr int16_t RCY = RY + ROW_H / 2;
// ESP-NOW 縮寫為 E-NOW,FSS9 約 5×9=45px,BW=64 綽綽有餘
// 3×64 + 2×6 = 204,起點 VAL_X=110,終點 110+204=314
constexpr int16_t SX = VAL_X;
constexpr int16_t BW = 64, BH = 22, GAP = 6;
static const char* srcLabels[] = { "E-NOW", "MIC", "BOTH" };
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("SOURCE", LBL_X, RCY, GFXFF);
for (uint8_t i = 0; i < 3; i++) {
int16_t bx = SX + i * (BW + GAP);
bool sel = (static_cast<uint8_t>(s.hitSource) == i);
uint16_t bg = sel ? 0x0060 : UIColor::CARD_BG;
uint16_t col = sel ? TFT_GREEN : UIColor::TEXT_DIM;
M5.Lcd.fillRoundRect(bx, RY + 6, BW, BH, 3, bg);
M5.Lcd.drawRoundRect(bx, RY + 6, BW, BH, 3,
sel ? TFT_GREEN : UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(col, bg);
M5.Lcd.drawString(srcLabels[i], bx + BW/2, RCY, GFXFF);
}
drawDiv(2);
}
// ── Row 2:THRESH ────────────────────────────────────────────
{
constexpr int16_t RY = Y0 + 2 * ROW_H;
constexpr int16_t RCY = RY + ROW_H / 2;
constexpr int16_t BW = 28, BH = 22;
constexpr int16_t LX = VAL_X;
constexpr int16_t RX = HW::LCD_W - BW - 4;
constexpr int16_t NCX = (LX + BW + RX) / 2; // 數值中心 = 兩按鈕之間中點
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("THRESH", LBL_X, RCY, GFXFF);
// − 按鈕
M5.Lcd.fillRoundRect(LX, RY + 6, BW, BH, 3, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(LX, RY + 6, BW, BH, 3, UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("-", LX + BW/2, RCY, GFXFF);
// 數值(置中於兩按鈕之間)
char buf[8]; snprintf(buf, sizeof(buf), "%d", s.micThresh);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(buf, NCX, RCY, GFXFF);
// + 按鈕
M5.Lcd.fillRoundRect(RX, RY + 6, BW, BH, 3, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(RX, RY + 6, BW, BH, 3, UIColor::CARD_BORD);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("+", RX + BW/2, RCY, GFXFF);
drawDiv(3);
}
// ── Row 3:RAND DELAY ────────────────────────────────────────
{
constexpr int16_t RY = Y0 + 3 * ROW_H;
constexpr int16_t RCY = RY + ROW_H / 2;
constexpr int16_t BW = 60, BH = 22;
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("RAND DLY", LBL_X, RCY, GFXFF);
bool on = s.randomDelay;
uint16_t bg = on ? 0x0060 : UIColor::CARD_BG;
uint16_t col = on ? TFT_GREEN : UIColor::TEXT_DIM;
M5.Lcd.fillRoundRect(VAL_X, RY + 6, BW, BH, 3, bg);
M5.Lcd.drawRoundRect(VAL_X, RY + 6, BW, BH, 3,
on ? TFT_GREEN : UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(col, bg);
M5.Lcd.drawString(on ? "ON" : "OFF", VAL_X + BW/2, RCY, GFXFF);
drawDiv(4);
}
// ── Row 4:MAX SHOTS(Free/RO 最大發數)────────────────────
{
constexpr int16_t RY = Y0 + 4 * ROW_H;
constexpr int16_t RCY = RY + ROW_H / 2;
constexpr int16_t BW = 28, BH = 20;
constexpr int16_t LX = VAL_X;
constexpr int16_t RX = HW::LCD_W - BW - 4;
constexpr int16_t NCX = (LX + BW + RX) / 2;
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("MAX SHOTS", LBL_X, RCY, GFXFF);
// − 按鈕
M5.Lcd.fillRoundRect(LX, RY + 4, BW, BH, 3, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(LX, RY + 4, BW, BH, 3, UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("-", LX + BW/2, RCY, GFXFF);
// 數值(0 顯示 "∞")
char buf[8];
if (s.maxShots == 0) strlcpy(buf, "INF", sizeof(buf));
else snprintf(buf, sizeof(buf), "%u", s.maxShots);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(s.maxShots == 0 ? TFT_CYAN : TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(buf, NCX, RCY, GFXFF);
// + 按鈕
M5.Lcd.fillRoundRect(RX, RY + 4, BW, BH, 3, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(RX, RY + 4, BW, BH, 3, UIColor::CARD_BORD);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("+", RX + BW/2, RCY, GFXFF);
}
drawBtnBar("HOME", nullptr, "SAVE");
// IP bar 清除(Settings 模式不顯示 IP)
M5.Lcd.fillRect(0, Layout::IP_Y, HW::LCD_W, Layout::IP_H, TFT_BLACK);
}
/**
* @brief 繪製 Par Time 設定畫面
*
* 顯示目前 par time 值和 −500ms / +500ms 調整按鈕,
* 以及 CANCEL / DONE 操作按鈕。
*
* @param parMs 目前 par time(ms),0 = 停用
*/
void UIScreen::drawParSetScreen(uint32_t parMs) {
// ── 清除遊戲區 ──────────────────────────────────────────────
M5.Lcd.fillRect(0, Layout::GAME_AREA_Y, HW::LCD_W, Layout::GAME_AREA_H, TFT_BLACK);
// ── Header ──────────────────────────────────────────────────
drawModeHeader("FREE SHOOTING", "PAR SET", 0xFD20);
constexpr int16_t CX = HW::LCD_W / 2; // 160
constexpr int16_t Y0 = Layout::GAME_AREA_Y + Layout::MODE_HDR_H + 8;
// ── 標籤 ─────────────────────────────────────────────────────
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("PAR TIME", CX, Y0 + 14, GFXFF);
// ── 數值(大字)─────────────────────────────────────────────
char valBuf[12];
if (parMs == 0) {
strlcpy(valBuf, "OFF", sizeof(valBuf));
} else {
snprintf(valBuf, sizeof(valBuf), "%.1f s", parMs / 1000.0f);
}
M5.Lcd.setFreeFont(FSB12);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(valBuf, CX, Y0 + 52, GFXFF);
// ── − / + 按鈕(各 50×36px)────────────────────────────────
constexpr int16_t BTN_W = 50;
constexpr int16_t BTN_H = 36;
constexpr int16_t BTN_Y = Y0 + 34;
constexpr int16_t BTN_LX = CX - 80 - BTN_W; // 左按鈕 X = 30
constexpr int16_t BTN_RX = CX + 80; // 右按鈕 X = 240
M5.Lcd.fillRoundRect(BTN_LX, BTN_Y, BTN_W, BTN_H, 4, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(BTN_LX, BTN_Y, BTN_W, BTN_H, 4, UIColor::CARD_BORD);
M5.Lcd.setFreeFont(FSB12);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.drawString("-", BTN_LX + BTN_W/2, BTN_Y + BTN_H/2, GFXFF);
M5.Lcd.fillRoundRect(BTN_RX, BTN_Y, BTN_W, BTN_H, 4, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(BTN_RX, BTN_Y, BTN_W, BTN_H, 4, UIColor::CARD_BORD);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("+", BTN_RX + BTN_W/2, BTN_Y + BTN_H/2, GFXFF);
// ── Step 提示 ────────────────────────────────────────────────
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString("step: 0.5s (0 = OFF)", CX, Y0 + 84, GFXFF);
// ── CANCEL / DONE 按鈕 ───────────────────────────────────────
constexpr int16_t ACT_W = 100;
constexpr int16_t ACT_H = 28;
constexpr int16_t ACT_Y = Y0 + 104;
constexpr int16_t ACT_LX = CX - 110; // CANCEL X = 50
constexpr int16_t ACT_RX = CX + 10; // DONE X = 170
M5.Lcd.fillRoundRect(ACT_LX, ACT_Y, ACT_W, ACT_H, 4, 0x4000);
M5.Lcd.drawRoundRect(ACT_LX, ACT_Y, ACT_W, ACT_H, 4, 0xF800);
M5.Lcd.setTextColor(0xF800, 0x4000);
M5.Lcd.drawString("CANCEL", ACT_LX + ACT_W/2, ACT_Y + ACT_H/2, GFXFF);
M5.Lcd.fillRoundRect(ACT_RX, ACT_Y, ACT_W, ACT_H, 4, 0x0060);
M5.Lcd.drawRoundRect(ACT_RX, ACT_Y, ACT_W, ACT_H, 4, TFT_GREEN);
M5.Lcd.setTextColor(TFT_GREEN, 0x0060);
M5.Lcd.drawString("DONE", ACT_RX + ACT_W/2, ACT_Y + ACT_H/2, GFXFF);
// ── Btn bar ──────────────────────────────────────────────────
drawBtnBar(nullptr, nullptr, nullptr);
}
/**
* @brief 繪製 DRILL SETUP 畫面(發數 / Par Time / Pass % / Rand Delay)
* @param def 目前 DrillDef 設定
*/
void UIScreen::drawDrillSetup(const DrillDef& def) {
// 可用高度 = GAME_AREA_H - MODE_HDR_H = 184 - 18 = 166px
// 4 行 × 40px = 160px,剩 6px 頂部邊距
constexpr int16_t ROW_H = 40;
constexpr int16_t LBL_X = 4;
constexpr int16_t Y0 = Layout::GAME_AREA_Y + Layout::MODE_HDR_H + 4;
constexpr int16_t CX = HW::LCD_W / 2;
// 按鍵佈局:值在中間,[-] 靠左,[+] 靠右,各留足夠空間
constexpr int16_t BTN_W = 40; // 按鍵寬度(比原本 28px 大)
constexpr int16_t BTN_H = 28; // 按鍵高度
constexpr int16_t VAL_W = 70; // 數值顯示寬度
constexpr int16_t LBL_W = 80; // 標籤寬度
// 按鍵 X 座標:[-] 在標籤右邊,[+] 靠右側
constexpr int16_t BTN_L_X = LBL_W + 4; // [-] 起點 x=84
constexpr int16_t VAL_X = BTN_L_X + BTN_W + 4; // 數值中心基準
constexpr int16_t BTN_R_X = HW::LCD_W - BTN_W - 4; // [+] 靠右 x=276
M5.Lcd.setFreeFont(FSS9);
M5.Lcd.setTextSize(1);
auto drawRow = [&](uint8_t row, const char* label, const char* val,
bool hasBtn, bool isBig = false) {
int16_t ry = Y0 + row * ROW_H;
int16_t rcy = ry + ROW_H / 2;
// 分隔線
if (row > 0)
M5.Lcd.drawFastHLine(0, ry, HW::LCD_W, UIColor::CARD_BORD);
// 標籤
M5.Lcd.setTextDatum(ML_DATUM);
M5.Lcd.setTextColor(UIColor::TEXT_DIM, TFT_BLACK);
M5.Lcd.drawString(label, LBL_X, rcy, GFXFF);
if (hasBtn) {
// [-] 按鍵
M5.Lcd.fillRoundRect(BTN_L_X, ry + 6, BTN_W, BTN_H, 4, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(BTN_L_X, ry + 6, BTN_W, BTN_H, 4, UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("-", BTN_L_X + BTN_W/2, rcy, GFXFF);
// 數值
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
M5.Lcd.drawString(val, CX, rcy, GFXFF);
// [+] 按鍵
M5.Lcd.fillRoundRect(BTN_R_X, ry + 6, BTN_W, BTN_H, 4, UIColor::CARD_BG);
M5.Lcd.drawRoundRect(BTN_R_X, ry + 6, BTN_W, BTN_H, 4, UIColor::CARD_BORD);
M5.Lcd.setTextColor(TFT_WHITE, UIColor::CARD_BG);
M5.Lcd.drawString("+", BTN_R_X + BTN_W/2, rcy, GFXFF);
} else {
// 無按鍵:只顯示數值 badge
uint16_t bg = (strcmp(val, "ON") == 0) ? 0x0060 : UIColor::CARD_BG;
uint16_t col = (strcmp(val, "ON") == 0) ? TFT_GREEN : UIColor::TEXT_DIM;
M5.Lcd.fillRoundRect(VAL_X, ry + 6, VAL_W, BTN_H, 4, bg);
M5.Lcd.drawRoundRect(VAL_X, ry + 6, VAL_W, BTN_H, 4,
(strcmp(val, "ON") == 0) ? TFT_GREEN : UIColor::CARD_BORD);
M5.Lcd.setTextDatum(MC_DATUM);
M5.Lcd.setTextColor(col, bg);
M5.Lcd.drawString(val, VAL_X + VAL_W/2, rcy, GFXFF);
}
};