CraftSynth.ImageEditor – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System.Drawing;
2 using System.Windows.Forms;
3  
4 namespace CraftSynth.ImageEditor
5 {
6 /// <summary>
7 /// Pointer tool
8 /// </summary>
9 internal class ToolPointer : Tool
10 {
11 private enum SelectionMode
12 {
13 None,
14 NetSelection, // group selection is active
15 Move, // object(s) are moves
16 Size // object is resized
17 }
18  
19 private SelectionMode selectMode = SelectionMode.None;
20  
21 // Object which is currently resized:
22 private DrawObject resizedObject;
23 private int resizedObjectHandle;
24  
25 // Keep state about last and current point (used to move and resize objects)
26 private Point lastPoint = new Point(0, 0);
27 private Point startPoint = new Point(0, 0);
28 private CommandChangeState commandChangeState;
29 private bool wasMove;
30 private ToolTip toolTip = new ToolTip();
31  
32 /// <summary>
33 /// Left mouse button is pressed
34 /// </summary>
35 /// <param name="drawArea"></param>
36 /// <param name="e"></param>
37 public override void OnMouseDown(DrawArea drawArea, MouseEventArgs e)
38 {
39 commandChangeState = null;
40 wasMove = false;
41  
42 selectMode = SelectionMode.None;
43 Point point = drawArea.BackTrackMouse(new Point(e.X, e.Y));
44  
45 // Test for resizing (only if control is selected, cursor is on the handle)
46 int al = drawArea.TheLayers.ActiveLayerIndex;
47 int n = drawArea.TheLayers[al].Graphics.SelectionCount;
48  
49 for (int i = 0; i < n; i++)
50 {
51 DrawObject o = drawArea.TheLayers[al].Graphics.GetSelectedObject(i);
52 int handleNumber = o.HitTest(point);
53  
54 if (handleNumber > 0)
55 {
56 selectMode = SelectionMode.Size;
57 // keep resized object in class members
58 resizedObject = o;
59 resizedObjectHandle = handleNumber;
60 // Since we want to resize only one object, unselect all other objects
61 drawArea.TheLayers[al].Graphics.UnselectAll();
62 o.Selected = true;
63 commandChangeState = new CommandChangeState(drawArea.TheLayers);
64 break;
65 }
66 }
67  
68 // Test for move (cursor is on the object)
69 if (selectMode == SelectionMode.None)
70 {
71 int n1 = drawArea.TheLayers[al].Graphics.Count;
72 DrawObject o = null;
73  
74 for (int i = 0; i < n1; i++)
75 {
76 if (drawArea.TheLayers[al].Graphics[i].HitTest(point) == 0)
77 {
78 o = drawArea.TheLayers[al].Graphics[i];
79 break;
80 }
81 }
82  
83 if (o != null)
84 {
85 selectMode = SelectionMode.Move;
86  
87 // Unselect all if Ctrl is not pressed and clicked object is not selected yet
88 if ((Control.ModifierKeys & Keys.Control) == 0 &&
89 !o.Selected)
90 drawArea.TheLayers[al].Graphics.UnselectAll();
91  
92 // Select clicked object
93 o.Selected = true;
94 commandChangeState = new CommandChangeState(drawArea.TheLayers);
95  
96 drawArea.Cursor = Cursors.SizeAll;
97 }
98 }
99  
100 // Net selection
101 if (selectMode == SelectionMode.None)
102 {
103 // click on background
104 if ((Control.ModifierKeys & Keys.Control) == 0)
105 drawArea.TheLayers[al].Graphics.UnselectAll();
106  
107 selectMode = SelectionMode.NetSelection;
108 drawArea.DrawNetRectangle = true;
109 }
110  
111 lastPoint.X = point.X;
112 lastPoint.Y = point.Y;
113 startPoint.X = point.X;
114 startPoint.Y = point.Y;
115  
116 drawArea.Capture = true;
117 drawArea.NetRectangle = DrawRectangle.GetNormalizedRectangle(startPoint, lastPoint);
118 drawArea.Refresh();
119 }
120  
121  
122 /// <summary>
123 /// Mouse is moved.
124 /// None button is pressed, ot left button is pressed.
125 /// </summary>
126 /// <param name="drawArea"></param>
127 /// <param name="e"></param>
128 public override void OnMouseMove(DrawArea drawArea, MouseEventArgs e)
129 {
130 Point point = drawArea.BackTrackMouse(new Point(e.X, e.Y));
131 int al = drawArea.TheLayers.ActiveLayerIndex;
132 wasMove = true;
133 //toolTip.InitialDelay = 1;
134  
135 // set cursor when mouse button is not pressed
136 if (e.Button ==
137 MouseButtons.None)
138 {
139 Cursor cursor = null;
140  
141 if (drawArea.TheLayers[al].Graphics != null)
142 {
143 // Hide tooltip in case it was displayed
144 //toolTip.Hide(drawArea);
145 for (int i = 0; i < drawArea.TheLayers[al].Graphics.Count; i++)
146 {
147 int n = drawArea.TheLayers[al].Graphics[i].HitTest(point);
148 if (n > 0)
149 {
150 cursor = drawArea.TheLayers[al].Graphics[i].GetHandleCursor(n);
151 break;
152 }
153 //if (n == 0)
154 // toolTip.Show(drawArea.TheLayers[al].Graphics[i].TipText, drawArea, point, 250);
155 }
156 }
157  
158 if (cursor == null)
159 cursor = Cursors.Default;
160  
161 drawArea.Cursor = cursor;
162 return;
163 }
164  
165 if (e.Button !=
166 MouseButtons.Left)
167 return;
168  
169 // Left button is pressed
170  
171 // Find difference between previous and current position
172 int dx = point.X - lastPoint.X;
173 int dy = point.Y - lastPoint.Y;
174  
175 lastPoint.X = point.X;
176 lastPoint.Y = point.Y;
177  
178 // resize
179 if (selectMode == SelectionMode.Size)
180 {
181 if (resizedObject != null)
182 {
183 resizedObject.MoveHandleTo(point, resizedObjectHandle);
184 drawArea.Refresh();
185 }
186 }
187  
188 // move
189 if (selectMode == SelectionMode.Move)
190 {
191 int n = drawArea.TheLayers[al].Graphics.SelectionCount;
192  
193 for (int i = 0; i < n; i++)
194 {
195 drawArea.TheLayers[al].Graphics.GetSelectedObject(i).Move(dx, dy);
196 }
197  
198 drawArea.Cursor = Cursors.SizeAll;
199 drawArea.Refresh();
200 }
201  
202 if (selectMode == SelectionMode.NetSelection)
203 {
204 drawArea.NetRectangle = DrawRectangle.GetNormalizedRectangle(startPoint, lastPoint);
205 drawArea.Refresh();
206 return;
207 }
208 }
209  
210 /// <summary>
211 /// Right mouse button is released
212 /// </summary>
213 /// <param name="drawArea"></param>
214 /// <param name="e"></param>
215 public override void OnMouseUp(DrawArea drawArea, MouseEventArgs e)
216 {
217 int al = drawArea.TheLayers.ActiveLayerIndex;
218 if (selectMode == SelectionMode.NetSelection)
219 {
220 // Group selection
221 drawArea.TheLayers[al].Graphics.SelectInRectangle(drawArea.NetRectangle);
222  
223 selectMode = SelectionMode.None;
224 drawArea.DrawNetRectangle = false;
225 }
226  
227 if (resizedObject != null)
228 {
229 // after resizing
230 resizedObject.Normalize();
231 resizedObject = null;
232 }
233  
234 drawArea.Capture = false;
235 drawArea.Refresh();
236  
237 if (commandChangeState != null && wasMove)
238 {
239 // Keep state after moving/resizing and add command to history
240 commandChangeState.NewState(drawArea.TheLayers);
241 drawArea.AddCommandToHistory(commandChangeState);
242 commandChangeState = null;
243 }
244 lastPoint = drawArea.BackTrackMouse(e.Location);
245 }
246  
247 #region Destruction
248 private bool _disposed = false;
249  
250 protected override void Dispose(bool disposing)
251 {
252 if (!this._disposed)
253 {
254 if (disposing)
255 {
256 // Free any managed objects here.
257 if (this.resizedObject != null)
258 {
259 this.resizedObject.Dispose();
260 }
261 if (this.commandChangeState != null)
262 {
263 this.commandChangeState.Dispose();
264 }
265 if (this.toolTip != null)
266 {
267 this.toolTip.Dispose();
268 }
269 }
270  
271 // Free any unmanaged objects here.
272  
273 this._disposed = true;
274 }
275 base.Dispose(disposing);
276 }
277  
278 ~ToolPointer()
279 {
280 this.Dispose(false);
281 }
282 #endregion
283 }
284 }