Repository navigation
Expand file tree
/
Copy pathPolylineDrawTool.cs
More file actions
152 lines (113 loc) · 4.09 KB
/
Copy pathPolylineDrawTool.cs
File metadata and controls
152 lines (113 loc) · 4.09 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
using DrawTools.Serialize;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;
namespace DrawTools
{
/// <summary>
/// 折线
/// </summary>
public sealed class PolylineDrawTool : DrawGeometryBase
{
public PolylineDrawTool(DrawingCanvas drawingCanvas) : base(drawingCanvas)
{
this.DrawingToolType = DrawToolType.Polyline;
// 准备要处理的事件
this.CanTouchDown = true;
}
#region 鼠标键盘事件
public override Boolean OnTouchLeave(Point point)
{
if (mousePoint.HasValue)
{
points.Add(mousePoint.Value);
mousePoint = null;
}
if (points.Count < 2)
this.drawingCanvas.DeleteVisual(this);
else
{
geometry = geometry.GetWidenedPathGeometry(pen);
Draw();
}
this.drawingCanvas.DeleteWorkingDrawTool(this);
this.IsFinish = true;
this.CanTouchDown = false;
this.CanTouchMove = false;
this.CanTouchLeave = false;
if (this.TouchId == 0 && this.drawingCanvas.IsMouseCaptured)
this.drawingCanvas.ReleaseMouseCapture();
return true;
}
public override Boolean OnTouchDown(Int32 touchId, Point point)
{
this.TouchId = touchId;
if (points.Count == 0)
{
this.drawingCanvas.AddWorkingDrawTool(this);
this.pen = this.drawingCanvas.Pen;
points.Add(point);
geometry = new PathGeometry();
var figure = new PathFigure { StartPoint = point };
pathGeometry.Figures.Add(figure);
this.CanTouchMove = true;
if (this.TouchId != 0 || !this.drawingCanvas.CaptureMouse())
this.CanTouchLeave = true;
this.drawingCanvas.AddVisual(this);
}
else if ((point - points.Last()).Length <= pen.Thickness)
OnTouchLeave(point);
else if (mousePoint.HasValue)
{
points.Add(mousePoint.Value);
mousePoint = null;
}
return true;
}
public override Boolean OnTouchMove(Point point)
{
if ((point - points.Last()).Length <= pen.Thickness)
return true;
var figure = pathGeometry.Figures[0];
var line = new LineSegment(point, true) { IsSmoothJoin = true };
if (mousePoint.HasValue)
figure.Segments[figure.Segments.Count - 1] = line;
else
figure.Segments.Add(line);
mousePoint = point;
var dc = this.RenderOpen();
dc.DrawGeometry(null, pen, geometry);
dc.Close();
return true;
}
#endregion
#region 序列化
public override DrawGeometrySerializerBase ToSerializer()
{
var serializer = new DrawPolylineSerializer
{
Color = ((SolidColorBrush)pen.Brush).Color,
StrokeThickness = pen.Thickness,
Geometry = geometry.ToString()
};
if (geometry.Transform != null)
serializer.Matrix = geometry.Transform.Value;
return serializer;
}
public override void DeserializeFrom(DrawGeometrySerializerBase serializer)
{
this.pen = new Pen(new SolidColorBrush(serializer.Color), serializer.StrokeThickness);
this.geometry = Geometry.Parse(serializer.Geometry).GetFlattenedPathGeometry();
this.geometry.Transform = new TranslateTransform(serializer.Matrix.OffsetX, serializer.Matrix.OffsetY);
this.IsFinish = true;
this.Draw();
}
#endregion
#region 字段
private List<Point> points = new List<Point>();
private Point? mousePoint;
#endregion
}
}