1 // Interface for 2D Canvas elements
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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
19 #ifndef CANVAS_ELEMENT_HXX_
20 #define CANVAS_ELEMENT_HXX_
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>
28 #include <osg/BoundingBox>
29 #include <osg/MatrixTransform>
31 #include <boost/bind.hpp>
32 #include <boost/function.hpp>
45 public PropertyBasedElement
48 typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
49 typedef std::map<std::string, StyleSetter> StyleSetters;
52 * Remove the property listener of the element.
54 * You will need to call the appropriate methods (#childAdded,
55 * #childRemoved, #valueChanged) yourself to ensure the element still
56 * receives the needed events.
58 void removeListener();
63 virtual ~Element() = 0;
65 ElementWeakPtr getWeakPtr() const;
68 * Called every frame to update internal state
70 * @param dt Frame time in seconds
72 virtual void update(double dt);
74 naRef addEventListener(const nasal::CallContext& ctx);
76 virtual bool accept(EventVisitor& visitor);
77 virtual bool ascend(EventVisitor& visitor);
78 virtual bool traverse(EventVisitor& visitor);
80 void callListeners(const canvas::EventPtr& event);
82 virtual bool hitBound( const osg::Vec2f& pos,
83 const osg::Vec2f& local_pos ) const;
86 osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
88 virtual void childAdded( SGPropertyNode * parent,
89 SGPropertyNode * child );
90 virtual void childRemoved( SGPropertyNode * parent,
91 SGPropertyNode * child );
92 virtual void valueChanged(SGPropertyNode * child);
94 virtual bool setStyle(const SGPropertyNode* child);
99 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
100 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
102 void setClip(const std::string& clip);
105 * Write the given bounding box to the property tree
107 void setBoundingBox(const osg::BoundingBox& bb);
110 * Get bounding box with children/drawables transformed by passed matrix
112 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
118 LAST_ATTRIBUTE = 0x0001
130 CanvasWeakPtr _canvas;
133 uint32_t _attributes_dirty;
135 bool _transform_dirty;
136 osg::ref_ptr<osg::MatrixTransform> _transform;
137 std::vector<TransformType> _transform_types;
140 StyleSetters _style_setters;
141 std::vector<SGPropertyNode_ptr> _bounding_box;
143 typedef std::vector<EventListenerPtr> Listener;
144 typedef std::map<Event::Type, Listener> ListenerMap;
146 ListenerMap _listener;
148 Element( const CanvasWeakPtr& canvas,
149 const SGPropertyNode_ptr& node,
150 const Style& parent_style,
153 template<typename T, class C1, class C2>
155 addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
157 return _style_setters[ name ] =
158 bindStyleSetter<T>(name, setter, instance);
161 template<typename T1, typename T2, class C1, class C2>
163 addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
165 return _style_setters[ name ] =
166 bindStyleSetter<T1>(name, setter, instance);
169 template<class C1, class C2>
171 addStyle( const std::string& name,
172 void (C1::*setter)(const std::string&),
175 return _style_setters[ name ] =
176 bindStyleSetter<const char*>(name, setter, instance);
179 template<typename T1, typename T2, class C1, class C2>
181 bindStyleSetter( const std::string& name,
182 void (C1::*setter)(T2),
185 return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
188 virtual void childAdded(SGPropertyNode * child) {}
189 virtual void childRemoved(SGPropertyNode * child){}
190 virtual void childChanged(SGPropertyNode * child){}
192 void setDrawable(osg::Drawable* drawable);
195 * Get stateset of drawable if available or use transform otherwise
197 osg::StateSet* getOrCreateStateSet();
203 osg::ref_ptr<osg::Drawable> _drawable;
205 Element(const Element&);// = delete
208 } // namespace canvas
209 } // namespace simgear
211 #endif /* CANVAS_ELEMENT_HXX_ */