1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Collections;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Drawing;
|
---|
6 | using System.Windows.Forms;
|
---|
7 |
|
---|
8 | namespace Netron.Diagramming.Core
|
---|
9 | {
|
---|
10 |
|
---|
11 | class ScribbleTool : AbstractTool, IMouseListener, IKeyboardListener
|
---|
12 | {
|
---|
13 |
|
---|
14 | #region Fields
|
---|
15 | /// <summary>
|
---|
16 | /// the location of the mouse when the motion starts
|
---|
17 | /// </summary>
|
---|
18 |
|
---|
19 | private bool doDraw;
|
---|
20 | private Point[] points;
|
---|
21 | #endregion
|
---|
22 | #region Constructor
|
---|
23 | /// <summary>
|
---|
24 | /// Initializes a new instance of the <see cref="T:ScribbleTool"/> class.
|
---|
25 | /// </summary>
|
---|
26 | /// <param name="name">The name of the tool.</param>
|
---|
27 | public ScribbleTool(string name)
|
---|
28 | : base(name)
|
---|
29 | {
|
---|
30 | }
|
---|
31 | #endregion
|
---|
32 |
|
---|
33 | #region Methods
|
---|
34 |
|
---|
35 | /// <summary>
|
---|
36 | /// Called when the tool is activated.
|
---|
37 | /// </summary>
|
---|
38 | protected override void OnActivateTool()
|
---|
39 | {
|
---|
40 | Controller.View.CurrentCursor = CursorPalette.Cross;
|
---|
41 | this.SuspendOtherTools();
|
---|
42 | doDraw = false;
|
---|
43 | points = new Point[1];
|
---|
44 | }
|
---|
45 |
|
---|
46 | /// <summary>
|
---|
47 | /// Handles the mouse down event
|
---|
48 | /// </summary>
|
---|
49 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
50 | /// <returns>Returns 'true' if the event was handled, otherwise 'false'.</returns>
|
---|
51 | public bool MouseDown(MouseEventArgs e)
|
---|
52 | {
|
---|
53 | if (e == null)
|
---|
54 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
55 | if (e.Button == MouseButtons.Left && Enabled && !IsSuspended)
|
---|
56 | {
|
---|
57 | Point p = e.Location;
|
---|
58 | if (Closure(p))
|
---|
59 | {
|
---|
60 | points[points.Length - 1] = points[0];
|
---|
61 | doDraw = false;
|
---|
62 | DeactivateTool();
|
---|
63 | Package();
|
---|
64 |
|
---|
65 | return true;
|
---|
66 | }
|
---|
67 | Array.Resize<Point>(ref points, points.Length + 1);
|
---|
68 | points[points.Length - 2] = e.Location;
|
---|
69 | doDraw = true;
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 | return false;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// Handles the mouse move event
|
---|
77 | /// </summary>
|
---|
78 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
79 | public void MouseMove(MouseEventArgs e)
|
---|
80 | {
|
---|
81 | if (e == null)
|
---|
82 | throw new ArgumentNullException("The argument object is 'null'");
|
---|
83 | Point point = e.Location;
|
---|
84 | if (IsActive && doDraw)
|
---|
85 | {
|
---|
86 | points[points.Length - 1] = e.Location;
|
---|
87 | Controller.View.PaintGhostLine(MultiPointType.Curve, points);
|
---|
88 | //Controller.View.Invalidate(System.Drawing.Rectangle.Inflate(Controller.View.Ghost.Rectangle, 20, 20));
|
---|
89 | //TODO: find a more performant way to invalidate the area
|
---|
90 | Controller.View.Invalidate();
|
---|
91 | }
|
---|
92 | }
|
---|
93 | private bool Closure(Point p)
|
---|
94 | {
|
---|
95 | for (int k = 0; k < points.Length - 1; k++)
|
---|
96 | {
|
---|
97 | if (new Rectangle(points[k].X - 6, points[k].Y - 6, 12, 12).Contains(p))
|
---|
98 | {
|
---|
99 | DialogResult res = MessageBox.Show("Do you wish to close the curve?", "Closure?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
|
---|
100 | if (res == DialogResult.Yes)
|
---|
101 | return true;
|
---|
102 |
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | /// <summary>
|
---|
108 | ///
|
---|
109 | /// </summary>
|
---|
110 | /// <param name="e">The <see cref="T:System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
|
---|
111 | public void MouseUp(MouseEventArgs e)
|
---|
112 | {
|
---|
113 | if (IsActive)
|
---|
114 | {
|
---|
115 |
|
---|
116 | }
|
---|
117 | }
|
---|
118 | private void Package()
|
---|
119 | {
|
---|
120 | if ((points == null) ||
|
---|
121 | (points.Length < 1) ||
|
---|
122 | (points[0] == Point.Empty) )
|
---|
123 | {
|
---|
124 | return;
|
---|
125 | }
|
---|
126 |
|
---|
127 | MultiPointShape shape = new MultiPointShape(
|
---|
128 | this.Controller.Model,
|
---|
129 | points,
|
---|
130 | MultiPointType.Curve);
|
---|
131 |
|
---|
132 | AddMultiPointShapeCommand cmd = new AddMultiPointShapeCommand(this.Controller, shape);
|
---|
133 | this.Controller.UndoManager.AddUndoCommand(cmd);
|
---|
134 | this.Controller.View.ResetGhost();
|
---|
135 | cmd.Redo();
|
---|
136 | }
|
---|
137 | #endregion
|
---|
138 |
|
---|
139 | #region IKeyboardListener Members
|
---|
140 |
|
---|
141 | public void KeyDown(KeyEventArgs e)
|
---|
142 | {
|
---|
143 |
|
---|
144 |
|
---|
145 | if (e.KeyData == System.Windows.Forms.Keys.Escape && IsActive)
|
---|
146 | {
|
---|
147 | DeactivateTool();
|
---|
148 | Package();
|
---|
149 | e.Handled = true;
|
---|
150 | }
|
---|
151 | }
|
---|
152 |
|
---|
153 | public void KeyUp(KeyEventArgs e)
|
---|
154 | {
|
---|
155 | }
|
---|
156 |
|
---|
157 | public void KeyPress(KeyPressEventArgs e)
|
---|
158 | {
|
---|
159 | }
|
---|
160 |
|
---|
161 | #endregion
|
---|
162 | }
|
---|
163 |
|
---|
164 | }
|
---|