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
50 * Store pointer to window as user data
53 public osg::Referenced
57 OSGUserData(ElementPtr element);
60 typedef boost::function<bool(Element&, const SGPropertyNode*)>
62 typedef boost::function<void(Element&, const SGPropertyNode*)>
63 StyleSetterFuncUnchecked;
68 SGSharedPtr<StyleSetter> next;
72 StyleSetter setter; ///!< Function(s) for setting this style
73 std::string type; ///!< Interpolation type
79 virtual ~Element() = 0;
81 virtual void setSelf(const PropertyBasedElementPtr& self);
82 virtual void onDestroy();
84 ElementWeakPtr getWeakPtr() const;
87 * Called every frame to update internal state
89 * @param dt Frame time in seconds
91 virtual void update(double dt);
93 naRef addEventListener(const nasal::CallContext& ctx);
94 virtual void clearEventListener();
96 virtual bool accept(EventVisitor& visitor);
97 virtual bool ascend(EventVisitor& visitor);
98 virtual bool traverse(EventVisitor& visitor);
100 virtual bool handleEvent(canvas::EventPtr event);
102 virtual bool hitBound( const osg::Vec2f& pos,
103 const osg::Vec2f& local_pos ) const;
106 * Get whether the element is visible or hidden (Can be changed with
107 * setting property "visible" accordingly).
109 bool isVisible() const;
111 osg::MatrixTransform* getMatrixTransform();
112 osg::MatrixTransform const* getMatrixTransform() const;
114 virtual void childAdded( SGPropertyNode * parent,
115 SGPropertyNode * child );
116 virtual void childRemoved( SGPropertyNode * parent,
117 SGPropertyNode * child );
118 virtual void valueChanged(SGPropertyNode * child);
120 virtual bool setStyle( const SGPropertyNode* child,
121 const StyleSetter* setter = 0 );
126 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
127 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
129 void setClip(const std::string& clip);
132 * Write the given bounding box to the property tree
134 void setBoundingBox(const osg::BoundingBox& bb);
137 * Get bounding box with children/drawables transformed by passed matrix
139 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
142 * Create an canvas Element
144 * @tparam Derived Type of element (needs to derive from Element)
146 template<typename Derived>
148 typename boost::enable_if<
149 boost::is_base_of<Element, Derived>,
151 >::type create( const CanvasWeakPtr& canvas,
152 const SGPropertyNode_ptr& node,
156 ElementPtr el( new Derived(canvas, node, style, parent) );
166 LAST_ATTRIBUTE = BLEND_FUNC << 1
178 CanvasWeakPtr _canvas;
181 uint32_t _attributes_dirty;
183 bool _transform_dirty;
184 osg::observer_ptr<osg::MatrixTransform> _transform;
185 std::vector<TransformType> _transform_types;
188 std::vector<SGPropertyNode_ptr> _bounding_box;
190 typedef std::vector<EventListenerPtr> Listener;
191 typedef std::map<Event::Type, Listener> ListenerMap;
193 ListenerMap _listener;
195 typedef std::map<std::string, StyleInfo> StyleSetters;
196 static StyleSetters _style_setters;
198 Element( const CanvasWeakPtr& canvas,
199 const SGPropertyNode_ptr& node,
200 const Style& parent_style,
204 * Returns false on first call and true on any successive call. Use to
205 * perform initialization tasks which are only required once per element
208 * @tparam Derived (Derived) class type
210 template<class Derived>
213 static bool is_init = false;
222 * Register a function for setting a style specified by the given property
224 * @param name Property name
225 * @param type Interpolation type
226 * @param setter Setter function
228 * @tparam T1 Type of value used to retrieve value from property
230 * @tparam T2 Type of value the setter function expects
231 * @tparam Derived Type of class the setter can be applied to
233 * @note T1 needs to be convertible to T2
241 addStyle( const std::string& name,
242 const std::string& type,
243 const boost::function<void (Derived&, T2)>& setter )
245 StyleInfo& style_info = _style_setters[ name ];
248 if( !style_info.type.empty() && type != style_info.type )
253 "changing animation type for '" << name << "': "
254 << style_info.type << " -> " << type
257 style_info.type = type;
260 StyleSetter* style = &style_info.setter;
264 style = style->next = new StyleSetter;
266 style->func = boost::bind
268 &type_match<Derived>::call,
271 bindStyleSetter<T1>(name, setter)
281 addStyle( const std::string& name,
282 const std::string& type,
283 const boost::function<void (Derived&, T)>& setter )
285 return addStyle<T, T>(name, type, setter);
293 addStyle( const std::string& name,
294 const std::string& type,
295 void (Derived::*setter)(T) )
297 return addStyle<T, T>
301 boost::function<void (Derived&, T)>(setter)
311 addStyle( const std::string& name,
312 const std::string& type,
313 void (Derived::*setter)(T2) )
319 boost::function<void (Derived&, T2)>(setter)
327 addStyle( const std::string& name,
328 const std::string& type,
329 void (Derived::*setter)(const std::string&) )
331 return addStyle<const char*, const std::string&>
335 boost::function<void (Derived&, const std::string&)>(setter)
346 addStyle( const std::string& name,
347 const std::string& type,
348 void (Other::*setter)(T),
349 OtherRef Derived::*instance_ref )
351 return addStyle<T, T>(name, type, bindOther(setter, instance_ref));
362 addStyle( const std::string& name,
363 const std::string& type,
364 void (Other::*setter)(T2),
365 OtherRef Derived::*instance_ref )
367 return addStyle<T1>(name, type, bindOther(setter, instance_ref));
378 addStyle( const std::string& name,
379 const std::string& type,
380 const boost::function<void (Other&, T2)>& setter,
381 OtherRef Derived::*instance_ref )
383 return addStyle<T1>(name, type, bindOther(setter, instance_ref));
392 addStyle( const std::string& name,
393 const std::string& type,
394 void (Other::*setter)(const std::string&),
395 OtherRef Derived::*instance_ref )
397 return addStyle<const char*, const std::string&>
401 boost::function<void (Other&, const std::string&)>(setter),
406 template<typename T, class Derived, class Other, class OtherRef>
407 boost::function<void (Derived&, T)>
408 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
410 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
413 template<typename T, class Derived, class Other, class OtherRef>
414 boost::function<void (Derived&, T)>
415 bindOther( const boost::function<void (Other&, T)>& setter,
416 OtherRef Derived::*instance_ref )
423 &reference_from_pointer<Other, OtherRef>,
424 boost::bind(instance_ref, _1)
430 template<typename T1, typename T2, class Derived>
431 StyleSetterFuncUnchecked
432 bindStyleSetter( const std::string& name,
433 const boost::function<void (Derived&, T2)>& setter )
438 // We will only call setters with Derived instances, so we can safely
440 boost::bind(&derived_cast<Derived>, _1),
441 boost::bind(&getValue<T1>, _2)
445 bool isStyleEmpty(const SGPropertyNode* child) const;
446 bool canApplyStyle(const SGPropertyNode* child) const;
447 bool setStyleImpl( const SGPropertyNode* child,
448 const StyleSetter* setter = 0 );
450 const StyleSetter* getStyleSetter(const std::string& name) const;
451 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
453 virtual void childAdded(SGPropertyNode * child) {}
454 virtual void childRemoved(SGPropertyNode * child){}
455 virtual void childChanged(SGPropertyNode * child){}
457 void setDrawable(osg::Drawable* drawable);
460 * Get stateset of drawable if available or use transform otherwise
462 osg::StateSet* getOrCreateStateSet();
468 osg::ref_ptr<osg::Drawable> _drawable;
470 Element(const Element&);// = delete
472 template<class Derived>
473 static Derived& derived_cast(Element& el)
475 return static_cast<Derived&>(el);
478 template<class T, class SharedPtr>
479 static T& reference_from_pointer(const SharedPtr& p)
481 return *get_pointer(p);
485 * Helper to call a function only of the element type can be converted to
488 * @return Whether the function has been called
490 template<class Derived>
493 static bool call( Element& el,
494 const SGPropertyNode* prop,
495 const StyleSetterFuncUnchecked& func )
497 Derived* d = dynamic_cast<Derived*>(&el);
506 } // namespace canvas
507 } // namespace simgear
509 #endif /* CANVAS_ELEMENT_HXX_ */