]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventManager.hxx
Implement Canvas single/double/tripple click handling.
[simgear.git] / simgear / canvas / CanvasEventManager.hxx
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 #ifndef CANVAS_EVENT_MANAGER_HXX_
20 #define CANVAS_EVENT_MANAGER_HXX_
21
22 #include "canvas_fwd.hxx"
23 #include <deque>
24
25 namespace simgear
26 {
27 namespace canvas
28 {
29
30   struct EventTarget
31   {
32     ElementWeakPtr  element;
33     osg::Vec2f      local_pos,
34                     local_delta;
35   };
36   typedef std::deque<EventTarget> EventPropagationPath;
37
38   class EventManager
39   {
40     public:
41       EventManager();
42
43       bool handleEvent( const MouseEventPtr& event,
44                         const EventPropagationPath& path );
45
46     protected:
47       struct StampedPropagationPath
48       {
49         StampedPropagationPath():
50           time(0)
51         {}
52
53         StampedPropagationPath(const EventPropagationPath& path, double time):
54           path(path),
55           time(time)
56         {}
57
58         EventPropagationPath path;
59         double time;
60       };
61
62       // TODO if we really need the paths modify to not copy around the paths
63       //      that much.
64       StampedPropagationPath _last_mouse_down,
65                              _last_click;
66       size_t _current_click_count;
67
68       /**
69        * Propagate click event and handle multi-click (eg. create dblclick)
70        */
71       void handleClick( const MouseEventPtr& event,
72                         const EventPropagationPath& path );
73
74       bool propagateEvent( const EventPtr& event,
75                            const EventPropagationPath& path );
76
77       /**
78        * Check if two click events (either mousedown/up or two consecutive
79        * clicks) are inside a maximum distance to still create a click or
80        * dblclick event respectively.
81        */
82       bool checkClickDistance( const EventPropagationPath& path1,
83                                const EventPropagationPath& path2 ) const;
84       EventPropagationPath
85       getCommonAncestor( const EventPropagationPath& path1,
86                          const EventPropagationPath& path2 ) const;
87   };
88
89 } // namespace canvas
90 } // namespace simgear
91
92 #endif /* CANVAS_EVENT_MANAGER_HXX_ */