]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventManager.cxx
Canvas: clear mousedown/click if mouse leaves canvas.
[simgear.git] / simgear / canvas / CanvasEventManager.cxx
1 // Manage event handling inside a Canvas similar to the DOM Level 3 Event Model
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #include "CanvasEventManager.hxx"
20 #include "MouseEvent.hxx"
21 #include <simgear/canvas/elements/CanvasElement.hxx>
22
23 namespace simgear
24 {
25 namespace canvas
26 {
27
28   const unsigned int drag_threshold = 8;
29   const double multi_click_timeout = 0.4;
30
31   //----------------------------------------------------------------------------
32   EventManager::StampedPropagationPath::StampedPropagationPath():
33     time(0)
34   {
35
36   }
37
38   //----------------------------------------------------------------------------
39
40   EventManager::StampedPropagationPath::StampedPropagationPath(
41     const EventPropagationPath& path,
42     double time
43   ):
44     path(path),
45     time(time)
46   {
47
48   }
49
50   //----------------------------------------------------------------------------
51   void EventManager::StampedPropagationPath::clear()
52   {
53     path.clear();
54     time = 0;
55   }
56
57   //----------------------------------------------------------------------------
58   bool EventManager::StampedPropagationPath::valid() const
59   {
60     return !path.empty() && time > 0;
61   }
62
63   //----------------------------------------------------------------------------
64   EventManager::EventManager():
65     _current_click_count(0)
66   {
67
68   }
69
70   //----------------------------------------------------------------------------
71   bool EventManager::handleEvent( const MouseEventPtr& event,
72                                   const EventPropagationPath& path )
73   {
74     bool handled = false;
75     switch( event->type )
76     {
77       case Event::MOUSE_DOWN:
78         _last_mouse_down = StampedPropagationPath(path, event->getTime());
79         break;
80       case Event::MOUSE_UP:
81       {
82         // If the mouse has moved while a button was down (aka. dragging) we
83         // need to notify the original element that the mouse has left it, and
84         // the new element that it has been entered
85         if( _last_mouse_down.path != path )
86           handled |= handleMove(event, path);
87
88         // normal mouseup
89         handled |= propagateEvent(event, path);
90
91         if( _last_mouse_down.path.empty() )
92           // Ignore mouse up without any previous mouse down
93           return handled;
94
95         // now handle click/dblclick
96         if( checkClickDistance(path, _last_mouse_down.path) )
97           handled |=
98             handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
99
100         _last_mouse_down.clear();
101
102         return handled;
103       }
104       case Event::DRAG:
105         if( !_last_mouse_down.valid() )
106           return false;
107         else
108           return propagateEvent(event, _last_mouse_down.path);
109       case Event::MOUSE_MOVE:
110         handled |= handleMove(event, path);
111         break;
112       case Event::MOUSE_LEAVE:
113         // Mouse leaves window and therefore also current mouseover element
114         handleMove(event, EventPropagationPath());
115
116         // Event is only send if mouse is moved outside the window or dragging
117         // has ended somewhere outside the window. In both cases a mouse button
118         // has been released, so no more mouse down or click...
119         _last_mouse_down.clear();
120         _last_click.clear();
121
122         return true;
123       case Event::WHEEL:
124         break;
125       default:
126         return false;
127     }
128
129     return handled | propagateEvent(event, path);
130   }
131
132   //----------------------------------------------------------------------------
133   bool EventManager::handleClick( const MouseEventPtr& event,
134                                   const EventPropagationPath& path )
135   {
136     MouseEventPtr click(new MouseEvent(*event));
137     click->type = Event::CLICK;
138
139     if( event->getTime() > _last_click.time + multi_click_timeout )
140       _current_click_count = 1;
141     else
142     {
143       // Maximum current click count is 3
144       _current_click_count = (_current_click_count % 3) + 1;
145
146       if( _current_click_count > 1 )
147       {
148         // Reset current click count if moved too far
149         if( !checkClickDistance(path, _last_click.path) )
150           _current_click_count = 1;
151       }
152     }
153
154     click->click_count = _current_click_count;
155
156     MouseEventPtr dbl_click;
157     if( _current_click_count == 2 )
158     {
159       dbl_click.reset(new MouseEvent(*click));
160       dbl_click->type = Event::DBL_CLICK;
161     }
162
163     bool handled = propagateEvent(click, path);
164
165     if( dbl_click )
166       handled |= propagateEvent( dbl_click,
167                                  getCommonAncestor(_last_click.path, path) );
168
169     _last_click = StampedPropagationPath(path, event->getTime());
170
171     return handled;
172   }
173
174   //----------------------------------------------------------------------------
175   bool EventManager::handleMove( const MouseEventPtr& event,
176                                  const EventPropagationPath& path )
177   {
178     EventPropagationPath& last_path = _last_mouse_over.path;
179     if( last_path == path )
180       return false;
181
182     bool handled = false;
183
184     // Leave old element
185     if( !last_path.empty() )
186     {
187       MouseEventPtr mouseout(new MouseEvent(*event));
188       mouseout->type = Event::MOUSE_OUT;
189       handled |= propagateEvent(mouseout, last_path);
190
191       // Send a mouseleave event to all ancestors of the currently left element
192       // which are not ancestor of the new element currently entered
193       EventPropagationPath path_leave = last_path;
194       for(size_t i = path_leave.size() - 1; i > 0; --i)
195       {
196         if( i < path.size() && path[i] == path_leave[i] )
197           break;
198
199         MouseEventPtr mouseleave(new MouseEvent(*event));
200         mouseleave->type = Event::MOUSE_LEAVE;
201         handled |= propagateEvent(mouseleave, path_leave);
202
203         path_leave.pop_back();
204       }
205     }
206
207     // Enter new element
208     if( !path.empty() )
209     {
210       MouseEventPtr mouseover(new MouseEvent(*event));
211       mouseover->type = Event::MOUSE_OVER;
212       handled |= propagateEvent(mouseover, path);
213
214       // Send a mouseenter event to all ancestors of the currently entered
215       // element which are not ancestor of the old element currently being
216       // left
217       EventPropagationPath path_enter;
218       for(size_t i = 0; i < path.size(); ++i)
219       {
220         path_enter.push_back(path[i]);
221
222         if( i < last_path.size() && path[i] == last_path[i] )
223           continue;
224
225         MouseEventPtr mouseenter(new MouseEvent(*event));
226         mouseenter->type = Event::MOUSE_ENTER;
227         handled |= propagateEvent(mouseenter, path_enter);
228       }
229     }
230
231     _last_mouse_over.path = path;
232     return handled;
233   }
234
235   //----------------------------------------------------------------------------
236   bool EventManager::propagateEvent( const EventPtr& event,
237                                      const EventPropagationPath& path )
238   {
239     event->target = path.back().element;
240     MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
241
242     // Event propagation similar to DOM Level 3 event flow:
243     // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
244
245     // Capturing phase
246 //    for( EventPropagationPath::const_iterator it = path.begin();
247 //                                              it != path.end();
248 //                                            ++it )
249 //    {
250 //      if( !it->element.expired() )
251 //        std::cout << it->element.lock()->getProps()->getPath() << std::endl;
252 //    }
253
254     // Bubbling phase
255     for( EventPropagationPath::const_reverse_iterator
256            it = path.rbegin();
257            it != path.rend();
258          ++it )
259     {
260       ElementPtr el = it->element.lock();
261
262       if( !el )
263         // Ignore element if it has been destroyed while traversing the event
264         // (eg. removed by another event handler)
265         continue;
266
267       // TODO provide functions to convert delta to local coordinates on demand.
268       //      Maybe also provide a clone method for events as local coordinates
269       //      might differ between different elements receiving the same event.
270       if( mouse_event ) //&& event->type != Event::DRAG )
271       {
272         // TODO transform pos and delta for drag events. Maybe we should just
273         //      store the global coordinates and convert to local coordinates
274         //      on demand.
275
276         // Position and delta are specified in local coordinate system of
277         // current element
278         mouse_event->local_pos = it->local_pos;
279         //mouse_event->delta = it->local_delta;
280       }
281
282       el->handleEvent(event);
283
284       if( event->propagation_stopped )
285         return true;
286     }
287
288     return true;
289   }
290
291   //----------------------------------------------------------------------------
292   bool
293   EventManager::checkClickDistance( const EventPropagationPath& path1,
294                                     const EventPropagationPath& path2 ) const
295   {
296     osg::Vec2 delta = path1.front().local_pos - path2.front().local_pos;
297     return std::fabs(delta.x()) < drag_threshold
298         && std::fabs(delta.y()) < drag_threshold;
299   }
300
301   //----------------------------------------------------------------------------
302   EventPropagationPath
303   EventManager::getCommonAncestor( const EventPropagationPath& path1,
304                                    const EventPropagationPath& path2 ) const
305   {
306     if( path1.empty() || path2.empty() )
307       return EventPropagationPath();
308
309     if( path1.back().element.lock() == path2.back().element.lock() )
310       return path2;
311
312     EventPropagationPath path;
313
314     for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
315     {
316       if( path1[i].element.lock() != path2[i].element.lock() )
317         break;
318
319       path.push_back(path2[i]);
320     }
321
322     return path;
323   }
324
325 } // namespace canvas
326 } // namespace simgear