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
26 #include <simgear/nasal/cppbind/Ghost.hxx>
28 #include <osg/BoundingBox>
29 #include <osg/MatrixTransform>
31 #include <boost/bind.hpp>
32 #include <boost/function.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 bool addNasalEventListener(const std::string& type, naRef code);
98 virtual void clearEventListener();
100 virtual bool accept(EventVisitor& visitor);
101 virtual bool ascend(EventVisitor& visitor);
102 virtual bool traverse(EventVisitor& visitor);
104 virtual bool handleEvent(canvas::EventPtr event);
106 virtual bool hitBound( const osg::Vec2f& pos,
107 const osg::Vec2f& local_pos ) const;
110 * Get whether the element is visible or hidden (Can be changed with
111 * setting property "visible" accordingly).
113 bool isVisible() const;
115 osg::MatrixTransform* getMatrixTransform();
116 osg::MatrixTransform const* getMatrixTransform() const;
118 virtual void childAdded( SGPropertyNode * parent,
119 SGPropertyNode * child );
120 virtual void childRemoved( SGPropertyNode * parent,
121 SGPropertyNode * child );
122 virtual void valueChanged(SGPropertyNode * child);
124 virtual bool setStyle( const SGPropertyNode* child,
125 const StyleInfo* style_info = 0 );
130 * @note Only "rect(<top>, <right>, <bottom>, <left>)" is supported
131 * @see http://www.w3.org/TR/CSS21/visufx.html#propdef-clip
133 void setClip(const std::string& clip);
136 * Write the given bounding box to the property tree
138 void setBoundingBox(const osg::BoundingBox& bb);
141 * Get bounding box with children/drawables transformed by passed matrix
143 virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const;
146 * Create an canvas Element
148 * @tparam Derived Type of element (needs to derive from Element)
150 template<typename Derived>
152 typename boost::enable_if<
153 boost::is_base_of<Element, Derived>,
155 >::type create( const CanvasWeakPtr& canvas,
156 const SGPropertyNode_ptr& node,
160 ElementPtr el( new Derived(canvas, node, style, parent) );
170 LAST_ATTRIBUTE = BLEND_FUNC << 1
182 CanvasWeakPtr _canvas;
185 uint32_t _attributes_dirty;
187 bool _transform_dirty;
188 osg::observer_ptr<osg::MatrixTransform> _transform;
189 std::vector<TransformType> _transform_types;
192 std::vector<SGPropertyNode_ptr> _bounding_box;
194 typedef std::vector<EventListener> Listener;
195 typedef std::map<Event::Type, Listener> ListenerMap;
197 ListenerMap _listener;
199 typedef std::map<std::string, StyleInfo> StyleSetters;
200 static StyleSetters _style_setters;
202 static void staticInit();
204 Element( const CanvasWeakPtr& canvas,
205 const SGPropertyNode_ptr& node,
206 const Style& parent_style,
210 * Returns false on first call and true on any successive call. Use to
211 * perform initialization tasks which are only required once per element
214 * @tparam Derived (Derived) class type
216 template<class Derived>
219 static bool is_init = false;
228 * Register a function for setting a style specified by the given property
230 * @param name Property name
231 * @param type Interpolation type
232 * @param setter Setter function
234 * @tparam T1 Type of value used to retrieve value from property
236 * @tparam T2 Type of value the setter function expects
237 * @tparam Derived Type of class the setter can be applied to
239 * @note T1 needs to be convertible to T2
248 addStyle( const std::string& name,
249 const std::string& type,
250 const boost::function<void (Derived&, T2)>& setter,
251 bool inheritable = true )
253 StyleInfo& style_info = _style_setters[ name ];
256 if( !style_info.type.empty() && type != style_info.type )
261 "changing animation type for '" << name << "': "
262 << style_info.type << " -> " << type
265 style_info.type = type;
267 // TODO check if changed?
268 style_info.inheritable = inheritable;
270 StyleSetter* style = &style_info.setter;
274 style = style->next = new StyleSetter;
276 style->func = boost::bind
278 &type_match<Derived>::call,
281 bindStyleSetter<T1>(name, setter)
292 addStyle( const std::string& name,
293 const std::string& type,
294 const boost::function<void (Derived&, T)>& setter,
295 bool inheritable = true )
297 return addStyle<T, T>(name, type, setter, inheritable);
306 addStyle( const std::string& name,
307 const std::string& type,
308 void (Derived::*setter)(T),
309 bool inheritable = true )
311 return addStyle<T, T>
315 boost::function<void (Derived&, T)>(setter),
327 addStyle( const std::string& name,
328 const std::string& type,
329 void (Derived::*setter)(T2),
330 bool inheritable = true )
336 boost::function<void (Derived&, T2)>(setter),
346 addStyle( const std::string& name,
347 const std::string& type,
348 void (Derived::*setter)(const std::string&),
349 bool inheritable = true )
351 return addStyle<const char*, const std::string&>
355 boost::function<void (Derived&, const std::string&)>(setter),
368 addStyle( const std::string& name,
369 const std::string& type,
370 void (Other::*setter)(T),
371 OtherRef Derived::*instance_ref,
372 bool inheritable = true )
374 return addStyle<T, T>
378 bindOther(setter, instance_ref),
392 addStyle( const std::string& name,
393 const std::string& type,
394 void (Other::*setter)(T2),
395 OtherRef Derived::*instance_ref,
396 bool inheritable = true )
402 bindOther(setter, instance_ref),
416 addStyle( const std::string& name,
417 const std::string& type,
418 const boost::function<void (Other&, T2)>& setter,
419 OtherRef Derived::*instance_ref,
420 bool inheritable = true )
426 bindOther(setter, instance_ref),
438 addStyle( const std::string& name,
439 const std::string& type,
440 void (Other::*setter)(const std::string&),
441 OtherRef Derived::*instance_ref,
442 bool inheritable = true )
444 return addStyle<const char*, const std::string&>
448 boost::function<void (Other&, const std::string&)>(setter),
454 template<typename T, class Derived, class Other, class OtherRef>
456 boost::function<void (Derived&, T)>
457 bindOther( void (Other::*setter)(T), OtherRef Derived::*instance_ref )
459 return boost::bind(setter, boost::bind(instance_ref, _1), _2);
462 template<typename T, class Derived, class Other, class OtherRef>
464 boost::function<void (Derived&, T)>
465 bindOther( const boost::function<void (Other&, T)>& setter,
466 OtherRef Derived::*instance_ref )
473 &reference_from_pointer<Other, OtherRef>,
474 boost::bind(instance_ref, _1)
480 template<typename T1, typename T2, class Derived>
482 StyleSetterFuncUnchecked
483 bindStyleSetter( const std::string& name,
484 const boost::function<void (Derived&, T2)>& setter )
489 // We will only call setters with Derived instances, so we can safely
491 boost::bind(&derived_cast<Derived>, _1),
492 boost::bind(&getValue<T1>, _2)
496 bool isStyleEmpty(const SGPropertyNode* child) const;
497 bool canApplyStyle(const SGPropertyNode* child) const;
498 bool setStyleImpl( const SGPropertyNode* child,
499 const StyleInfo* style_info = 0 );
501 const StyleInfo* getStyleInfo(const std::string& name) const;
502 const StyleSetter* getStyleSetter(const std::string& name) const;
503 const SGPropertyNode* getParentStyle(const SGPropertyNode* child) const;
505 virtual void childAdded(SGPropertyNode * child) {}
506 virtual void childRemoved(SGPropertyNode * child){}
507 virtual void childChanged(SGPropertyNode * child){}
509 void setDrawable(osg::Drawable* drawable);
512 * Get stateset of drawable if available or use transform otherwise
514 osg::StateSet* getOrCreateStateSet();
520 osg::ref_ptr<osg::Drawable> _drawable;
522 Element(const Element&);// = delete
524 template<class Derived>
525 static Derived& derived_cast(Element& el)
527 return static_cast<Derived&>(el);
530 template<class T, class SharedPtr>
531 static T& reference_from_pointer(const SharedPtr& p)
533 return *get_pointer(p);
537 * Helper to call a function only of the element type can be converted to
540 * @return Whether the function has been called
542 template<class Derived>
545 static bool call( Element& el,
546 const SGPropertyNode* prop,
547 const StyleSetterFuncUnchecked& func )
549 Derived* d = dynamic_cast<Derived*>(&el);
558 } // namespace canvas
559 } // namespace simgear
561 #endif /* CANVAS_ELEMENT_HXX_ */