-
Notifications
You must be signed in to change notification settings - Fork 8
/
ConnectionAdorner.cs
257 lines (218 loc) · 9.15 KB
/
ConnectionAdorner.cs
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
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace DiagramDesigner
{
public class ConnectionAdorner : Adorner
{
private DesignerCanvas designerCanvas;
private Canvas adornerCanvas;
private Connection connection;
private PathGeometry pathGeometry;
private Connector fixConnector, dragConnector;
private Thumb sourceDragThumb, sinkDragThumb;
private Pen drawingPen;
private DesignerItem hitDesignerItem;
private DesignerItem HitDesignerItem
{
get { return hitDesignerItem; }
set
{
if (hitDesignerItem != value)
{
if (hitDesignerItem != null)
hitDesignerItem.IsDragConnectionOver = false;
hitDesignerItem = value;
if (hitDesignerItem != null)
hitDesignerItem.IsDragConnectionOver = true;
}
}
}
private Connector hitConnector;
private Connector HitConnector
{
get { return hitConnector; }
set
{
if (hitConnector != value)
{
hitConnector = value;
}
}
}
private VisualCollection visualChildren;
protected override int VisualChildrenCount
{
get
{
return this.visualChildren.Count;
}
}
protected override Visual GetVisualChild(int index)
{
return this.visualChildren[index];
}
public ConnectionAdorner(DesignerCanvas designer, Connection connection)
: base(designer)
{
this.designerCanvas = designer;
adornerCanvas = new Canvas();
this.visualChildren = new VisualCollection(this);
this.visualChildren.Add(adornerCanvas);
this.connection = connection;
this.connection.PropertyChanged += new PropertyChangedEventHandler(AnchorPositionChanged);
InitializeDragThumbs();
drawingPen = new Pen(Brushes.LightSlateGray, 1);
drawingPen.LineJoin = PenLineJoin.Round;
base.Unloaded += new RoutedEventHandler(ConnectionAdorner_Unloaded);
}
void AnchorPositionChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName.Equals("AnchorPositionSource"))
{
Canvas.SetLeft(sourceDragThumb, connection.AnchorPositionSource.X);
Canvas.SetTop(sourceDragThumb, connection.AnchorPositionSource.Y);
}
if (e.PropertyName.Equals("AnchorPositionSink"))
{
Canvas.SetLeft(sinkDragThumb, connection.AnchorPositionSink.X);
Canvas.SetTop(sinkDragThumb, connection.AnchorPositionSink.Y);
}
}
void thumbDragThumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
if (HitConnector != null)
{
if (connection != null)
{
if (connection.Source == fixConnector)
connection.Sink = this.HitConnector;
else
connection.Source = this.HitConnector;
}
}
this.HitDesignerItem = null;
this.HitConnector = null;
this.pathGeometry = null;
this.connection.StrokeDashArray = null;
this.InvalidateVisual();
}
void thumbDragThumb_DragStarted(object sender, DragStartedEventArgs e)
{
this.HitDesignerItem = null;
this.HitConnector = null;
this.pathGeometry = null;
this.Cursor = Cursors.Cross;
this.connection.StrokeDashArray = new DoubleCollection(new double[] { 1, 2 });
if (sender == sourceDragThumb)
{
fixConnector = connection.Sink;
dragConnector = connection.Source;
}
else if (sender == sinkDragThumb)
{
dragConnector = connection.Sink;
fixConnector = connection.Source;
}
}
void thumbDragThumb_DragDelta(object sender, DragDeltaEventArgs e)
{
Point currentPosition = Mouse.GetPosition(this);
this.HitTesting(currentPosition);
this.pathGeometry = UpdatePathGeometry(currentPosition);
this.InvalidateVisual();
}
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
dc.DrawGeometry(null, drawingPen, this.pathGeometry);
}
protected override Size ArrangeOverride(Size finalSize)
{
adornerCanvas.Arrange(new Rect(0, 0, this.designerCanvas.ActualWidth, this.designerCanvas.ActualHeight));
return finalSize;
}
private void ConnectionAdorner_Unloaded(object sender, RoutedEventArgs e)
{
sourceDragThumb.DragDelta -= new DragDeltaEventHandler(thumbDragThumb_DragDelta);
sourceDragThumb.DragStarted -= new DragStartedEventHandler(thumbDragThumb_DragStarted);
sourceDragThumb.DragCompleted -= new DragCompletedEventHandler(thumbDragThumb_DragCompleted);
sinkDragThumb.DragDelta -= new DragDeltaEventHandler(thumbDragThumb_DragDelta);
sinkDragThumb.DragStarted -= new DragStartedEventHandler(thumbDragThumb_DragStarted);
sinkDragThumb.DragCompleted -= new DragCompletedEventHandler(thumbDragThumb_DragCompleted);
}
private void InitializeDragThumbs()
{
Style dragThumbStyle = connection.FindResource("ConnectionAdornerThumbStyle") as Style;
//source drag thumb
sourceDragThumb = new Thumb();
Canvas.SetLeft(sourceDragThumb, connection.AnchorPositionSource.X);
Canvas.SetTop(sourceDragThumb, connection.AnchorPositionSource.Y);
this.adornerCanvas.Children.Add(sourceDragThumb);
if (dragThumbStyle != null)
sourceDragThumb.Style = dragThumbStyle;
sourceDragThumb.DragDelta += new DragDeltaEventHandler(thumbDragThumb_DragDelta);
sourceDragThumb.DragStarted += new DragStartedEventHandler(thumbDragThumb_DragStarted);
sourceDragThumb.DragCompleted += new DragCompletedEventHandler(thumbDragThumb_DragCompleted);
// sink drag thumb
sinkDragThumb = new Thumb();
Canvas.SetLeft(sinkDragThumb, connection.AnchorPositionSink.X);
Canvas.SetTop(sinkDragThumb, connection.AnchorPositionSink.Y);
this.adornerCanvas.Children.Add(sinkDragThumb);
if (dragThumbStyle != null)
sinkDragThumb.Style = dragThumbStyle;
sinkDragThumb.DragDelta += new DragDeltaEventHandler(thumbDragThumb_DragDelta);
sinkDragThumb.DragStarted += new DragStartedEventHandler(thumbDragThumb_DragStarted);
sinkDragThumb.DragCompleted += new DragCompletedEventHandler(thumbDragThumb_DragCompleted);
}
private PathGeometry UpdatePathGeometry(Point position)
{
PathGeometry geometry = new PathGeometry();
ConnectorOrientation targetOrientation;
if (HitConnector != null)
targetOrientation = HitConnector.Orientation;
else
targetOrientation = dragConnector.Orientation;
List<Point> linePoints = PathFinder.GetConnectionLine(fixConnector.GetInfo(), position, targetOrientation);
if (linePoints.Count > 0)
{
PathFigure figure = new PathFigure();
figure.StartPoint = linePoints[0];
linePoints.Remove(linePoints[0]);
figure.Segments.Add(new PolyLineSegment(linePoints, true));
geometry.Figures.Add(figure);
}
return geometry;
}
private void HitTesting(Point hitPoint)
{
bool hitConnectorFlag = false;
DependencyObject hitObject = designerCanvas.InputHitTest(hitPoint) as DependencyObject;
while (hitObject != null &&
hitObject != fixConnector.ParentDesignerItem &&
hitObject.GetType() != typeof(DesignerCanvas))
{
if (hitObject is Connector)
{
HitConnector = hitObject as Connector;
hitConnectorFlag = true;
}
if (hitObject is DesignerItem)
{
HitDesignerItem = hitObject as DesignerItem;
if (!hitConnectorFlag)
HitConnector = null;
return;
}
hitObject = VisualTreeHelper.GetParent(hitObject);
}
HitConnector = null;
HitDesignerItem = null;
}
}
}