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;
132 virtual void childAdded( SGPropertyNode * parent,
133 SGPropertyNode * child );
134 virtual void childRemoved( SGPropertyNode * parent,
135 SGPropertyNode * child );
136 virtual void valueChanged(SGPropertyNode * child);
138 virtual bool setStyle( const SGPropertyNode* child,
139 const StyleInfo* style_info = 0 );
144 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
145 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
147 void setClip(const std::string& clip);
150 * Clipping coordinates reference frame
152 void setClipFrame(ReferenceFrame rf);
155 * Write the given bounding box to the property tree
157 void setBoundingBox(const osg::BoundingBox& bb);
160 * Get bounding box with children/drawables transformed by passed matrix
162 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
165 * Get the transformation matrix (product of all transforms)
167 osg::Matrix getMatrix() const;
170 * Create an canvas Element
172 * @tparam Derived Type of element (needs to derive from Element)
174 template<typename Derived>
176 typename boost::enable_if<
177 boost::is_base_of<Element, Derived>,
179 >::type create( const CanvasWeakPtr& canvas,
180 const SGPropertyNode_ptr& node,
184 ElementPtr el( new Derived(canvas, node, style, parent) );
194 BLEND_FUNC = TRANSFORM << 1,
195 SCISSOR_COORDS = BLEND_FUNC << 1,
196 LAST_ATTRIBUTE = SCISSOR_COORDS << 1
208 class RelativeScissor;
210 CanvasWeakPtr _canvas;
213 mutable uint32_t _attributes_dirty;
215 osg::observer_ptr<osg::MatrixTransform> _transform;
216 std::vector<TransformType> _transform_types;
219 std::vector<SGPropertyNode_ptr> _bounding_box;
220 RelativeScissor *_scissor;
222 typedef std::vector<EventListener> Listener;
223 typedef std::map<Event::Type, Listener> ListenerMap;
225 ListenerMap _listener;
227 typedef std::map<std::string, StyleInfo> StyleSetters;
228 static StyleSetters _style_setters;
230 static void staticInit();
232 Element( const CanvasWeakPtr& canvas,
233 const SGPropertyNode_ptr& node,
234 const Style& parent_style,
238 * Returns false on first call and true on any successive call. Use to
239 * perform initialization tasks which are only required once per element
242 * @tparam Derived (Derived) class type
244 template<class Derived>
247 static bool is_init = false;
256 * Register a function for setting a style specified by the given property
258 * @param name Property name
259 * @param type Interpolation type
260 * @param setter Setter function
262 * @tparam T1 Type of value used to retrieve value from property
264 * @tparam T2 Type of value the setter function expects
265 * @tparam Derived Type of class the setter can be applied to
267 * @note T1 needs to be convertible to T2
276 addStyle( const std::string& name,
277 const std::string& type,
278 const boost::function<void (Derived&, T2)>& setter,
279 bool inheritable = true )
281 StyleInfo& style_info = _style_setters[ name ];
284 if( !style_info.type.empty() && type != style_info.type )
289 "changing animation type for '" << name << "': "
290 << style_info.type << " -> " << type
293 style_info.type = type;
295 // TODO check if changed?
296 style_info.inheritable = inheritable;
298 StyleSetter* style = &style_info.setter;
302 style = style->next = new StyleSetter;
304 style->func = boost::bind
306 &type_match<Derived>::call,
309 bindStyleSetter<T1>(name, setter)
320 addStyle( const std::string& name,
321 const std::string& type,
322 const boost::function<void (Derived&, T)>& setter,
323 bool inheritable = true )
325 return addStyle<T, T>(name, type, setter, inheritable);
334 addStyle( const std::string& name,
335 const std::string& type,
336 void (Derived::*setter)(T),
337 bool inheritable = true )
339 return addStyle<T, T>
343 boost::function<void (Derived&, T)>(setter),
355 addStyle( const std::string& name,
356 const std::string& type,
357 void (Derived::*setter)(T2),
358 bool inheritable = true )
364 boost::function<void (Derived&, T2)>(setter),
374 addStyle( const std::string& name,
375 const std::string& type,
376 void (Derived::*setter)(const std::string&),
377 bool inheritable = true )
379 return addStyle<const char*, const std::string&>
383 boost::function<void (Derived&, const std::string&)>(setter),
396 addStyle( const std::string& name,
397 const std::string& type,
398 void (Other::*setter)(T),
399 OtherRef Derived::*instance_ref,
400 bool inheritable = true )
402 return addStyle<T, T>
406 bindOther(setter, instance_ref),
420 addStyle( const std::string& name,
421 const std::string& type,
422 void (Other::*setter)(T2),
423 OtherRef Derived::*instance_ref,
424 bool inheritable = true )
430 bindOther(setter, instance_ref),
444 addStyle( const std::string& name,
445 const std::string& type,
446 const boost::function<void (Other&, T2)>& setter,
447 OtherRef Derived::*instance_ref,
448 bool inheritable = true )
454 bindOther(setter, instance_ref),
466 addStyle( const std::string& name,
467 const std::string& type,
468 void (Other::*setter)(const std::string&),
469 OtherRef Derived::*instance_ref,
470 bool inheritable = true )
472 return addStyle<const char*, const std::string&>
476 boost::function<void (Other&, const std::string&)>(setter),
482 template<typename T, class Derived, class Other, class OtherRef>
484 boost::function<void (Derived&, T)>
485 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
487 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
490 template<typename T, class Derived, class Other, class OtherRef>
492 boost::function<void (Derived&, T)>
493 bindOther( const boost::function<void (Other&, T)>& setter,
494 OtherRef Derived::*instance_ref )
501 &reference_from_pointer<Other, OtherRef>,
502 boost::bind(instance_ref, _1)
508 template<typename T1, typename T2, class Derived>
510 StyleSetterFuncUnchecked
511 bindStyleSetter( const std::string& name,
512 const boost::function<void (Derived&, T2)>& setter )
517 // We will only call setters with Derived instances, so we can safely
519 boost::bind(&derived_cast<Derived>, _1),
520 boost::bind(&getValue<T1>, _2)
524 bool isStyleEmpty(const SGPropertyNode* child) const;
525 bool canApplyStyle(const SGPropertyNode* child) const;
526 bool setStyleImpl( const SGPropertyNode* child,
527 const StyleInfo* style_info = 0 );
529 const StyleInfo* getStyleInfo(const std::string& name) const;
530 const StyleSetter* getStyleSetter(const std::string& name) const;
531 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
533 virtual void childAdded(SGPropertyNode * child) {}
534 virtual void childRemoved(SGPropertyNode * child){}
535 virtual void childChanged(SGPropertyNode * child){}
537 void setDrawable(osg::Drawable* drawable);
540 * Get stateset of drawable if available or use transform otherwise
542 osg::StateSet* getOrCreateStateSet();
548 osg::ref_ptr<osg::Drawable> _drawable;
550 Element(const Element&);// = delete
552 template<class Derived>
553 static Derived& derived_cast(Element& el)
555 return static_cast<Derived&>(el);
558 template<class T, class SharedPtr>
559 static T& reference_from_pointer(const SharedPtr& p)
561 return *get_pointer(p);
565 * Helper to call a function only of the element type can be converted to
568 * @return Whether the function has been called
570 template<class Derived>
573 static bool call( Element& el,
574 const SGPropertyNode* prop,
575 const StyleSetterFuncUnchecked& func )
577 Derived* d = dynamic_cast<Derived*>(&el);
586 } // namespace canvas
589 struct enum_traits<canvas::Element::ReferenceFrame>
591 static const char* name()
593 return "canvas::Element::ReferenceFrame";
596 static canvas::Element::ReferenceFrame defVal()
598 return canvas::Element::GLOBAL;
601 static bool validate(int frame)
603 return frame >= canvas::Element::GLOBAL
604 && frame <= canvas::Element::LOCAL;
608 } // namespace simgear
610 #endif /* CANVAS_ELEMENT_HXX_ */