Repository navigation
Expand file tree
/
Copy pathRectangleDrawTool.cs
More file actions
172 lines (125 loc) · 4.82 KB
/
Copy pathRectangleDrawTool.cs
File metadata and controls
172 lines (125 loc) · 4.82 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
using DrawTools.Serialize;
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace DrawTools
{
/// <summary>
/// 矩形
/// </summary>
public sealed class RectangleDrawTool : DrawGeometryBase
{
public RectangleDrawTool(DrawingCanvas drawingCanvas) : base(drawingCanvas)
{
this.DrawingToolType = DrawToolType.Rectangle;
// 准备要处理的事件
this.CanTouchDown = true;
}
#region 鼠标键盘事件
public override Boolean OnKeyDown(Key key)
{
if (key != Key.LeftShift || !bottomRight.HasValue)
return false;
return OnTouchMove(bottomRight.Value);
}
public override Boolean OnKeyUp(Key key)
{
if (key != Key.LeftShift || !bottomRight.HasValue)
return false;
return OnTouchMove(bottomRight.Value);
}
public override Boolean OnTouchLeave(Point point)
{
if (!bottomRight.HasValue)
this.drawingCanvas.DeleteVisual(this);
else
{
var figure = pathGeometry.Figures[0];
var line = new LineSegment(new Point(bottomRight.Value.X, topLeft.Value.Y), true);
figure.Segments.Add(line);
line = new LineSegment(bottomRight.Value, true);
figure.Segments.Add(line);
line = new LineSegment(new Point(topLeft.Value.X, bottomRight.Value.Y), true);
figure.Segments.Add(line);
geometry = geometry.GetWidenedPathGeometry(pen);
Draw();
}
this.drawingCanvas.DeleteWorkingDrawTool(this);
this.IsFinish = true;
this.CanTouchDown = false;
this.CanTouchMove = false;
this.CanTouchLeave = false;
this.CanKeyDown = false;
this.CanKeyUp = 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 (!topLeft.HasValue)
{
this.drawingCanvas.AddWorkingDrawTool(this);
this.pen = this.drawingCanvas.Pen;
topLeft = point;
this.geometry = new PathGeometry();
var figure = new PathFigure { StartPoint = point, IsClosed = true };
pathGeometry.Figures.Add(figure);
this.CanTouchMove = true;
this.CanKeyDown = true;
this.CanKeyUp = true;
if (this.TouchId != 0 || !this.drawingCanvas.CaptureMouse())
this.CanTouchLeave = true;
this.drawingCanvas.AddVisual(this);
}
else
return OnTouchLeave(point);
return true;
}
public override Boolean OnTouchMove(Point point)
{
var dc = this.RenderOpen();
var startPoint = topLeft.Value;
if (Keyboard.IsKeyDown(Key.LeftShift))
{
var len = Math.Min(Math.Abs(point.X - startPoint.X), Math.Abs(point.Y - startPoint.Y));
point = new Point(startPoint.X + (point.X > startPoint.X ? len : -len), startPoint.Y + (point.Y > startPoint.Y ? len : -len));
}
if ((startPoint - point).Length > pen.Thickness)
{
bottomRight = point;
dc.DrawRectangle(null, pen, new Rect(startPoint, point));
}
dc.Close();
return true;
}
#endregion
#region 序列化
public override DrawGeometrySerializerBase ToSerializer()
{
var serializer = new DrawRectangleSerializer
{
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 Point? topLeft, bottomRight;
#endregion
}
}