]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/elements/element.hxx
Fix a Clang warning in Shiva.
[flightgear.git] / src / Canvas / elements / element.hxx
1 // Interface for 2D canvas element
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18
19 #ifndef CANVAS_ELEMENT_HXX_
20 #define CANVAS_ELEMENT_HXX_
21
22 #include <simgear/props/props.hxx>
23 #include <simgear/misc/stdint.hxx> // for uint32_t
24 #include <osg/MatrixTransform>
25
26 #include <boost/bind.hpp>
27 #include <boost/function.hpp>
28
29 namespace osg
30 {
31   class Drawable;
32 }
33
34 namespace canvas
35 {
36
37   class MouseEvent;
38   class Element:
39     public SGPropertyChangeListener
40   {
41     public:
42       typedef std::map<std::string, const SGPropertyNode*> Style;
43       typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
44       typedef std::map<std::string, StyleSetter> StyleSetters;
45
46       void removeListener();
47       virtual ~Element() = 0;
48
49       /**
50        * Called every frame to update internal state
51        *
52        * @param dt  Frame time in seconds
53        */
54       virtual void update(double dt);
55
56       virtual bool handleMouseEvent(const canvas::MouseEvent& event);
57
58       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
59
60       virtual void childAdded( SGPropertyNode * parent,
61                                SGPropertyNode * child );
62       virtual void childRemoved( SGPropertyNode * parent,
63                                  SGPropertyNode * child );
64       virtual void valueChanged(SGPropertyNode * child);
65
66     protected:
67
68       enum Attributes
69       {
70         BOUNDING_BOX    = 0x0001,
71         LAST_ATTRIBUTE  = BOUNDING_BOX
72       };
73
74       enum TransformType
75       {
76         TT_NONE,
77         TT_MATRIX,
78         TT_TRANSLATE,
79         TT_ROTATE,
80         TT_SCALE
81       };
82
83       uint32_t _attributes_used;
84       uint32_t _attributes_dirty;
85
86       bool _transform_dirty;
87       osg::ref_ptr<osg::MatrixTransform>    _transform;
88       std::vector<TransformType>            _transform_types;
89
90       SGPropertyNode_ptr                _node;
91       Style                             _style;
92       StyleSetters                      _style_setters;
93       std::vector<SGPropertyNode_ptr>   _bounding_box;
94
95       Element( SGPropertyNode_ptr node,
96                const Style& parent_style,
97                uint32_t attributes_used = 0 );
98
99       template<typename T, class C1, class C2>
100       Element::StyleSetter
101       addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
102       {
103         return _style_setters[ name ] =
104                  bindStyleSetter<T>(name, setter, instance);
105       }
106
107       template<typename T1, typename T2, class C1, class C2>
108       Element::StyleSetter
109       addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
110       {
111         return _style_setters[ name ] =
112                  bindStyleSetter<T1>(name, setter, instance);
113       }
114
115       template<class C1, class C2>
116       Element::StyleSetter
117       addStyle( const std::string& name,
118                 void (C1::*setter)(const std::string&),
119                 C2 instance )
120       {
121         return _style_setters[ name ] =
122                  bindStyleSetter<const char*>(name, setter, instance);
123       }
124
125       template<typename T1, typename T2, class C1, class C2>
126       Element::StyleSetter
127       bindStyleSetter( const std::string& name,
128                        void (C1::*setter)(T2),
129                        C2 instance )
130       {
131         return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
132       }
133
134       virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
135
136       virtual void childAdded(SGPropertyNode * child)  {}
137       virtual void childRemoved(SGPropertyNode * child){}
138       virtual void childChanged(SGPropertyNode * child){}
139
140       void setDrawable( osg::Drawable* drawable );
141       void setupStyle();
142
143       bool setStyle(const SGPropertyNode* child);
144
145     private:
146
147       osg::ref_ptr<osg::Drawable> _drawable;
148
149       Element(const Element&);// = delete
150   };
151
152 }  // namespace canvas
153
154 #endif /* CANVAS_ELEMENT_HXX_ */