]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasElement.hxx
First working version of DOM like Canvas event 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/props.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 SGPropertyChangeListener
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       /**
66        * Called every frame to update internal state
67        *
68        * @param dt  Frame time in seconds
69        */
70       virtual void update(double dt);
71
72       naRef addEventListener(const nasal::CallContext& ctx);
73
74       SGConstPropertyNode_ptr getProps() const;
75       SGPropertyNode_ptr getProps();
76
77       virtual bool accept(EventVisitor& visitor);
78       virtual bool ascend(EventVisitor& visitor);
79       virtual bool traverse(EventVisitor& visitor);
80
81       void callListeners(const canvas::EventPtr& event);
82
83       virtual bool hitBound(const osg::Vec2f& pos) const;
84
85
86       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
87
88       virtual void childAdded( SGPropertyNode * parent,
89                                SGPropertyNode * child );
90       virtual void childRemoved( SGPropertyNode * parent,
91                                  SGPropertyNode * child );
92       virtual void valueChanged(SGPropertyNode * child);
93
94       /**
95        * Write the given bounding box to the property tree
96        */
97       void setBoundingBox(const osg::BoundingBox& bb);
98
99     protected:
100
101       enum Attributes
102       {
103         LAST_ATTRIBUTE  = 0x0001
104       };
105
106       enum TransformType
107       {
108         TT_NONE,
109         TT_MATRIX,
110         TT_TRANSLATE,
111         TT_ROTATE,
112         TT_SCALE
113       };
114
115       CanvasWeakPtr _canvas;
116       Element      *_parent;
117       uint32_t _attributes_dirty;
118
119       bool _transform_dirty;
120       osg::ref_ptr<osg::MatrixTransform>    _transform;
121       std::vector<TransformType>            _transform_types;
122
123       SGPropertyNode_ptr                _node;
124       Style                             _style;
125       StyleSetters                      _style_setters;
126       std::vector<SGPropertyNode_ptr>   _bounding_box;
127
128       typedef std::vector<EventListenerPtr> Listener;
129       typedef std::map<Event::Type, Listener> ListenerMap;
130
131       ListenerMap _listener;
132
133       Element( const CanvasWeakPtr& canvas,
134                const SGPropertyNode_ptr& node,
135                const Style& parent_style,
136                Element* parent );
137
138       template<typename T, class C1, class C2>
139       Element::StyleSetter
140       addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
141       {
142         return _style_setters[ name ] =
143                  bindStyleSetter<T>(name, setter, instance);
144       }
145
146       template<typename T1, typename T2, class C1, class C2>
147       Element::StyleSetter
148       addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
149       {
150         return _style_setters[ name ] =
151                  bindStyleSetter<T1>(name, setter, instance);
152       }
153
154       template<class C1, class C2>
155       Element::StyleSetter
156       addStyle( const std::string& name,
157                 void (C1::*setter)(const std::string&),
158                 C2 instance )
159       {
160         return _style_setters[ name ] =
161                  bindStyleSetter<const char*>(name, setter, instance);
162       }
163
164       template<typename T1, typename T2, class C1, class C2>
165       Element::StyleSetter
166       bindStyleSetter( const std::string& name,
167                        void (C1::*setter)(T2),
168                        C2 instance )
169       {
170         return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
171       }
172
173       virtual void childAdded(SGPropertyNode * child)  {}
174       virtual void childRemoved(SGPropertyNode * child){}
175       virtual void childChanged(SGPropertyNode * child){}
176
177       void setDrawable(osg::Drawable* drawable);
178
179       void setupStyle();
180       bool setStyle(const SGPropertyNode* child);
181
182     private:
183
184       osg::ref_ptr<osg::Drawable> _drawable;
185
186       Element(const Element&);// = delete
187   };
188
189 } // namespace canvas
190 } // namespace simgear
191
192 #endif /* CANVAS_ELEMENT_HXX_ */