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