-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDrawingAndErasingLogic.cs.BACKUP
More file actions
1962 lines (1637 loc) · 72.7 KB
/
DrawingAndErasingLogic.cs.BACKUP
File metadata and controls
1962 lines (1637 loc) · 72.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.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
using Point = System.Windows.Point;
namespace Ink_Canvas
{
public partial class MainWindow : Window
{
#region 橡皮擦系统核心变量
public bool isUsingGeometryEraser = false;
private IncrementalStrokeHitTester hitTester = null;
public double eraserWidth = 64;
public bool isEraserCircleShape = false;
public bool isUsingStrokesEraser = false;
private Matrix scaleMatrix = new Matrix();
private System.Windows.Controls.Canvas eraserOverlayCanvas;
private Image eraserFeedback;
private TranslateTransform eraserFeedbackTranslateTransform;
private static readonly Guid IsLockGuid = new Guid("12345678-1234-1234-1234-123456789ABC");
#endregion
#region 绘制系统核心变量
private int drawingShapeMode;
private bool isLongPressSelected;
private bool isMouseDown;
private bool isTouchDown;
private Point iniP = new Point(0, 0);
private bool isFirstTouchCuboid = true;
private Point CuboidFrontRectIniP;
private Point CuboidFrontRectEndP;
private Stroke lastTempStroke;
private StrokeCollection lastTempStrokeCollection = new StrokeCollection();
private DateTime lastUpdateTime = DateTime.MinValue;
private const int UpdateThrottleMs = 16;
private StrokeCollection newStrokes = new StrokeCollection();
private List<Circle> circles = new List<Circle>();
private const double LINE_STRAIGHTEN_THRESHOLD = 0.20;
#endregion
#region 多点触控系统变量
private bool isInMultiTouchMode;
private List<int> dec = new List<int>();
private bool isSingleFingerDragMode;
private Point centerPoint = new Point(0, 0);
private InkCanvasEditingMode lastInkCanvasEditingMode = InkCanvasEditingMode.Ink;
private DateTime lastTouchDownTime = DateTime.MinValue;
private const double MULTI_TOUCH_DELAY_MS = 100;
private Dictionary<int, InkCanvasEditingMode> TouchDownPointsList { get; } =
new Dictionary<int, InkCanvasEditingMode>();
private Dictionary<int, StrokeVisual> StrokeVisualList { get; } = new Dictionary<int, StrokeVisual>();
private Dictionary<int, VisualCanvas> VisualCanvasList { get; } = new Dictionary<int, VisualCanvas>();
#endregion
#region 橡皮擦系统实现
private void EraserOverlayCanvas_Loaded(object sender, RoutedEventArgs e)
{
var canvas = (System.Windows.Controls.Canvas)sender;
eraserOverlayCanvas = canvas;
eraserFeedback = FindName("EraserFeedback") as Image;
if (eraserFeedback != null)
{
eraserFeedbackTranslateTransform = eraserFeedback.RenderTransform as TranslateTransform;
}
canvas.StylusDown += ((o, args) =>
{
e.Handled = true;
if (args.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus) canvas.CaptureStylus();
EraserOverlay_PointerDown(sender);
});
canvas.StylusUp += ((o, args) =>
{
e.Handled = true;
if (args.StylusDevice.TabletDevice.Type == TabletDeviceType.Stylus) canvas.ReleaseStylusCapture();
EraserOverlay_PointerUp(sender);
});
canvas.StylusMove += ((o, args) =>
{
e.Handled = true;
EraserOverlay_PointerMove(sender, args.GetPosition(inkCanvas));
});
canvas.MouseDown += ((o, args) =>
{
canvas.CaptureMouse();
EraserOverlay_PointerDown(sender);
});
canvas.MouseUp += ((o, args) =>
{
canvas.ReleaseMouseCapture();
EraserOverlay_PointerUp(sender);
});
canvas.MouseMove += ((o, args) =>
{
EraserOverlay_PointerMove(sender, args.GetPosition(inkCanvas));
});
UpdateEraserStyle();
}
private void UpdateEraserStyle()
{
if (eraserFeedback == null) return;
string resourceKey = isEraserCircleShape ? "EllipseEraserImageSource" : "RectangleEraserImageSource";
var imageSource = TryFindResource(resourceKey) as DrawingImage;
if (imageSource != null)
{
eraserFeedback.Source = imageSource;
}
}
private void EraserOverlay_PointerDown(object sender)
{
if (isUsingGeometryEraser) return;
isUsingGeometryEraser = true;
var _h = eraserWidth * 56 / 38;
StylusShape eraserShape;
if (isEraserCircleShape)
{
eraserShape = new EllipseStylusShape(eraserWidth, eraserWidth);
}
else
{
eraserShape = new RectangleStylusShape(eraserWidth, _h);
}
hitTester = inkCanvas.Strokes.GetIncrementalStrokeHitTester(eraserShape);
hitTester.StrokeHit += EraserGeometry_StrokeHit;
var scaleX = eraserWidth / 38;
var scaleY = _h / 56;
scaleMatrix = new Matrix();
scaleMatrix.ScaleAt(scaleX, scaleY, 0, 0);
if (eraserFeedback != null)
{
eraserFeedback.Width = Math.Max(eraserWidth, 10);
eraserFeedback.Height = isEraserCircleShape ? eraserFeedback.Width : _h;
eraserFeedback.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
eraserFeedback.Visibility = Visibility.Collapsed;
}
}
private void EraserOverlay_PointerUp(object sender)
{
if (!isUsingGeometryEraser) return;
isUsingGeometryEraser = false;
((UIElement)sender).ReleaseMouseCapture();
if (eraserFeedback != null)
{
eraserFeedback.Visibility = Visibility.Collapsed;
}
if (hitTester != null)
{
hitTester.EndHitTesting();
hitTester = null;
}
if (ReplacedStroke != null || AddedStroke != null)
{
timeMachine.CommitStrokeEraseHistory(ReplacedStroke, AddedStroke);
AddedStroke = null;
ReplacedStroke = null;
}
}
private void EraserOverlay_PointerMove(object sender, Point pt)
{
if (!isUsingGeometryEraser) return;
if (isUsingStrokesEraser)
{
var _filtered = inkCanvas.Strokes.HitTest(pt).Where(stroke => !stroke.ContainsPropertyData(IsLockGuid));
var filtered = _filtered as Stroke[] ?? _filtered.ToArray();
if (!filtered.Any()) return;
inkCanvas.Strokes.Remove(new StrokeCollection(filtered));
}
else
{
if (eraserFeedback != null && eraserFeedback.Visibility == Visibility.Collapsed)
{
eraserFeedback.Visibility = Visibility.Visible;
}
if (eraserFeedbackTranslateTransform != null)
{
eraserFeedbackTranslateTransform.X = pt.X - eraserFeedback.ActualWidth / 2;
eraserFeedbackTranslateTransform.Y = pt.Y - eraserFeedback.ActualHeight / 2;
}
if (hitTester != null)
{
hitTester.AddPoint(pt);
}
}
}
private void EraserGeometry_StrokeHit(object sender, StrokeHitEventArgs args)
{
StrokeCollection eraseResult = args.GetPointEraseResults();
StrokeCollection strokesToReplace = new StrokeCollection { args.HitStroke };
var filtered_2replace = strokesToReplace.Where(stroke => !stroke.ContainsPropertyData(IsLockGuid));
var filtered2Replace = filtered_2replace as Stroke[] ?? filtered_2replace.ToArray();
if (!filtered2Replace.Any()) return;
var filtered_result = eraseResult.Where(stroke => !stroke.ContainsPropertyData(IsLockGuid));
var filteredResult = filtered_result as Stroke[] ?? filtered_result.ToArray();
if (filteredResult.Any())
{
inkCanvas.Strokes.Replace(new StrokeCollection(filtered2Replace), new StrokeCollection(filteredResult));
}
else
{
inkCanvas.Strokes.Remove(new StrokeCollection(filtered2Replace));
}
}
public void EnableEraserOverlay()
{
if (eraserOverlayCanvas != null)
{
eraserOverlayCanvas.IsHitTestVisible = true;
eraserOverlayCanvas.Visibility = Visibility.Visible;
}
}
public void DisableEraserOverlay()
{
if (eraserOverlayCanvas != null)
{
eraserOverlayCanvas.IsHitTestVisible = false;
eraserOverlayCanvas.Visibility = Visibility.Collapsed;
}
if (isUsingGeometryEraser)
{
isUsingGeometryEraser = false;
if (hitTester != null)
{
hitTester.EndHitTesting();
hitTester = null;
}
}
if (eraserFeedback != null)
{
eraserFeedback.Visibility = Visibility.Collapsed;
}
}
public void UpdateEraserSize()
{
double k = 1.0;
switch (Settings.Canvas.EraserSize)
{
case 0: k = Settings.Canvas.EraserShapeType == 0 ? 0.5 : 0.7; break;
case 1: k = Settings.Canvas.EraserShapeType == 0 ? 0.8 : 0.9; break;
case 2: k = 1.0; break;
case 3: k = Settings.Canvas.EraserShapeType == 0 ? 1.25 : 1.2; break;
case 4: k = Settings.Canvas.EraserShapeType == 0 ? 1.5 : 1.3; break;
}
isEraserCircleShape = (Settings.Canvas.EraserShapeType == 0);
if (isEraserCircleShape)
{
eraserWidth = k * 90;
}
else
{
eraserWidth = k * 90 * 0.6;
}
UpdateEraserStyle();
}
public void ToggleEraserShape()
{
isEraserCircleShape = !isEraserCircleShape;
Settings.Canvas.EraserShapeType = isEraserCircleShape ? 0 : 1;
UpdateEraserStyle();
}
public void ToggleEraserMode()
{
isUsingStrokesEraser = !isUsingStrokesEraser;
}
public void ApplyAdvancedEraserShape()
{
try
{
UpdateEraserSize();
StylusShape eraserShape;
if (isEraserCircleShape)
{
eraserShape = new EllipseStylusShape(eraserWidth, eraserWidth);
}
else
{
var height = eraserWidth * 56 / 38;
eraserShape = new RectangleStylusShape(eraserWidth, height);
}
inkCanvas.EraserShape = eraserShape;
Trace.WriteLine($"Eraser: Applied shape - Size: {eraserWidth}, Circle: {isEraserCircleShape}");
}
catch (Exception ex)
{
Trace.WriteLine($"Eraser: Error applying shape - {ex.Message}");
}
}
internal void EraserIcon_Click(object sender, RoutedEventArgs e)
{
bool isAlreadyEraser = inkCanvas.EditingMode == InkCanvasEditingMode.EraseByPoint;
forceEraser = false;
forcePointEraser = true;
drawingShapeMode = 0;
if (!isAlreadyEraser && currentMode != 0)
{
SaveStrokes();
}
if (!isAlreadyEraser)
{
ResetTouchStates();
}
EnableEraserOverlay();
SetCurrentToolMode(InkCanvasEditingMode.EraseByPoint);
UpdateCurrentToolMode("eraser");
ApplyAdvancedEraserShape();
SetCursorBasedOnEditingMode(inkCanvas);
HideSubPanels("eraser");
Trace.WriteLine($"Eraser: Eraser button clicked, current size: {eraserWidth}, circle: {isEraserCircleShape}");
if (isAlreadyEraser)
{
if (EraserSizePanel.Visibility == Visibility.Collapsed)
{
AnimationsHelper.ShowWithSlideFromBottomAndFade(EraserSizePanel);
if (BoardEraserSizePanel != null)
AnimationsHelper.ShowWithSlideFromBottomAndFade(BoardEraserSizePanel);
}
else
{
AnimationsHelper.HideWithSlideAndFade(EraserSizePanel);
if (BoardEraserSizePanel != null)
AnimationsHelper.HideWithSlideAndFade(BoardEraserSizePanel);
}
}
}
private void EraserIconByStrokes_Click(object sender, RoutedEventArgs e)
{
if (lastBorderMouseDownObject is Panel panel)
panel.Background = new SolidColorBrush(Colors.Transparent);
if (sender == EraserByStrokes_Icon && lastBorderMouseDownObject != EraserByStrokes_Icon) return;
DisableEraserOverlay();
forceEraser = true;
forcePointEraser = false;
inkCanvas.EraserShape = new EllipseStylusShape(5, 5);
SetCurrentToolMode(InkCanvasEditingMode.EraseByStroke);
UpdateCurrentToolMode("eraserByStrokes");
drawingShapeMode = 0;
inkCanvas_EditingModeChanged(inkCanvas, null);
CancelSingleFingerDragMode();
HideSubPanels("eraserByStrokes");
}
#endregion
#region 绘制系统实现
private void inkCanvas_MouseDown(object sender, MouseButtonEventArgs e)
{
inkCanvas.CaptureMouse();
ViewboxFloatingBar.IsHitTestVisible = false;
BlackboardUIGridForInkReplay.IsHitTestVisible = false;
isMouseDown = true;
if (NeedUpdateIniP()) iniP = e.GetPosition(inkCanvas);
}
private void inkCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown) MouseTouchMove(e.GetPosition(inkCanvas));
if (Settings.Canvas.IsShowCursor)
{
SetCursorBasedOnEditingMode(inkCanvas);
}
}
private void inkCanvas_MouseUp(object sender, MouseButtonEventArgs e)
{
inkCanvas.ReleaseMouseCapture();
ViewboxFloatingBar.IsHitTestVisible = true;
BlackboardUIGridForInkReplay.IsHitTestVisible = true;
if (drawingShapeMode == 5)
{
if (lastTempStroke != null)
{
var circle = new Circle(new Point(), 0, lastTempStroke);
circle.R = GetDistance(circle.Stroke.StylusPoints[0].ToPoint(),
circle.Stroke.StylusPoints[circle.Stroke.StylusPoints.Count / 2].ToPoint()) / 2;
circle.Centroid = new Point(
(circle.Stroke.StylusPoints[0].X +
circle.Stroke.StylusPoints[circle.Stroke.StylusPoints.Count / 2].X) / 2,
(circle.Stroke.StylusPoints[0].Y +
circle.Stroke.StylusPoints[circle.Stroke.StylusPoints.Count / 2].Y) / 2);
circles.Add(circle);
}
if (lastIsInMultiTouchMode)
{
ToggleSwitchEnableMultiTouchMode.IsOn = true;
lastIsInMultiTouchMode = false;
}
}
if (drawingShapeMode != 9 && drawingShapeMode != 0 && drawingShapeMode != 24 && drawingShapeMode != 25)
{
if (isLongPressSelected) { }
else
{
BtnPen_Click(null, null);
if (lastIsInMultiTouchMode)
{
ToggleSwitchEnableMultiTouchMode.IsOn = true;
lastIsInMultiTouchMode = false;
}
}
}
if (drawingShapeMode == 9)
{
if (isFirstTouchCuboid)
{
if (CuboidStrokeCollection == null) CuboidStrokeCollection = new StrokeCollection();
isFirstTouchCuboid = false;
var newIniP = new Point(Math.Min(CuboidFrontRectIniP.X, CuboidFrontRectEndP.X),
Math.Min(CuboidFrontRectIniP.Y, CuboidFrontRectEndP.Y));
var newEndP = new Point(Math.Max(CuboidFrontRectIniP.X, CuboidFrontRectEndP.X),
Math.Max(CuboidFrontRectIniP.Y, CuboidFrontRectEndP.Y));
CuboidFrontRectIniP = newIniP;
CuboidFrontRectEndP = newEndP;
try
{
CuboidStrokeCollection.Add(lastTempStrokeCollection);
}
catch
{
Trace.WriteLine("lastTempStrokeCollection failed.");
}
}
else
{
BtnPen_Click(null, null);
if (lastIsInMultiTouchMode)
{
ToggleSwitchEnableMultiTouchMode.IsOn = true;
lastIsInMultiTouchMode = false;
}
if (_currentCommitType == CommitReason.ShapeDrawing)
{
try
{
CuboidStrokeCollection.Add(lastTempStrokeCollection);
}
catch
{
Trace.WriteLine("lastTempStrokeCollection failed.");
}
_currentCommitType = CommitReason.UserInput;
timeMachine.CommitStrokeUserInputHistory(CuboidStrokeCollection);
CuboidStrokeCollection = null;
}
}
}
if (drawingShapeMode == 24 || drawingShapeMode == 25)
{
if (drawMultiStepShapeCurrentStep == 0)
{
drawMultiStepShapeCurrentStep = 1;
}
else
{
drawMultiStepShapeCurrentStep = 0;
if (drawMultiStepShapeSpecialStrokeCollection != null)
{
var opFlag = false;
switch (Settings.Canvas.HyperbolaAsymptoteOption)
{
case OptionalOperation.Yes:
opFlag = true;
break;
case OptionalOperation.No:
opFlag = false;
break;
case OptionalOperation.Ask:
opFlag = MessageBox.Show("是否移除渐近线?", "Ink Canvas", MessageBoxButton.YesNo) !=
MessageBoxResult.Yes;
break;
}
if (!opFlag) inkCanvas.Strokes.Remove(drawMultiStepShapeSpecialStrokeCollection);
}
BtnPen_Click(null, null);
if (lastIsInMultiTouchMode)
{
ToggleSwitchEnableMultiTouchMode.IsOn = true;
lastIsInMultiTouchMode = false;
}
}
}
isMouseDown = false;
if (ReplacedStroke != null || AddedStroke != null)
{
timeMachine.CommitStrokeEraseHistory(ReplacedStroke, AddedStroke);
AddedStroke = null;
ReplacedStroke = null;
}
if (_currentCommitType == CommitReason.ShapeDrawing && drawingShapeMode != 9)
{
_currentCommitType = CommitReason.UserInput;
StrokeCollection collection = null;
if (lastTempStrokeCollection != null && lastTempStrokeCollection.Count > 0)
collection = lastTempStrokeCollection;
else if (lastTempStroke != null) collection = new StrokeCollection { lastTempStroke };
if (collection != null) timeMachine.CommitStrokeUserInputHistory(collection);
}
lastTempStroke = null;
lastTempStrokeCollection = null;
if (StrokeManipulationHistory?.Count > 0)
{
timeMachine.CommitStrokeManipulationHistory(StrokeManipulationHistory);
foreach (var item in StrokeManipulationHistory)
{
StrokeInitialHistory[item.Key] = item.Value.Item2;
}
StrokeManipulationHistory = null;
}
if (DrawingAttributesHistory.Count > 0)
{
timeMachine.CommitStrokeDrawingAttributesHistory(DrawingAttributesHistory);
DrawingAttributesHistory = new Dictionary<Stroke, Tuple<DrawingAttributes, DrawingAttributes>>();
foreach (var item in DrawingAttributesHistoryFlag)
{
item.Value.Clear();
}
}
if (Settings.Canvas.FitToCurve == true) drawingAttributes.FitToCurve = true;
}
private bool NeedUpdateIniP()
{
if (drawingShapeMode == 24 || drawingShapeMode == 25)
{
if (drawMultiStepShapeCurrentStep == 1)
return false;
}
return true;
}
private void UpdateTempStrokeSafely(Stroke newStroke)
{
var now = DateTime.Now;
if ((now - lastUpdateTime).TotalMilliseconds < UpdateThrottleMs)
{
return;
}
lastUpdateTime = now;
try
{
Dispatcher.BeginInvoke(new Action(() =>
{
try
{
inkCanvas.Strokes.Add(newStroke);
if (lastTempStroke != null && inkCanvas.Strokes.Contains(lastTempStroke))
{
inkCanvas.Strokes.Remove(lastTempStroke);
}
lastTempStroke = newStroke;
}
catch (Exception ex)
{
Debug.WriteLine($"UpdateTempStrokeSafely 失败: {ex.Message}");
if (lastTempStroke != null && inkCanvas.Strokes.Contains(lastTempStroke))
{
try { inkCanvas.Strokes.Remove(lastTempStroke); } catch { }
}
lastTempStroke = newStroke;
try { inkCanvas.Strokes.Add(newStroke); } catch { }
}
}), DispatcherPriority.Render);
}
catch (Exception ex)
{
Debug.WriteLine($"UpdateTempStrokeSafely Dispatcher 失败: {ex.Message}");
}
}
private void UpdateTempStrokeCollectionSafely(StrokeCollection newStrokeCollection)
{
var now = DateTime.Now;
if ((now - lastUpdateTime).TotalMilliseconds < UpdateThrottleMs)
{
return;
}
lastUpdateTime = now;
try
{
Dispatcher.BeginInvoke(new Action(() =>
{
try
{
inkCanvas.Strokes.Add(newStrokeCollection);
if (lastTempStrokeCollection != null && lastTempStrokeCollection.Count > 0)
{
foreach (var stroke in lastTempStrokeCollection)
{
if (inkCanvas.Strokes.Contains(stroke))
{
inkCanvas.Strokes.Remove(stroke);
}
}
}
lastTempStrokeCollection = newStrokeCollection;
}
catch (Exception ex)
{
Debug.WriteLine($"UpdateTempStrokeCollectionSafely 失败: {ex.Message}");
if (lastTempStrokeCollection != null && lastTempStrokeCollection.Count > 0)
{
foreach (var stroke in lastTempStrokeCollection)
{
try { inkCanvas.Strokes.Remove(stroke); } catch { }
}
}
lastTempStrokeCollection = newStrokeCollection;
try { inkCanvas.Strokes.Add(newStrokeCollection); } catch { }
}
}), DispatcherPriority.Render);
}
catch (Exception ex)
{
Debug.WriteLine($"UpdateTempStrokeCollectionSafely Dispatcher 失败: {ex.Message}");
}
}
private List<Point> GenerateEllipseGeometry(Point st, Point ed, bool isDrawTop = true,
bool isDrawBottom = true)
{
var a = 0.5 * (ed.X - st.X);
var b = 0.5 * (ed.Y - st.Y);
var pointList = new List<Point>();
if (isDrawTop && isDrawBottom)
{
for (double r = 0; r <= 2 * Math.PI; r = r + 0.01)
pointList.Add(new Point(0.5 * (st.X + ed.X) + a * Math.Cos(r),
0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
}
else
{
if (isDrawBottom)
for (double r = 0; r <= Math.PI; r = r + 0.01)
pointList.Add(new Point(0.5 * (st.X + ed.X) + a * Math.Cos(r),
0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
if (isDrawTop)
for (var r = Math.PI; r <= 2 * Math.PI; r = r + 0.01)
pointList.Add(new Point(0.5 * (st.X + ed.X) + a * Math.Cos(r),
0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
}
return pointList;
}
private StrokeCollection GenerateDashedLineEllipseStrokeCollection(Point st, Point ed, bool isDrawTop = true,
bool isDrawBottom = true)
{
var a = 0.5 * (ed.X - st.X);
var b = 0.5 * (ed.Y - st.Y);
var step = 0.05;
var pointList = new List<Point>();
StylusPointCollection point;
Stroke stroke;
var strokes = new StrokeCollection();
if (isDrawBottom)
for (var i = 0.0; i < 1.0; i += step * 1.66)
{
pointList = new List<Point>();
for (var r = Math.PI * i; r <= Math.PI * (i + step); r = r + 0.01)
pointList.Add(new Point(0.5 * (st.X + ed.X) + a * Math.Cos(r),
0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
strokes.Add(stroke.Clone());
}
if (isDrawTop)
for (var i = 1.0; i < 2.0; i += step * 1.66)
{
pointList = new List<Point>();
for (var r = Math.PI * i; r <= Math.PI * (i + step); r = r + 0.01)
pointList.Add(new Point(0.5 * (st.X + ed.X) + a * Math.Cos(r),
0.5 * (st.Y + ed.Y) + b * Math.Sin(r)));
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
strokes.Add(stroke.Clone());
}
return strokes;
}
private Stroke GenerateLineStroke(Point st, Point ed)
{
var pointList = new List<Point>();
StylusPointCollection point;
Stroke stroke;
pointList = new List<Point> {
new Point(st.X, st.Y),
new Point(ed.X, ed.Y)
};
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
return stroke;
}
private Stroke GenerateArrowLineStroke(Point st, Point ed)
{
var pointList = new List<Point>();
StylusPointCollection point;
Stroke stroke;
double w = 20, h = 7;
var theta = Math.Atan2(st.Y - ed.Y, st.X - ed.X);
var sint = Math.Sin(theta);
var cost = Math.Cos(theta);
pointList = new List<Point> {
new Point(st.X, st.Y),
new Point(ed.X, ed.Y),
new Point(ed.X + (w * cost - h * sint), ed.Y + (w * sint + h * cost)),
new Point(ed.X, ed.Y),
new Point(ed.X + (w * cost + h * sint), ed.Y - (h * cost - w * sint))
};
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
return stroke;
}
private StrokeCollection GenerateDashedLineStrokeCollection(Point st, Point ed)
{
double step = 5;
var pointList = new List<Point>();
StylusPointCollection point;
Stroke stroke;
var strokes = new StrokeCollection();
var d = GetDistance(st, ed);
var sinTheta = (ed.Y - st.Y) / d;
var cosTheta = (ed.X - st.X) / d;
for (var i = 0.0; i < d; i += step * 2.76)
{
pointList = new List<Point> {
new Point(st.X + i * cosTheta, st.Y + i * sinTheta),
new Point(st.X + Math.Min(i + step, d) * cosTheta, st.Y + Math.Min(i + step, d) * sinTheta)
};
point = new StylusPointCollection(pointList);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
strokes.Add(stroke.Clone());
}
return strokes;
}
private StrokeCollection GenerateDotLineStrokeCollection(Point st, Point ed)
{
double step = 3;
var pointList = new List<Point>();
StylusPointCollection point;
Stroke stroke;
var strokes = new StrokeCollection();
var d = GetDistance(st, ed);
var sinTheta = (ed.Y - st.Y) / d;
var cosTheta = (ed.X - st.X) / d;
for (var i = 0.0; i < d; i += step * 2.76)
{
var stylusPoint = new StylusPoint(st.X + i * cosTheta, st.Y + i * sinTheta, (float)0.8);
point = new StylusPointCollection();
point.Add(stylusPoint);
stroke = new Stroke(point)
{
DrawingAttributes = inkCanvas.DefaultDrawingAttributes.Clone()
};
strokes.Add(stroke.Clone());
}
return strokes;
}
#endregion
#region 多点触控系统实现
private void MainWindow_StylusDown(object sender, StylusDownEventArgs e)
{
var stylusPoint = e.GetPosition(this);
var floatingBarBounds = ViewboxFloatingBar.TransformToAncestor(this).TransformBounds(
new Rect(0, 0, ViewboxFloatingBar.ActualWidth, ViewboxFloatingBar.ActualHeight));
if (floatingBarBounds.Contains(stylusPoint))
{
return;
}
if (e.StylusDevice.Inverted)
{
inkCanvas.EditingMode = InkCanvasEditingMode.EraseByPoint;
}
else
{
if (drawingShapeMode != 0)
{
inkCanvas.EditingMode = InkCanvasEditingMode.None;
isTouchDown = true;
ViewboxFloatingBar.IsHitTestVisible = false;
BlackboardUIGridForInkReplay.IsHitTestVisible = false;
if (NeedUpdateIniP()) iniP = e.GetPosition(inkCanvas);
return;
}
if (inkCanvas.EditingMode != InkCanvasEditingMode.EraseByStroke)
{
inkCanvas.EditingMode = InkCanvasEditingMode.Ink;
}
else
{
LogHelper.WriteLogToFile("保持当前线擦模式");
}
}
inkCanvas.CaptureStylus();
ViewboxFloatingBar.IsHitTestVisible = false;
BlackboardUIGridForInkReplay.IsHitTestVisible = false;
SetCursorBasedOnEditingMode(inkCanvas);
if (inkCanvas.EditingMode == InkCanvasEditingMode.EraseByPoint
|| inkCanvas.EditingMode == InkCanvasEditingMode.EraseByStroke
|| inkCanvas.EditingMode == InkCanvasEditingMode.Select) return;
TouchDownPointsList[e.StylusDevice.Id] = InkCanvasEditingMode.None;
}
private async void MainWindow_StylusUp(object sender, StylusEventArgs e)
{
if (drawingShapeMode != 0)
{
isTouchDown = false;
ViewboxFloatingBar.IsHitTestVisible = true;
BlackboardUIGridForInkReplay.IsHitTestVisible = true;
if (drawingShapeMode == 24 || drawingShapeMode == 25)
{
if (drawMultiStepShapeCurrentStep == 0)
{
drawMultiStepShapeCurrentStep = 1;
}
else
{
var mouseArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = MouseLeftButtonUpEvent,
Source = inkCanvas
};
inkCanvas_MouseUp(inkCanvas, mouseArgs);
}
}
else
{
var mouseArgs = new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = MouseLeftButtonUpEvent,
Source = inkCanvas
};
inkCanvas_MouseUp(inkCanvas, mouseArgs);
}
return;
}
try
{
var stroke = GetStrokeVisual(e.StylusDevice.Id).Stroke;
if (stroke != null)
{
inkCanvas.Strokes.Add(stroke);
await Task.Delay(5);
inkCanvas.Children.Remove(GetVisualCanvas(e.StylusDevice.Id));
inkCanvas_StrokeCollected(inkCanvas,
new InkCanvasStrokeCollectedEventArgs(stroke));
}
else
{
await Task.Delay(5);
inkCanvas.Children.Remove(GetVisualCanvas(e.StylusDevice.Id));
}
}
catch (Exception ex)
{