Callback 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSG_CALLBACK
  14. #define OSG_CALLBACK 1
  15. #include <osg/Object>
  16. #include <osg/UserDataContainer>
  17. // forward declare
  18. namespace osgGA { class EventHandler; }
  19. namespace osg {
  20. // forward declare
  21. class CallbackObject;
  22. class NodeCallback;
  23. class StateAttributeCallback;
  24. class UniformCallback;
  25. class DrawableUpdateCallback;
  26. class DrawableEventCallback;
  27. class DrawableCullCallback;
  28. class OSG_EXPORT Callback : public virtual Object {
  29. public :
  30. Callback(){}
  31. Callback(const Callback& cb,const CopyOp& copyop):
  32. osg::Object(cb, copyop),
  33. _nestedCallback(cb._nestedCallback) {}
  34. META_Object(osg, Callback);
  35. virtual Callback* asCallback() { return this; }
  36. virtual const Callback* asCallback() const { return this; }
  37. virtual CallbackObject* asCallbackObject() { return 0; }
  38. virtual const CallbackObject* asCallbackObject() const { return 0; }
  39. virtual NodeCallback* asNodeCallback() { return 0; }
  40. virtual const NodeCallback* asNodeCallback() const { return 0; }
  41. virtual StateAttributeCallback* asStateAttributeCallback() { return 0; }
  42. virtual const StateAttributeCallback* asStateAttributeCallback() const { return 0; }
  43. virtual UniformCallback* asUniformCallback() { return 0; }
  44. virtual const UniformCallback* asUniformCallback() const { return 0; }
  45. virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return 0; }
  46. virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return 0; }
  47. virtual DrawableEventCallback* asDrawableEventCallback() { return 0; }
  48. virtual const DrawableEventCallback* asDrawableEventCallback() const { return 0; }
  49. virtual DrawableCullCallback* asDrawableCullCallback() { return 0; }
  50. virtual const DrawableCullCallback* asDrawableCullCallback() const { return 0; }
  51. virtual osgGA::EventHandler* asEventHandler() { return 0; }
  52. virtual const osgGA::EventHandler* asEventHandler() const { return 0; }
  53. /** Invoke the callback, first parameter is the Object that the callback is attached to,
  54. * the second parameter, the data, is typically the NodeVisitor that is invoking the callback.
  55. * The run(..) method may be overridden by users directly, or if the user is using one of the old
  56. * style callbacks such as NodeCallback or Drawable::UpdateCallback then you can just override
  57. * the appropriate callback method on those callback subclasses.
  58. * If you are implementing your own callback then one should call traverse() to make sure nested callbacks
  59. * and visitor traversal() is completed. */
  60. virtual bool run(osg::Object* object, osg::Object* data)
  61. {
  62. return traverse(object, data);
  63. }
  64. /** traverse the nested callbacks or call NodeVisitor::traverse() if the object is Node, and data is NodeVisitor.*/
  65. bool traverse(osg::Object* object, osg::Object* data);
  66. void setNestedCallback(osg::Callback* cb) { _nestedCallback = cb; }
  67. osg::Callback* getNestedCallback() { return _nestedCallback.get(); }
  68. const osg::Callback* getNestedCallback() const { return _nestedCallback.get(); }
  69. inline void addNestedCallback(osg::Callback* nc)
  70. {
  71. if (nc)
  72. {
  73. if (_nestedCallback.valid())
  74. {
  75. _nestedCallback->addNestedCallback(nc);
  76. }
  77. else
  78. {
  79. _nestedCallback = nc;
  80. }
  81. }
  82. }
  83. inline void removeNestedCallback(osg::Callback* nc)
  84. {
  85. if (nc)
  86. {
  87. if (_nestedCallback==nc)
  88. {
  89. ref_ptr<osg::Callback> new_nested_callback = _nestedCallback->getNestedCallback();
  90. _nestedCallback->setNestedCallback(0);
  91. _nestedCallback = new_nested_callback;
  92. }
  93. else if (_nestedCallback.valid())
  94. {
  95. _nestedCallback->removeNestedCallback(nc);
  96. }
  97. }
  98. }
  99. protected:
  100. virtual ~Callback() {}
  101. ref_ptr<Callback> _nestedCallback;
  102. };
  103. typedef std::vector< osg::ref_ptr<osg::Object> > Parameters;
  104. /** Callback for attaching a script to a Node's via there UserDataContainer for the purpose of overriding class methods within scripts.*/
  105. class OSG_EXPORT CallbackObject : public virtual osg::Callback
  106. {
  107. public:
  108. CallbackObject() {}
  109. CallbackObject(const std::string& name) { setName(name); }
  110. CallbackObject(const CallbackObject& co, const osg::CopyOp copyop=osg::CopyOp::SHALLOW_COPY):
  111. osg::Object(co, copyop),
  112. osg::Callback(co,copyop) {}
  113. META_Object(osg, CallbackObject);
  114. virtual CallbackObject* asCallbackObject() { return this; }
  115. virtual const CallbackObject* asCallbackObject() const { return this; }
  116. /** override Callback::run() entry point to adapt to CallbackObject::run(..) method.*/
  117. bool run(osg::Object* object, osg::Object* data);
  118. inline bool run(osg::Object* object) const
  119. {
  120. osg::Parameters inputParameters;
  121. osg::Parameters outputParameters;
  122. return run(object, inputParameters, outputParameters);
  123. }
  124. virtual bool run(osg::Object* object, osg::Parameters& inputParameters, osg::Parameters& outputParameters) const;
  125. };
  126. /** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
  127. inline CallbackObject* getCallbackObject(osg::Object* object, const std::string& name)
  128. {
  129. osg::UserDataContainer* udc = object->getUserDataContainer();
  130. if (!udc) return 0;
  131. osg::Object* obj = udc->getUserObject(name);
  132. if (!obj) return 0;
  133. return obj->asCallbackObject();
  134. }
  135. /** Convenience function for getting the CallbackObject associated with specified name from an Object's UserDataContainer.*/
  136. inline const CallbackObject* getCallbackObject(const osg::Object* object, const std::string& name)
  137. {
  138. const osg::UserDataContainer* udc = object->getUserDataContainer();
  139. if (!udc) return 0;
  140. const osg::Object* obj = udc->getUserObject(name);
  141. if (!obj) return 0;
  142. return obj->asCallbackObject();
  143. }
  144. /** Call run(..) on named CallbackObjects attached to specified Object. Return true if at least one CallbackObject has been successfully invoked.*/
  145. inline bool runNamedCallbackObjects(osg::Object* object, const std::string& name, osg::Parameters& inputParameters, osg::Parameters& outputParameters)
  146. {
  147. bool result = false;
  148. osg::UserDataContainer* udc = object->getUserDataContainer();
  149. if (udc)
  150. {
  151. for(unsigned int i = 0; i<udc->getNumUserObjects(); ++i)
  152. {
  153. osg::Object* obj = udc->getUserObject(i);
  154. if (obj && obj->getName()==name)
  155. {
  156. osg::CallbackObject* co = obj->asCallbackObject();
  157. if (co) result = co->run(object, inputParameters, outputParameters) | result;
  158. }
  159. }
  160. }
  161. return result;
  162. }
  163. // forward declare
  164. class Node;
  165. class NodeVisitor;
  166. /** Deprecated. */
  167. class OSG_EXPORT NodeCallback : public virtual Callback {
  168. public :
  169. NodeCallback(){}
  170. NodeCallback(const NodeCallback& nc,const CopyOp& copyop):
  171. Object(nc, copyop),
  172. Callback(nc, copyop) {}
  173. META_Object(osg,NodeCallback);
  174. virtual NodeCallback* asNodeCallback() { return this; }
  175. virtual const NodeCallback* asNodeCallback() const { return this; }
  176. /** NodeCallback overrides the Callback::run() method to adapt it the old style NodeCallback::operator()(Node* node, NodeVisitor* nv) method.*/
  177. virtual bool run(osg::Object* object, osg::Object* data);
  178. /** Callback method called by the NodeVisitor when visiting a node.*/
  179. virtual void operator()(Node* node, NodeVisitor* nv);
  180. protected:
  181. virtual ~NodeCallback() {}
  182. };
  183. // forward declare
  184. class StateAttribute;
  185. /** Deprecated. */
  186. class OSG_EXPORT StateAttributeCallback : public virtual osg::Callback
  187. {
  188. public:
  189. StateAttributeCallback() {}
  190. StateAttributeCallback(const StateAttributeCallback& org,const CopyOp& copyop) :
  191. Object(org, copyop),
  192. Callback(org, copyop) {}
  193. META_Object(osg,StateAttributeCallback);
  194. virtual StateAttributeCallback* asStateAttributeCallback() { return this; }
  195. virtual const StateAttributeCallback* asStateAttributeCallback() const { return this; }
  196. /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
  197. virtual bool run(osg::Object* object, osg::Object* data);
  198. /** do customized update code.*/
  199. virtual void operator () (StateAttribute*, NodeVisitor*) {}
  200. };
  201. // forward declare
  202. class Uniform;
  203. /** Deprecated. */
  204. class OSG_EXPORT UniformCallback : public virtual osg::Callback
  205. {
  206. public:
  207. UniformCallback() {}
  208. UniformCallback(const UniformCallback& org, const CopyOp& copyop) :
  209. Object(org, copyop),
  210. Callback(org, copyop) {}
  211. META_Object(osg, UniformCallback);
  212. virtual UniformCallback* asUniformCallback() { return this; }
  213. virtual const UniformCallback* asUniformCallback() const { return this; }
  214. /** override Callback::run() entry point to adapt to UniformCallback::run(..) method.*/
  215. virtual bool run(osg::Object* object, osg::Object* data);
  216. /** do customized update code.*/
  217. virtual void operator () (Uniform*, NodeVisitor*) {}
  218. };
  219. // forward declare
  220. class Drawable;
  221. class State;
  222. class RenderInfo;
  223. class OSG_EXPORT DrawableUpdateCallback : public virtual Callback
  224. {
  225. public:
  226. DrawableUpdateCallback() {}
  227. DrawableUpdateCallback(const DrawableUpdateCallback& org,const CopyOp& copyop):
  228. Object(org, copyop),
  229. Callback(org, copyop) {}
  230. META_Object(osg,DrawableUpdateCallback);
  231. virtual DrawableUpdateCallback* asDrawableUpdateCallback() { return this; }
  232. virtual const DrawableUpdateCallback* asDrawableUpdateCallback() const { return this; }
  233. /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
  234. virtual bool run(osg::Object* object, osg::Object* data);
  235. /** do customized update code.*/
  236. virtual void update(osg::NodeVisitor*, osg::Drawable*) {}
  237. };
  238. class OSG_EXPORT DrawableEventCallback : public virtual Callback
  239. {
  240. public:
  241. DrawableEventCallback() {}
  242. DrawableEventCallback(const DrawableEventCallback& org, const CopyOp& copyop) :
  243. Object(org, copyop),
  244. Callback(org, copyop) {}
  245. META_Object(osg,DrawableEventCallback);
  246. virtual DrawableEventCallback* asDrawableEventCallback() { return this; }
  247. virtual const DrawableEventCallback* asDrawableEventCallback() const { return this; }
  248. /** override Callback::run() entry point to adapt to StateAttributeCallback::run(..) method.*/
  249. virtual bool run(osg::Object* object, osg::Object* data);
  250. /** do customized Event code. */
  251. virtual void event(osg::NodeVisitor*, osg::Drawable*) {}
  252. };
  253. class OSG_EXPORT DrawableCullCallback : public virtual Callback
  254. {
  255. public:
  256. DrawableCullCallback() {}
  257. DrawableCullCallback(const DrawableCullCallback& org, const CopyOp& copyop) :
  258. Object(org, copyop),
  259. Callback(org, copyop) {}
  260. META_Object(osg,DrawableCullCallback);
  261. virtual DrawableCullCallback* asDrawableCullCallback() { return this; }
  262. virtual const DrawableCullCallback* asDrawableCullCallback() const { return this; }
  263. // just use the standard run implementation to passes run onto any nested callbacks.
  264. using Callback::run;
  265. /** deprecated.*/
  266. virtual bool cull(osg::NodeVisitor*, osg::Drawable*, osg::State*) const { return false; }
  267. /** do customized cull code, return true if drawable should be culled.*/
  268. virtual bool cull(osg::NodeVisitor* nv, osg::Drawable* drawable, osg::RenderInfo* renderInfo) const;
  269. };
  270. } // namespace
  271. #endif