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 "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::handleClick( const MouseEventPtr& event,
149 const EventPropagationPath& path )
151 MouseEventPtr click(new MouseEvent(*event));
152 click->type = Event::CLICK;
154 if( event->getTime() > _last_click.time + multi_click_timeout )
155 _current_click_count = 1;
158 // Maximum current click count is 3
159 _current_click_count = (_current_click_count % 3) + 1;
161 if( _current_click_count > 1 )
163 // Reset current click count if moved too far or different button has
165 if( !checkClickDistance(event->screen_pos, _last_click.pos)
166 || _last_click.button != event->button )
167 _current_click_count = 1;
171 click->click_count = _current_click_count;
173 MouseEventPtr dbl_click;
174 if( _current_click_count == 2 )
176 dbl_click.reset(new MouseEvent(*click));
177 dbl_click->type = Event::DBL_CLICK;
180 bool handled = propagateEvent(click, path);
183 handled |= propagateEvent( dbl_click,
184 getCommonAncestor(_last_click.path, path) );
186 _last_click.set(event, path);
191 //----------------------------------------------------------------------------
192 bool EventManager::handleMove( const MouseEventPtr& event,
193 const EventPropagationPath& path )
195 EventPropagationPath& last_path = _last_mouse_over.path;
196 if( last_path == path )
199 bool handled = false;
202 if( !last_path.empty() )
204 MouseEventPtr mouseout(new MouseEvent(*event));
205 mouseout->type = Event::MOUSE_OUT;
206 handled |= propagateEvent(mouseout, last_path);
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)
213 if( i < path.size() && path[i] == path_leave[i] )
216 MouseEventPtr mouseleave(new MouseEvent(*event));
217 mouseleave->type = Event::MOUSE_LEAVE;
218 handled |= propagateEvent(mouseleave, path_leave);
220 path_leave.pop_back();
227 MouseEventPtr mouseover(new MouseEvent(*event));
228 mouseover->type = Event::MOUSE_OVER;
229 handled |= propagateEvent(mouseover, path);
231 // Send a mouseenter event to all ancestors of the currently entered
232 // element which are not ancestor of the old element currently being
234 EventPropagationPath path_enter;
235 for(size_t i = 0; i < path.size(); ++i)
237 path_enter.push_back(path[i]);
239 if( i < last_path.size() && path[i] == last_path[i] )
242 MouseEventPtr mouseenter(new MouseEvent(*event));
243 mouseenter->type = Event::MOUSE_ENTER;
244 handled |= propagateEvent(mouseenter, path_enter);
248 _last_mouse_over.path = path;
252 //----------------------------------------------------------------------------
253 bool EventManager::propagateEvent( const EventPtr& event,
254 const EventPropagationPath& path )
256 event->target = path.back().element;
257 MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
259 // Event propagation similar to DOM Level 3 event flow:
260 // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
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 )
266 osg::Vec2f local_pos = mouse_event->client_pos;
268 // Capturing phase (currently just update position)
269 for( EventPropagationPath::const_iterator it = path.begin();
273 ElementPtr el = it->element.lock();
277 it->local_pos = local_pos = el->posToLocal(local_pos);
281 // Check if event supports bubbling
282 const Event::Type types_no_bubbling[] = {
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] )
297 for( EventPropagationPath::const_reverse_iterator
302 ElementPtr el = it->element.lock();
306 // Ignore element if it has been destroyed while traversing the event
307 // (eg. removed by another event handler)
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.
318 mouse_event->local_pos = it->local_pos;
320 event->current_target = el;
321 el->handleEvent(event);
323 if( event->propagation_stopped || !do_bubble )
330 //----------------------------------------------------------------------------
332 EventManager::checkClickDistance( const osg::Vec2f& pos1,
333 const osg::Vec2f& pos2 ) const
335 osg::Vec2 delta = pos1 - pos2;
336 return std::fabs(delta.x()) < drag_threshold
337 && std::fabs(delta.y()) < drag_threshold;
340 //----------------------------------------------------------------------------
342 EventManager::getCommonAncestor( const EventPropagationPath& path1,
343 const EventPropagationPath& path2 ) const
345 if( path1.empty() || path2.empty() )
346 return EventPropagationPath();
348 if( path1.back().element.lock() == path2.back().element.lock() )
351 EventPropagationPath path;
353 for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
355 if( path1[i].element.lock() != path2[i].element.lock() )
358 path.push_back(path2[i]);
364 } // namespace canvas
365 } // namespace simgear