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>
28 const unsigned int drag_threshold = 8;
29 const double multi_click_timeout = 0.4;
31 //----------------------------------------------------------------------------
32 EventManager::StampedPropagationPath::StampedPropagationPath():
38 //----------------------------------------------------------------------------
40 EventManager::StampedPropagationPath::StampedPropagationPath(
41 const EventPropagationPath& path,
50 //----------------------------------------------------------------------------
51 void EventManager::StampedPropagationPath::clear()
57 //----------------------------------------------------------------------------
58 bool EventManager::StampedPropagationPath::valid() const
60 return !path.empty() && time > 0;
63 //----------------------------------------------------------------------------
64 EventManager::EventManager():
65 _current_click_count(0)
70 //----------------------------------------------------------------------------
71 bool EventManager::handleEvent( const MouseEventPtr& event,
72 const EventPropagationPath& path )
76 case Event::MOUSE_DOWN:
77 _last_mouse_down = StampedPropagationPath(path, event->getTime());
81 if( _last_mouse_down.path.empty() )
82 // Ignore mouse up without any previous mouse down
86 propagateEvent(event, path);
88 // now handle click/dblclick
89 if( checkClickDistance(path, _last_mouse_down.path) )
90 handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
92 _last_mouse_down.clear();
97 if( !_last_mouse_down.valid() )
100 return propagateEvent(event, _last_mouse_down.path);
101 case Event::MOUSE_MOVE:
102 handleMove(event, path);
104 case Event::MOUSE_LEAVE:
105 // Mouse leaves window and therefore also current mouseover element
106 handleMove(event, EventPropagationPath());
114 return propagateEvent(event, path);
117 //----------------------------------------------------------------------------
118 void EventManager::handleClick( const MouseEventPtr& event,
119 const EventPropagationPath& path )
121 MouseEventPtr click(new MouseEvent(*event));
122 click->type = Event::CLICK;
124 if( event->getTime() > _last_click.time + multi_click_timeout )
125 _current_click_count = 1;
128 // Maximum current click count is 3
129 _current_click_count = (_current_click_count % 3) + 1;
131 if( _current_click_count > 1 )
133 // Reset current click count if moved too far
134 if( !checkClickDistance(path, _last_click.path) )
135 _current_click_count = 1;
139 click->click_count = _current_click_count;
141 MouseEventPtr dbl_click;
142 if( _current_click_count == 2 )
144 dbl_click.reset(new MouseEvent(*click));
145 dbl_click->type = Event::DBL_CLICK;
148 propagateEvent(click, path);
151 propagateEvent(dbl_click, getCommonAncestor(_last_click.path, path));
153 _last_click = StampedPropagationPath(path, event->getTime());
156 //----------------------------------------------------------------------------
157 void EventManager::handleMove( const MouseEventPtr& event,
158 const EventPropagationPath& path )
160 EventPropagationPath& last_path = _last_mouse_over.path;
161 if( last_path == path )
165 if( !last_path.empty() )
167 MouseEventPtr mouseout(new MouseEvent(*event));
168 mouseout->type = Event::MOUSE_OUT;
169 propagateEvent(mouseout, last_path);
171 // Send a mouseleave event to all ancestors of the currently left element
172 // which are not ancestor of the new element currently entered
173 EventPropagationPath path_leave = last_path;
174 for(size_t i = path_leave.size() - 1; i > 0; --i)
176 if( i < path.size() && path[i] == path_leave[i] )
179 MouseEventPtr mouseleave(new MouseEvent(*event));
180 mouseleave->type = Event::MOUSE_LEAVE;
181 propagateEvent(mouseleave, path_leave);
183 path_leave.pop_back();
190 MouseEventPtr mouseover(new MouseEvent(*event));
191 mouseover->type = Event::MOUSE_OVER;
192 propagateEvent(mouseover, path);
194 // Send a mouseenter event to all ancestors of the currently entered
195 // element which are not ancestor of the old element currently being
197 EventPropagationPath path_enter;
198 for(size_t i = 0; i < path.size(); ++i)
200 path_enter.push_back(path[i]);
202 if( i < last_path.size() && path[i] == last_path[i] )
205 MouseEventPtr mouseenter(new MouseEvent(*event));
206 mouseenter->type = Event::MOUSE_ENTER;
207 propagateEvent(mouseenter, path_enter);
211 _last_mouse_over.path = path;
214 //----------------------------------------------------------------------------
215 bool EventManager::propagateEvent( const EventPtr& event,
216 const EventPropagationPath& path )
218 event->target = path.back().element;
219 MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
221 // Event propagation similar to DOM Level 3 event flow:
222 // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
225 // for( EventPropagationPath::const_iterator it = path.begin();
229 // if( !it->element.expired() )
230 // std::cout << it->element.lock()->getProps()->getPath() << std::endl;
234 for( EventPropagationPath::const_reverse_iterator
239 ElementPtr el = it->element.lock();
242 // Ignore element if it has been destroyed while traversing the event
243 // (eg. removed by another event handler)
246 // TODO provide functions to convert delta to local coordinates on demand.
247 // Maybe also provide a clone method for events as local coordinates
248 // might differ between different elements receiving the same event.
249 if( mouse_event ) //&& event->type != Event::DRAG )
251 // TODO transform pos and delta for drag events. Maybe we should just
252 // store the global coordinates and convert to local coordinates
255 // Position and delta are specified in local coordinate system of
257 mouse_event->local_pos = it->local_pos;
258 //mouse_event->delta = it->local_delta;
261 el->handleEvent(event);
263 if( event->propagation_stopped )
270 //----------------------------------------------------------------------------
272 EventManager::checkClickDistance( const EventPropagationPath& path1,
273 const EventPropagationPath& path2 ) const
275 osg::Vec2 delta = path1.front().local_pos - path2.front().local_pos;
276 return delta.x() < drag_threshold
277 && delta.y() < drag_threshold;
280 //----------------------------------------------------------------------------
282 EventManager::getCommonAncestor( const EventPropagationPath& path1,
283 const EventPropagationPath& path2 ) const
285 if( path1.empty() || path2.empty() )
286 return EventPropagationPath();
288 if( path1.back().element.lock() == path2.back().element.lock() )
291 EventPropagationPath path;
293 for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
295 if( path1[i].element.lock() != path2[i].element.lock() )
298 path.push_back(path2[i]);
304 } // namespace canvas
305 } // namespace simgear