]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasEventManager.hxx
canvas: BoxLayout shared pointer typedef.
[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
34     // Used as storage by EventManager during event propagation
35     mutable osg::Vec2f  local_pos;
36
37     EventTarget( Element* el,
38                  const osg::Vec2f pos = osg::Vec2f() ):
39       element(el),
40       local_pos(pos)
41     {}
42   };
43
44   inline bool operator==(const EventTarget& t1, const EventTarget& t2)
45   {
46     return t1.element.lock() == t2.element.lock();
47   }
48
49   class EventManager
50   {
51     public:
52       EventManager();
53
54       bool handleEvent( const MouseEventPtr& event,
55                         const EventPropagationPath& path );
56
57       bool propagateEvent( const EventPtr& event,
58                            const EventPropagationPath& path );
59
60     protected:
61       struct StampedPropagationPath
62       {
63         StampedPropagationPath();
64         StampedPropagationPath(const EventPropagationPath& path, double time);
65
66         void clear();
67         bool valid() const;
68
69         EventPropagationPath path;
70         double time;
71       };
72
73       // TODO if we really need the paths modify to not copy around the paths
74       //      that much.
75       StampedPropagationPath _last_mouse_over;
76       size_t _current_click_count;
77
78       struct MouseEventInfo:
79         public StampedPropagationPath
80       {
81         int button;
82         osg::Vec2f pos;
83
84         void set( const MouseEventPtr& event,
85                   const EventPropagationPath& path );
86       } _last_mouse_down,
87         _last_click;
88
89       /**
90        * Propagate click event and handle multi-click (eg. create dblclick)
91        */
92       bool handleClick( const MouseEventPtr& event,
93                         const EventPropagationPath& path );
94
95       /**
96        * Handle mouseover/enter/out/leave
97        */
98       bool handleMove( const MouseEventPtr& event,
99                        const EventPropagationPath& path );
100
101       /**
102        * Check if two click events (either mousedown/up or two consecutive
103        * clicks) are inside a maximum distance to still create a click or
104        * dblclick event respectively.
105        */
106       bool checkClickDistance( const osg::Vec2f& pos1,
107                                const osg::Vec2f& pos2 ) const;
108       EventPropagationPath
109       getCommonAncestor( const EventPropagationPath& path1,
110                          const EventPropagationPath& path2 ) const;
111   };
112
113 } // namespace canvas
114 } // namespace simgear
115
116 #endif /* CANVAS_EVENT_MANAGER_HXX_ */