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