-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathInkCanvas.cs
More file actions
1145 lines (971 loc) · 39.6 KB
/
Copy pathInkCanvas.cs
File metadata and controls
1145 lines (971 loc) · 39.6 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 Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Threading;
using SkiaSharp;
using System;
using System.Collections.Generic;
namespace ShowWrite
{
public class InkCanvas : Control, IDisposable
{
private int _videoWidth;
private int _videoHeight;
private bool _isPhotoMode;
private double _photoWidth;
private double _photoHeight;
private bool _isWhiteboardMode;
private string? _whiteboardBackgroundPath;
private SKBitmap? _whiteboardBackgroundBitmap;
private readonly List<InkStroke> _strokes = new();
private List<SKPoint>? _currentVideoPoints;
private List<SKPoint>? _currentScreenPoints;
private List<float>? _currentPointWidths;
private List<float>? _currentZoomFactors;
private List<long>? _currentTimestamps;
private bool _currentIsEraser;
private float _currentSize;
private SKColor _currentColor;
private float _currentRatio = 0.5f;
private const float BasePenWidthScale = 0.8f;
private List<InkStroke>? _tempStrokes;
private SKPoint _lastEraserPoint;
private bool _hasLastEraserPoint;
private WriteableBitmap? _displayBitmap;
private bool _isDrawing;
private bool _invalidateScheduled = false;
private Image? _videoImage;
private double _currentZoom = 1.0;
private Point _currentPan = new Point(0, 0);
private Point _zoomBorderOffset = new Point(0, 0);
private bool _isPalmEraserActive = false;
private bool _lastModeBeforePalmEraser = false;
private double _palmEraserThreshold = 5000.0;
private double _currentTouchArea = 0.0;
private bool _enablePalmEraser = true;
private int _palmActivationHitCount = 0;
private int _palmReleaseHitCount = 0;
private DateTime _lastPalmHitTimeUtc = DateTime.MinValue;
private const int PalmReleaseDebounceMs = 90;
// 橡皮光标事件
public event Action<Point, float, bool>? EraserCursorUpdate;
public int PenSize { get; set; } = 4;
public SKColor PenColor { get; set; } = SKColors.Red;
public int EraserSize { get; set; } = 20;
public PenSettings PenSettings { get; set; } = new PenSettings();
public double PalmEraserThreshold
{
get => _palmEraserThreshold;
set => _palmEraserThreshold = Math.Max(1000, value);
}
public bool EnablePalmEraser
{
get => _enablePalmEraser;
set => _enablePalmEraser = value;
}
public bool IsPalmEraserActive => _isPalmEraserActive;
public bool IsPenMode { get; private set; }
public bool IsEraserMode { get; private set; }
public InkCanvas()
{
ClipToBounds = false;
// 高 DPI 适配(已使用布局取整)
UseLayoutRounding = true;
PointerPressed += OnPointerPressed;
PointerMoved += OnPointerMoved;
PointerReleased += OnPointerReleased;
PointerCaptureLost += OnPointerCaptureLost;
}
public void SetVideoImage(Image videoImage)
{
_videoImage = videoImage;
}
public void SetVideoFrame(OpenCvSharp.Mat? frame)
{
if (frame != null && !frame.Empty())
{
SetVideoSize(frame.Width, frame.Height);
}
}
public void SetTransform(double zoom, Point pan, Point zoomBorderOffset)
{
if (Math.Abs(_currentZoom - zoom) > 0.001 || _currentPan != pan || _zoomBorderOffset != zoomBorderOffset)
{
_currentZoom = zoom;
_currentPan = pan;
_zoomBorderOffset = zoomBorderOffset;
ScheduleInvalidate();
}
}
private void SetVideoSize(int width, int height)
{
if (width <= 0 || height <= 0) return;
if (_videoWidth == width && _videoHeight == height) return;
_videoWidth = width;
_videoHeight = height;
}
public void SetPenMode()
{
IsPenMode = true;
IsEraserMode = false;
if (_videoImage != null)
{
_videoImage.Cursor = Cursor.Default;
}
IsHitTestVisible = true;
Cursor = Cursor.Default;
EraserCursorUpdate?.Invoke(default, 0, false);
}
public void SetEraserMode()
{
IsPenMode = false;
IsEraserMode = true;
if (_videoImage != null)
{
_videoImage.Cursor = Cursor.Default;
}
IsHitTestVisible = true;
Cursor = Cursor.Default;
EraserCursorUpdate?.Invoke(default, EraserSize, true);
}
public void SetMoveMode()
{
IsPenMode = false;
IsEraserMode = false;
if (_videoImage != null)
{
_videoImage.Cursor = Cursor.Default;
}
IsHitTestVisible = false;
Cursor = Cursor.Default;
EraserCursorUpdate?.Invoke(default, 0, false);
}
public void SetPenColor(SKColor color)
{
PenColor = color;
}
public void SetPenSize(int size)
{
PenSize = size;
}
public void SetPhotoMode(double photoWidth, double photoHeight)
{
_photoWidth = photoWidth;
_photoHeight = photoHeight;
_isPhotoMode = true;
IsHitTestVisible = IsPenMode || IsEraserMode;
}
public void ExitPhotoMode()
{
_isPhotoMode = false;
_photoWidth = 0;
_photoHeight = 0;
}
public void SetWhiteboardMode()
{
_isWhiteboardMode = true;
}
public void ExitWhiteboardMode()
{
_isWhiteboardMode = false;
_whiteboardBackgroundPath = null;
_whiteboardBackgroundBitmap?.Dispose();
_whiteboardBackgroundBitmap = null;
}
public void SetWhiteboardBackground(string? imagePath)
{
_whiteboardBackgroundPath = imagePath;
_whiteboardBackgroundBitmap?.Dispose();
_whiteboardBackgroundBitmap = null;
if (!string.IsNullOrEmpty(imagePath) && System.IO.File.Exists(imagePath))
{
try
{
using var stream = System.IO.File.OpenRead(imagePath);
_whiteboardBackgroundBitmap = SKBitmap.Decode(stream);
}
catch
{
_whiteboardBackgroundBitmap = null;
}
}
InvalidateVisual();
}
public List<InkStroke> GetStrokes()
{
return new List<InkStroke>(_strokes);
}
public void SetStrokes(List<InkStroke> strokes)
{
_strokes.Clear();
_strokes.AddRange(strokes);
InvalidateVisual();
}
public void ClearStrokes()
{
_strokes.Clear();
InvalidateVisual();
}
private SKPoint ScreenToVideo(Point screenPoint)
{
if (_isWhiteboardMode)
{
return new SKPoint((float)screenPoint.X, (float)screenPoint.Y);
}
// screenPoint 是相对于 InkCanvasOverlay 的坐标
// 需要先减去 ZoomBorder 相对于 InkCanvasOverlay 的位置偏移,
// 再减去 PAZ 的 Pan 偏移,最后除以 Zoom
var videoX = (float)((screenPoint.X - _zoomBorderOffset.X - _currentPan.X) / _currentZoom);
var videoY = (float)((screenPoint.Y - _zoomBorderOffset.Y - _currentPan.Y) / _currentZoom);
return new SKPoint(videoX, videoY);
}
private SKPoint VideoToScreen(SKPoint videoPoint)
{
if (_isWhiteboardMode)
{
return videoPoint;
}
var screenX = videoPoint.X * (float)_currentZoom + (float)(_currentPan.X + _zoomBorderOffset.X);
var screenY = videoPoint.Y * (float)_currentZoom + (float)(_currentPan.Y + _zoomBorderOffset.Y);
return new SKPoint(screenX, screenY);
}
private static SKPoint ToSkPoint(Point point)
{
return new SKPoint((float)point.X, (float)point.Y);
}
private float GetRenderScaling()
{
return Math.Max(1.0f, (float)(TopLevel.GetTopLevel(this)?.RenderScaling ?? 1.0));
}
private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (!IsPenMode && !IsEraserMode) return;
var point = e.GetPosition(this);
var pointerPoint = e.GetCurrentPoint(this);
if (IsPenMode && pointerPoint.Properties.IsRightButtonPressed)
{
return;
}
if (TryHandlePalmEraser(e, point, isMoveEvent: false))
return;
_isDrawing = true;
e.Pointer.Capture(this);
var videoPoint = ScreenToVideo(point);
var screenPoint = ToSkPoint(point);
_currentVideoPoints = new List<SKPoint> { videoPoint };
_currentIsEraser = IsEraserMode;
_currentSize = IsEraserMode ? EraserSize : PenSize;
_currentColor = PenColor;
float zoomFactor = (_isWhiteboardMode) ? 1.0f : (float)_currentZoom;
if (_currentIsEraser)
{
_lastEraserPoint = videoPoint;
_hasLastEraserPoint = true;
_tempStrokes = CloneStrokes(_strokes);
float eraserWidthScreen = _currentSize * 1.6f;
float eraserHeightScreen = _currentSize * 2.0f;
var eraserRectVideo = GetEraserRectInVideo(videoPoint, eraserWidthScreen, eraserHeightScreen);
ApplyEraserToPoint(videoPoint, eraserRectVideo);
}
else
{
_currentRatio = 0.5f;
_currentScreenPoints = new List<SKPoint> { screenPoint };
float widthScale = BasePenWidthScale / GetRenderScaling();
_currentPointWidths = new List<float> { _currentSize * _currentRatio * widthScale };
_currentZoomFactors = new List<float> { zoomFactor };
_currentTimestamps = new List<long> { DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() };
}
}
private void OnPointerMoved(object? sender, PointerEventArgs e)
{
if (!IsPenMode && !IsEraserMode) return;
var currentPoint = e.GetPosition(this);
var videoPoint = ScreenToVideo(currentPoint);
if (IsEraserMode && !_isPalmEraserActive)
{
EraserCursorUpdate?.Invoke(currentPoint, EraserSize, true);
}
if (TryHandlePalmEraser(e, currentPoint, isMoveEvent: true))
return;
if (!_isDrawing || _currentVideoPoints == null) return;
var lastVideoPoint = _currentVideoPoints[^1];
_currentVideoPoints.Add(videoPoint);
float zoomFactor = (_isWhiteboardMode) ? 1.0f : (float)_currentZoom;
if (_currentIsEraser)
{
if (_hasLastEraserPoint)
{
float eraserWidthScreen = _currentSize * 1.6f;
float eraserHeightScreen = _currentSize * 2.0f;
var eraserRectVideo = GetEraserRectInVideo(videoPoint, eraserWidthScreen, eraserHeightScreen);
ApplyEraserToSegment(_lastEraserPoint, videoPoint, eraserRectVideo);
}
_lastEraserPoint = videoPoint;
_hasLastEraserPoint = true;
}
else
{
var currentTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
_currentTimestamps!.Add(currentTime);
var currentScreenPoint = ToSkPoint(currentPoint);
var lastScreenPoint = _currentScreenPoints![^1];
_currentScreenPoints.Add(currentScreenPoint);
float screenDistance = CalculateDistance(lastScreenPoint, currentScreenPoint);
UpdateRatioBySpeed(screenDistance);
float widthScale = BasePenWidthScale / GetRenderScaling();
_currentPointWidths!.Add(_currentSize * _currentRatio * widthScale);
_currentZoomFactors!.Add(zoomFactor);
}
InvalidateVisual();
}
private float CalculateDistance(SKPoint from, SKPoint to)
{
float dx = to.X - from.X;
float dy = to.Y - from.Y;
return (float)Math.Sqrt(dx * dx + dy * dy);
}
private void UpdateRatioBySpeed(float screenDistance)
{
var settings = PenSettings;
if (screenDistance > settings.SpeedThresholdFast)
{
if (_currentRatio > settings.RatioMin)
_currentRatio *= settings.RatioChangeCoefficient;
}
else if (screenDistance < settings.SpeedThresholdSlow)
{
if (_currentRatio < settings.RatioMax)
_currentRatio *= (1 + (1 - settings.RatioChangeCoefficient));
}
else
{
if (_currentRatio > 1f)
_currentRatio *= settings.RatioChangeCoefficient;
else
_currentRatio *= (1 + (1 - settings.RatioChangeCoefficient) / 2);
}
_currentRatio = Math.Clamp(_currentRatio, settings.RatioMin, settings.RatioMax);
}
private void OnPointerReleased(object? sender, PointerReleasedEventArgs e)
{
if (_isPalmEraserActive)
{
if (_tempStrokes != null)
{
_strokes.Clear();
_strokes.AddRange(_tempStrokes);
}
DeactivatePalmEraser();
}
ResetPalmDetectionState();
if (!_isDrawing) return;
if (_currentVideoPoints != null && _currentVideoPoints.Count > 1)
{
if (_currentIsEraser && _tempStrokes != null)
{
_strokes.Clear();
_strokes.AddRange(_tempStrokes);
}
else
{
CommitCurrentStroke();
}
}
ResetCurrentInteraction();
InvalidateVisual();
e.Pointer.Capture(null);
if (IsEraserMode && EraserCursorUpdate != null)
{
var pos = e.GetPosition(this);
EraserCursorUpdate(pos, EraserSize, true);
}
}
private void ApplyInkStyle(List<float> widths, float baseWidth)
{
if (widths.Count < 2) return;
int n = widths.Count - 1;
float minPressure = 0.2f;
int taperLength = Math.Min(widths.Count / 5, 12);
if (n >= taperLength * 2)
{
for (int i = 0; i < taperLength; i++)
{
float factor = (float)i / taperLength;
float pressure = minPressure + (1.0f - minPressure) * factor;
float targetWidth = baseWidth * pressure;
widths[i] = widths[i] * factor + targetWidth * (1 - factor);
}
for (int i = 0; i < taperLength; i++)
{
float factor = (float)i / taperLength;
float pressure = minPressure + (1.0f - minPressure) * factor;
float targetWidth = baseWidth * pressure;
int idx = n - i;
widths[idx] = widths[idx] * factor + targetWidth * (1 - factor);
}
}
else
{
for (int i = 0; i <= n; i++)
{
float startFactor = (float)i / n;
float endFactor = (float)(n - i) / n;
float positionFactor = Math.Min(startFactor, endFactor) * 2.0f;
float pressure = minPressure + (1.0f - minPressure) * Math.Min(positionFactor, 1.0f);
float targetWidth = baseWidth * pressure;
float blendFactor = 1.0f - Math.Min(positionFactor, 1.0f);
widths[i] = widths[i] * (1 - blendFactor) + targetWidth * blendFactor;
}
}
}
private void OnPointerCaptureLost(object? sender, PointerCaptureLostEventArgs e)
{
if (_isDrawing && _currentVideoPoints != null && _currentVideoPoints.Count > 1)
{
if (_currentIsEraser && _tempStrokes != null)
{
_strokes.Clear();
_strokes.AddRange(_tempStrokes);
}
else
{
CommitCurrentStroke();
}
}
ResetCurrentInteraction();
InvalidateVisual();
}
private void CommitCurrentStroke()
{
if (_currentVideoPoints == null ||
_currentPointWidths == null ||
_currentZoomFactors == null ||
_currentVideoPoints.Count <= 1 ||
_currentPointWidths.Count != _currentVideoPoints.Count)
{
return;
}
ApplyInkStyle(_currentPointWidths, _currentSize);
var videoWidths = ConvertScreenWidthsToVideo(_currentPointWidths, _currentZoomFactors);
float lastZoomFactor = _currentZoomFactors.Count > 0 ? _currentZoomFactors[^1] : 1.0f;
float widthScale = BasePenWidthScale / GetRenderScaling();
var stroke = new InkStroke
{
VideoPoints = new List<SKPoint>(_currentVideoPoints),
PointWidths = videoWidths,
IsEraser = false,
Size = (_currentSize * widthScale) / Math.Max(lastZoomFactor, 0.001f),
Color = _currentColor
};
_strokes.Add(stroke);
}
private List<float> ConvertScreenWidthsToVideo(List<float> screenWidths, List<float> zoomFactors)
{
var videoWidths = new List<float>(screenWidths.Count);
for (int i = 0; i < screenWidths.Count; i++)
{
float zoomFactor = i < zoomFactors.Count ? zoomFactors[i] : 1.0f;
videoWidths.Add(screenWidths[i] / Math.Max(zoomFactor, 0.001f));
}
return videoWidths;
}
private void ResetCurrentInteraction()
{
_currentVideoPoints = null;
_currentScreenPoints = null;
_currentPointWidths = null;
_currentZoomFactors = null;
_currentTimestamps = null;
_tempStrokes = null;
_hasLastEraserPoint = false;
_isDrawing = false;
}
private List<InkStroke> CloneStrokes(List<InkStroke> source)
{
var result = new List<InkStroke>();
foreach (var stroke in source)
{
result.Add(new InkStroke
{
VideoPoints = new List<SKPoint>(stroke.VideoPoints ?? new List<SKPoint>()),
PointWidths = stroke.PointWidths != null ? new List<float>(stroke.PointWidths) : null,
IsEraser = stroke.IsEraser,
Size = stroke.Size,
Color = stroke.Color
});
}
return result;
}
private SKRect GetEraserRectInVideo(SKPoint videoCenter, float eraserWidthScreen, float eraserHeightScreen)
{
float zoom = (_isWhiteboardMode) ? 1.0f : (float)_currentZoom;
float halfWidthVideo = (eraserWidthScreen / 2) / zoom;
float halfHeightVideo = (eraserHeightScreen / 2) / zoom;
return new SKRect(
videoCenter.X - halfWidthVideo,
videoCenter.Y - halfHeightVideo,
videoCenter.X + halfWidthVideo,
videoCenter.Y + halfHeightVideo);
}
private void ApplyEraserToPoint(SKPoint eraserCenter, SKRect eraserRectVideo)
{
if (_tempStrokes == null) return;
var newStrokes = new List<InkStroke>();
foreach (var stroke in _tempStrokes)
{
if (stroke.VideoPoints == null || stroke.VideoPoints.Count < 2)
{
newStrokes.Add(stroke);
continue;
}
var segments = EraseStrokeWithRectangle(stroke, eraserRectVideo);
newStrokes.AddRange(segments);
}
_tempStrokes = newStrokes;
}
private void ApplyEraserToSegment(SKPoint from, SKPoint to, SKRect eraserRectVideo)
{
if (_tempStrokes == null) return;
var newStrokes = new List<InkStroke>();
float step = 1.0f;
float dx = to.X - from.X;
float dy = to.Y - from.Y;
float length = (float)Math.Sqrt(dx * dx + dy * dy);
if (length < step)
{
ApplyEraserToPoint(to, eraserRectVideo);
return;
}
int steps = (int)Math.Ceiling(length / step);
for (int i = 0; i <= steps; i++)
{
float t = i / (float)steps;
SKPoint point = new SKPoint(
from.X + dx * t,
from.Y + dy * t
);
ApplyEraserToPoint(point, eraserRectVideo);
}
}
private List<InkStroke> EraseStrokeWithRectangle(InkStroke stroke, SKRect rectVideo)
{
var result = new List<InkStroke>();
if (stroke.VideoPoints == null)
{
return result;
}
var videoPoints = stroke.VideoPoints;
var currentSegment = new List<SKPoint>();
var currentWidths = new List<float>();
var hasWidths = stroke.PointWidths != null && stroke.PointWidths.Count == videoPoints.Count;
for (int i = 0; i < videoPoints.Count; i++)
{
var point = videoPoints[i];
var width = hasWidths ? stroke.PointWidths![i] : stroke.Size;
float radius = width / 2;
// 判断圆是否与矩形相交
bool intersects = CircleIntersectsRect(point, radius, rectVideo);
if (!intersects)
{
currentSegment.Add(point);
currentWidths.Add(width);
}
else
{
// 擦除该点,结束当前段
if (currentSegment.Count >= 2)
{
result.Add(CreateStrokeSegment(currentSegment, currentWidths, stroke));
}
currentSegment.Clear();
currentWidths.Clear();
}
}
// 添加最后一段
if (currentSegment.Count >= 2)
{
result.Add(CreateStrokeSegment(currentSegment, currentWidths, stroke));
}
return result;
}
private bool CircleIntersectsRect(SKPoint center, float radius, SKRect rect)
{
float closestX = Math.Max(rect.Left, Math.Min(center.X, rect.Right));
float closestY = Math.Max(rect.Top, Math.Min(center.Y, rect.Bottom));
float dx = center.X - closestX;
float dy = center.Y - closestY;
return (dx * dx + dy * dy) < radius * radius;
}
private InkStroke CreateStrokeSegment(List<SKPoint> points, List<float> widths, InkStroke source)
{
return new InkStroke
{
VideoPoints = new List<SKPoint>(points),
PointWidths = new List<float>(widths),
IsEraser = source.IsEraser,
Size = source.Size,
Color = source.Color
};
}
private SKPaint CreatePenPaint(SKColor color, float size)
{
return new SKPaint
{
Style = SKPaintStyle.Stroke,
StrokeCap = SKStrokeCap.Round,
StrokeJoin = SKStrokeJoin.Round,
IsAntialias = true,
Color = color,
StrokeWidth = size
};
}
private SKPaint CreateEraserPaint(float size)
{
return new SKPaint
{
Style = SKPaintStyle.Stroke,
StrokeCap = SKStrokeCap.Round,
StrokeJoin = SKStrokeJoin.Round,
IsAntialias = true,
BlendMode = SKBlendMode.Clear,
StrokeWidth = size
};
}
public override void Render(DrawingContext context)
{
base.Render(context);
var displayWidth = (int)Bounds.Width;
var displayHeight = (int)Bounds.Height;
var renderScaling = GetRenderScaling();
var pixelWidth = Math.Max(1, (int)Math.Ceiling(displayWidth * renderScaling));
var pixelHeight = Math.Max(1, (int)Math.Ceiling(displayHeight * renderScaling));
if (displayWidth <= 0 || displayHeight <= 0) return;
if (_displayBitmap == null || _displayBitmap.PixelSize.Width != pixelWidth || _displayBitmap.PixelSize.Height != pixelHeight)
{
_displayBitmap?.Dispose();
_displayBitmap = new WriteableBitmap(
new PixelSize(pixelWidth, pixelHeight),
new Vector(96 * renderScaling, 96 * renderScaling),
PixelFormat.Bgra8888,
AlphaFormat.Premul);
}
using (var fb = _displayBitmap.Lock())
{
var info = new SKImageInfo(fb.Size.Width, fb.Size.Height, SKColorType.Bgra8888, SKAlphaType.Premul);
using var surface = SKSurface.Create(info, fb.Address, fb.RowBytes);
var canvas = surface.Canvas;
canvas.Clear(SKColors.Transparent);
canvas.Save();
canvas.Scale(renderScaling, renderScaling);
if (_isWhiteboardMode && _whiteboardBackgroundBitmap != null)
{
var bounds = Bounds;
var imgWidth = _whiteboardBackgroundBitmap.Width;
var imgHeight = _whiteboardBackgroundBitmap.Height;
var scaleX = (float)bounds.Width / imgWidth;
var scaleY = (float)bounds.Height / imgHeight;
var scale = Math.Min(scaleX, scaleY);
var destWidth = imgWidth * scale;
var destHeight = imgHeight * scale;
var destX = ((float)bounds.Width - destWidth) / 2;
var destY = ((float)bounds.Height - destHeight) / 2;
var destRect = new SKRect(destX, destY, destX + destWidth, destY + destHeight);
canvas.DrawBitmap(_whiteboardBackgroundBitmap, destRect);
}
if (_isDrawing && _currentIsEraser && _tempStrokes != null)
{
RenderStrokes(canvas, _tempStrokes);
}
else
{
RenderStrokes(canvas, _strokes);
}
if (_isDrawing && !_currentIsEraser)
{
RenderCurrentWetStroke(canvas);
}
canvas.Restore();
}
context.DrawImage(_displayBitmap, new Rect(_displayBitmap.Size), Bounds);
}
private void RenderStrokes(SKCanvas canvas, List<InkStroke> strokes)
{
float zoom = (_isWhiteboardMode) ? 1.0f : (float)_currentZoom;
float renderScaling = GetRenderScaling();
foreach (var stroke in strokes)
{
RenderStroke(canvas, stroke, zoom, renderScaling);
}
}
private void RenderStroke(SKCanvas canvas, InkStroke stroke, float zoom, float renderScaling)
{
if (stroke.VideoPoints == null || stroke.VideoPoints.Count < 2) return;
if (stroke.PointWidths != null && stroke.PointWidths.Count == stroke.VideoPoints.Count)
{
RenderVariableWidthStroke(canvas, stroke, zoom, renderScaling);
}
else
{
var displaySize = stroke.Size * zoom;
var paint = CreatePenPaint(stroke.Color, displaySize);
for (int i = 1; i < stroke.VideoPoints.Count; i++)
{
var screenFrom = VideoToScreen(stroke.VideoPoints[i - 1]);
var screenTo = VideoToScreen(stroke.VideoPoints[i]);
canvas.DrawLine(
new SKPoint(screenFrom.X / renderScaling, screenFrom.Y / renderScaling),
new SKPoint(screenTo.X / renderScaling, screenTo.Y / renderScaling),
paint);
}
}
}
private void RenderVariableWidthStroke(SKCanvas canvas, InkStroke stroke, float zoom, float renderScaling)
{
if (stroke.VideoPoints == null || stroke.PointWidths == null) return;
using var paint = new SKPaint
{
Style = SKPaintStyle.Fill,
IsAntialias = true,
Color = stroke.Color
};
for (int i = 0; i < stroke.VideoPoints.Count - 1; i++)
{
var p1 = VideoToScreen(stroke.VideoPoints[i]);
var p2 = VideoToScreen(stroke.VideoPoints[i + 1]);
var w1 = stroke.PointWidths[i] * zoom;
var w2 = stroke.PointWidths[i + 1] * zoom;
RenderSmoothSegment(canvas,
new SKPoint(p1.X / renderScaling, p1.Y / renderScaling),
new SKPoint(p2.X / renderScaling, p2.Y / renderScaling),
w1, w2, paint);
}
}
private void RenderCurrentWetStroke(SKCanvas canvas)
{
if (_currentScreenPoints == null ||
_currentPointWidths == null ||
_currentScreenPoints.Count < 2 ||
_currentPointWidths.Count != _currentScreenPoints.Count)
{
return;
}
float renderScaling = GetRenderScaling();
using var paint = new SKPaint
{
Style = SKPaintStyle.Fill,
IsAntialias = true,
Color = _currentColor
};
for (int i = 0; i < _currentScreenPoints.Count - 1; i++)
{
var p1 = _currentScreenPoints[i];
var p2 = _currentScreenPoints[i + 1];
RenderSmoothSegment(
canvas,
new SKPoint(p1.X / renderScaling, p1.Y / renderScaling),
new SKPoint(p2.X / renderScaling, p2.Y / renderScaling),
_currentPointWidths[i],
_currentPointWidths[i + 1],
paint);
}
}
private void RenderSmoothSegment(SKCanvas canvas, SKPoint p1, SKPoint p2, float w1, float w2, SKPaint paint)
{
float dx = p2.X - p1.X;
float dy = p2.Y - p1.Y;
float length = (float)Math.Sqrt(dx * dx + dy * dy);
if (length < 0.5f)
{
canvas.DrawCircle(p1, w1 / 2, paint);
return;
}
int subdivisions = Math.Max(2, (int)(length / 1f));
for (int i = 0; i <= subdivisions; i++)
{
float t = i / (float)subdivisions;
float x = p1.X + dx * t;
float y = p1.Y + dy * t;
float w = w1 + (w2 - w1) * t;
canvas.DrawCircle(new SKPoint(x, y), w / 2, paint);
}
}
protected override Size MeasureOverride(Size availableSize)
{
return availableSize;
}
public void ClearAll()
{
_strokes.Clear();
ResetCurrentInteraction();
InvalidateVisual();
}
public void Undo()
{
if (_strokes.Count == 0) return;
_strokes.RemoveAt(_strokes.Count - 1);
InvalidateVisual();
}
public int StrokeCount => _strokes.Count;
private double CalculateTouchArea(PointerEventArgs e)
{
try
{
var pointerPoint = e.GetCurrentPoint(this);
var bounds = pointerPoint.Properties.ContactRect;
var width = Math.Abs(bounds.Width);
var height = Math.Abs(bounds.Height);
if (width <= 0 || height <= 0)
{
return 0;
}
// 电容屏用面积,红外屏更适合用等效边长(sqrt(area))避免误放大
double metric = PenSettings.IsInfraredScreen
? Math.Sqrt(width * height)
: width * height;
var multiplier = PenSettings.PalmTouchMultiplier;
if (multiplier > 0)
{
metric *= multiplier;
}
return metric;
}
catch
{
return 0;
}
}
private bool TryHandlePalmEraser(PointerEventArgs e, Point screenPoint, bool isMoveEvent)
{
if (!EnablePalmEraser || !IsPenMode)
return false;
// 状态锁:激活后允许短时抖动,防止在手掌擦和书写之间来回抖动。
var touchMetric = CalculateTouchArea(e);
_currentTouchArea = touchMetric;