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