]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventManager.cxx
canvas::Text: add heightForWidth method.
[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 <simgear/canvas/events/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::propagateEvent( const EventPtr& event,
149                                      const EventPropagationPath& path )
150   {
151     event->target = path.back().element;
152     MouseEventPtr mouse_event = dynamic_cast<MouseEvent*>(event.get());
153
154     // Event propagation similar to DOM Level 3 event flow:
155     // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
156
157     // Position update only needed for drag event (as event needs to be
158     // delivered to element of initial mousedown, but with update positions)
159     if( mouse_event && mouse_event->type == MouseEvent::DRAG )
160     {
161       osg::Vec2f local_pos = mouse_event->client_pos;
162
163       // Capturing phase (currently just update position)
164       for( EventPropagationPath::const_iterator it = path.begin();
165                                                 it != path.end();
166                                               ++it )
167       {
168         ElementPtr el = it->element.lock();
169         if( !el )
170           continue;
171
172         it->local_pos = local_pos = el->posToLocal(local_pos);
173       }
174     }
175
176     bool const do_bubble = event->canBubble();
177
178     // Bubbling phase
179     for( EventPropagationPath::const_reverse_iterator
180            it = path.rbegin();
181            it != path.rend();
182          ++it )
183     {
184       ElementPtr el = it->element.lock();
185
186       if( !el )
187       {
188         // Ignore element if it has been destroyed while traversing the event
189         // (eg. removed by another event handler)
190         if( do_bubble )
191           continue;
192         else
193           break;
194       }
195
196       // TODO provide functions to convert delta to local coordinates on demand.
197       //      Maybe also provide a clone method for events as local coordinates
198       //      might differ between different elements receiving the same event.
199       if( mouse_event )
200         mouse_event->local_pos = it->local_pos;
201
202       event->current_target = el;
203       el->handleEvent(event);
204
205       if( event->propagation_stopped || !do_bubble )
206         return true;
207     }
208
209     return true;
210   }
211
212   //----------------------------------------------------------------------------
213   bool EventManager::handleClick( const MouseEventPtr& event,
214                                   const EventPropagationPath& path )
215   {
216     MouseEventPtr click(new MouseEvent(*event));
217     click->type = Event::CLICK;
218
219     if( event->getTime() > _last_click.time + multi_click_timeout )
220       _current_click_count = 1;
221     else
222     {
223       // Maximum current click count is 3
224       _current_click_count = (_current_click_count % 3) + 1;
225
226       if( _current_click_count > 1 )
227       {
228         // Reset current click count if moved too far or different button has
229         // been clicked
230         if(   !checkClickDistance(event->screen_pos, _last_click.pos)
231             || _last_click.button != event->button )
232           _current_click_count = 1;
233       }
234     }
235
236     click->click_count = _current_click_count;
237
238     MouseEventPtr dbl_click;
239     if( _current_click_count == 2 )
240     {
241       dbl_click.reset(new MouseEvent(*click));
242       dbl_click->type = Event::DBL_CLICK;
243     }
244
245     bool handled = propagateEvent(click, path);
246
247     if( dbl_click )
248       handled |= propagateEvent( dbl_click,
249                                  getCommonAncestor(_last_click.path, path) );
250
251     _last_click.set(event, path);
252
253     return handled;
254   }
255
256   //----------------------------------------------------------------------------
257   bool EventManager::handleMove( const MouseEventPtr& event,
258                                  const EventPropagationPath& path )
259   {
260     EventPropagationPath& last_path = _last_mouse_over.path;
261     if( last_path == path )
262       return false;
263
264     bool handled = false;
265
266     // Leave old element
267     if( !last_path.empty() )
268     {
269       MouseEventPtr mouseout(new MouseEvent(*event));
270       mouseout->type = Event::MOUSE_OUT;
271       handled |= propagateEvent(mouseout, last_path);
272
273       // Send a mouseleave event to all ancestors of the currently left element
274       // which are not ancestor of the new element currently entered
275       EventPropagationPath path_leave = last_path;
276       for(size_t i = path_leave.size() - 1; i > 0; --i)
277       {
278         if( i < path.size() && path[i] == path_leave[i] )
279           break;
280
281         MouseEventPtr mouseleave(new MouseEvent(*event));
282         mouseleave->type = Event::MOUSE_LEAVE;
283         handled |= propagateEvent(mouseleave, path_leave);
284
285         path_leave.pop_back();
286       }
287     }
288
289     // Enter new element
290     if( !path.empty() )
291     {
292       MouseEventPtr mouseover(new MouseEvent(*event));
293       mouseover->type = Event::MOUSE_OVER;
294       handled |= propagateEvent(mouseover, path);
295
296       // Send a mouseenter event to all ancestors of the currently entered
297       // element which are not ancestor of the old element currently being
298       // left
299       EventPropagationPath path_enter;
300       for(size_t i = 0; i < path.size(); ++i)
301       {
302         path_enter.push_back(path[i]);
303
304         if( i < last_path.size() && path[i] == last_path[i] )
305           continue;
306
307         MouseEventPtr mouseenter(new MouseEvent(*event));
308         mouseenter->type = Event::MOUSE_ENTER;
309         handled |= propagateEvent(mouseenter, path_enter);
310       }
311     }
312
313     _last_mouse_over.path = path;
314     return handled;
315   }
316
317   //----------------------------------------------------------------------------
318   bool
319   EventManager::checkClickDistance( const osg::Vec2f& pos1,
320                                     const osg::Vec2f& pos2 ) const
321   {
322     osg::Vec2 delta = pos1 - pos2;
323     return std::fabs(delta.x()) < drag_threshold
324         && std::fabs(delta.y()) < drag_threshold;
325   }
326
327   //----------------------------------------------------------------------------
328   EventPropagationPath
329   EventManager::getCommonAncestor( const EventPropagationPath& path1,
330                                    const EventPropagationPath& path2 ) const
331   {
332     if( path1.empty() || path2.empty() )
333       return EventPropagationPath();
334
335     if( path1.back().element.lock() == path2.back().element.lock() )
336       return path2;
337
338     EventPropagationPath path;
339
340     for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
341     {
342       if( path1[i].element.lock() != path2[i].element.lock() )
343         break;
344
345       path.push_back(path2[i]);
346     }
347
348     return path;
349   }
350
351 } // namespace canvas
352 } // namespace simgear