]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasElement.hxx
Nasal bindings: Always pass object by reference
[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/props/props.hxx>
24 #include <simgear/misc/stdint.hxx> // for uint32_t
25 #include <osg/BoundingBox>
26 #include <osg/MatrixTransform>
27
28 #include <boost/bind.hpp>
29 #include <boost/function.hpp>
30
31 namespace osg
32 {
33   class Drawable;
34 }
35
36 namespace simgear
37 {
38 namespace canvas
39 {
40
41   class MouseEvent;
42   class Element:
43     public SGPropertyChangeListener
44   {
45     public:
46       typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
47       typedef std::map<std::string, StyleSetter> StyleSetters;
48
49       /**
50        * Remove the property listener of the element.
51        *
52        * You will need to call the appropriate methods (#childAdded,
53        * #childRemoved, #valueChanged) yourself to ensure the element still
54        * receives the needed events.
55        */
56       void removeListener();
57
58       /**
59        *
60        */
61       virtual ~Element() = 0;
62
63       /**
64        * Called every frame to update internal state
65        *
66        * @param dt  Frame time in seconds
67        */
68       virtual void update(double dt);
69
70       SGConstPropertyNode_ptr getProps() const;
71       SGPropertyNode_ptr getProps();
72
73       /**
74        * Handle mouse event (transforms coordinates to local coordinate frame
75        * and forwards event to #handleLocalMouseEvent)
76        */
77       virtual bool handleMouseEvent(const canvas::MouseEvent& event);
78
79       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
80
81       virtual void childAdded( SGPropertyNode * parent,
82                                SGPropertyNode * child );
83       virtual void childRemoved( SGPropertyNode * parent,
84                                  SGPropertyNode * child );
85       virtual void valueChanged(SGPropertyNode * child);
86
87       /**
88        * Write the given bounding box to the property tree
89        */
90       void setBoundingBox(const osg::BoundingBox& bb);
91
92     protected:
93
94       enum Attributes
95       {
96         LAST_ATTRIBUTE  = 0x0001
97       };
98
99       enum TransformType
100       {
101         TT_NONE,
102         TT_MATRIX,
103         TT_TRANSLATE,
104         TT_ROTATE,
105         TT_SCALE
106       };
107
108       CanvasWeakPtr _canvas;
109       uint32_t _attributes_dirty;
110
111       bool _transform_dirty;
112       osg::ref_ptr<osg::MatrixTransform>    _transform;
113       std::vector<TransformType>            _transform_types;
114
115       SGPropertyNode_ptr                _node;
116       Style                             _style;
117       StyleSetters                      _style_setters;
118       std::vector<SGPropertyNode_ptr>   _bounding_box;
119
120       Element( const CanvasWeakPtr& canvas,
121                const SGPropertyNode_ptr& node,
122                const Style& parent_style );
123
124       template<typename T, class C1, class C2>
125       Element::StyleSetter
126       addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
127       {
128         return _style_setters[ name ] =
129                  bindStyleSetter<T>(name, setter, instance);
130       }
131
132       template<typename T1, typename T2, class C1, class C2>
133       Element::StyleSetter
134       addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
135       {
136         return _style_setters[ name ] =
137                  bindStyleSetter<T1>(name, setter, instance);
138       }
139
140       template<class C1, class C2>
141       Element::StyleSetter
142       addStyle( const std::string& name,
143                 void (C1::*setter)(const std::string&),
144                 C2 instance )
145       {
146         return _style_setters[ name ] =
147                  bindStyleSetter<const char*>(name, setter, instance);
148       }
149
150       template<typename T1, typename T2, class C1, class C2>
151       Element::StyleSetter
152       bindStyleSetter( const std::string& name,
153                        void (C1::*setter)(T2),
154                        C2 instance )
155       {
156         return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
157       }
158
159       virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
160
161       virtual void childAdded(SGPropertyNode * child)  {}
162       virtual void childRemoved(SGPropertyNode * child){}
163       virtual void childChanged(SGPropertyNode * child){}
164
165       void setDrawable(osg::Drawable* drawable);
166
167       void setupStyle();
168       bool setStyle(const SGPropertyNode* child);
169
170     private:
171
172       osg::ref_ptr<osg::Drawable> _drawable;
173
174       Element(const Element&);// = delete
175   };
176
177 } // namespace canvas
178 } // namespace simgear
179
180 #endif /* CANVAS_ELEMENT_HXX_ */