CraftSynth.ImageEditor – Blame information for rev 1

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