]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasElement.hxx
Implement Canvas single/double/tripple click handling.
[simgear.git] / simgear / canvas / elements / CanvasElement.hxx
1 // Interface for 2D Canvas elements
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_ELEMENT_HXX_
20 #define CANVAS_ELEMENT_HXX_
21
22 #include <simgear/canvas/canvas_fwd.hxx>
23 #include <simgear/canvas/CanvasEvent.hxx>
24 #include <simgear/props/PropertyBasedElement.hxx>
25 #include <simgear/misc/stdint.hxx> // for uint32_t
26 #include <simgear/nasal/cppbind/Ghost.hxx>
27
28 #include <osg/BoundingBox>
29 #include <osg/MatrixTransform>
30
31 #include <boost/bind.hpp>
32 #include <boost/function.hpp>
33
34 namespace osg
35 {
36   class Drawable;
37 }
38
39 namespace simgear
40 {
41 namespace canvas
42 {
43
44   class Element:
45     public PropertyBasedElement
46   {
47     public:
48       typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
49       typedef std::map<std::string, StyleSetter> StyleSetters;
50
51       /**
52        * Remove the property listener of the element.
53        *
54        * You will need to call the appropriate methods (#childAdded,
55        * #childRemoved, #valueChanged) yourself to ensure the element still
56        * receives the needed events.
57        */
58       void removeListener();
59
60       /**
61        *
62        */
63       virtual ~Element() = 0;
64
65       ElementWeakPtr getWeakPtr() const;
66
67       /**
68        * Called every frame to update internal state
69        *
70        * @param dt  Frame time in seconds
71        */
72       virtual void update(double dt);
73
74       naRef addEventListener(const nasal::CallContext& ctx);
75
76       virtual bool accept(EventVisitor& visitor);
77       virtual bool ascend(EventVisitor& visitor);
78       virtual bool traverse(EventVisitor& visitor);
79
80       void callListeners(const canvas::EventPtr& event);
81
82       virtual bool hitBound(const osg::Vec2f& pos) const;
83
84
85       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
86
87       virtual void childAdded( SGPropertyNode * parent,
88                                SGPropertyNode * child );
89       virtual void childRemoved( SGPropertyNode * parent,
90                                  SGPropertyNode * child );
91       virtual void valueChanged(SGPropertyNode * child);
92
93       virtual bool setStyle(const SGPropertyNode* child);
94
95       /**
96        * Write the given bounding box to the property tree
97        */
98       void setBoundingBox(const osg::BoundingBox& bb);
99
100       /**
101        * Get bounding box with children/drawables transformed by passed matrix
102        */
103       virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
104
105     protected:
106
107       enum Attributes
108       {
109         LAST_ATTRIBUTE  = 0x0001
110       };
111
112       enum TransformType
113       {
114         TT_NONE,
115         TT_MATRIX,
116         TT_TRANSLATE,
117         TT_ROTATE,
118         TT_SCALE
119       };
120
121       CanvasWeakPtr _canvas;
122       Element      *_parent;
123
124       uint32_t _attributes_dirty;
125
126       bool _transform_dirty;
127       osg::ref_ptr<osg::MatrixTransform>    _transform;
128       std::vector<TransformType>            _transform_types;
129
130       Style                             _style;
131       StyleSetters                      _style_setters;
132       std::vector<SGPropertyNode_ptr>   _bounding_box;
133
134       typedef std::vector<EventListenerPtr> Listener;
135       typedef std::map<Event::Type, Listener> ListenerMap;
136
137       ListenerMap _listener;
138
139       Element( const CanvasWeakPtr& canvas,
140                const SGPropertyNode_ptr& node,
141                const Style& parent_style,
142                Element* parent );
143
144       template<typename T, class C1, class C2>
145       Element::StyleSetter
146       addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
147       {
148         return _style_setters[ name ] =
149                  bindStyleSetter<T>(name, setter, instance);
150       }
151
152       template<typename T1, typename T2, class C1, class C2>
153       Element::StyleSetter
154       addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
155       {
156         return _style_setters[ name ] =
157                  bindStyleSetter<T1>(name, setter, instance);
158       }
159
160       template<class C1, class C2>
161       Element::StyleSetter
162       addStyle( const std::string& name,
163                 void (C1::*setter)(const std::string&),
164                 C2 instance )
165       {
166         return _style_setters[ name ] =
167                  bindStyleSetter<const char*>(name, setter, instance);
168       }
169
170       template<typename T1, typename T2, class C1, class C2>
171       Element::StyleSetter
172       bindStyleSetter( const std::string& name,
173                        void (C1::*setter)(T2),
174                        C2 instance )
175       {
176         return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
177       }
178
179       virtual void childAdded(SGPropertyNode * child)  {}
180       virtual void childRemoved(SGPropertyNode * child){}
181       virtual void childChanged(SGPropertyNode * child){}
182
183       void setDrawable(osg::Drawable* drawable);
184
185       void setupStyle();
186
187     private:
188
189       osg::ref_ptr<osg::Drawable> _drawable;
190
191       Element(const Element&);// = delete
192   };
193
194 } // namespace canvas
195 } // namespace simgear
196
197 #endif /* CANVAS_ELEMENT_HXX_ */