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;
89 * Called every frame to update internal state
91 * @param dt Frame time in seconds
93 virtual void update(double dt);
95 bool addEventListener(const std::string& type, const EventListener& cb);
96 virtual void clearEventListener();
98 virtual bool accept(EventVisitor& visitor);
99 virtual bool ascend(EventVisitor& visitor);
100 virtual bool traverse(EventVisitor& visitor);
102 virtual bool handleEvent(canvas::EventPtr event);
104 virtual bool hitBound( const osg::Vec2f& pos,
105 const osg::Vec2f& local_pos ) const;
108 * Get whether the element is visible or hidden (Can be changed with
109 * setting property "visible" accordingly).
111 bool isVisible() const;
113 osg::MatrixTransform* getMatrixTransform();
114 osg::MatrixTransform const* getMatrixTransform() const;
116 virtual void childAdded( SGPropertyNode * parent,
117 SGPropertyNode * child );
118 virtual void childRemoved( SGPropertyNode * parent,
119 SGPropertyNode * child );
120 virtual void valueChanged(SGPropertyNode * child);
122 virtual bool setStyle( const SGPropertyNode* child,
123 const StyleInfo* style_info = 0 );
128 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
129 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
131 void setClip(const std::string& clip);
134 * Write the given bounding box to the property tree
136 void setBoundingBox(const osg::BoundingBox& bb);
139 * Get bounding box with children/drawables transformed by passed matrix
141 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
144 * Create an canvas Element
146 * @tparam Derived Type of element (needs to derive from Element)
148 template<typename Derived>
150 typename boost::enable_if<
151 boost::is_base_of<Element, Derived>,
153 >::type create( const CanvasWeakPtr& canvas,
154 const SGPropertyNode_ptr& node,
158 ElementPtr el( new Derived(canvas, node, style, parent) );
168 LAST_ATTRIBUTE = BLEND_FUNC << 1
180 CanvasWeakPtr _canvas;
183 uint32_t _attributes_dirty;
185 bool _transform_dirty;
186 osg::observer_ptr<osg::MatrixTransform> _transform;
187 std::vector<TransformType> _transform_types;
190 std::vector<SGPropertyNode_ptr> _bounding_box;
192 typedef std::vector<EventListener> Listener;
193 typedef std::map<Event::Type, Listener> ListenerMap;
195 ListenerMap _listener;
197 typedef std::map<std::string, StyleInfo> StyleSetters;
198 static StyleSetters _style_setters;
200 static void staticInit();
202 Element( const CanvasWeakPtr& canvas,
203 const SGPropertyNode_ptr& node,
204 const Style& parent_style,
208 * Returns false on first call and true on any successive call. Use to
209 * perform initialization tasks which are only required once per element
212 * @tparam Derived (Derived) class type
214 template<class Derived>
217 static bool is_init = false;
226 * Register a function for setting a style specified by the given property
228 * @param name Property name
229 * @param type Interpolation type
230 * @param setter Setter function
232 * @tparam T1 Type of value used to retrieve value from property
234 * @tparam T2 Type of value the setter function expects
235 * @tparam Derived Type of class the setter can be applied to
237 * @note T1 needs to be convertible to T2
246 addStyle( const std::string& name,
247 const std::string& type,
248 const boost::function<void (Derived&, T2)>& setter,
249 bool inheritable = true )
251 StyleInfo& style_info = _style_setters[ name ];
254 if( !style_info.type.empty() && type != style_info.type )
259 "changing animation type for '" << name << "': "
260 << style_info.type << " -> " << type
263 style_info.type = type;
265 // TODO check if changed?
266 style_info.inheritable = inheritable;
268 StyleSetter* style = &style_info.setter;
272 style = style->next = new StyleSetter;
274 style->func = boost::bind
276 &type_match<Derived>::call,
279 bindStyleSetter<T1>(name, setter)
290 addStyle( const std::string& name,
291 const std::string& type,
292 const boost::function<void (Derived&, T)>& setter,
293 bool inheritable = true )
295 return addStyle<T, T>(name, type, setter, inheritable);
304 addStyle( const std::string& name,
305 const std::string& type,
306 void (Derived::*setter)(T),
307 bool inheritable = true )
309 return addStyle<T, T>
313 boost::function<void (Derived&, T)>(setter),
325 addStyle( const std::string& name,
326 const std::string& type,
327 void (Derived::*setter)(T2),
328 bool inheritable = true )
334 boost::function<void (Derived&, T2)>(setter),
344 addStyle( const std::string& name,
345 const std::string& type,
346 void (Derived::*setter)(const std::string&),
347 bool inheritable = true )
349 return addStyle<const char*, const std::string&>
353 boost::function<void (Derived&, const std::string&)>(setter),
366 addStyle( const std::string& name,
367 const std::string& type,
368 void (Other::*setter)(T),
369 OtherRef Derived::*instance_ref,
370 bool inheritable = true )
372 return addStyle<T, T>
376 bindOther(setter, instance_ref),
390 addStyle( const std::string& name,
391 const std::string& type,
392 void (Other::*setter)(T2),
393 OtherRef Derived::*instance_ref,
394 bool inheritable = true )
400 bindOther(setter, instance_ref),
414 addStyle( const std::string& name,
415 const std::string& type,
416 const boost::function<void (Other&, T2)>& setter,
417 OtherRef Derived::*instance_ref,
418 bool inheritable = true )
424 bindOther(setter, instance_ref),
436 addStyle( const std::string& name,
437 const std::string& type,
438 void (Other::*setter)(const std::string&),
439 OtherRef Derived::*instance_ref,
440 bool inheritable = true )
442 return addStyle<const char*, const std::string&>
446 boost::function<void (Other&, const std::string&)>(setter),
452 template<typename T, class Derived, class Other, class OtherRef>
454 boost::function<void (Derived&, T)>
455 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
457 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
460 template<typename T, class Derived, class Other, class OtherRef>
462 boost::function<void (Derived&, T)>
463 bindOther( const boost::function<void (Other&, T)>& setter,
464 OtherRef Derived::*instance_ref )
471 &reference_from_pointer<Other, OtherRef>,
472 boost::bind(instance_ref, _1)
478 template<typename T1, typename T2, class Derived>
480 StyleSetterFuncUnchecked
481 bindStyleSetter( const std::string& name,
482 const boost::function<void (Derived&, T2)>& setter )
487 // We will only call setters with Derived instances, so we can safely
489 boost::bind(&derived_cast<Derived>, _1),
490 boost::bind(&getValue<T1>, _2)
494 bool isStyleEmpty(const SGPropertyNode* child) const;
495 bool canApplyStyle(const SGPropertyNode* child) const;
496 bool setStyleImpl( const SGPropertyNode* child,
497 const StyleInfo* style_info = 0 );
499 const StyleInfo* getStyleInfo(const std::string& name) const;
500 const StyleSetter* getStyleSetter(const std::string& name) const;
501 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
503 virtual void childAdded(SGPropertyNode * child) {}
504 virtual void childRemoved(SGPropertyNode * child){}
505 virtual void childChanged(SGPropertyNode * child){}
507 void setDrawable(osg::Drawable* drawable);
510 * Get stateset of drawable if available or use transform otherwise
512 osg::StateSet* getOrCreateStateSet();
518 osg::ref_ptr<osg::Drawable> _drawable;
520 Element(const Element&);// = delete
522 template<class Derived>
523 static Derived& derived_cast(Element& el)
525 return static_cast<Derived&>(el);
528 template<class T, class SharedPtr>
529 static T& reference_from_pointer(const SharedPtr& p)
531 return *get_pointer(p);
535 * Helper to call a function only of the element type can be converted to
538 * @return Whether the function has been called
540 template<class Derived>
543 static bool call( Element& el,
544 const SGPropertyNode* prop,
545 const StyleSetterFuncUnchecked& func )
547 Derived* d = dynamic_cast<Derived*>(&el);
556 } // namespace canvas
557 } // namespace simgear
559 #endif /* CANVAS_ELEMENT_HXX_ */