]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasElement.hxx
Clean up Canvas element creation
[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       /**
71        * Handle mouse event (transforms coordinates to local coordinate frame
72        * and forwards event to #handleLocalMouseEvent)
73        */
74       virtual bool handleMouseEvent(const canvas::MouseEvent& event);
75
76       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
77
78       virtual void childAdded( SGPropertyNode * parent,
79                                SGPropertyNode * child );
80       virtual void childRemoved( SGPropertyNode * parent,
81                                  SGPropertyNode * child );
82       virtual void valueChanged(SGPropertyNode * child);
83
84       /**
85        * Write the given bounding box to the property tree
86        */
87       void setBoundingBox(const osg::BoundingBox& bb);
88
89     protected:
90
91       enum Attributes
92       {
93         LAST_ATTRIBUTE  = 0x0001
94       };
95
96       enum TransformType
97       {
98         TT_NONE,
99         TT_MATRIX,
100         TT_TRANSLATE,
101         TT_ROTATE,
102         TT_SCALE
103       };
104
105       CanvasWeakPtr _canvas;
106       uint32_t _attributes_dirty;
107
108       bool _transform_dirty;
109       osg::ref_ptr<osg::MatrixTransform>    _transform;
110       std::vector<TransformType>            _transform_types;
111
112       SGPropertyNode_ptr                _node;
113       Style                             _style;
114       StyleSetters                      _style_setters;
115       std::vector<SGPropertyNode_ptr>   _bounding_box;
116
117       Element( const CanvasWeakPtr& canvas,
118                const SGPropertyNode_ptr& node,
119                const Style& parent_style );
120
121       template<typename T, class C1, class C2>
122       Element::StyleSetter
123       addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
124       {
125         return _style_setters[ name ] =
126                  bindStyleSetter<T>(name, setter, instance);
127       }
128
129       template<typename T1, typename T2, class C1, class C2>
130       Element::StyleSetter
131       addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
132       {
133         return _style_setters[ name ] =
134                  bindStyleSetter<T1>(name, setter, instance);
135       }
136
137       template<class C1, class C2>
138       Element::StyleSetter
139       addStyle( const std::string& name,
140                 void (C1::*setter)(const std::string&),
141                 C2 instance )
142       {
143         return _style_setters[ name ] =
144                  bindStyleSetter<const char*>(name, setter, instance);
145       }
146
147       template<typename T1, typename T2, class C1, class C2>
148       Element::StyleSetter
149       bindStyleSetter( const std::string& name,
150                        void (C1::*setter)(T2),
151                        C2 instance )
152       {
153         return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
154       }
155
156       virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
157
158       virtual void childAdded(SGPropertyNode * child)  {}
159       virtual void childRemoved(SGPropertyNode * child){}
160       virtual void childChanged(SGPropertyNode * child){}
161
162       void setDrawable(osg::Drawable* drawable);
163
164       void setupStyle();
165       bool setStyle(const SGPropertyNode* child);
166
167     private:
168
169       osg::ref_ptr<osg::Drawable> _drawable;
170
171       Element(const Element&);// = delete
172   };
173
174 } // namespace canvas
175 } // namespace simgear
176
177 #endif /* CANVAS_ELEMENT_HXX_ */