Repository navigation
Expand file tree
/
Copy pathPointerDrawTool.cs
More file actions
320 lines (246 loc) · 9.31 KB
/
Copy pathPointerDrawTool.cs
File metadata and controls
320 lines (246 loc) · 9.31 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
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
namespace DrawTools
{
/// <summary>
/// 指针(拾取)
/// </summary>
public sealed class PointerDrawTool : DrawingVisual, IDrawTool
{
public PointerDrawTool(DrawingCanvas drawingCanvas)
{
this.drawingCanvas = drawingCanvas;
// 准备要处理的事件
this.CanTouchDown = true;
this.selectedDrawGeometries = new List<DrawGeometryBase>();
}
#region 公开方法
public Boolean OnKeyDown(Key key)
{
throw new NotImplementedException();
}
public Boolean OnKeyUp(Key key)
{
throw new NotImplementedException();
}
public Boolean OnTouchDown(Int32 touchId, Point point)
{
if (this.drawingCanvas.DrawingToolType != this.DrawingToolType)
{
Delete();
return false;
}
this.TouchId = touchId;
this.mode = 0;
if (this.VisualParent == null)
{
this.drawingCanvas.AddWorkingDrawTool(this);
this.drawingCanvas.Insert(0, this);
}
else
{
this.drawingCanvas.MovoToHead(this);
var visual = this.drawingCanvas.GetVisual(point);
if (visual == this || (visual is DrawGeometryBase draw && selectedDrawGeometries.Contains(draw)))
{
this.mode = 1;
this.lastPoint = point;
}
}
this.startPoint = point;
if (this.mode == 0)
{
selectRect = drawRect = null;
this.selectedDrawGeometries.Clear();
foreach (var draw in this.drawingCanvas.GetDrawGeometries())
{
if (draw.Select(point))
{
if (selectRect.HasValue)
selectRect = Rect.Union(selectRect.Value, draw.Selected());
else
selectRect = draw.Selected();
selectedDrawGeometries.Add(draw);
}
else
draw.Unselected();
}
if (this.geometry == null)
{
this.geometry = new PathGeometry();
var figure = new PathFigure { StartPoint = point, IsClosed = true, IsFilled = true };
geometry.Figures.Add(figure);
}
else
geometry.Figures[0].StartPoint = point;
}
this.CanTouchMove = true;
this.CanTouchUp = true;
if (this.TouchId != 0 || !this.drawingCanvas.CaptureMouse())
this.CanTouchLeave = true;
return true;
}
public Boolean OnTouchEnter(Point point)
{
throw new NotImplementedException();
}
public Boolean OnTouchLeave(Point point)
{
return OnTouchUp(point);
}
public Boolean OnTouchMove(Point point)
{
if (mode == 0)
{
var figure = geometry.Figures[0];
var topRight = new Point(point.X, startPoint.Y);
var bottomLeft = new Point(startPoint.X, point.Y);
if (figure.Segments.Count == 0)
{
var line = new LineSegment(topRight, false);
figure.Segments.Add(line);
line = new LineSegment(point, false);
figure.Segments.Add(line);
line = new LineSegment(bottomLeft, false);
figure.Segments.Add(line);
}
else
{
var line = (LineSegment)figure.Segments[0];
line.Point = topRight;
line = (LineSegment)figure.Segments[1];
line.Point = point;
line = (LineSegment)figure.Segments[2];
line.Point = bottomLeft;
}
selectedDrawGeometries.Clear();
foreach (var draw in this.drawingCanvas.GetDrawGeometries())
{
if (draw.Select(geometry))
{
if (selectRect.HasValue)
selectRect = Rect.Union(selectRect.Value, draw.Selected());
else
selectRect = draw.Selected();
selectedDrawGeometries.Add(draw);
}
else
draw.Unselected();
}
drawRect = new Rect(startPoint, point);
var dc = this.RenderOpen();
dc.DrawRectangle(null, this.drawingCanvas.SelectBackgroundPen, drawRect.Value);
dc.DrawRectangle(null, this.drawingCanvas.SelectPen, drawRect.Value);
dc.Close();
}
else if (mode == 1)
{
// 移动
var dx = point.X - lastPoint.X;
var dy = point.Y - lastPoint.Y;
lastPoint = point;
foreach (var draw in selectedDrawGeometries)
{
draw.Move(dx, dy);
}
var rect = selectRect.Value;
rect.X += dx;
rect.Y += dy;
selectRect = rect;
var dc = this.RenderOpen();
dc.DrawRectangle(Brushes.Transparent, this.drawingCanvas.SelectBackgroundPen, selectRect.Value);
dc.DrawRectangle(null, this.drawingCanvas.SelectPen, selectRect.Value);
dc.Close();
}
return true;
}
public Boolean OnTouchUp(Point point)
{
if (mode == 0)
{
if (selectedDrawGeometries.Count == 0)
{
Delete();
return true;
}
if (drawRect.HasValue || selectedDrawGeometries.Count > 1)
{
if (drawRect.HasValue)
selectRect = Rect.Union(selectRect.Value, drawRect.Value);
var dc = this.RenderOpen();
dc.DrawRectangle(Brushes.Transparent, this.drawingCanvas.SelectBackgroundPen, selectRect.Value);
dc.DrawRectangle(null, this.drawingCanvas.SelectPen, selectRect.Value);
dc.Close();
}
}
else if (mode == 1 && startPoint == lastPoint)
{
var visual = this.drawingCanvas.GetVisual(point);
if (visual is DrawGeometryBase selectDraw && selectDraw.CanEdit)
{
selectDraw.Edit();
foreach (var draw in selectedDrawGeometries)
{
if (draw != selectDraw)
draw.Unselected();
}
selectedDrawGeometries.Clear();
Delete();
return true;
}
}
this.CanTouchMove = false;
this.CanTouchUp = false;
this.CanTouchLeave = false;
if (this.TouchId == 0 && this.drawingCanvas.IsMouseCaptured)
this.drawingCanvas.ReleaseMouseCapture();
return true;
}
#endregion
#region 私有方法
private void Delete()
{
foreach (var draw in selectedDrawGeometries)
{
draw.Unselected();
}
selectedDrawGeometries.Clear();
this.drawingCanvas.DeleteVisual(this);
this.drawingCanvas.DeleteWorkingDrawTool(this);
IsFinish = true;
this.CanTouchDown = false;
this.CanTouchMove = false;
this.CanTouchUp = false;
this.CanTouchLeave = false;
if (this.TouchId == 0 && this.drawingCanvas.IsMouseCaptured)
this.drawingCanvas.ReleaseMouseCapture();
}
#endregion
#region 属性
public Int32 TouchId { get; private set; }
public Boolean CanTouchEnter { get; private set; }
public Boolean CanTouchLeave { get; private set; }
public Boolean CanTouchDown { get; private set; }
public Boolean CanTouchMove { get; private set; }
public Boolean CanTouchUp { get; private set; }
public Boolean CanKeyDown { get; private set; }
public Boolean CanKeyUp { get; private set; }
public Boolean IsFinish { get; private set; }
public DrawToolType DrawingToolType => DrawToolType.Pointer;
#endregion
#region 字段
private DrawingCanvas drawingCanvas;
private Point startPoint, lastPoint;
private List<DrawGeometryBase> selectedDrawGeometries;
private PathGeometry geometry;
private Rect? selectRect, drawRect;
/// <summary>
/// 0:拾取|1:拖动
/// </summary>
private Int32 mode;
#endregion
}
}