1 | using System.Drawing;
|
---|
2 | using System.Windows.Forms;
|
---|
3 |
|
---|
4 | namespace Netron.Diagramming.Core {
|
---|
5 | /// <summary>
|
---|
6 | /// The base class for a drawing tool.
|
---|
7 | /// </summary>
|
---|
8 | public abstract class AbstractDrawingTool :
|
---|
9 | AbstractTool,
|
---|
10 | IMouseListener,
|
---|
11 | IKeyboardListener {
|
---|
12 |
|
---|
13 | #region Fields
|
---|
14 |
|
---|
15 | // ------------------------------------------------------------------
|
---|
16 | /// <summary>
|
---|
17 | /// The starting point of the rectangle being drawn.
|
---|
18 | /// </summary>
|
---|
19 | // ------------------------------------------------------------------
|
---|
20 | protected Point startingPoint;
|
---|
21 |
|
---|
22 | // ------------------------------------------------------------------
|
---|
23 | /// <summary>
|
---|
24 | /// Says whether the startingPoint was set, otherwise the ghost will
|
---|
25 | /// appear even before an initial point was set!
|
---|
26 | /// </summary>
|
---|
27 | // ------------------------------------------------------------------
|
---|
28 | protected bool started;
|
---|
29 |
|
---|
30 | // ------------------------------------------------------------------
|
---|
31 | /// <summary>
|
---|
32 | /// The actual rectangle which serves as a basis for the drawing of
|
---|
33 | /// ellipses, rectangles, etc.
|
---|
34 | /// </summary>
|
---|
35 | // ------------------------------------------------------------------
|
---|
36 | private RectangleF mRectangle;
|
---|
37 |
|
---|
38 |
|
---|
39 | #endregion
|
---|
40 |
|
---|
41 | #region Properties
|
---|
42 | protected RectangleF Rectangle {
|
---|
43 | get { return mRectangle; }
|
---|
44 | set { mRectangle = value; }
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 |
|
---|
49 | #endregion
|
---|
50 |
|
---|
51 | #region Constructor
|
---|
52 | /// <summary>
|
---|
53 | /// Default constructor
|
---|
54 | /// </summary>
|
---|
55 | /// <param name="name"></param>
|
---|
56 | protected AbstractDrawingTool(string name)
|
---|
57 | : base(name) {
|
---|
58 | }
|
---|
59 | #endregion
|
---|
60 |
|
---|
61 | #region Methods
|
---|
62 |
|
---|
63 | protected override void OnActivateTool() {
|
---|
64 |
|
---|
65 | Controller.View.CurrentCursor = CursorPalette.Add;
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | protected override void OnDeactivateTool() {
|
---|
70 |
|
---|
71 | base.OnDeactivateTool();
|
---|
72 | }
|
---|
73 |
|
---|
74 | #region Explicit implementation of IKeyboardListener
|
---|
75 | void IKeyboardListener.KeyDown(KeyEventArgs e) {
|
---|
76 | OnKeyDown(e);
|
---|
77 | }
|
---|
78 |
|
---|
79 | void IKeyboardListener.KeyUp(KeyEventArgs e) {
|
---|
80 | OnKeyUp(e);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void IKeyboardListener.KeyPress(KeyPressEventArgs e) {
|
---|
84 | OnKeyPress(e);
|
---|
85 | }
|
---|
86 |
|
---|
87 | protected virtual void OnKeyDown(KeyEventArgs e) {
|
---|
88 | //if (e.Handled) return;
|
---|
89 |
|
---|
90 | if (e.KeyData == Keys.Escape) {
|
---|
91 | DeactivateTool();
|
---|
92 | Controller.View.ResetGhost();
|
---|
93 | e.Handled = true;
|
---|
94 | }
|
---|
95 | }
|
---|
96 | protected virtual void OnKeyUp(KeyEventArgs e) {
|
---|
97 | if (e.Handled) return;
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected virtual void OnKeyPress(KeyPressEventArgs e) {
|
---|
101 | if (e.Handled) return;
|
---|
102 | }
|
---|
103 | #endregion
|
---|
104 |
|
---|
105 | #region Explicit implementation of IMouseListener
|
---|
106 | bool IMouseListener.MouseDown(MouseEventArgs e) {
|
---|
107 | return OnMouseDown(e);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void IMouseListener.MouseMove(MouseEventArgs e) {
|
---|
111 | OnMouseMove(e);
|
---|
112 | }
|
---|
113 |
|
---|
114 | void IMouseListener.MouseUp(MouseEventArgs e) {
|
---|
115 | OnMouseUp(e);
|
---|
116 | }
|
---|
117 |
|
---|
118 |
|
---|
119 | protected virtual bool OnMouseDown(MouseEventArgs e) {
|
---|
120 | if (IsActive && e.Button == MouseButtons.Left) {
|
---|
121 | startingPoint = new Point(e.X, e.Y);
|
---|
122 | started = true;
|
---|
123 | return true;
|
---|
124 | }
|
---|
125 | return false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | protected virtual void OnMouseMove(MouseEventArgs e) {
|
---|
129 |
|
---|
130 | }
|
---|
131 |
|
---|
132 | protected virtual void OnMouseUp(MouseEventArgs e) {
|
---|
133 | if ((IsActive) &&
|
---|
134 | (started) &&
|
---|
135 | (Controller.View.Ghost != null)) {
|
---|
136 |
|
---|
137 | //base.RestoreCursor();
|
---|
138 | Point point = new Point(e.X, e.Y);
|
---|
139 | //mRectangle = new Rectangle(startingPoint.X, startingPoint.Y, point.X - startingPoint.X, point.Y - startingPoint.Y);
|
---|
140 | //mRectangle = base.Controller.View.ViewToWorld(base.Controller.View.DeviceToView(rectangle));
|
---|
141 | mRectangle = Controller.View.Ghost.Rectangle;
|
---|
142 | GhostDrawingComplete();
|
---|
143 | Controller.View.ResetGhost();
|
---|
144 | started = false;
|
---|
145 | }
|
---|
146 | }
|
---|
147 | #endregion
|
---|
148 |
|
---|
149 | /// <summary>
|
---|
150 | /// This method will be called when the user has finished drawing a
|
---|
151 | /// ghost rectangle or bundle and initiates the actual creation of a
|
---|
152 | /// bundle and the addition to the model via the appropriate command.
|
---|
153 | /// </summary>
|
---|
154 | protected abstract void GhostDrawingComplete();
|
---|
155 |
|
---|
156 | #endregion
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | }
|
---|