CraftSynth.ImageEditor – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | using System; |
2 | using System.Collections; |
||
3 | using System.Drawing; |
||
4 | using System.Drawing.Drawing2D; |
||
5 | using System.Globalization; |
||
6 | using System.Runtime.Serialization; |
||
7 | using System.Windows.Forms; |
||
8 | |||
9 | namespace CraftSynth.ImageEditor |
||
10 | { |
||
11 | /// <summary> |
||
12 | /// Connector graphic object - a Connector is a series of connected straight lines |
||
13 | /// where each line is drawn individually and at least |
||
14 | /// one of the ends is anchored to another object |
||
15 | /// </summary> |
||
16 | //[Serializable] |
||
17 | public class DrawConnector : DrawLine |
||
18 | { |
||
19 | // Connector-specific fields |
||
20 | private LineCap startCap = LineCap.NoAnchor; |
||
21 | private LineCap endCap = LineCap.ArrowAnchor; |
||
22 | private bool startIsAnchored = false; |
||
23 | private bool endIsAnchored = false; |
||
24 | private int startObjectId = -1; |
||
25 | private int endObjectId = -1; |
||
26 | |||
27 | // Last Segment start and end points |
||
28 | private Point startPoint; |
||
29 | private Point endPoint; |
||
30 | |||
31 | private ArrayList pointArray; // list of points |
||
32 | private Cursor handleCursor; |
||
33 | |||
34 | private const string entryLength = "Length"; |
||
35 | private const string entryPoint = "Point"; |
||
36 | |||
37 | private bool _disposed = false; |
||
38 | |||
39 | public LineCap StartCap |
||
40 | { |
||
41 | get { return startCap; } |
||
42 | set { startCap = value; } |
||
43 | } |
||
44 | |||
45 | public LineCap EndCap |
||
46 | { |
||
47 | get { return endCap; } |
||
48 | set { endCap = value; } |
||
49 | } |
||
50 | |||
51 | public bool StartIsAnchored |
||
52 | { |
||
53 | get { return startIsAnchored; } |
||
54 | set { startIsAnchored = value; } |
||
55 | } |
||
56 | |||
57 | public bool EndIsAnchored |
||
58 | { |
||
59 | get { return endIsAnchored; } |
||
60 | set { endIsAnchored = value; } |
||
61 | } |
||
62 | |||
63 | public int StartObjectId |
||
64 | { |
||
65 | get { return startObjectId; } |
||
66 | set { startObjectId = value; } |
||
67 | } |
||
68 | |||
69 | public int EndObjectId |
||
70 | { |
||
71 | get { return endObjectId; } |
||
72 | set { endObjectId = value; } |
||
73 | } |
||
74 | |||
75 | |||
76 | public Point StartPoint |
||
77 | { |
||
78 | get { return startPoint; } |
||
79 | set { startPoint = value; } |
||
80 | } |
||
81 | |||
82 | public Point EndPoint |
||
83 | { |
||
84 | get { return endPoint; } |
||
85 | set { endPoint = value; } |
||
86 | } |
||
87 | |||
88 | /// <summary> |
||
89 | /// Clone this instance |
||
90 | /// </summary> |
||
91 | public override DrawObject Clone() |
||
92 | { |
||
93 | DrawConnector drawPolyLine = new DrawConnector(); |
||
94 | |||
95 | drawPolyLine.startPoint = startPoint; |
||
96 | drawPolyLine.endPoint = endPoint; |
||
97 | drawPolyLine.pointArray = pointArray; |
||
98 | |||
99 | FillDrawObjectFields(drawPolyLine); |
||
100 | return drawPolyLine; |
||
101 | } |
||
102 | |||
103 | public DrawConnector() |
||
104 | { |
||
105 | pointArray = new ArrayList(); |
||
106 | |||
107 | LoadCursor(); |
||
108 | Initialize(); |
||
109 | } |
||
110 | |||
111 | #region Destruction |
||
112 | protected override void Dispose(bool disposing) |
||
113 | { |
||
114 | if (!this._disposed) |
||
115 | { |
||
116 | if (disposing) |
||
117 | { |
||
118 | // Free any managed objects here. |
||
119 | if (this.handleCursor != null) |
||
120 | { |
||
121 | this.handleCursor.Dispose(); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | // Free any unmanaged objects here. |
||
126 | |||
127 | this._disposed = true; |
||
128 | } |
||
129 | base.Dispose(disposing); |
||
130 | } |
||
131 | |||
132 | ~DrawConnector() |
||
133 | { |
||
134 | this.Dispose(false); |
||
135 | } |
||
136 | #endregion |
||
137 | |||
138 | public DrawConnector(int x1, int y1, int x2, int y2, Color lineColor, int lineWidth, DrawingPens.PenType penType, LineCap endCap) |
||
139 | { |
||
140 | pointArray = new ArrayList(); |
||
141 | pointArray.Add(new Point(x1, y1)); |
||
142 | pointArray.Add(new Point(x2, y2)); |
||
143 | TipText = String.Format("Start @ {0}-{1}, End @ {2}, {3}", x1, y1, x2, y2); |
||
144 | Color = lineColor; |
||
145 | PenWidth = lineWidth; |
||
146 | PenType = penType; |
||
147 | EndCap = endCap; |
||
148 | |||
149 | LoadCursor(); |
||
150 | Initialize(); |
||
151 | } |
||
152 | |||
153 | public override void Draw(Graphics g) |
||
154 | { |
||
155 | g.SmoothingMode = SmoothingMode.AntiAlias; |
||
156 | Pen pen; |
||
157 | |||
158 | if (DrawPen == null) |
||
159 | { |
||
160 | pen = new Pen(Color, PenWidth); |
||
161 | DrawingPens.SetCurrentPen(ref pen, PenType, EndCap); |
||
162 | } |
||
163 | else |
||
164 | pen = DrawPen.Clone() as Pen; |
||
165 | |||
166 | Point[] pts = new Point[pointArray.Count]; |
||
167 | for (int i = 0; i < pointArray.Count; i++) |
||
168 | { |
||
169 | Point px = (Point)pointArray[i]; |
||
170 | pts[i] = px; |
||
171 | } |
||
172 | byte[] types = new byte[pointArray.Count]; |
||
173 | for (int i = 0; i < pointArray.Count; i++) |
||
174 | types[i] = (byte)PathPointType.Line; |
||
175 | GraphicsPath gp = new GraphicsPath(pts, types); |
||
176 | // Rotate the path about it's center if necessary |
||
177 | if (Rotation != 0) |
||
178 | { |
||
179 | RectangleF pathBounds = gp.GetBounds(); |
||
180 | Matrix m = new Matrix(); |
||
181 | m.RotateAt(Rotation, new PointF(pathBounds.Left + (pathBounds.Width / 2), pathBounds.Top + (pathBounds.Height / 2)), MatrixOrder.Append); |
||
182 | gp.Transform(m); |
||
183 | } |
||
184 | g.DrawPath(pen, gp); |
||
185 | gp.Dispose(); |
||
186 | if (pen != null) |
||
187 | pen.Dispose(); |
||
188 | } |
||
189 | |||
190 | public void AddPoint(Point point) |
||
191 | { |
||
192 | pointArray.Add(point); |
||
193 | } |
||
194 | |||
195 | public override int HandleCount |
||
196 | { |
||
197 | get { return pointArray.Count; } |
||
198 | } |
||
199 | |||
200 | /// <summary> |
||
201 | /// Get handle point by 1-based number |
||
202 | /// </summary> |
||
203 | /// <param name="handleNumber"></param> |
||
204 | /// <returns></returns> |
||
205 | public override Point GetHandle(int handleNumber) |
||
206 | { |
||
207 | if (handleNumber < 1) |
||
208 | handleNumber = 1; |
||
209 | if (handleNumber > pointArray.Count) |
||
210 | handleNumber = pointArray.Count; |
||
211 | return ((Point)pointArray[handleNumber - 1]); |
||
212 | } |
||
213 | |||
214 | public override Cursor GetHandleCursor(int handleNumber) |
||
215 | { |
||
216 | return handleCursor; |
||
217 | } |
||
218 | |||
219 | public override void MoveHandleTo(Point point, int handleNumber) |
||
220 | { |
||
221 | if (handleNumber < 1) |
||
222 | handleNumber = 1; |
||
223 | |||
224 | if (handleNumber > pointArray.Count) |
||
225 | handleNumber = pointArray.Count; |
||
226 | pointArray[handleNumber - 1] = point; |
||
227 | Dirty = true; |
||
228 | Invalidate(); |
||
229 | } |
||
230 | |||
231 | public override void Move(int deltaX, int deltaY) |
||
232 | { |
||
233 | int n = pointArray.Count; |
||
234 | |||
235 | for (int i = 0; i < n; i++) |
||
236 | { |
||
237 | Point point; |
||
238 | point = new Point(((Point)pointArray[i]).X + deltaX, ((Point)pointArray[i]).Y + deltaY); |
||
239 | pointArray[i] = point; |
||
240 | } |
||
241 | Dirty = true; |
||
242 | Invalidate(); |
||
243 | } |
||
244 | |||
245 | public override void SaveToStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
246 | { |
||
247 | info.AddValue( |
||
248 | String.Format(CultureInfo.InvariantCulture, |
||
249 | "{0}{1}-{2}", |
||
250 | entryLength, orderNumber, objectIndex), |
||
251 | pointArray.Count); |
||
252 | |||
253 | int i = 0; |
||
254 | foreach (Point p in pointArray) |
||
255 | { |
||
256 | info.AddValue( |
||
257 | String.Format(CultureInfo.InvariantCulture, |
||
258 | "{0}{1}-{2}-{3}", |
||
259 | new object[] { entryPoint, orderNumber, objectIndex, i++ }), |
||
260 | p); |
||
261 | } |
||
262 | base.SaveToStream(info, orderNumber, objectIndex); |
||
263 | } |
||
264 | |||
265 | public override void LoadFromStream(SerializationInfo info, int orderNumber, int objectIndex) |
||
266 | { |
||
267 | int n = info.GetInt32( |
||
268 | String.Format(CultureInfo.InvariantCulture, |
||
269 | "{0}{1}-{2}", |
||
270 | entryLength, orderNumber, objectIndex)); |
||
271 | |||
272 | for (int i = 0; i < n; i++) |
||
273 | { |
||
274 | Point point; |
||
275 | point = (Point)info.GetValue( |
||
276 | String.Format(CultureInfo.InvariantCulture, |
||
277 | "{0}{1}-{2}-{3}", |
||
278 | new object[] { entryPoint, orderNumber, objectIndex, i }), |
||
279 | typeof(Point)); |
||
280 | pointArray.Add(point); |
||
281 | } |
||
282 | base.LoadFromStream(info, orderNumber, objectIndex); |
||
283 | } |
||
284 | |||
285 | /// <summary> |
||
286 | /// Create graphic object used for hit test |
||
287 | /// </summary> |
||
288 | protected override void CreateObjects() |
||
289 | { |
||
290 | if (AreaPath != null) |
||
291 | return; |
||
292 | |||
293 | // Create closed path which contains all polygon vertexes |
||
294 | AreaPath = new GraphicsPath(); |
||
295 | |||
296 | int x1 = 0, y1 = 0; // previous point |
||
297 | |||
298 | IEnumerator enumerator = pointArray.GetEnumerator(); |
||
299 | |||
300 | if (enumerator.MoveNext()) |
||
301 | { |
||
302 | x1 = ((Point)enumerator.Current).X; |
||
303 | y1 = ((Point)enumerator.Current).Y; |
||
304 | } |
||
305 | |||
306 | while (enumerator.MoveNext()) |
||
307 | { |
||
308 | int x2, y2; // current point |
||
309 | x2 = ((Point)enumerator.Current).X; |
||
310 | y2 = ((Point)enumerator.Current).Y; |
||
311 | |||
312 | AreaPath.AddLine(x1, y1, x2, y2); |
||
313 | |||
314 | x1 = x2; |
||
315 | y1 = y2; |
||
316 | } |
||
317 | |||
318 | AreaPath.CloseFigure(); |
||
319 | |||
320 | // Create region from the path |
||
321 | AreaRegion = new Region(AreaPath); |
||
322 | } |
||
323 | |||
324 | private void LoadCursor() |
||
325 | { |
||
326 | handleCursor = new Cursor(GetType(), "PolyHandle.cur"); |
||
327 | } |
||
328 | } |
||
329 | } |