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 | // ----------------------------------------------------------------------
|
---|
10 | /// <summary>
|
---|
11 | /// This tool allows entities to give feedback (e.g. via a change of
|
---|
12 | /// cursor) when the mouse is hovering over them.
|
---|
13 | /// </summary>
|
---|
14 | // ----------------------------------------------------------------------
|
---|
15 | class HoverTool : AbstractTool, IMouseListener
|
---|
16 | {
|
---|
17 |
|
---|
18 | #region Fields
|
---|
19 |
|
---|
20 | private IHoverListener currentListener = null;
|
---|
21 |
|
---|
22 | private IDiagramEntity previousHovered = null;
|
---|
23 |
|
---|
24 |
|
---|
25 | #endregion
|
---|
26 |
|
---|
27 | #region Constructor
|
---|
28 |
|
---|
29 | // ------------------------------------------------------------------
|
---|
30 | /// <summary>
|
---|
31 | /// Initializes a new instance of the <see cref="T:HoverTool"/> class.
|
---|
32 | /// </summary>
|
---|
33 | /// <param name="name">The name of the tool.</param>
|
---|
34 | // ------------------------------------------------------------------
|
---|
35 | public HoverTool(string name)
|
---|
36 | : base(name)
|
---|
37 | {
|
---|
38 | }
|
---|
39 |
|
---|
40 | #endregion
|
---|
41 |
|
---|
42 | #region Methods
|
---|
43 |
|
---|
44 | // ------------------------------------------------------------------
|
---|
45 | /// <summary>
|
---|
46 | /// Called when the tool is activated.
|
---|
47 | /// </summary>
|
---|
48 | // ------------------------------------------------------------------
|
---|
49 | protected override void OnActivateTool()
|
---|
50 | {
|
---|
51 |
|
---|
52 |
|
---|
53 | }
|
---|
54 |
|
---|
55 | // ------------------------------------------------------------------
|
---|
56 | /// <summary>
|
---|
57 | /// Handles the mouse down event.
|
---|
58 | /// </summary>
|
---|
59 | /// <param name="e">The <see cref=
|
---|
60 | /// "T:System.Windows.Forms.MouseEventArgs"/> instance containing
|
---|
61 | /// the event data.</param>
|
---|
62 | // ------------------------------------------------------------------
|
---|
63 | public bool MouseDown(MouseEventArgs e)
|
---|
64 | {
|
---|
65 | return false;
|
---|
66 | }
|
---|
67 |
|
---|
68 | // ------------------------------------------------------------------
|
---|
69 | /// <summary>
|
---|
70 | /// Handles the mouse move event
|
---|
71 | /// </summary>
|
---|
72 | /// <param name="e">The <see cref=
|
---|
73 | /// "T:System.Windows.Forms.MouseEventArgs"/> instance containing
|
---|
74 | /// the event data.</param>
|
---|
75 | // ------------------------------------------------------------------
|
---|
76 | public void MouseMove(MouseEventArgs e)
|
---|
77 | {
|
---|
78 |
|
---|
79 | if (!IsSuspended && this.Enabled)
|
---|
80 | {
|
---|
81 | IHoverListener listener = null;
|
---|
82 |
|
---|
83 | CollectionBase<IDiagramEntity> paintables=
|
---|
84 | this.Controller.Model.Paintables;
|
---|
85 | IDiagramEntity entity;
|
---|
86 | if(paintables.Count==0) return;
|
---|
87 | //going from top to the bottom of the z-order
|
---|
88 | for (int k=paintables.Count-1; k>=0; k--)
|
---|
89 | {
|
---|
90 | entity = paintables[k];
|
---|
91 | if(entity.Hit(e.Location)) //we caught an entity
|
---|
92 | {
|
---|
93 | //unhover the previous, if any
|
---|
94 | if (previousHovered != null)
|
---|
95 | {
|
---|
96 | previousHovered.Hovered = false;
|
---|
97 | }
|
---|
98 |
|
---|
99 | //tell the current one it's being hovered
|
---|
100 | entity.Hovered = true;
|
---|
101 |
|
---|
102 | //fetch the hovering service, if defined
|
---|
103 | listener = entity.GetService(
|
---|
104 | typeof(IHoverListener)) as IHoverListener;
|
---|
105 | if(listener != null) //the caught entity does listen
|
---|
106 | {
|
---|
107 | if(currentListener == listener) //it's the same as the previous time
|
---|
108 | listener.MouseHover(e);
|
---|
109 | else //we moved from one entity to another listening entity
|
---|
110 | {
|
---|
111 | if(currentListener!=null) //tell the previous entity we are leaving
|
---|
112 | currentListener.MouseLeave(e);
|
---|
113 | listener.MouseEnter(e); //tell the current one we enter
|
---|
114 | currentListener = listener;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | else //the caught entity does not listen
|
---|
118 | {
|
---|
119 | if(currentListener != null)
|
---|
120 | {
|
---|
121 | currentListener.MouseLeave(e);
|
---|
122 | currentListener = null;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | previousHovered = entity;//remember, for the next time
|
---|
126 | return; //if another entity is listening underneath this entity it will not receive the notification
|
---|
127 | }
|
---|
128 | }
|
---|
129 | if(currentListener != null)
|
---|
130 | {
|
---|
131 | currentListener.MouseLeave(e);
|
---|
132 | currentListener = null;
|
---|
133 | }
|
---|
134 | //unhover the previous, if any
|
---|
135 | if(previousHovered != null)
|
---|
136 | previousHovered.Hovered = false;
|
---|
137 |
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | // ------------------------------------------------------------------
|
---|
142 | /// <summary>
|
---|
143 | /// Handles the mouse up event.
|
---|
144 | /// </summary>
|
---|
145 | /// <param name="e">The <see cref=
|
---|
146 | /// "T:System.Windows.Forms.MouseEventArgs"/> instance containing
|
---|
147 | /// the event data.</param>
|
---|
148 | // ------------------------------------------------------------------
|
---|
149 | public void MouseUp(MouseEventArgs e)
|
---|
150 | {
|
---|
151 |
|
---|
152 | }
|
---|
153 | #endregion
|
---|
154 | }
|
---|
155 |
|
---|
156 | }
|
---|