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);
70 SGConstPropertyNode_ptr getProps() const;
71 SGPropertyNode_ptr getProps();
74 * Handle mouse event (transforms coordinates to local coordinate frame
75 * and forwards event to #handleLocalMouseEvent)
77 virtual bool handleMouseEvent(const canvas::MouseEvent& event);
79 osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
81 virtual void childAdded( SGPropertyNode * parent,
82 SGPropertyNode * child );
83 virtual void childRemoved( SGPropertyNode * parent,
84 SGPropertyNode * child );
85 virtual void valueChanged(SGPropertyNode * child);
88 * Write the given bounding box to the property tree
90 void setBoundingBox(const osg::BoundingBox& bb);
96 LAST_ATTRIBUTE = 0x0001
108 CanvasWeakPtr _canvas;
109 uint32_t _attributes_dirty;
111 bool _transform_dirty;
112 osg::ref_ptr<osg::MatrixTransform> _transform;
113 std::vector<TransformType> _transform_types;
115 SGPropertyNode_ptr _node;
117 StyleSetters _style_setters;
118 std::vector<SGPropertyNode_ptr> _bounding_box;
120 Element( const CanvasWeakPtr& canvas,
121 const SGPropertyNode_ptr& node,
122 const Style& parent_style );
124 template<typename T, class C1, class C2>
126 addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
128 return _style_setters[ name ] =
129 bindStyleSetter<T>(name, setter, instance);
132 template<typename T1, typename T2, class C1, class C2>
134 addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
136 return _style_setters[ name ] =
137 bindStyleSetter<T1>(name, setter, instance);
140 template<class C1, class C2>
142 addStyle( const std::string& name,
143 void (C1::*setter)(const std::string&),
146 return _style_setters[ name ] =
147 bindStyleSetter<const char*>(name, setter, instance);
150 template<typename T1, typename T2, class C1, class C2>
152 bindStyleSetter( const std::string& name,
153 void (C1::*setter)(T2),
156 return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
159 virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
161 virtual void childAdded(SGPropertyNode * child) {}
162 virtual void childRemoved(SGPropertyNode * child){}
163 virtual void childChanged(SGPropertyNode * child){}
165 void setDrawable(osg::Drawable* drawable);
168 bool setStyle(const SGPropertyNode* child);
172 osg::ref_ptr<osg::Drawable> _drawable;
174 Element(const Element&);// = delete
177 } // namespace canvas
178 } // namespace simgear
180 #endif /* CANVAS_ELEMENT_HXX_ */