1 // Interface for 2D canvas element
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License as
7 // published by the Free Software Foundation; either version 2 of the
8 // License, or (at your option) any later version.
10 // This program is distributed in the hope that it will be useful, but
11 // WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #ifndef CANVAS_ELEMENT_HXX_
20 #define CANVAS_ELEMENT_HXX_
22 #include <simgear/props/props.hxx>
23 #include <simgear/misc/stdint.hxx> // for uint32_t
24 #include <osg/MatrixTransform>
26 #include <boost/bind.hpp>
27 #include <boost/function.hpp>
39 public SGPropertyChangeListener
42 typedef std::map<std::string, const SGPropertyNode*> Style;
43 typedef boost::function<void(const SGPropertyNode*)> StyleSetter;
44 typedef std::map<std::string, StyleSetter> StyleSetters;
46 void removeListener();
47 virtual ~Element() = 0;
50 * Called every frame to update internal state
52 * @param dt Frame time in seconds
54 virtual void update(double dt);
56 virtual bool handleMouseEvent(const canvas::MouseEvent& event);
58 osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
60 virtual void childAdded( SGPropertyNode * parent,
61 SGPropertyNode * child );
62 virtual void childRemoved( SGPropertyNode * parent,
63 SGPropertyNode * child );
64 virtual void valueChanged(SGPropertyNode * child);
70 BOUNDING_BOX = 0x0001,
71 LAST_ATTRIBUTE = BOUNDING_BOX
83 uint32_t _attributes_used;
84 uint32_t _attributes_dirty;
86 bool _transform_dirty;
87 osg::ref_ptr<osg::MatrixTransform> _transform;
88 std::vector<TransformType> _transform_types;
90 SGPropertyNode_ptr _node;
92 StyleSetters _style_setters;
93 std::vector<SGPropertyNode_ptr> _bounding_box;
95 Element( SGPropertyNode_ptr node,
96 const Style& parent_style,
97 uint32_t attributes_used = 0 );
99 template<typename T, class C1, class C2>
101 addStyle(const std::string& name, void (C1::*setter)(T), C2 instance)
103 return _style_setters[ name ] =
104 bindStyleSetter<T>(name, setter, instance);
107 template<typename T1, typename T2, class C1, class C2>
109 addStyle(const std::string& name, void (C1::*setter)(T2), C2 instance)
111 return _style_setters[ name ] =
112 bindStyleSetter<T1>(name, setter, instance);
115 template<class C1, class C2>
117 addStyle( const std::string& name,
118 void (C1::*setter)(const std::string&),
121 return _style_setters[ name ] =
122 bindStyleSetter<const char*>(name, setter, instance);
125 template<typename T1, typename T2, class C1, class C2>
127 bindStyleSetter( const std::string& name,
128 void (C1::*setter)(T2),
131 return boost::bind(setter, instance, boost::bind(&getValue<T1>, _1));
134 virtual bool handleLocalMouseEvent(const canvas::MouseEvent& event);
136 virtual void childAdded(SGPropertyNode * child) {}
137 virtual void childRemoved(SGPropertyNode * child){}
138 virtual void childChanged(SGPropertyNode * child){}
140 void setDrawable( osg::Drawable* drawable );
143 bool setStyle(const SGPropertyNode* child);
147 osg::ref_ptr<osg::Drawable> _drawable;
149 Element(const Element&);// = delete
152 } // namespace canvas
154 #endif /* CANVAS_ELEMENT_HXX_ */