]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventManager.cxx
Rename simgear::Rect to SGRect and make interface more similar to SGBox
[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::WHEEL:
102       case Event::MOUSE_MOVE:
103         break;
104       default:
105         return false;
106     }
107
108     return propagateEvent(event, path);
109   }
110
111   //----------------------------------------------------------------------------
112   void EventManager::handleClick( const MouseEventPtr& event,
113                                   const EventPropagationPath& path )
114   {
115     MouseEventPtr click(new MouseEvent(*event));
116     click->type = Event::CLICK;
117
118     if( event->getTime() > _last_click.time + multi_click_timeout )
119       _current_click_count = 1;
120     else
121     {
122       // Maximum current click count is 3
123       _current_click_count = (_current_click_count % 3) + 1;
124
125       if( _current_click_count > 1 )
126       {
127         // Reset current click count if moved too far
128         if( !checkClickDistance(path, _last_click.path) )
129           _current_click_count = 1;
130       }
131     }
132
133     click->click_count = _current_click_count;
134
135     MouseEventPtr dbl_click;
136     if( _current_click_count == 2 )
137     {
138       dbl_click.reset(new MouseEvent(*click));
139       dbl_click->type = Event::DBL_CLICK;
140     }
141
142     propagateEvent(click, path);
143
144     if( dbl_click )
145       propagateEvent(dbl_click, getCommonAncestor(_last_click.path, path));
146
147     _last_click = StampedPropagationPath(path, event->getTime());
148   }
149
150   //----------------------------------------------------------------------------
151   bool EventManager::propagateEvent( const EventPtr& event,
152                                      const EventPropagationPath& path )
153   {
154     event->target = path.back().element;
155     MouseEventPtr mouse_event = boost::dynamic_pointer_cast<MouseEvent>(event);
156
157     // Event propagation similar to DOM Level 3 event flow:
158     // http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
159
160     // Capturing phase
161 //    for( EventTargets::iterator it = _target_path.begin();
162 //                                it != _target_path.end();
163 //                              ++it )
164 //    {
165 //      if( it->element )
166 //        std::cout << it->element->getProps()->getPath() << " "
167 //                  << "(" << it->local_pos.x() << "|" << it->local_pos.y() << ")\n";
168 //    }
169
170     // Bubbling phase
171     for( EventPropagationPath::const_reverse_iterator
172            it = path.rbegin();
173            it != path.rend();
174          ++it )
175     {
176       ElementPtr el = it->element.lock();
177
178       if( !el )
179         // Ignore element if it has been destroyed while traversing the event
180         // (eg. removed by another event handler)
181         continue;
182
183       // TODO provide functions to convert position and delta to local
184       //      coordinates on demand. Events shouldn't contain informations in
185       //      local coordinates as they might differe between different elements
186       //      receiving the same event.
187 //      if( mouse_event && event->type != Event::DRAG )
188 //      {
189 //        // TODO transform pos and delta for drag events. Maybe we should just
190 //        //      store the global coordinates and convert to local coordinates
191 //        //      on demand.
192 //
193 //        // Position and delta are specified in local coordinate system of
194 //        // current element
195 //        mouse_event->pos = it->local_pos;
196 //        mouse_event->delta = it->local_delta;
197 //      }
198
199       el->callListeners(event);
200
201       if( event->propagation_stopped )
202         return true;
203     }
204
205     return true;
206   }
207
208   //----------------------------------------------------------------------------
209   bool
210   EventManager::checkClickDistance( const EventPropagationPath& path1,
211                                     const EventPropagationPath& path2 ) const
212   {
213     osg::Vec2 delta = path1.front().local_pos - path2.front().local_pos;
214     return delta.x() < drag_threshold
215         && delta.y() < drag_threshold;
216   }
217
218   //----------------------------------------------------------------------------
219   EventPropagationPath
220   EventManager::getCommonAncestor( const EventPropagationPath& path1,
221                                    const EventPropagationPath& path2 ) const
222   {
223     if( path1.back().element.lock() == path2.back().element.lock() )
224       return path2;
225
226     EventPropagationPath path;
227
228     for( size_t i = 0; i < path1.size() && i < path2.size(); ++i )
229     {
230       if( path1[i].element.lock() != path2[i].element.lock() )
231         break;
232
233       path.push_back(path2[i]);
234     }
235
236     return path;
237   }
238
239 } // namespace canvas
240 } // namespace simgear