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
79 * Coordinate reference frame (eg. "clip" property)
83 GLOBAL, ///!< Global coordinates
84 PARENT, ///!< Coordinates relative to parent coordinate frame
85 LOCAL ///!< Coordinates relative to local coordinates (parent
86 /// coordinates with local transformations applied)
92 virtual ~Element() = 0;
94 virtual void setSelf(const PropertyBasedElementPtr& self);
95 virtual void onDestroy();
97 ElementWeakPtr getWeakPtr() const;
98 ElementPtr getParent();
101 * Called every frame to update internal state
103 * @param dt Frame time in seconds
105 virtual void update(double dt);
107 bool addEventListener(const std::string& type, const EventListener& cb);
108 virtual void clearEventListener();
110 virtual bool accept(EventVisitor& visitor);
111 virtual bool ascend(EventVisitor& visitor);
112 virtual bool traverse(EventVisitor& visitor);
114 virtual bool handleEvent(canvas::EventPtr event);
116 virtual bool hitBound( const osg::Vec2f& pos,
117 const osg::Vec2f& local_pos ) const;
120 * Set visibility of the element.
122 void setVisible(bool visible);
125 * Get whether the element is visible or hidden.
127 bool isVisible() const;
129 osg::MatrixTransform* getMatrixTransform();
130 osg::MatrixTransform const* getMatrixTransform() const;
133 * Transform position to local coordinages.
135 osg::Vec2f posToLocal(const osg::Vec2f& pos) const;
137 virtual void childAdded( SGPropertyNode * parent,
138 SGPropertyNode * child );
139 virtual void childRemoved( SGPropertyNode * parent,
140 SGPropertyNode * child );
141 virtual void valueChanged(SGPropertyNode * child);
143 virtual bool setStyle( const SGPropertyNode* child,
144 const StyleInfo* style_info = 0 );
149 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
150 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
152 void setClip(const std::string& clip);
155 * Clipping coordinates reference frame
157 void setClipFrame(ReferenceFrame rf);
160 * Write the given bounding box to the property tree
162 void setBoundingBox(const osg::BoundingBox& bb);
165 * Get bounding box with children/drawables transformed by passed matrix
167 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
170 * Get the transformation matrix (product of all transforms)
172 osg::Matrix getMatrix() const;
175 * Create an canvas Element
177 * @tparam Derived Type of element (needs to derive from Element)
179 template<typename Derived>
181 typename boost::enable_if<
182 boost::is_base_of<Element, Derived>,
184 >::type create( const CanvasWeakPtr& canvas,
185 const SGPropertyNode_ptr& node,
189 ElementPtr el( new Derived(canvas, node, style, parent) );
199 BLEND_FUNC = TRANSFORM << 1,
200 SCISSOR_COORDS = BLEND_FUNC << 1,
201 LAST_ATTRIBUTE = SCISSOR_COORDS << 1
213 class RelativeScissor;
215 CanvasWeakPtr _canvas;
218 mutable uint32_t _attributes_dirty;
220 osg::observer_ptr<osg::MatrixTransform> _transform;
221 std::vector<TransformType> _transform_types;
224 std::vector<SGPropertyNode_ptr> _bounding_box;
225 RelativeScissor *_scissor;
227 typedef std::vector<EventListener> Listener;
228 typedef std::map<Event::Type, Listener> ListenerMap;
230 ListenerMap _listener;
232 typedef std::map<std::string, StyleInfo> StyleSetters;
233 static StyleSetters _style_setters;
235 static void staticInit();
237 Element( const CanvasWeakPtr& canvas,
238 const SGPropertyNode_ptr& node,
239 const Style& parent_style,
243 * Returns false on first call and true on any successive call. Use to
244 * perform initialization tasks which are only required once per element
247 * @tparam Derived (Derived) class type
249 template<class Derived>
252 static bool is_init = false;
261 * Register a function for setting a style specified by the given property
263 * @param name Property name
264 * @param type Interpolation type
265 * @param setter Setter function
267 * @tparam T1 Type of value used to retrieve value from property
269 * @tparam T2 Type of value the setter function expects
270 * @tparam Derived Type of class the setter can be applied to
272 * @note T1 needs to be convertible to T2
281 addStyle( const std::string& name,
282 const std::string& type,
283 const boost::function<void (Derived&, T2)>& setter,
284 bool inheritable = true )
286 StyleInfo& style_info = _style_setters[ name ];
289 if( !style_info.type.empty() && type != style_info.type )
294 "changing animation type for '" << name << "': "
295 << style_info.type << " -> " << type
298 style_info.type = type;
300 // TODO check if changed?
301 style_info.inheritable = inheritable;
303 StyleSetter* style = &style_info.setter;
307 style = style->next = new StyleSetter;
309 style->func = boost::bind
311 &type_match<Derived>::call,
314 bindStyleSetter<T1>(name, setter)
325 addStyle( const std::string& name,
326 const std::string& type,
327 const boost::function<void (Derived&, T)>& setter,
328 bool inheritable = true )
330 return addStyle<T, T>(name, type, setter, inheritable);
339 addStyle( const std::string& name,
340 const std::string& type,
341 void (Derived::*setter)(T),
342 bool inheritable = true )
344 return addStyle<T, T>
348 boost::function<void (Derived&, T)>(setter),
360 addStyle( const std::string& name,
361 const std::string& type,
362 void (Derived::*setter)(T2),
363 bool inheritable = true )
369 boost::function<void (Derived&, T2)>(setter),
379 addStyle( const std::string& name,
380 const std::string& type,
381 void (Derived::*setter)(const std::string&),
382 bool inheritable = true )
384 return addStyle<const char*, const std::string&>
388 boost::function<void (Derived&, const std::string&)>(setter),
401 addStyle( const std::string& name,
402 const std::string& type,
403 void (Other::*setter)(T),
404 OtherRef Derived::*instance_ref,
405 bool inheritable = true )
407 return addStyle<T, T>
411 bindOther(setter, instance_ref),
425 addStyle( const std::string& name,
426 const std::string& type,
427 void (Other::*setter)(T2),
428 OtherRef Derived::*instance_ref,
429 bool inheritable = true )
435 bindOther(setter, instance_ref),
449 addStyle( const std::string& name,
450 const std::string& type,
451 const boost::function<void (Other&, T2)>& setter,
452 OtherRef Derived::*instance_ref,
453 bool inheritable = true )
459 bindOther(setter, instance_ref),
471 addStyle( const std::string& name,
472 const std::string& type,
473 void (Other::*setter)(const std::string&),
474 OtherRef Derived::*instance_ref,
475 bool inheritable = true )
477 return addStyle<const char*, const std::string&>
481 boost::function<void (Other&, const std::string&)>(setter),
487 template<typename T, class Derived, class Other, class OtherRef>
489 boost::function<void (Derived&, T)>
490 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
492 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
495 template<typename T, class Derived, class Other, class OtherRef>
497 boost::function<void (Derived&, T)>
498 bindOther( const boost::function<void (Other&, T)>& setter,
499 OtherRef Derived::*instance_ref )
506 &reference_from_pointer<Other, OtherRef>,
507 boost::bind(instance_ref, _1)
513 template<typename T1, typename T2, class Derived>
515 StyleSetterFuncUnchecked
516 bindStyleSetter( const std::string& name,
517 const boost::function<void (Derived&, T2)>& setter )
522 // We will only call setters with Derived instances, so we can safely
524 boost::bind(&derived_cast<Derived>, _1),
525 boost::bind(&getValue<T1>, _2)
529 bool isStyleEmpty(const SGPropertyNode* child) const;
530 bool canApplyStyle(const SGPropertyNode* child) const;
531 bool setStyleImpl( const SGPropertyNode* child,
532 const StyleInfo* style_info = 0 );
534 const StyleInfo* getStyleInfo(const std::string& name) const;
535 const StyleSetter* getStyleSetter(const std::string& name) const;
536 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
538 virtual void childAdded(SGPropertyNode * child) {}
539 virtual void childRemoved(SGPropertyNode * child){}
540 virtual void childChanged(SGPropertyNode * child){}
542 void setDrawable(osg::Drawable* drawable);
545 * Get stateset of drawable if available or use transform otherwise
547 osg::StateSet* getOrCreateStateSet();
553 osg::ref_ptr<osg::Drawable> _drawable;
555 Element(const Element&);// = delete
557 template<class Derived>
558 static Derived& derived_cast(Element& el)
560 return static_cast<Derived&>(el);
563 template<class T, class SharedPtr>
564 static T& reference_from_pointer(const SharedPtr& p)
566 return *get_pointer(p);
570 * Helper to call a function only of the element type can be converted to
573 * @return Whether the function has been called
575 template<class Derived>
578 static bool call( Element& el,
579 const SGPropertyNode* prop,
580 const StyleSetterFuncUnchecked& func )
582 Derived* d = dynamic_cast<Derived*>(&el);
591 } // namespace canvas
594 struct enum_traits<canvas::Element::ReferenceFrame>
596 static const char* name()
598 return "canvas::Element::ReferenceFrame";
601 static canvas::Element::ReferenceFrame defVal()
603 return canvas::Element::GLOBAL;
606 static bool validate(int frame)
608 return frame >= canvas::Element::GLOBAL
609 && frame <= canvas::Element::LOCAL;
613 } // namespace simgear
615 #endif /* CANVAS_ELEMENT_HXX_ */