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<bool(Element&, const SGPropertyNode*)>
50 typedef boost::function<void(Element&, const SGPropertyNode*)>
51 StyleSetterFuncUnchecked;
56 SGSharedPtr<StyleSetter> next;
60 StyleSetter setter; ///!< Function(s) for setting this style
61 std::string type; ///!< Interpolation type
65 * Remove the property listener of the element.
67 * You will need to call the appropriate methods (#childAdded,
68 * #childRemoved, #valueChanged) yourself to ensure the element still
69 * receives the needed events.
71 void removeListener();
76 virtual ~Element() = 0;
78 ElementWeakPtr getWeakPtr() const;
81 * Called every frame to update internal state
83 * @param dt Frame time in seconds
85 virtual void update(double dt);
87 naRef addEventListener(const nasal::CallContext& ctx);
88 virtual void clearEventListener();
90 virtual bool accept(EventVisitor& visitor);
91 virtual bool ascend(EventVisitor& visitor);
92 virtual bool traverse(EventVisitor& visitor);
94 virtual bool handleEvent(canvas::EventPtr event);
96 virtual bool hitBound( const osg::Vec2f& pos,
97 const osg::Vec2f& local_pos ) const;
100 * Get whether the element is visible or hidden (Can be changed with
101 * setting property "visible" accordingly).
103 bool isVisible() const;
105 osg::ref_ptr<osg::MatrixTransform> getMatrixTransform();
107 virtual void childAdded( SGPropertyNode * parent,
108 SGPropertyNode * child );
109 virtual void childRemoved( SGPropertyNode * parent,
110 SGPropertyNode * child );
111 virtual void valueChanged(SGPropertyNode * child);
113 virtual bool setStyle(const SGPropertyNode* child);
118 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
119 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
121 void setClip(const std::string& clip);
124 * Write the given bounding box to the property tree
126 void setBoundingBox(const osg::BoundingBox& bb);
129 * Get bounding box with children/drawables transformed by passed matrix
131 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
138 LAST_ATTRIBUTE = BLEND_FUNC << 1
150 CanvasWeakPtr _canvas;
153 uint32_t _attributes_dirty;
155 bool _transform_dirty;
156 osg::ref_ptr<osg::MatrixTransform> _transform;
157 std::vector<TransformType> _transform_types;
160 std::vector<SGPropertyNode_ptr> _bounding_box;
162 typedef std::vector<EventListenerPtr> Listener;
163 typedef std::map<Event::Type, Listener> ListenerMap;
165 ListenerMap _listener;
167 typedef std::map<std::string, StyleInfo> StyleSetters;
168 static StyleSetters _style_setters;
170 Element( const CanvasWeakPtr& canvas,
171 const SGPropertyNode_ptr& node,
172 const Style& parent_style,
176 * Returns false on first call and true on any successive call. Use to
177 * perform initialization tasks which are only required once per element
180 * @tparam Derived (Derived) class type
182 template<class Derived>
185 static bool is_init = false;
194 * Register a function for setting a style specified by the given property
196 * @param name Property name
197 * @param type Interpolation type
198 * @param setter Setter function
200 * @tparam T1 Type of value used to retrieve value from property
202 * @tparam T2 Type of value the setter function expects
203 * @tparam Derived Type of class the setter can be applied to
205 * @note T1 needs to be convertible to T2
213 addStyle( const std::string& name,
214 const std::string& type,
215 const boost::function<void (Derived&, T2)>& setter )
217 StyleInfo& style_info = _style_setters[ name ];
220 if( !style_info.type.empty() && type != style_info.type )
225 "changing animation type for '" << name << "': "
226 << style_info.type << " -> " << type
229 style_info.type = type;
232 StyleSetter* style = &style_info.setter;
236 style = style->next = new StyleSetter;
238 style->func = boost::bind
240 &type_match<Derived>::call,
243 bindStyleSetter<T1>(name, setter)
253 addStyle( const std::string& name,
254 const std::string& type,
255 const boost::function<void (Derived&, T)>& setter )
257 return addStyle<T, T>(name, type, setter);
265 addStyle( const std::string& name,
266 const std::string& type,
267 void (Derived::*setter)(T) )
269 return addStyle<T, T>
273 boost::function<void (Derived&, T)>(setter)
283 addStyle( const std::string& name,
284 const std::string& type,
285 void (Derived::*setter)(T2) )
291 boost::function<void (Derived&, T2)>(setter)
299 addStyle( const std::string& name,
300 const std::string& type,
301 void (Derived::*setter)(const std::string&) )
303 return addStyle<const char*, const std::string&>
307 boost::function<void (Derived&, const std::string&)>(setter)
318 addStyle( const std::string& name,
319 const std::string& type,
320 void (Other::*setter)(T),
321 OtherRef Derived::*instance_ref )
323 return addStyle<T, T>(name, type, bindOther(setter, instance_ref));
334 addStyle( const std::string& name,
335 const std::string& type,
336 void (Other::*setter)(T2),
337 OtherRef Derived::*instance_ref )
339 return addStyle<T1>(name, type, bindOther(setter, instance_ref));
350 addStyle( const std::string& name,
351 const std::string& type,
352 const boost::function<void (Other&, T2)>& setter,
353 OtherRef Derived::*instance_ref )
355 return addStyle<T1>(name, type, bindOther(setter, instance_ref));
364 addStyle( const std::string& name,
365 const std::string& type,
366 void (Other::*setter)(const std::string&),
367 OtherRef Derived::*instance_ref )
369 return addStyle<const char*, const std::string&>
373 boost::function<void (Other&, const std::string&)>(setter),
378 template<typename T, class Derived, class Other, class OtherRef>
379 boost::function<void (Derived&, T)>
380 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
382 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
385 template<typename T, class Derived, class Other, class OtherRef>
386 boost::function<void (Derived&, T)>
387 bindOther( const boost::function<void (Other&, T)>& setter,
388 OtherRef Derived::*instance_ref )
395 &reference_from_pointer<Other, OtherRef>,
396 boost::bind(instance_ref, _1)
402 template<typename T1, typename T2, class Derived>
403 StyleSetterFuncUnchecked
404 bindStyleSetter( const std::string& name,
405 const boost::function<void (Derived&, T2)>& setter )
410 // We will only call setters with Derived instances, so we can safely
412 boost::bind(&derived_cast<Derived>, _1),
413 boost::bind(&getValue<T1>, _2)
417 virtual void childAdded(SGPropertyNode * child) {}
418 virtual void childRemoved(SGPropertyNode * child){}
419 virtual void childChanged(SGPropertyNode * child){}
421 void setDrawable(osg::Drawable* drawable);
424 * Get stateset of drawable if available or use transform otherwise
426 osg::StateSet* getOrCreateStateSet();
432 osg::ref_ptr<osg::Drawable> _drawable;
434 Element(const Element&);// = delete
436 template<class Derived>
437 static Derived& derived_cast(Element& el)
439 return static_cast<Derived&>(el);
442 template<class T, class SharedPtr>
443 static T& reference_from_pointer(const SharedPtr& p)
445 return *get_pointer(p);
449 * Helper to call a function only of the element type can be converted to
452 * @return Whether the function has been called
454 template<class Derived>
457 static bool call( Element& el,
458 const SGPropertyNode* prop,
459 const StyleSetterFuncUnchecked& func )
461 Derived* d = dynamic_cast<Derived*>(&el);
470 } // namespace canvas
471 } // namespace simgear
473 #endif /* CANVAS_ELEMENT_HXX_ */