]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventManager.cxx
Canvas: trigger missing events on mouseup.
[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     switch( event->type )
75     {
76       case Event::MOUSE_DOWN:
77         _last_mouse_down = StampedPropagationPath(path, event->getTime());
78         break;
79       case Event::MOUSE_UP:
80       {
81         // If the mouse has moved while a button was down (aka. dragging) we
82         // need to notify the original element that the mouse has left it, and
83         // the new element that it has been entered
84         if( _last_mouse_down.path != path )
85           handleMove(event, path);
86
87         // normal mouseup
88         propagateEvent(event, path);
89
90         if( _last_mouse_down.path.empty() )
91           // Ignore mouse up without any previous mouse down
92           return false;
93
94         // now handle click/dblclick
95         if( checkClickDistance(path, _last_mouse_down.path) )
96           handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
97
98         _last_mouse_down.clear();
99
100         return true;
101       }
102       case Event::DRAG:
103         if( !_last_mouse_down.valid() )
104           return false;
105         else
106           return propagateEvent(event, _last_mouse_down.path);
107       case Event::MOUSE_MOVE:
108         handleMove(event, path);
109         break;
110       case Event::MOUSE_LEAVE:
111         // Mouse leaves window and therefore also current mouseover element
112         handleMove(event, EventPropagationPath());
113         return true;
114       case Event::WHEEL:
115         break;
116       default:
117         return false;
118     }
119
120     return propagateEvent(event, path);
121   }
122
123   //----------------------------------------------------------------------------
124   void EventManager::handleClick( const MouseEventPtr& event,
125                                   const EventPropagationPath& path )
126   {
127     MouseEventPtr click(new MouseEvent(*event));
128     click->type = Event::CLICK;
129
130     if( event->getTime() > _last_click.time + multi_click_timeout )
131       _current_click_count = 1;
132     else
133     {
134       // Maximum current click count is 3
135       _current_click_count = (_current_click_count % 3) + 1;
136
137       if( _current_click_count > 1 )
138       {
139         // Reset current click count if moved too far
140         if( !checkClickDistance(path, _last_click.path) )
141           _current_click_count = 1;
142       }
143     }
144
145     click->click_count = _current_click_count;
146
147     MouseEventPtr dbl_click;
148     if( _current_click_count == 2 )
149     {
150       dbl_click.reset(new MouseEvent(*click));
151       dbl_click->type = Event::DBL_CLICK;
152     }
153
154     propagateEvent(click, path);
155
156     if( dbl_click )
157       propagateEvent(dbl_click, getCommonAncestor(_last_click.path, path));
158
159     _last_click = StampedPropagationPath(path, event->getTime());
160   }
161
162   //----------------------------------------------------------------------------
163   void EventManager::handleMove( const MouseEventPtr& event,
164                                  const EventPropagationPath& path )
165   {
166     EventPropagationPath& last_path = _last_mouse_over.path;
167     if( last_path == path )
168       return;
169
170     // Leave old element
171     if( !last_path.empty() )
172     {
173       MouseEventPtr mouseout(new MouseEvent(*event));
174       mouseout->type = Event::MOUSE_OUT;
175       propagateEvent(mouseout, last_path);
176
177       // Send a mouseleave event to all ancestors of the currently left element
178       // which are not ancestor of the new element currently entered
179       EventPropagationPath path_leave = last_path;
180       for(size_t i = path_leave.size() - 1; i > 0; --i)
181       {
182         if( i < path.size() && path[i] == path_leave[i] )
183           break;
184
185         MouseEventPtr mouseleave(new MouseEvent(*event));
186         mouseleave->type = Event::MOUSE_LEAVE;
187         propagateEvent(mouseleave, path_leave);
188
189         path_leave.pop_back();
190       }
191     }
192
193     // Enter new element
194     if( !path.empty() )
195     {
196       MouseEventPtr mouseover(new MouseEvent(*event));
197       mouseover->type = Event::MOUSE_OVER;
198       propagateEvent(mouseover, path);
199
200       // Send a mouseenter event to all ancestors of the currently entered
201       // element which are not ancestor of the old element currently being
202       // left
203       EventPropagationPath path_enter;
204       for(size_t i = 0; i < path.size(); ++i)
205       {
206         path_enter.push_back(path[i]);
207
208         if( i < last_path.size() && path[i] == last_path[i] )
209           continue;
210
211         MouseEventPtr mouseenter(new MouseEvent(*event));
212         mouseenter->type = Event::MOUSE_ENTER;
213         propagateEvent(mouseenter, path_enter);
214       }
215     }
216
217     _last_mouse_over.path = path;
218   }
219
220   //----------------------------------------------------------------------------
221   bool EventManager::propagateEvent( const EventPtr& event,
222                                      const EventPropagationPath& path )
223   {
224     event->target = path.back().element;
225     MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
226
227     // Event propagation similar to DOM Level 3 event flow:
228     // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
229
230     // Capturing phase
231 //    for( EventPropagationPath::const_iterator it = path.begin();
232 //                                              it != path.end();
233 //                                            ++it )
234 //    {
235 //      if( !it->element.expired() )
236 //        std::cout << it->element.lock()->getProps()->getPath() << std::endl;
237 //    }
238
239     // Bubbling phase
240     for( EventPropagationPath::const_reverse_iterator
241            it = path.rbegin();
242            it != path.rend();
243          ++it )
244     {
245       ElementPtr el = it->element.lock();
246
247       if( !el )
248         // Ignore element if it has been destroyed while traversing the event
249         // (eg. removed by another event handler)
250         continue;
251
252       // TODO provide functions to convert delta to local coordinates on demand.
253       //      Maybe also provide a clone method for events as local coordinates
254       //      might differ between different elements receiving the same event.
255       if( mouse_event ) //&& event->type != Event::DRAG )
256       {
257         // TODO transform pos and delta for drag events. Maybe we should just
258         //      store the global coordinates and convert to local coordinates
259         //      on demand.
260
261         // Position and delta are specified in local coordinate system of
262         // current element
263         mouse_event->local_pos = it->local_pos;
264         //mouse_event->delta = it->local_delta;
265       }
266
267       el->handleEvent(event);
268
269       if( event->propagation_stopped )
270         return true;
271     }
272
273     return true;
274   }
275
276   //----------------------------------------------------------------------------
277   bool
278   EventManager::checkClickDistance( const EventPropagationPath& path1,
279                                     const EventPropagationPath& path2 ) const
280   {
281     osg::Vec2 delta = path1.front().local_pos - path2.front().local_pos;
282     return delta.x() < drag_threshold
283         && delta.y() < drag_threshold;
284   }
285
286   //----------------------------------------------------------------------------
287   EventPropagationPath
288   EventManager::getCommonAncestor( const EventPropagationPath& path1,
289                                    const EventPropagationPath& path2 ) const
290   {
291     if( path1.empty() || path2.empty() )
292       return EventPropagationPath();
293
294     if( path1.back().element.lock() == path2.back().element.lock() )
295       return path2;
296
297     EventPropagationPath path;
298
299     for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
300     {
301       if( path1[i].element.lock() != path2[i].element.lock() )
302         break;
303
304       path.push_back(path2[i]);
305     }
306
307     return path;
308   }
309
310 } // namespace canvas
311 } // namespace simgear