1 // Manage event handling inside a Canvas similar to the DOM Level 3 Event Model
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #include "CanvasEventManager.hxx"
20 #include <simgear/canvas/events/MouseEvent.hxx>
21 #include <simgear/canvas/elements/CanvasElement.hxx>
29 const unsigned int drag_threshold = 8;
30 const double multi_click_timeout = 0.4;
32 //----------------------------------------------------------------------------
33 EventManager::StampedPropagationPath::StampedPropagationPath():
39 //----------------------------------------------------------------------------
41 EventManager::StampedPropagationPath::StampedPropagationPath(
42 const EventPropagationPath& path,
51 //----------------------------------------------------------------------------
52 void EventManager::StampedPropagationPath::clear()
58 //----------------------------------------------------------------------------
59 bool EventManager::StampedPropagationPath::valid() const
61 return !path.empty() && time > 0;
64 //----------------------------------------------------------------------------
65 void EventManager::MouseEventInfo::set( const MouseEventPtr& event,
66 const EventPropagationPath& p )
70 button = event->button;
71 pos = event->screen_pos;
74 //----------------------------------------------------------------------------
75 EventManager::EventManager():
76 _current_click_count(0)
81 //----------------------------------------------------------------------------
82 bool EventManager::handleEvent( const MouseEventPtr& event,
83 const EventPropagationPath& path )
88 case Event::MOUSE_DOWN:
89 _last_mouse_down.set(event, path);
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);
100 handled |= propagateEvent(event, path);
102 if( !_last_mouse_down.valid() )
103 // Ignore mouse up without any previous mouse down
106 // now handle click/dblclick
107 if( checkClickDistance(event->screen_pos, _last_mouse_down.pos) )
109 handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
111 _last_mouse_down.clear();
116 if( !_last_mouse_down.valid() )
120 // OSG does not set button for drag events.
121 event->button = _last_mouse_down.button;
122 return propagateEvent(event, _last_mouse_down.path);
124 case Event::MOUSE_MOVE:
125 handled |= handleMove(event, path);
127 case Event::MOUSE_LEAVE:
128 // Mouse leaves window and therefore also current mouseover element
129 handleMove(event, EventPropagationPath());
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();
144 return handled | propagateEvent(event, path);
147 //----------------------------------------------------------------------------
148 bool EventManager::propagateEvent( const EventPtr& event,
149 const EventPropagationPath& path )
151 event->target = path.back().element;
152 MouseEventPtr mouse_event = dynamic_cast<MouseEvent*>(event.get());
154 // Event propagation similar to DOM Level 3 event flow:
155 // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
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 )
161 osg::Vec2f local_pos = mouse_event->client_pos;
163 // Capturing phase (currently just update position)
164 for( EventPropagationPath::const_iterator it = path.begin();
168 ElementPtr el = it->element.lock();
172 it->local_pos = local_pos = el->posToLocal(local_pos);
176 bool const do_bubble = event->canBubble();
179 for( EventPropagationPath::const_reverse_iterator
184 ElementPtr el = it->element.lock();
188 // Ignore element if it has been destroyed while traversing the event
189 // (eg. removed by another event handler)
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.
200 mouse_event->local_pos = it->local_pos;
202 event->current_target = el;
203 el->handleEvent(event);
205 if( event->propagation_stopped || !do_bubble )
212 //----------------------------------------------------------------------------
213 bool EventManager::handleClick( const MouseEventPtr& event,
214 const EventPropagationPath& path )
216 MouseEventPtr click(new MouseEvent(*event));
217 click->type = Event::CLICK;
219 if( event->getTime() > _last_click.time + multi_click_timeout )
220 _current_click_count = 1;
223 // Maximum current click count is 3
224 _current_click_count = (_current_click_count % 3) + 1;
226 if( _current_click_count > 1 )
228 // Reset current click count if moved too far or different button has
230 if( !checkClickDistance(event->screen_pos, _last_click.pos)
231 || _last_click.button != event->button )
232 _current_click_count = 1;
236 click->click_count = _current_click_count;
238 MouseEventPtr dbl_click;
239 if( _current_click_count == 2 )
241 dbl_click.reset(new MouseEvent(*click));
242 dbl_click->type = Event::DBL_CLICK;
245 bool handled = propagateEvent(click, path);
248 handled |= propagateEvent( dbl_click,
249 getCommonAncestor(_last_click.path, path) );
251 _last_click.set(event, path);
256 //----------------------------------------------------------------------------
257 bool EventManager::handleMove( const MouseEventPtr& event,
258 const EventPropagationPath& path )
260 EventPropagationPath& last_path = _last_mouse_over.path;
261 if( last_path == path )
264 bool handled = false;
267 if( !last_path.empty() )
269 MouseEventPtr mouseout(new MouseEvent(*event));
270 mouseout->type = Event::MOUSE_OUT;
271 handled |= propagateEvent(mouseout, last_path);
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)
278 if( i < path.size() && path[i] == path_leave[i] )
281 MouseEventPtr mouseleave(new MouseEvent(*event));
282 mouseleave->type = Event::MOUSE_LEAVE;
283 handled |= propagateEvent(mouseleave, path_leave);
285 path_leave.pop_back();
292 MouseEventPtr mouseover(new MouseEvent(*event));
293 mouseover->type = Event::MOUSE_OVER;
294 handled |= propagateEvent(mouseover, path);
296 // Send a mouseenter event to all ancestors of the currently entered
297 // element which are not ancestor of the old element currently being
299 EventPropagationPath path_enter;
300 for(size_t i = 0; i < path.size(); ++i)
302 path_enter.push_back(path[i]);
304 if( i < last_path.size() && path[i] == last_path[i] )
307 MouseEventPtr mouseenter(new MouseEvent(*event));
308 mouseenter->type = Event::MOUSE_ENTER;
309 handled |= propagateEvent(mouseenter, path_enter);
313 _last_mouse_over.path = path;
317 //----------------------------------------------------------------------------
319 EventManager::checkClickDistance( const osg::Vec2f& pos1,
320 const osg::Vec2f& pos2 ) const
322 osg::Vec2 delta = pos1 - pos2;
323 return std::fabs(delta.x()) < drag_threshold
324 && std::fabs(delta.y()) < drag_threshold;
327 //----------------------------------------------------------------------------
329 EventManager::getCommonAncestor( const EventPropagationPath& path1,
330 const EventPropagationPath& path2 ) const
332 if( path1.empty() || path2.empty() )
333 return EventPropagationPath();
335 if( path1.back().element.lock() == path2.back().element.lock() )
338 EventPropagationPath path;
340 for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
342 if( path1[i].element.lock() != path2[i].element.lock() )
345 path.push_back(path2[i]);
351 } // namespace canvas
352 } // namespace simgear