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
27 #include <osg/BoundingBox>
28 #include <osg/MatrixTransform>
30 #include <boost/bind.hpp>
31 #include <boost/function.hpp>
32 #include <boost/type_traits/is_base_of.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
74 bool inheritable; ///!< Whether children can inherit this style from
81 virtual ~Element() = 0;
83 virtual void setSelf(const PropertyBasedElementPtr& self);
84 virtual void onDestroy();
86 ElementWeakPtr getWeakPtr() const;
87 ElementPtr getParent();
90 * Called every frame to update internal state
92 * @param dt Frame time in seconds
94 virtual void update(double dt);
96 bool addEventListener(const std::string& type, const EventListener& cb);
97 virtual void clearEventListener();
99 virtual bool accept(EventVisitor& visitor);
100 virtual bool ascend(EventVisitor& visitor);
101 virtual bool traverse(EventVisitor& visitor);
103 virtual bool handleEvent(canvas::EventPtr event);
105 virtual bool hitBound( const osg::Vec2f& pos,
106 const osg::Vec2f& local_pos ) const;
109 * Get whether the element is visible or hidden (Can be changed with
110 * setting property "visible" accordingly).
112 bool isVisible() const;
114 osg::MatrixTransform* getMatrixTransform();
115 osg::MatrixTransform const* getMatrixTransform() const;
117 virtual void childAdded( SGPropertyNode * parent,
118 SGPropertyNode * child );
119 virtual void childRemoved( SGPropertyNode * parent,
120 SGPropertyNode * child );
121 virtual void valueChanged(SGPropertyNode * child);
123 virtual bool setStyle( const SGPropertyNode* child,
124 const StyleInfo* style_info = 0 );
129 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
130 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
132 void setClip(const std::string& clip);
135 * Write the given bounding box to the property tree
137 void setBoundingBox(const osg::BoundingBox& bb);
140 * Get bounding box with children/drawables transformed by passed matrix
142 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
145 * Create an canvas Element
147 * @tparam Derived Type of element (needs to derive from Element)
149 template<typename Derived>
151 typename boost::enable_if<
152 boost::is_base_of<Element, Derived>,
154 >::type create( const CanvasWeakPtr& canvas,
155 const SGPropertyNode_ptr& node,
159 ElementPtr el( new Derived(canvas, node, style, parent) );
169 LAST_ATTRIBUTE = BLEND_FUNC << 1
181 CanvasWeakPtr _canvas;
184 uint32_t _attributes_dirty;
186 bool _transform_dirty;
187 osg::observer_ptr<osg::MatrixTransform> _transform;
188 std::vector<TransformType> _transform_types;
191 std::vector<SGPropertyNode_ptr> _bounding_box;
193 typedef std::vector<EventListener> Listener;
194 typedef std::map<Event::Type, Listener> ListenerMap;
196 ListenerMap _listener;
198 typedef std::map<std::string, StyleInfo> StyleSetters;
199 static StyleSetters _style_setters;
201 static void staticInit();
203 Element( const CanvasWeakPtr& canvas,
204 const SGPropertyNode_ptr& node,
205 const Style& parent_style,
209 * Returns false on first call and true on any successive call. Use to
210 * perform initialization tasks which are only required once per element
213 * @tparam Derived (Derived) class type
215 template<class Derived>
218 static bool is_init = false;
227 * Register a function for setting a style specified by the given property
229 * @param name Property name
230 * @param type Interpolation type
231 * @param setter Setter function
233 * @tparam T1 Type of value used to retrieve value from property
235 * @tparam T2 Type of value the setter function expects
236 * @tparam Derived Type of class the setter can be applied to
238 * @note T1 needs to be convertible to T2
247 addStyle( const std::string& name,
248 const std::string& type,
249 const boost::function<void (Derived&, T2)>& setter,
250 bool inheritable = true )
252 StyleInfo& style_info = _style_setters[ name ];
255 if( !style_info.type.empty() && type != style_info.type )
260 "changing animation type for '" << name << "': "
261 << style_info.type << " -> " << type
264 style_info.type = type;
266 // TODO check if changed?
267 style_info.inheritable = inheritable;
269 StyleSetter* style = &style_info.setter;
273 style = style->next = new StyleSetter;
275 style->func = boost::bind
277 &type_match<Derived>::call,
280 bindStyleSetter<T1>(name, setter)
291 addStyle( const std::string& name,
292 const std::string& type,
293 const boost::function<void (Derived&, T)>& setter,
294 bool inheritable = true )
296 return addStyle<T, T>(name, type, setter, inheritable);
305 addStyle( const std::string& name,
306 const std::string& type,
307 void (Derived::*setter)(T),
308 bool inheritable = true )
310 return addStyle<T, T>
314 boost::function<void (Derived&, T)>(setter),
326 addStyle( const std::string& name,
327 const std::string& type,
328 void (Derived::*setter)(T2),
329 bool inheritable = true )
335 boost::function<void (Derived&, T2)>(setter),
345 addStyle( const std::string& name,
346 const std::string& type,
347 void (Derived::*setter)(const std::string&),
348 bool inheritable = true )
350 return addStyle<const char*, const std::string&>
354 boost::function<void (Derived&, const std::string&)>(setter),
367 addStyle( const std::string& name,
368 const std::string& type,
369 void (Other::*setter)(T),
370 OtherRef Derived::*instance_ref,
371 bool inheritable = true )
373 return addStyle<T, T>
377 bindOther(setter, instance_ref),
391 addStyle( const std::string& name,
392 const std::string& type,
393 void (Other::*setter)(T2),
394 OtherRef Derived::*instance_ref,
395 bool inheritable = true )
401 bindOther(setter, instance_ref),
415 addStyle( const std::string& name,
416 const std::string& type,
417 const boost::function<void (Other&, T2)>& setter,
418 OtherRef Derived::*instance_ref,
419 bool inheritable = true )
425 bindOther(setter, instance_ref),
437 addStyle( const std::string& name,
438 const std::string& type,
439 void (Other::*setter)(const std::string&),
440 OtherRef Derived::*instance_ref,
441 bool inheritable = true )
443 return addStyle<const char*, const std::string&>
447 boost::function<void (Other&, const std::string&)>(setter),
453 template<typename T, class Derived, class Other, class OtherRef>
455 boost::function<void (Derived&, T)>
456 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
458 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
461 template<typename T, class Derived, class Other, class OtherRef>
463 boost::function<void (Derived&, T)>
464 bindOther( const boost::function<void (Other&, T)>& setter,
465 OtherRef Derived::*instance_ref )
472 &reference_from_pointer<Other, OtherRef>,
473 boost::bind(instance_ref, _1)
479 template<typename T1, typename T2, class Derived>
481 StyleSetterFuncUnchecked
482 bindStyleSetter( const std::string& name,
483 const boost::function<void (Derived&, T2)>& setter )
488 // We will only call setters with Derived instances, so we can safely
490 boost::bind(&derived_cast<Derived>, _1),
491 boost::bind(&getValue<T1>, _2)
495 bool isStyleEmpty(const SGPropertyNode* child) const;
496 bool canApplyStyle(const SGPropertyNode* child) const;
497 bool setStyleImpl( const SGPropertyNode* child,
498 const StyleInfo* style_info = 0 );
500 const StyleInfo* getStyleInfo(const std::string& name) const;
501 const StyleSetter* getStyleSetter(const std::string& name) const;
502 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
504 virtual void childAdded(SGPropertyNode * child) {}
505 virtual void childRemoved(SGPropertyNode * child){}
506 virtual void childChanged(SGPropertyNode * child){}
508 void setDrawable(osg::Drawable* drawable);
511 * Get stateset of drawable if available or use transform otherwise
513 osg::StateSet* getOrCreateStateSet();
519 osg::ref_ptr<osg::Drawable> _drawable;
521 Element(const Element&);// = delete
523 template<class Derived>
524 static Derived& derived_cast(Element& el)
526 return static_cast<Derived&>(el);
529 template<class T, class SharedPtr>
530 static T& reference_from_pointer(const SharedPtr& p)
532 return *get_pointer(p);
536 * Helper to call a function only of the element type can be converted to
539 * @return Whether the function has been called
541 template<class Derived>
544 static bool call( Element& el,
545 const SGPropertyNode* prop,
546 const StyleSetterFuncUnchecked& func )
548 Derived* d = dynamic_cast<Derived*>(&el);
557 } // namespace canvas
558 } // namespace simgear
560 #endif /* CANVAS_ELEMENT_HXX_ */