CraftSynth.ImageEditor – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Drawing;
3 using System.Windows.Forms;
4  
5 namespace CraftSynth.ImageEditor
6 {
7 /// <summary>
8 /// Scribble tool
9 /// </summary>
10 internal class ToolPolygon : ToolObject
11 {
12 public ToolPolygon()
13 {
14 Cursor = new Cursor(GetType(), "Pencil.cur");
15 }
16  
17 private int lastX;
18 private int lastY;
19 private DrawPolygon newPolygon;
20 private int minDistance = 15 * 15;
21  
22 /// <summary>
23 /// Left nouse button is pressed
24 /// </summary>
25 /// <param name="drawArea"></param>
26 /// <param name="e"></param>
27 public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
28 {
29 // Create new polygon, add it to the list
30 // and keep reference to it
31 Point p = drawArea.BackTrackMouse(new Point(e.X, e.Y));
32  
33 newPolygon = new DrawPolygon(p.X, p.Y, p.X + 1, p.Y + 1, drawArea.LineColor, drawArea.LineWidth, drawArea.PenType, drawArea.EndCap);
34  
35 // Set the minimum distance variable according to current zoom level.
36 minDistance = Convert.ToInt32((15 * drawArea.Zoom) * (15 * drawArea.Zoom));
37  
38 AddNewObject(drawArea, newPolygon);
39 lastX = e.X;
40 lastY = e.Y;
41 }
42  
43 /// <summary>
44 /// Mouse move - resize new polygon
45 /// </summary>
46 /// <param name="drawArea"></param>
47 /// <param name="e"></param>
48 public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
49 {
50 drawArea.Cursor = Cursor;
51  
52 if (e.Button !=
53 MouseButtons.Left)
54 return;
55  
56 if (newPolygon == null)
57 return; // precaution
58  
59 Point point = drawArea.BackTrackMouse(new Point(e.X, e.Y));
60 int distance = (e.X - lastX) * (e.X - lastX) + (e.Y - lastY) * (e.Y - lastY);
61  
62 if (distance < minDistance)
63 {
64 // Distance between last two points is less than minimum -
65 // move last point
66 newPolygon.MoveHandleTo(point, newPolygon.HandleCount);
67 }
68 else
69 {
70 // Add new point
71 newPolygon.AddPoint(point);
72 lastX = e.X;
73 lastY = e.Y;
74 }
75 drawArea.Refresh();
76 }
77  
78 public override void OnMouseUp(DrawArea drawArea, MouseEventArgs e)
79 {
80 newPolygon = null;
81 base.OnMouseUp(drawArea, e);
82 }
83  
84 #region Destruction
85 private bool _disposed = false;
86  
87 protected override void Dispose(bool disposing)
88 {
89 if (!this._disposed)
90 {
91 if (disposing)
92 {
93 // Free any managed objects here.
94 if (this.newPolygon != null)
95 {
96 this.newPolygon.Dispose();
97 }
98 }
99  
100 // Free any unmanaged objects here.
101  
102 this._disposed = true;
103 }
104 base.Dispose(disposing);
105 }
106  
107 ~ToolPolygon()
108 {
109 this.Dispose(false);
110 }
111 #endregion
112 }
113 }