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 * Get whether the element is visible or hidden (Can be changed with
121 * setting property "visible" accordingly).
123 bool isVisible() const;
125 osg::MatrixTransform* getMatrixTransform();
126 osg::MatrixTransform const* getMatrixTransform() const;
128 virtual void childAdded( SGPropertyNode * parent,
129 SGPropertyNode * child );
130 virtual void childRemoved( SGPropertyNode * parent,
131 SGPropertyNode * child );
132 virtual void valueChanged(SGPropertyNode * child);
134 virtual bool setStyle( const SGPropertyNode* child,
135 const StyleInfo* style_info = 0 );
140 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
141 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
143 void setClip(const std::string& clip);
146 * Clipping coordinates reference frame
148 void setClipFrame(ReferenceFrame rf);
151 * Write the given bounding box to the property tree
153 void setBoundingBox(const osg::BoundingBox& bb);
156 * Get bounding box with children/drawables transformed by passed matrix
158 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
161 * Get the transformation matrix (product of all transforms)
163 osg::Matrix getMatrix() const;
166 * Create an canvas Element
168 * @tparam Derived Type of element (needs to derive from Element)
170 template<typename Derived>
172 typename boost::enable_if<
173 boost::is_base_of<Element, Derived>,
175 >::type create( const CanvasWeakPtr& canvas,
176 const SGPropertyNode_ptr& node,
180 ElementPtr el( new Derived(canvas, node, style, parent) );
190 BLEND_FUNC = TRANSFORM << 1,
191 SCISSOR_COORDS = BLEND_FUNC << 1,
192 LAST_ATTRIBUTE = SCISSOR_COORDS << 1
204 class RelativeScissor;
206 CanvasWeakPtr _canvas;
209 mutable uint32_t _attributes_dirty;
211 osg::observer_ptr<osg::MatrixTransform> _transform;
212 std::vector<TransformType> _transform_types;
215 std::vector<SGPropertyNode_ptr> _bounding_box;
216 RelativeScissor *_scissor;
218 typedef std::vector<EventListener> Listener;
219 typedef std::map<Event::Type, Listener> ListenerMap;
221 ListenerMap _listener;
223 typedef std::map<std::string, StyleInfo> StyleSetters;
224 static StyleSetters _style_setters;
226 static void staticInit();
228 Element( const CanvasWeakPtr& canvas,
229 const SGPropertyNode_ptr& node,
230 const Style& parent_style,
234 * Returns false on first call and true on any successive call. Use to
235 * perform initialization tasks which are only required once per element
238 * @tparam Derived (Derived) class type
240 template<class Derived>
243 static bool is_init = false;
252 * Register a function for setting a style specified by the given property
254 * @param name Property name
255 * @param type Interpolation type
256 * @param setter Setter function
258 * @tparam T1 Type of value used to retrieve value from property
260 * @tparam T2 Type of value the setter function expects
261 * @tparam Derived Type of class the setter can be applied to
263 * @note T1 needs to be convertible to T2
272 addStyle( const std::string& name,
273 const std::string& type,
274 const boost::function<void (Derived&, T2)>& setter,
275 bool inheritable = true )
277 StyleInfo& style_info = _style_setters[ name ];
280 if( !style_info.type.empty() && type != style_info.type )
285 "changing animation type for '" << name << "': "
286 << style_info.type << " -> " << type
289 style_info.type = type;
291 // TODO check if changed?
292 style_info.inheritable = inheritable;
294 StyleSetter* style = &style_info.setter;
298 style = style->next = new StyleSetter;
300 style->func = boost::bind
302 &type_match<Derived>::call,
305 bindStyleSetter<T1>(name, setter)
316 addStyle( const std::string& name,
317 const std::string& type,
318 const boost::function<void (Derived&, T)>& setter,
319 bool inheritable = true )
321 return addStyle<T, T>(name, type, setter, inheritable);
330 addStyle( const std::string& name,
331 const std::string& type,
332 void (Derived::*setter)(T),
333 bool inheritable = true )
335 return addStyle<T, T>
339 boost::function<void (Derived&, T)>(setter),
351 addStyle( const std::string& name,
352 const std::string& type,
353 void (Derived::*setter)(T2),
354 bool inheritable = true )
360 boost::function<void (Derived&, T2)>(setter),
370 addStyle( const std::string& name,
371 const std::string& type,
372 void (Derived::*setter)(const std::string&),
373 bool inheritable = true )
375 return addStyle<const char*, const std::string&>
379 boost::function<void (Derived&, const std::string&)>(setter),
392 addStyle( const std::string& name,
393 const std::string& type,
394 void (Other::*setter)(T),
395 OtherRef Derived::*instance_ref,
396 bool inheritable = true )
398 return addStyle<T, T>
402 bindOther(setter, instance_ref),
416 addStyle( const std::string& name,
417 const std::string& type,
418 void (Other::*setter)(T2),
419 OtherRef Derived::*instance_ref,
420 bool inheritable = true )
426 bindOther(setter, instance_ref),
440 addStyle( const std::string& name,
441 const std::string& type,
442 const boost::function<void (Other&, T2)>& setter,
443 OtherRef Derived::*instance_ref,
444 bool inheritable = true )
450 bindOther(setter, instance_ref),
462 addStyle( const std::string& name,
463 const std::string& type,
464 void (Other::*setter)(const std::string&),
465 OtherRef Derived::*instance_ref,
466 bool inheritable = true )
468 return addStyle<const char*, const std::string&>
472 boost::function<void (Other&, const std::string&)>(setter),
478 template<typename T, class Derived, class Other, class OtherRef>
480 boost::function<void (Derived&, T)>
481 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
483 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
486 template<typename T, class Derived, class Other, class OtherRef>
488 boost::function<void (Derived&, T)>
489 bindOther( const boost::function<void (Other&, T)>& setter,
490 OtherRef Derived::*instance_ref )
497 &reference_from_pointer<Other, OtherRef>,
498 boost::bind(instance_ref, _1)
504 template<typename T1, typename T2, class Derived>
506 StyleSetterFuncUnchecked
507 bindStyleSetter( const std::string& name,
508 const boost::function<void (Derived&, T2)>& setter )
513 // We will only call setters with Derived instances, so we can safely
515 boost::bind(&derived_cast<Derived>, _1),
516 boost::bind(&getValue<T1>, _2)
520 bool isStyleEmpty(const SGPropertyNode* child) const;
521 bool canApplyStyle(const SGPropertyNode* child) const;
522 bool setStyleImpl( const SGPropertyNode* child,
523 const StyleInfo* style_info = 0 );
525 const StyleInfo* getStyleInfo(const std::string& name) const;
526 const StyleSetter* getStyleSetter(const std::string& name) const;
527 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
529 virtual void childAdded(SGPropertyNode * child) {}
530 virtual void childRemoved(SGPropertyNode * child){}
531 virtual void childChanged(SGPropertyNode * child){}
533 void setDrawable(osg::Drawable* drawable);
536 * Get stateset of drawable if available or use transform otherwise
538 osg::StateSet* getOrCreateStateSet();
544 osg::ref_ptr<osg::Drawable> _drawable;
546 Element(const Element&);// = delete
548 template<class Derived>
549 static Derived& derived_cast(Element& el)
551 return static_cast<Derived&>(el);
554 template<class T, class SharedPtr>
555 static T& reference_from_pointer(const SharedPtr& p)
557 return *get_pointer(p);
561 * Helper to call a function only of the element type can be converted to
564 * @return Whether the function has been called
566 template<class Derived>
569 static bool call( Element& el,
570 const SGPropertyNode* prop,
571 const StyleSetterFuncUnchecked& func )
573 Derived* d = dynamic_cast<Derived*>(&el);
582 } // namespace canvas
585 struct enum_traits<canvas::Element::ReferenceFrame>
587 static const char* name()
589 return "canvas::Element::ReferenceFrame";
592 static canvas::Element::ReferenceFrame defVal()
594 return canvas::Element::GLOBAL;
597 static bool validate(int frame)
599 return frame >= canvas::Element::GLOBAL
600 && frame <= canvas::Element::LOCAL;
604 } // namespace simgear
606 #endif /* CANVAS_ELEMENT_HXX_ */