]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/elements/CanvasElement.hxx
Expose whether CanvasElement is visible
[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,
83                              const osg::Vec2f& local_pos ) const;
84
85       /**
86        * Get whether the element is visible or hidden (Can be changed with
87        * setting property "visible" accordingly).
88        */
89       bool isVisible() const;
90
91       osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
92
93       virtual void childAdded( SGPropertyNode * parent,
94                                SGPropertyNode * child );
95       virtual void childRemoved( SGPropertyNode * parent,
96                                  SGPropertyNode * child );
97       virtual void valueChanged(SGPropertyNode * child);
98
99       virtual bool setStyle(const SGPropertyNode* child);
100
101       /**
102        * Set clipping shape
103        *
104        * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
105        * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
106        */
107       void setClip(const std::string& clip);
108
109       /**
110        * Write the given bounding box to the property tree
111        */
112       void setBoundingBox(const osg::BoundingBox& bb);
113
114       /**
115        * Get bounding box with children/drawables transformed by passed matrix
116        */
117       virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
118
119     protected:
120
121       enum Attributes
122       {
123         LAST_ATTRIBUTE  = 0x0001
124       };
125
126       enum TransformType
127       {
128         TT_NONE,
129         TT_MATRIX,
130         TT_TRANSLATE,
131         TT_ROTATE,
132         TT_SCALE
133       };
134
135       CanvasWeakPtr _canvas;
136       Element      *_parent;
137
138       uint32_t _attributes_dirty;
139
140       bool _transform_dirty;
141       osg::ref_ptr<osg::MatrixTransform>    _transform;
142       std::vector<TransformType>            _transform_types;
143
144       Style                             _style;
145       StyleSetters                      _style_setters;
146       std::vector<SGPropertyNode_ptr>   _bounding_box;
147
148       typedef std::vector<EventListenerPtr> Listener;
149       typedef std::map<Event::Type, Listener> ListenerMap;
150
151       ListenerMap _listener;
152
153       Element( const CanvasWeakPtr& canvas,
154                const SGPropertyNode_ptr& node,
155                const Style& parent_style,
156                Element* parent );
157
158       template<typename T, class C1, class C2>
159       Element::StyleSetter
160       addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
161       {
162         return _style_setters[ name ] =
163                  bindStyleSetter<T>(name, setter, instance);
164       }
165
166       template<typename T1, typename T2, class C1, class C2>
167       Element::StyleSetter
168       addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
169       {
170         return _style_setters[ name ] =
171                  bindStyleSetter<T1>(name, setter, instance);
172       }
173
174       template<class C1, class C2>
175       Element::StyleSetter
176       addStyle( const std::string& name,
177                 void (C1::*setter)(const std::string&),
178                 C2 instance )
179       {
180         return _style_setters[ name ] =
181                  bindStyleSetter<const char*>(name, setter, instance);
182       }
183
184       template<typename T1, typename T2, class C1, class C2>
185       Element::StyleSetter
186       bindStyleSetter( const std::string& name,
187                        void (C1::*setter)(T2),
188                        C2 instance )
189       {
190         return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
191       }
192
193       virtual void childAdded(SGPropertyNode * child)  {}
194       virtual void childRemoved(SGPropertyNode * child){}
195       virtual void childChanged(SGPropertyNode * child){}
196
197       void setDrawable(osg::Drawable* drawable);
198
199       /**
200        * Get stateset of drawable if available or use transform otherwise
201        */
202       osg::StateSet* getOrCreateStateSet();
203
204       void setupStyle();
205
206     private:
207
208       osg::ref_ptr<osg::Drawable> _drawable;
209
210       Element(const Element&);// = delete
211   };
212
213 } // namespace canvas
214 } // namespace simgear
215
216 #endif /* CANVAS_ELEMENT_HXX_ */