forked from srwh1234/L1MapViewer
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMapForm.Overlay.cs
More file actions
1215 lines (1047 loc) · 50.7 KB
/
MapForm.Overlay.cs
File metadata and controls
1215 lines (1047 loc) · 50.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using SkiaSharp;
using L1MapViewer.Helper;
using L1MapViewer.Models;
using L1MapViewer.Other;
using L1MapViewer.Localization;
using Eto.Drawing;
using NLog;
namespace L1FlyMapViewer
{
public partial class MapForm
{
// Logger for overlay (MapForm 主檔案已有 _logger)
/// <summary>
/// 取得跨平台支援中文的 SKTypeface。
/// macOS 用 PingFang TC,Windows 用 Microsoft JhengHei,都沒有就用系統預設。
/// </summary>
private static SKTypeface GetCjkTypeface(SKFontStyle style = null)
{
style ??= SKFontStyle.Normal;
string[] families = { "Microsoft JhengHei", "PingFang TC", "Heiti TC" };
foreach (var family in families)
{
var tf = SKTypeface.FromFamilyName(family, style);
// 檢查是否真的找到了這個字型(SkiaSharp 找不到時會回傳預設字型)
if (tf != null && string.Equals(tf.FamilyName, family, StringComparison.OrdinalIgnoreCase))
return tf;
}
return SKTypeface.Default;
}
#region SkiaSharp Overlay 繪製函數
// 輔助函數:取得屬性對應的 SKColor
private SKColor GetAttributeColorSK(int attribute)
{
// 參考 GetAttributeColor 函數
return attribute switch
{
1 => new SKColor(255, 0, 0, 150), // 紅色
2 => new SKColor(0, 255, 0, 150), // 綠色
3 => new SKColor(0, 0, 255, 150), // 藍色
4 => new SKColor(255, 255, 0, 150), // 黃色
5 => new SKColor(255, 0, 255, 150), // 紫色
6 => new SKColor(0, 255, 255, 150), // 青色
7 => new SKColor(255, 128, 0, 150), // 橙色
8 => new SKColor(128, 0, 255, 150), // 紫羅蘭
_ => new SKColor(128, 128, 128, 150) // 灰色
};
}
// 繪製 Layer3 屬性覆蓋層 (SkiaSharp 版本)
// 參考 DrawLayer3AttributesViewport
private void DrawLayer3AttributesViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot)
{
foreach (var s32Data in s32FilesSnapshot)
{
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
var attr = s32Data.Layer3[y, x];
if (attr == null) continue;
if (attr.Attribute1 == 0 && attr.Attribute2 == 0) continue;
int x1 = x * 2;
int localBaseX = 0 - 24 * (x1 / 2);
int localBaseY = 63 * 12 - 12 * (x1 / 2);
int X = mx + localBaseX + x1 * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
// 跳過不在 Viewport 內的格子
if (X + 48 < 0 || X > worldRect.Width || Y + 24 < 0 || Y > worldRect.Height)
continue;
// 菱形的頂點
var pTop = new SKPoint(X + 24, Y + 0);
var pRight = new SKPoint(X + 48, Y + 12);
var pBottom = new SKPoint(X + 24, Y + 24);
var pLeft = new SKPoint(X + 0, Y + 12);
var pCenter = new SKPoint(X + 24, Y + 12);
// 左半邊 - 使用 Attribute1
if (attr.Attribute1 != 0)
{
var color1 = GetAttributeColorSK(attr.Attribute1);
using var pen = new SKPaint { Color = color1, Style = SKPaintStyle.Stroke, StrokeWidth = 3, IsAntialias = true };
canvas.DrawLine(pLeft, pTop, pen);
canvas.DrawLine(pTop, pCenter, pen);
canvas.DrawLine(pCenter, pBottom, pen);
canvas.DrawLine(pBottom, pLeft, pen);
}
// 右半邊 - 使用 Attribute2
if (attr.Attribute2 != 0)
{
var color2 = GetAttributeColorSK(attr.Attribute2);
using var pen = new SKPaint { Color = color2, Style = SKPaintStyle.Stroke, StrokeWidth = 3, IsAntialias = true };
canvas.DrawLine(pTop, pRight, pen);
canvas.DrawLine(pRight, pBottom, pen);
canvas.DrawLine(pBottom, pCenter, pen);
canvas.DrawLine(pCenter, pTop, pen);
}
}
}
}
}
// 繪製通行性覆蓋層 (SkiaSharp 版本)
// 參考 DrawPassableOverlayViewport - 繪製邊線而非填充
// Attribute1 = 左上邊線, Attribute2 = 右上邊線
private void DrawPassableOverlayViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot)
{
// 不可通行:紫色粗線,可通行:青色細線
using var penImpassable = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 3,
Color = new SKColor(128, 0, 128, 255) // 紫色
};
using var penPassable = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 2,
Color = new SKColor(50, 200, 255, 255) // 青色
};
foreach (var s32Data in s32FilesSnapshot)
{
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
var attr = s32Data.Layer3[y, x];
if (attr == null) continue;
int x1 = x * 2;
int localBaseX = 0 - 24 * (x1 / 2);
int localBaseY = 63 * 12 - 12 * (x1 / 2);
int X = mx + localBaseX + x1 * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
if (X + 48 < 0 || X > worldRect.Width || Y + 24 < 0 || Y > worldRect.Height)
continue;
// 菱形頂點
var pTop = new SKPoint(X + 24, Y + 0);
var pRight = new SKPoint(X + 48, Y + 12);
var pLeft = new SKPoint(X + 0, Y + 12);
// 左上邊線 - 使用 Attribute1 bit0 判斷
var pen1 = (attr.Attribute1 & 0x01) != 0 ? penImpassable : penPassable;
canvas.DrawLine(pLeft, pTop, pen1);
// 右上邊線 - 使用 Attribute2 bit0 判斷
var pen2 = (attr.Attribute2 & 0x01) != 0 ? penImpassable : penPassable;
canvas.DrawLine(pTop, pRight, pen2);
}
}
}
}
// 繪製區域覆蓋層 (SkiaSharp 版本)
// 參考 DrawRegionsOverlayViewport - 使用 Attribute1 低4位判斷整個格子
// 低4位: 0-3=一般, 4-7/C-F=安全(bit2), 8-B=戰鬥(bit3且非bit2)
private void DrawRegionsOverlayViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, bool showSafe, bool showCombat, IEnumerable<S32Data> s32FilesSnapshot)
{
// 安全區:藍色,戰鬥區:紅色(與舊版一致)
using var safeBrush = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(0, 150, 255, 80) // 藍色半透明
};
using var combatBrush = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(255, 50, 50, 80) // 紅色半透明
};
foreach (var s32Data in s32FilesSnapshot)
{
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
var attr = s32Data.Layer3[y, x];
if (attr == null) continue;
int x1 = x * 2;
int localBaseX = 0 - 24 * (x1 / 2);
int localBaseY = 63 * 12 - 12 * (x1 / 2);
int X = mx + localBaseX + x1 * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
if (X + 48 < 0 || X > worldRect.Width || Y + 24 < 0 || Y > worldRect.Height)
continue;
// 使用 Layer3AttributeDecoder 統一處理(含例外值替換)
bool isSafe = Layer3AttributeDecoder.IsSafeZone(attr.Attribute1);
bool isCombat = Layer3AttributeDecoder.IsCombatZone(attr.Attribute1);
// 決定整個格子的顏色
SKPaint regionBrush = null;
if (isCombat && showCombat)
regionBrush = combatBrush;
else if (isSafe && showSafe)
regionBrush = safeBrush;
if (regionBrush != null)
{
// 繪製整個菱形
using var path = new SKPath();
path.MoveTo(X + 24, Y + 0); // 上
path.LineTo(X + 48, Y + 12); // 右
path.LineTo(X + 24, Y + 24); // 下
path.LineTo(X + 0, Y + 12); // 左
path.Close();
canvas.DrawPath(path, regionBrush);
}
}
}
}
}
// 繪製商店區域覆蓋層 (SkiaSharp 版本)
private void DrawMarketZonesOverlayViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot)
{
using var marketBrush = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(0, 200, 0, 80) // 綠色半透明
};
int marketCount = 0;
int cellCount = 0;
foreach (var s32Data in s32FilesSnapshot)
{
// 如果沒有 MarketRegion 資料,跳過
if (s32Data.MarketRegion == null)
continue;
marketCount++;
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
// 取得 S32 的遊戲座標起點
int startGameX = s32Data.SegInfo.nLinBeginX;
int startGameY = s32Data.SegInfo.nLinBeginY;
for (int y = 0; y < 64; y++)
{
for (int x = 0; x < 64; x++)
{
// 使用世界座標查詢 MarketRegion
int worldGameX = startGameX + x;
int worldGameY = startGameY + y;
var isInRegion = s32Data.MarketRegion.IsInRegion(worldGameX, worldGameY);
if (isInRegion != true)
continue;
cellCount++;
int x1 = x * 2;
int localBaseX = 0 - 24 * (x1 / 2);
int localBaseY = 63 * 12 - 12 * (x1 / 2);
int X = mx + localBaseX + x1 * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
if (X + 48 < 0 || X > worldRect.Width || Y + 24 < 0 || Y > worldRect.Height)
continue;
// 繪製整個菱形
using var path = new SKPath();
path.MoveTo(X + 24, Y + 0); // 上
path.LineTo(X + 48, Y + 12); // 右
path.LineTo(X + 24, Y + 24); // 下
path.LineTo(X + 0, Y + 12); // 左
path.Close();
canvas.DrawPath(path, marketBrush);
}
}
}
// 改用 Info 層級以便確認有執行到
_logger.Info($"[RENDER-OVERLAY] MarketZones: marketCount={marketCount}, cellCount={cellCount}");
}
// 繪製格線 (SkiaSharp 版本)
// 參考 DrawS32GridViewport
private void DrawS32GridViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot)
{
using var gridPaint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Stroke,
StrokeWidth = 2,
Color = new SKColor(255, 0, 0, 100)
};
foreach (var s32Data in s32FilesSnapshot)
{
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
// 繪製 "/" 方向的線
for (int i = 0; i <= 64; i++)
{
int x3 = i;
int x = x3 * 2;
int startLocalBaseX = -24 * (x / 2);
int startLocalBaseY = 63 * 12 - 12 * (x / 2);
int startX = mx + startLocalBaseX + x * 24 + 0 * 24 + 24 - worldRect.X;
int startY = my + startLocalBaseY + 0 * 12 - worldRect.Y;
// 終點: y=63 格子的右頂點 = (X+48, Y+12)
int endX = mx + startLocalBaseX + x * 24 + 63 * 24 + 48 - worldRect.X;
int endY = my + startLocalBaseY + 63 * 12 + 12 - worldRect.Y;
if (endX >= 0 && startX <= worldRect.Width &&
Math.Max(startY, endY) >= 0 && Math.Min(startY, endY) <= worldRect.Height)
{
canvas.DrawLine(startX, startY, endX, endY, gridPaint);
}
}
// 繪製 "\" 方向的線(沿著 x 方向的邊)
// 總共需要 65 條線
for (int j = 0; j <= 64; j++)
{
int y = j;
// 起點 (x3=0 時的左頂點)
int x = 0;
int startLocalBaseX = -24 * (x / 2);
int startLocalBaseY = 63 * 12 - 12 * (x / 2);
int startX = mx + startLocalBaseX + x * 24 + y * 24 - worldRect.X;
int startY = my + startLocalBaseY + y * 12 + 12 - worldRect.Y; // +12 是到左頂點
// 終點 (x3=63 時的上頂點) = (X+24, Y)
x = 63 * 2;
int endLocalBaseX = -24 * (x / 2);
int endLocalBaseY = 63 * 12 - 12 * (x / 2);
int endX = mx + endLocalBaseX + x * 24 + y * 24 + 24 - worldRect.X;
int endY = my + endLocalBaseY + y * 12 - worldRect.Y;
if (endX >= 0 && startX <= worldRect.Width &&
Math.Max(startY, endY) >= 0 && Math.Min(startY, endY) <= worldRect.Height)
{
canvas.DrawLine(startX, startY, endX, endY, gridPaint);
}
}
}
}
// 繪製座標標籤 (SkiaSharp 版本)
// 參考 DrawCoordinateLabelsViewport
private void DrawCoordinateLabelsViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot)
{
using var textPaint = new SKPaint
{
IsAntialias = true,
TextSize = 10,
Color = SKColors.Blue,
Typeface = GetCjkTypeface(SKFontStyle.Bold)
};
using var bgPaint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(255, 255, 255, 180)
};
int interval = 10;
foreach (var s32Data in s32FilesSnapshot)
{
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
for (int y = 0; y < 64; y += interval)
{
for (int x = 0; x < 128; x += interval)
{
int localBaseX = -24 * (x / 2);
int localBaseY = 63 * 12 - 12 * (x / 2);
int pixelX = mx + localBaseX + x * 24 + y * 24 - worldRect.X;
int pixelY = my + localBaseY + y * 12 - worldRect.Y;
// 跳過不在 Viewport 內的格子
if (pixelX + 24 < 0 || pixelX > worldRect.Width ||
pixelY + 24 < 0 || pixelY > worldRect.Height)
continue;
// 計算遊戲座標
int gameX = s32Data.SegInfo.nLinBeginX + x / 2; // Layer1 座標轉遊戲座標
int gameY = s32Data.SegInfo.nLinBeginY + y;
string text = $"{gameX},{gameY}";
var bounds = new SKRect();
textPaint.MeasureText(text, ref bounds);
int textX = pixelX + 12 - (int)bounds.Width / 2;
int textY = pixelY + 12 - (int)bounds.Height / 2;
canvas.DrawRect(textX - 2, textY - 1, bounds.Width + 4, bounds.Height + 2, bgPaint);
canvas.DrawText(text, textX, textY + bounds.Height, textPaint);
}
}
}
}
// 繪製 S32 邊界 (SkiaSharp 版本)
// 參考 DrawS32BoundaryOnlyViewport
private void DrawS32BoundaryOnlyViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot)
{
using var boundaryPaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 2,
Color = SKColors.Cyan
};
using var textPaint = new SKPaint
{
IsAntialias = true,
TextSize = 10,
Color = SKColors.Lime,
Typeface = GetCjkTypeface(SKFontStyle.Bold)
};
using var bgPaint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(0, 0, 0, 200)
};
foreach (var s32Data in s32FilesSnapshot)
{
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
// 計算四個角點
var corners = new SKPoint[4];
int[] cornerX3 = { 0, 64, 64, 0 };
int[] cornerY = { 0, 0, 64, 64 };
for (int i = 0; i < 4; i++)
{
int x3 = cornerX3[i];
int y = cornerY[i];
int x = x3 * 2;
int localBaseX = -24 * (x / 2);
int localBaseY = 63 * 12 - 12 * (x / 2);
int X = mx + localBaseX + x * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
corners[i] = new SKPoint(X, Y + 12);
}
canvas.DrawLine(corners[0], corners[1], boundaryPaint);
canvas.DrawLine(corners[1], corners[2], boundaryPaint);
canvas.DrawLine(corners[2], corners[3], boundaryPaint);
canvas.DrawLine(corners[3], corners[0], boundaryPaint);
// 繪製中心標籤
float centerX = (corners[0].X + corners[2].X) / 2;
float centerY = (corners[0].Y + corners[2].Y) / 2;
string centerText = $"({mx},{my})";
var bounds = new SKRect();
textPaint.MeasureText(centerText, ref bounds);
canvas.DrawRect(centerX - bounds.Width / 2 - 2, centerY - bounds.Height / 2 - 1, bounds.Width + 4, bounds.Height + 2, bgPaint);
canvas.DrawText(centerText, centerX - bounds.Width / 2, centerY + bounds.Height / 2, textPaint);
}
}
// 繪製 Layer5 覆蓋層 (SkiaSharp 版本)
// 完全複製自 DrawLayer5OverlayViewport
private void DrawLayer5OverlayViewportSK(SKCanvas canvas, Struct.L1Map currentMap, Rectangle worldRect, bool isLayer5Edit, IEnumerable<S32Data> s32FilesSnapshot, HashSet<string> checkedFilePaths)
{
// 收集所有 Layer5 位置(去重)
var drawnPositions = new HashSet<(int mx, int my, int x, int y)>();
// 半透明藍色填充和邊框 (原版顏色: ARGB 80,60,140,255)
using var fillPaint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(60, 140, 255, 80)
};
using var borderPaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 1.5f,
Color = new SKColor(80, 160, 255, 180)
};
using var highlightPaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 1f,
Color = new SKColor(150, 200, 255, 200)
};
foreach (var s32Data in s32FilesSnapshot)
{
// 只繪製已啟用的 S32
if (!checkedFilePaths.Contains(s32Data.FilePath)) continue;
if (s32Data.Layer5.Count == 0) continue;
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
foreach (var item in s32Data.Layer5)
{
// 同位置只畫一次
var posKey = (mx, my, (int)item.X, (int)item.Y);
if (drawnPositions.Contains(posKey)) continue;
drawnPositions.Add(posKey);
// Layer5 的 X 是 0-127(Layer1 座標系),Y 是 0-63
// 繪製半格大小的三角形(X 切半)
int x1 = item.X; // 0-127
int y = item.Y; // 0-63
int localBaseX = 0 - 24 * (x1 / 2);
int localBaseY = 63 * 12 - 12 * (x1 / 2);
int X = mx + localBaseX + x1 * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
// 跳過不在 Viewport 內的格子
if (X + 24 < 0 || X > worldRect.Width || Y + 12 < 0 || Y > worldRect.Height)
continue;
// 繪製半格三角形(根據 X 奇偶決定左半或右半)
using var path = new SKPath();
if (x1 % 2 == 0)
{
// 偶數 X:左半三角形
float pLeftX = X + 0, pLeftY = Y + 12;
float pTopX = X + 24, pTopY = Y + 0;
float pBottomX = X + 24, pBottomY = Y + 24;
path.MoveTo(pLeftX, pLeftY);
path.LineTo(pTopX, pTopY);
path.LineTo(pBottomX, pBottomY);
path.Close();
canvas.DrawPath(path, fillPaint);
// 邊框(上亮下暗)
canvas.DrawLine(pLeftX, pLeftY, pTopX, pTopY, highlightPaint);
canvas.DrawLine(pTopX, pTopY, pBottomX, pBottomY, borderPaint);
canvas.DrawLine(pBottomX, pBottomY, pLeftX, pLeftY, borderPaint);
}
else
{
// 奇數 X:右半三角形
float pTopX = X + 0, pTopY = Y + 0;
float pRightX = X + 24, pRightY = Y + 12;
float pBottomX = X + 0, pBottomY = Y + 24;
path.MoveTo(pTopX, pTopY);
path.LineTo(pRightX, pRightY);
path.LineTo(pBottomX, pBottomY);
path.Close();
canvas.DrawPath(path, fillPaint);
// 邊框(上亮下暗)
canvas.DrawLine(pTopX, pTopY, pRightX, pRightY, highlightPaint);
canvas.DrawLine(pRightX, pRightY, pBottomX, pBottomY, borderPaint);
canvas.DrawLine(pBottomX, pBottomY, pTopX, pTopY, borderPaint);
}
}
}
// 在透明編輯模式下,繪製已設定 Layer5 的群組物件覆蓋層
if (isLayer5Edit)
{
DrawLayer5GroupOverlaySK(canvas, worldRect, s32FilesSnapshot, checkedFilePaths);
}
}
// 繪製 Layer5 群組覆蓋層 (SkiaSharp 版本)
// 完全複製自 DrawLayer5GroupOverlay
private void DrawLayer5GroupOverlaySK(SKCanvas canvas, Rectangle worldRect, IEnumerable<S32Data> s32FilesSnapshot, HashSet<string> checkedFilePaths)
{
// 只有在有選取格子時才顯示
if (_editState.SelectedCells.Count == 0) return;
// 從選取的格子收集 Layer5 的 GroupId 及其 Type
var groupLayer5Info = new Dictionary<int, byte>(); // GroupId -> Type
foreach (var selectedCell in _editState.SelectedCells)
{
var s32Data = selectedCell.S32Data;
int localX = selectedCell.LocalX; // Layer1 座標 (0-127)
int localY = selectedCell.LocalY; // Layer3 座標 (0-63)
// 查找該格子位置對應的 Layer5 設定
// Layer5 的 X 是 0-127,Y 是 0-63
// selectedCell.LocalX 是 Layer1 座標 (0-127),LocalY 是 (0-63)
// 一個遊戲格子對應兩個 Layer1 X 座標(localX 和 localX+1)
foreach (var item in s32Data.Layer5)
{
if ((item.X == localX || item.X == localX + 1) && item.Y == localY)
{
// 如果同一個 GroupId 有多個設定,保留第一個
if (!groupLayer5Info.ContainsKey(item.ObjectIndex))
{
groupLayer5Info[item.ObjectIndex] = item.Type;
}
}
}
}
if (groupLayer5Info.Count == 0) return;
// 半透明覆蓋色:Type=0 紫色(高對比),Type=1 紅色
using var type0Paint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(180, 0, 255, 100)
};
using var type1Paint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(255, 80, 80, 100)
};
foreach (var s32Data in s32FilesSnapshot)
{
// 只繪製已啟用的 S32
if (!checkedFilePaths.Contains(s32Data.FilePath)) continue;
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
foreach (var obj in s32Data.Layer4)
{
// 檢查該群組是否有 Layer5 設定
if (!groupLayer5Info.TryGetValue(obj.GroupId, out byte type))
continue;
// 使用與高亮格子相同的座標計算方式
int x1 = obj.X; // 0-127 (Layer1 座標系)
int y = obj.Y; // 0-63
int localBaseX = 0 - 24 * (x1 / 2);
int localBaseY = 63 * 12 - 12 * (x1 / 2);
int X = mx + localBaseX + x1 * 24 + y * 24 - worldRect.X;
int Y = my + localBaseY + y * 12 - worldRect.Y;
// 跳過不在 Viewport 內的物件
if (X + 24 < 0 || X > worldRect.Width || Y + 24 < 0 || Y > worldRect.Height)
continue;
// 繪製半格菱形覆蓋(與格子高亮相同大小)
var paint = type == 0 ? type0Paint : type1Paint;
using var path = new SKPath();
path.MoveTo(X + 0, Y + 12); // 左
path.LineTo(X + 12, Y + 0); // 上
path.LineTo(X + 24, Y + 12); // 右
path.LineTo(X + 12, Y + 24); // 下
path.Close();
canvas.DrawPath(path, paint);
}
}
}
// 繪製群組高亮覆蓋層 (SkiaSharp 版本)
// 參考 DrawGroupHighlightOverlay
private void DrawGroupHighlightOverlaySK(SKCanvas canvas, Rectangle worldRect, List<(int, int)> groupHighlightCells, IEnumerable<S32Data> s32FilesSnapshot)
{
using var fillPaint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(0, 255, 0, 80) // 綠色半透明
};
using var strokePaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 2,
Color = new SKColor(0, 255, 0, 200)
};
foreach (var (cellX, cellY) in groupHighlightCells)
{
// 找到包含這個遊戲座標的 S32
foreach (var s32Data in s32FilesSnapshot)
{
if (cellX < s32Data.SegInfo.nLinBeginX || cellX >= s32Data.SegInfo.nLinEndX ||
cellY < s32Data.SegInfo.nLinBeginY || cellY >= s32Data.SegInfo.nLinEndY)
continue;
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
int localX3 = cellX - s32Data.SegInfo.nLinBeginX;
int localY = cellY - s32Data.SegInfo.nLinBeginY;
int x = localX3 * 2;
int localBaseX = -24 * (x / 2);
int localBaseY = 63 * 12 - 12 * (x / 2);
int pixelX = mx + localBaseX + x * 24 + localY * 24 - worldRect.X;
int pixelY = my + localBaseY + localY * 12 - worldRect.Y;
if (pixelX + 48 < 0 || pixelX > worldRect.Width ||
pixelY + 24 < 0 || pixelY > worldRect.Height)
continue;
var path = new SKPath();
path.MoveTo(pixelX + 24, pixelY);
path.LineTo(pixelX + 48, pixelY + 12);
path.LineTo(pixelX + 24, pixelY + 24);
path.LineTo(pixelX, pixelY + 12);
path.Close();
canvas.DrawPath(path, fillPaint);
canvas.DrawPath(path, strokePaint);
break;
}
}
}
// 繪製選中格子高亮 (SkiaSharp 版本)
// 參考 DrawHighlightedCellViewport
private void DrawHighlightedCellViewportSK(SKCanvas canvas, Rectangle worldRect, S32Data highlightedS32Data, int highlightedCellX, int highlightedCellY)
{
_logger.Debug($"[HIGHLIGHT-SK] Enter: s32Data={highlightedS32Data?.FilePath}, cellX={highlightedCellX}, cellY={highlightedCellY}");
if (highlightedS32Data == null)
{
_logger.Debug("[HIGHLIGHT-SK] Exit: s32Data is null");
return;
}
// RGB565 不支援 alpha,使用 Fill + Stroke 繪製明顯的高亮
using var fillPaint = new SKPaint
{
IsAntialias = false,
Style = SKPaintStyle.Fill,
Color = new SKColor(255, 255, 0) // 亮黃色填充
};
using var strokePaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 3,
Color = new SKColor(255, 128, 0) // 橙色邊框
};
int[] loc = highlightedS32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
// highlightedCellX 是 Layer1 座標 (0-127)
// 公式與原版 DrawHighlightedCellViewport 完全一致
int localBaseX = 0 - 24 * (highlightedCellX / 2);
int localBaseY = 63 * 12 - 12 * (highlightedCellX / 2);
int X = mx + localBaseX + highlightedCellX * 24 + highlightedCellY * 24 - worldRect.X;
int Y = my + localBaseY + highlightedCellY * 12 - worldRect.Y;
_logger.Debug($"[HIGHLIGHT-SK] worldRect=({worldRect.X},{worldRect.Y},{worldRect.Width},{worldRect.Height}), mx={mx}, my={my}, X={X}, Y={Y}");
// 菱形四個角點 (與原版完全一致: p1=左, p2=上, p3=右, p4=下)
using var path = new SKPath();
path.MoveTo(X + 0, Y + 12); // p1 左
path.LineTo(X + 12, Y + 0); // p2 上
path.LineTo(X + 24, Y + 12); // p3 右
path.LineTo(X + 12, Y + 24); // p4 下
path.Close();
canvas.DrawPath(path, fillPaint);
canvas.DrawPath(path, strokePaint);
_logger.Debug($"[HIGHLIGHT-SK] Done drawing at ({X},{Y})");
}
// PaintOverlaySK callback - 繪製所有覆蓋層(Layer8、選取格子等)
private void DrawSelectedCellsSK(SKCanvas canvas, float zoomLevel, int scrollX, int scrollY)
{
// 繪製 Layer8 標記和 SPR 動畫
if (_viewState.ShowLayer8)
{
DrawLayer8OverlaySK(canvas, zoomLevel, scrollX, scrollY);
}
// 繪製選取的格子
DrawSelectedCellsOnlySK(canvas, zoomLevel, scrollX, scrollY);
// 繪製編輯模式的 Help Label
DrawEditModeHelpLabelSK(canvas);
}
// 繪製編輯模式的 Help Label(固定位置,不隨地圖捲動)
private void DrawEditModeHelpLabelSK(SKCanvas canvas)
{
string helpText = null;
SKColor bgColor;
SKColor textColor;
// 根據當前編輯模式決定顯示內容
if (_editState.IsLayer5EditMode)
{
helpText = LocalizationManager.L("Help_Layer5EditMode");
bgColor = new SKColor(30, 30, 50, 220);
textColor = new SKColor(100, 180, 255);
}
else if (currentPassableEditMode != PassableEditMode.None)
{
helpText = LocalizationManager.L("Help_PassEditMode");
bgColor = new SKColor(30, 30, 30, 200);
textColor = new SKColor(173, 216, 230); // LightBlue
}
else if (currentRegionEditMode != RegionEditMode.None)
{
helpText = LocalizationManager.L("Help_RegionEditMode");
bgColor = new SKColor(40, 80, 40, 200);
textColor = new SKColor(144, 238, 144); // LightGreen
}
else
{
// 沒有編輯模式,繪製預設提示
helpText = LocalizationManager.L("Hint_MouseControls");
if (string.IsNullOrEmpty(helpText))
{
helpText = "【操作說明】\n" +
"• 滾輪:縮放\n" +
"• 左鍵拖曳:移動地圖\n" +
"• 右鍵:選單";
}
bgColor = new SKColor(50, 50, 50, 180);
textColor = new SKColor(255, 255, 255);
}
// 繪製 Help Label
float x = 10;
float y = 10;
float padding = 8;
float fontSize = 13;
float lineHeight = fontSize * 1.4f;
using var textPaint = new SKPaint
{
IsAntialias = true,
TextSize = fontSize,
Color = textColor,
Typeface = GetCjkTypeface(SKFontStyle.Normal)
};
// 計算文字區域大小
string[] lines = helpText.Split('\n');
float maxWidth = 0;
foreach (var line in lines)
{
float lineWidth = textPaint.MeasureText(line);
if (lineWidth > maxWidth) maxWidth = lineWidth;
}
float boxWidth = maxWidth + padding * 2;
float boxHeight = lines.Length * lineHeight + padding * 2;
// 繪製背景
using var bgPaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Fill,
Color = bgColor
};
canvas.DrawRoundRect(x, y, boxWidth, boxHeight, 4, 4, bgPaint);
// 繪製邊框
using var borderPaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Stroke,
StrokeWidth = 1,
Color = new SKColor(100, 100, 100, 200)
};
canvas.DrawRoundRect(x, y, boxWidth, boxHeight, 4, 4, borderPaint);
// 繪製文字
float textY = y + padding + fontSize;
foreach (var line in lines)
{
canvas.DrawText(line, x + padding, textY, textPaint);
textY += lineHeight;
}
}
// 繪製 Layer8 覆蓋層(SK 版本)
// 參考 DrawLayer8OverlayOnControl
private void DrawLayer8OverlaySK(SKCanvas canvas, float zoomLevel, int scrollX, int scrollY)
{
if (_document?.S32Files == null || _document.S32Files.Count == 0) return;
int viewportWidth = (int)canvas.LocalClipBounds.Width;
int viewportHeight = (int)canvas.LocalClipBounds.Height;
foreach (var s32Data in _document.S32Files.Values)
{
if (s32Data.Layer8.Count == 0) continue;
int[] loc = s32Data.SegInfo.GetLoc(1.0);
int mx = loc[0];
int my = loc[1];
for (int i = 0; i < s32Data.Layer8.Count; i++)
{
var item = s32Data.Layer8[i];
// Layer8 X,Y 是絕對遊戲座標,先轉為本地座標
int localLayer3X = item.X - s32Data.SegInfo.nLinBeginX;
int localLayer3Y = item.Y - s32Data.SegInfo.nLinBeginY;
if (localLayer3X < 0 || localLayer3X > 63 || localLayer3Y < 0 || localLayer3Y > 63)
continue;
int layer1X = localLayer3X * 2;
int layer1Y = localLayer3Y;
int baseX = -24 * (layer1X / 2);
int baseY = 63 * 12 - 12 * (layer1X / 2);
// Marker 位置:格子中心 (+12, +12)
int markerWorldX = mx + baseX + layer1X * 24 + layer1Y * 24 + 12;
int markerWorldY = my + baseY + layer1Y * 12 + 12;
// SPR 位置:格子左上角(offset 由 SPR 檔案提供)
int sprWorldX = mx + baseX + layer1X * 24 + layer1Y * 24;
int sprWorldY = my + baseY + layer1Y * 12;
// 轉為螢幕座標
float markerX = (markerWorldX - scrollX) * zoomLevel;
float markerY = (markerWorldY - scrollY) * zoomLevel;
float sprX = (sprWorldX - scrollX) * zoomLevel;
float sprY = (sprWorldY - scrollY) * zoomLevel;
// 檢查是否在可見範圍內
if (markerX < -50 || markerX > viewportWidth + 50 || markerY < -50 || markerY > viewportHeight + 50)
continue;
bool isEnabled = _editState.EnabledLayer8Items.Contains((s32Data.FilePath, i));
// 繪製標記(圓點)- 受 ShowLayer8Marker 控制
float markerRadius = Math.Max(5, 10 * zoomLevel);
if (_viewState.ShowLayer8Marker)
{
if (isEnabled)
{
// 啟用狀態:灰色半透明 marker
using var fillPaint = new SKPaint
{
IsAntialias = true,
Style = SKPaintStyle.Fill,
Color = new SKColor(128, 128, 128, 25)
};
using var strokePaint = new SKPaint