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 )
77 case Event::MOUSE_DOWN:
78 _last_mouse_down = StampedPropagationPath(path, event->getTime());
82 // If the mouse has moved while a button was down (aka. dragging) we
83 // need to notify the original element that the mouse has left it, and
84 // the new element that it has been entered
85 if( _last_mouse_down.path != path )
86 handled |= handleMove(event, path);
89 handled |= propagateEvent(event, path);
91 if( _last_mouse_down.path.empty() )
92 // Ignore mouse up without any previous mouse down
95 // now handle click/dblclick
96 if( checkClickDistance(path, _last_mouse_down.path) )
98 handleClick(event, getCommonAncestor(_last_mouse_down.path, path));
100 _last_mouse_down.clear();
105 if( !_last_mouse_down.valid() )
108 return propagateEvent(event, _last_mouse_down.path);
109 case Event::MOUSE_MOVE:
110 handled |= handleMove(event, path);
112 case Event::MOUSE_LEAVE:
113 // Mouse leaves window and therefore also current mouseover element
114 handleMove(event, EventPropagationPath());
122 return handled | propagateEvent(event, path);
125 //----------------------------------------------------------------------------
126 bool EventManager::handleClick( const MouseEventPtr& event,
127 const EventPropagationPath& path )
129 MouseEventPtr click(new MouseEvent(*event));
130 click->type = Event::CLICK;
132 if( event->getTime() > _last_click.time + multi_click_timeout )
133 _current_click_count = 1;
136 // Maximum current click count is 3
137 _current_click_count = (_current_click_count % 3) + 1;
139 if( _current_click_count > 1 )
141 // Reset current click count if moved too far
142 if( !checkClickDistance(path, _last_click.path) )
143 _current_click_count = 1;
147 click->click_count = _current_click_count;
149 MouseEventPtr dbl_click;
150 if( _current_click_count == 2 )
152 dbl_click.reset(new MouseEvent(*click));
153 dbl_click->type = Event::DBL_CLICK;
156 bool handled = propagateEvent(click, path);
159 handled |= propagateEvent( dbl_click,
160 getCommonAncestor(_last_click.path, path) );
162 _last_click = StampedPropagationPath(path, event->getTime());
167 //----------------------------------------------------------------------------
168 bool EventManager::handleMove( const MouseEventPtr& event,
169 const EventPropagationPath& path )
171 EventPropagationPath& last_path = _last_mouse_over.path;
172 if( last_path == path )
175 bool handled = false;
178 if( !last_path.empty() )
180 MouseEventPtr mouseout(new MouseEvent(*event));
181 mouseout->type = Event::MOUSE_OUT;
182 handled |= propagateEvent(mouseout, last_path);
184 // Send a mouseleave event to all ancestors of the currently left element
185 // which are not ancestor of the new element currently entered
186 EventPropagationPath path_leave = last_path;
187 for(size_t i = path_leave.size() - 1; i > 0; --i)
189 if( i < path.size() && path[i] == path_leave[i] )
192 MouseEventPtr mouseleave(new MouseEvent(*event));
193 mouseleave->type = Event::MOUSE_LEAVE;
194 handled |= propagateEvent(mouseleave, path_leave);
196 path_leave.pop_back();
203 MouseEventPtr mouseover(new MouseEvent(*event));
204 mouseover->type = Event::MOUSE_OVER;
205 handled |= propagateEvent(mouseover, path);
207 // Send a mouseenter event to all ancestors of the currently entered
208 // element which are not ancestor of the old element currently being
210 EventPropagationPath path_enter;
211 for(size_t i = 0; i < path.size(); ++i)
213 path_enter.push_back(path[i]);
215 if( i < last_path.size() && path[i] == last_path[i] )
218 MouseEventPtr mouseenter(new MouseEvent(*event));
219 mouseenter->type = Event::MOUSE_ENTER;
220 handled |= propagateEvent(mouseenter, path_enter);
224 _last_mouse_over.path = path;
228 //----------------------------------------------------------------------------
229 bool EventManager::propagateEvent( const EventPtr& event,
230 const EventPropagationPath& path )
232 event->target = path.back().element;
233 MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
235 // Event propagation similar to DOM Level 3 event flow:
236 // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
239 // for( EventPropagationPath::const_iterator it = path.begin();
243 // if( !it->element.expired() )
244 // std::cout << it->element.lock()->getProps()->getPath() << std::endl;
248 for( EventPropagationPath::const_reverse_iterator
253 ElementPtr el = it->element.lock();
256 // Ignore element if it has been destroyed while traversing the event
257 // (eg. removed by another event handler)
260 // TODO provide functions to convert delta to local coordinates on demand.
261 // Maybe also provide a clone method for events as local coordinates
262 // might differ between different elements receiving the same event.
263 if( mouse_event ) //&& event->type != Event::DRAG )
265 // TODO transform pos and delta for drag events. Maybe we should just
266 // store the global coordinates and convert to local coordinates
269 // Position and delta are specified in local coordinate system of
271 mouse_event->local_pos = it->local_pos;
272 //mouse_event->delta = it->local_delta;
275 el->handleEvent(event);
277 if( event->propagation_stopped )
284 //----------------------------------------------------------------------------
286 EventManager::checkClickDistance( const EventPropagationPath& path1,
287 const EventPropagationPath& path2 ) const
289 osg::Vec2 delta = path1.front().local_pos - path2.front().local_pos;
290 return std::fabs(delta.x()) < drag_threshold
291 && std::fabs(delta.y()) < drag_threshold;
294 //----------------------------------------------------------------------------
296 EventManager::getCommonAncestor( const EventPropagationPath& path1,
297 const EventPropagationPath& path2 ) const
299 if( path1.empty() || path2.empty() )
300 return EventPropagationPath();
302 if( path1.back().element.lock() == path2.back().element.lock() )
305 EventPropagationPath path;
307 for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
309 if( path1[i].element.lock() != path2[i].element.lock() )
312 path.push_back(path2[i]);
318 } // namespace canvas
319 } // namespace simgear