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 std::map<std::string, const SGPropertyNode*> Style;
47 typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
48 typedef std::map<std::string, StyleSetter> StyleSetters;
51 * Remove the property listener of the element.
53 * You will need to call the appropriate methods (#childAdded,
54 * #childRemoved, #valueChanged) yourself to ensure the element still
55 * receives the needed events.
57 void removeListener();
62 virtual ~Element() = 0;
65 * Called every frame to update internal state
67 * @param dt Frame time in seconds
69 virtual void update(double dt);
72 * Handle mouse event (transforms coordinates to local coordinate frame
73 * and forwards event to #handleLocalMouseEvent)
75 virtual bool handleMouseEvent(const canvas::MouseEvent& event);
77 osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
79 virtual void childAdded( SGPropertyNode * parent,
80 SGPropertyNode * child );
81 virtual void childRemoved( SGPropertyNode * parent,
82 SGPropertyNode * child );
83 virtual void valueChanged(SGPropertyNode * child);
86 * Write the given bounding box to the property tree
88 void setBoundingBox(const osg::BoundingBox& bb);
94 LAST_ATTRIBUTE = 0x0001
106 CanvasWeakPtr _canvas;
107 uint32_t _attributes_dirty;
109 bool _transform_dirty;
110 osg::ref_ptr<osg::MatrixTransform> _transform;
111 std::vector<TransformType> _transform_types;
113 SGPropertyNode_ptr _node;
115 StyleSetters _style_setters;
116 std::vector<SGPropertyNode_ptr> _bounding_box;
118 Element( const CanvasWeakPtr& canvas,
119 SGPropertyNode_ptr node,
120 const Style& parent_style );
122 template<typename T, class C1, class C2>
124 addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
126 return _style_setters[ name ] =
127 bindStyleSetter<T>(name, setter, instance);
130 template<typename T1, typename T2, class C1, class C2>
132 addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
134 return _style_setters[ name ] =
135 bindStyleSetter<T1>(name, setter, instance);
138 template<class C1, class C2>
140 addStyle( const std::string& name,
141 void (C1::*setter)(const std::string&),
144 return _style_setters[ name ] =
145 bindStyleSetter<const char*>(name, setter, instance);
148 template<typename T1, typename T2, class C1, class C2>
150 bindStyleSetter( const std::string& name,
151 void (C1::*setter)(T2),
154 return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
157 virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
159 virtual void childAdded(SGPropertyNode * child) {}
160 virtual void childRemoved(SGPropertyNode * child){}
161 virtual void childChanged(SGPropertyNode * child){}
163 void setDrawable(osg::Drawable* drawable);
166 bool setStyle(const SGPropertyNode* child);
170 osg::ref_ptr<osg::Drawable> _drawable;
172 Element(const Element&);// = delete
175 } // namespace canvas
176 } // namespace simgear
178 #endif /* CANVAS_ELEMENT_HXX_ */