-
Notifications
You must be signed in to change notification settings - Fork 8
/
ConnectorAdorner.cs
164 lines (141 loc) · 5.34 KB
/
ConnectorAdorner.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
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Controls;
namespace DiagramDesigner
{
public class ConnectorAdorner : Adorner
{
private PathGeometry pathGeometry;
private DesignerCanvas designerCanvas;
private Connector sourceConnector;
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;
}
}
}
public ConnectorAdorner(DesignerCanvas designer, Connector sourceConnector)
: base(designer)
{
this.designerCanvas = designer;
this.sourceConnector = sourceConnector;
drawingPen = new Pen(Brushes.LightSlateGray, 1);
drawingPen.LineJoin = PenLineJoin.Round;
this.Cursor = Cursors.Cross;
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
if (HitConnector != null)
{
Connector sourceConnector = this.sourceConnector;
Connector sinkConnector = this.HitConnector;
Connection newConnection = new Connection(sourceConnector, sinkConnector);
Canvas.SetZIndex(newConnection, designerCanvas.Children.Count);
this.designerCanvas.Children.Add(newConnection);
}
if (HitDesignerItem != null)
{
this.HitDesignerItem.IsDragConnectionOver = false;
}
if (this.IsMouseCaptured) this.ReleaseMouseCapture();
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(this.designerCanvas);
if (adornerLayer != null)
{
adornerLayer.Remove(this);
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
if (!this.IsMouseCaptured) this.CaptureMouse();
HitTesting(e.GetPosition(this));
this.pathGeometry = GetPathGeometry(e.GetPosition(this));
this.InvalidateVisual();
}
else
{
if (this.IsMouseCaptured) this.ReleaseMouseCapture();
}
}
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
dc.DrawGeometry(null, drawingPen, this.pathGeometry);
// without a background the OnMouseMove event would not be fired
// Alternative: implement a Canvas as a child of this adorner, like
// the ConnectionAdorner does.
dc.DrawRectangle(Brushes.Transparent, null, new Rect(RenderSize));
}
private PathGeometry GetPathGeometry(Point position)
{
PathGeometry geometry = new PathGeometry();
ConnectorOrientation targetOrientation;
if (HitConnector != null)
targetOrientation = HitConnector.Orientation;
else
targetOrientation = ConnectorOrientation.None;
List<Point> pathPoints = PathFinder.GetConnectionLine(sourceConnector.GetInfo(), position, targetOrientation);
if (pathPoints.Count > 0)
{
PathFigure figure = new PathFigure();
figure.StartPoint = pathPoints[0];
pathPoints.Remove(pathPoints[0]);
figure.Segments.Add(new PolyLineSegment(pathPoints, 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 != sourceConnector.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;
}
}
}