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/props/props.hxx>
24 #include <simgear/misc/stdint.hxx> // for uint32_t
25 #include <osg/BoundingBox>
26 #include <osg/MatrixTransform>
28 #include <boost/bind.hpp>
29 #include <boost/function.hpp>
43 public SGPropertyChangeListener
46 typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
47 typedef std::map<std::string, StyleSetter> StyleSetters;
50 * Remove the property listener of the element.
52 * You will need to call the appropriate methods (#childAdded,
53 * #childRemoved, #valueChanged) yourself to ensure the element still
54 * receives the needed events.
56 void removeListener();
61 virtual ~Element() = 0;
64 * Called every frame to update internal state
66 * @param dt Frame time in seconds
68 virtual void update(double dt);
71 * Handle mouse event (transforms coordinates to local coordinate frame
72 * and forwards event to #handleLocalMouseEvent)
74 virtual bool handleMouseEvent(const canvas::MouseEvent& event);
76 osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
78 virtual void childAdded( SGPropertyNode * parent,
79 SGPropertyNode * child );
80 virtual void childRemoved( SGPropertyNode * parent,
81 SGPropertyNode * child );
82 virtual void valueChanged(SGPropertyNode * child);
85 * Write the given bounding box to the property tree
87 void setBoundingBox(const osg::BoundingBox& bb);
93 LAST_ATTRIBUTE = 0x0001
105 CanvasWeakPtr _canvas;
106 uint32_t _attributes_dirty;
108 bool _transform_dirty;
109 osg::ref_ptr<osg::MatrixTransform> _transform;
110 std::vector<TransformType> _transform_types;
112 SGPropertyNode_ptr _node;
114 StyleSetters _style_setters;
115 std::vector<SGPropertyNode_ptr> _bounding_box;
117 Element( const CanvasWeakPtr& canvas,
118 const SGPropertyNode_ptr& node,
119 const Style& parent_style );
121 template<typename T, class C1, class C2>
123 addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
125 return _style_setters[ name ] =
126 bindStyleSetter<T>(name, setter, instance);
129 template<typename T1, typename T2, class C1, class C2>
131 addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
133 return _style_setters[ name ] =
134 bindStyleSetter<T1>(name, setter, instance);
137 template<class C1, class C2>
139 addStyle( const std::string& name,
140 void (C1::*setter)(const std::string&),
143 return _style_setters[ name ] =
144 bindStyleSetter<const char*>(name, setter, instance);
147 template<typename T1, typename T2, class C1, class C2>
149 bindStyleSetter( const std::string& name,
150 void (C1::*setter)(T2),
153 return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
156 virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
158 virtual void childAdded(SGPropertyNode * child) {}
159 virtual void childRemoved(SGPropertyNode * child){}
160 virtual void childChanged(SGPropertyNode * child){}
162 void setDrawable(osg::Drawable* drawable);
165 bool setStyle(const SGPropertyNode* child);
169 osg::ref_ptr<osg::Drawable> _drawable;
171 Element(const Element&);// = delete
174 } // namespace canvas
175 } // namespace simgear
177 #endif /* CANVAS_ELEMENT_HXX_ */