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