1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Drawing;
|
---|
5 | using System.Windows.Forms;
|
---|
6 |
|
---|
7 | namespace Netron.Diagramming.Core
|
---|
8 | {
|
---|
9 | /// <summary>
|
---|
10 | /// This tool implement the action of moving shape connectors on the canvas.
|
---|
11 | /// </summary>
|
---|
12 | class ConnectorMoverTool : AbstractTool, IMouseListener
|
---|
13 | {
|
---|
14 |
|
---|
15 | #region Fields
|
---|
16 | /// <summary>
|
---|
17 | /// the location of the mouse when the motion starts
|
---|
18 | /// </summary>
|
---|
19 | private Point initialPoint;
|
---|
20 | /// <summary>
|
---|
21 | /// the intermediate location of the mouse during the motion
|
---|
22 | /// </summary>
|
---|
23 | private Point lastPoint;
|
---|
24 | /// <summary>
|
---|
25 | /// the connector being moved
|
---|
26 | /// </summary>
|
---|
27 | private IConnector fetchedConnector;
|
---|
28 | /// <summary>
|
---|
29 | /// whether we are currently moving something
|
---|
30 | /// </summary>
|
---|
31 | private bool motionStarted;
|
---|
32 |
|
---|
33 | #endregion
|
---|
34 |
|
---|
35 | #region Constructor
|
---|
36 | /// <summary>
|
---|
37 | /// Initializes a new instance of the <see cref="T:ConnectorMoverTool"/> class.
|
---|
38 | /// </summary>
|
---|
39 | /// <param name="name">The name of the tool.</param>
|
---|
40 | public ConnectorMoverTool(string name) : base(name)
|
---|
41 | {
|
---|
42 | }
|
---|
43 | #endregion
|
---|
44 |
|
---|
45 | #region Methods
|
---|
46 |
|
---|
47 | /// <summary>
|
---|
48 | /// Called when the tool is activated.
|
---|
49 | /// </summary>
|
---|
50 | protected override void OnActivateTool()
|
---|
51 | {
|
---|
52 | Controller.View.CurrentCursor = CursorPalette.Select;
|
---|
53 | motionStarted = false;
|
---|
54 | fetchedConnector = null;
|
---|
55 | }
|
---|
56 |
|
---|
57 | /// <summary>
|
---|
58 | /// Handles the mouse down event
|
---|
59 | /// </summary>
|
---|
60 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
61 | public bool MouseDown(MouseEventArgs e)
|
---|
62 | {
|
---|
63 | if (e == null)
|
---|
64 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
65 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended)
|
---|
66 | {
|
---|
67 | fetchedConnector = Selection.FindShapeConnector(e.Location);
|
---|
68 | if(fetchedConnector != null)
|
---|
69 | {
|
---|
70 |
|
---|
71 | initialPoint = e.Location;
|
---|
72 | lastPoint = initialPoint;
|
---|
73 | motionStarted = true;
|
---|
74 | return true;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /// <summary>
|
---|
81 | /// Handles the mouse move event
|
---|
82 | /// </summary>
|
---|
83 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
84 | public void MouseMove(MouseEventArgs e)
|
---|
85 | {
|
---|
86 | if (e == null)
|
---|
87 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
88 | Point point = e.Location;
|
---|
89 | if(IsActive && motionStarted)
|
---|
90 | {
|
---|
91 |
|
---|
92 | fetchedConnector.MoveBy(new Point(point.X - lastPoint.X, point.Y - lastPoint.Y));
|
---|
93 |
|
---|
94 | lastPoint = point;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | /// <summary>
|
---|
98 | /// Handles the mouse up event
|
---|
99 | /// </summary>
|
---|
100 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
101 | public void MouseUp(MouseEventArgs e)
|
---|
102 | {
|
---|
103 | if (IsActive)
|
---|
104 | {
|
---|
105 | DeactivateTool();
|
---|
106 | if(fetchedConnector == null)
|
---|
107 | return;
|
---|
108 | Bundle bundle = new Bundle(Controller.Model);
|
---|
109 | bundle.Entities.Add(fetchedConnector);
|
---|
110 | MoveCommand cmd = new MoveCommand(this.Controller, bundle, new Point(lastPoint.X - initialPoint.X, lastPoint.Y - initialPoint.Y));
|
---|
111 | Controller.UndoManager.AddUndoCommand(cmd);
|
---|
112 | //not necessary to perform the Redo action of the command since the mouse-move already moved the bundle!
|
---|
113 | }
|
---|
114 | }
|
---|
115 | #endregion
|
---|
116 | }
|
---|
117 |
|
---|
118 | }
|
---|