EventVisitor 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 OSGGA_EVENTVISITOR
  14. #define OSGGA_EVENTVISITOR 1
  15. #include <osg/NodeVisitor>
  16. #include <osg/Node>
  17. #include <osg/Geode>
  18. #include <osg/Billboard>
  19. #include <osg/LOD>
  20. #include <osg/Switch>
  21. #include <osg/LightSource>
  22. #include <osg/Transform>
  23. #include <osg/Projection>
  24. #include <osg/OccluderNode>
  25. #include <osg/ScriptEngine>
  26. #include <osgGA/GUIEventAdapter>
  27. #include <osgGA/GUIEventHandler>
  28. #include <osgGA/GUIActionAdapter>
  29. #include <osgGA/EventQueue>
  30. namespace osgGA {
  31. /**
  32. * Basic EventVisitor implementation for animating a scene.
  33. * This visitor traverses the scene graph, calling each nodes appCallback if
  34. * it exists.
  35. */
  36. class OSGGA_EXPORT EventVisitor : public osg::NodeVisitor
  37. {
  38. public:
  39. EventVisitor();
  40. virtual ~EventVisitor();
  41. META_NodeVisitor(osgGA, EventVisitor)
  42. /** Convert 'this' into a osgGA::EventVisitor pointer if Object is a osgGA::EventVisitor, otherwise return 0.
  43. * Equivalent to dynamic_cast<osgGA::EventVisitor*>(this).*/
  44. virtual osgGA::EventVisitor* asEventVisitor() { return this; }
  45. /** convert 'const this' into a const osgGA::EventVisitor pointer if Object is a osgGA::EventVisitor, otherwise return 0.
  46. * Equivalent to dynamic_cast<const osgGA::EventVisitor*>(this).*/
  47. virtual const osgGA::EventVisitor* asEventVisitor() const { return this; }
  48. void setActionAdapter(osgGA::GUIActionAdapter* actionAdapter) { _actionAdapter=actionAdapter; }
  49. osgGA::GUIActionAdapter* getActionAdapter() { return _actionAdapter; }
  50. const osgGA::GUIActionAdapter* getActionAdapter() const { return _actionAdapter; }
  51. void addEvent(Event* event);
  52. void removeEvent(Event* event);
  53. void setEventHandled(bool handled) { _handled = handled; }
  54. bool getEventHandled() const { return _handled; }
  55. void setEvents(const EventQueue::Events& events) { _events = events; }
  56. EventQueue::Events& getEvents() { return _events; }
  57. const EventQueue::Events& getEvents() const { return _events; }
  58. public:
  59. virtual void reset();
  60. /** During traversal each type of node calls its callbacks and its children traversed. */
  61. virtual void apply(osg::Node& node) { handle_callbacks_and_traverse(node); }
  62. virtual void apply(osg::Drawable& drawable)
  63. {
  64. osg::Callback* callback = drawable.getEventCallback();
  65. if (callback)
  66. {
  67. osgGA::EventHandler* eh = callback->asEventHandler();
  68. if (eh)
  69. {
  70. callback->run(&drawable,this);
  71. }
  72. else
  73. {
  74. osg::DrawableEventCallback* drawable_callback = callback->asDrawableEventCallback();
  75. osg::NodeCallback* node_callback = callback->asNodeCallback();
  76. osg::CallbackObject* callback_object = callback->asCallbackObject();
  77. if (drawable_callback) drawable_callback->event(this,&drawable);
  78. if (node_callback) (*node_callback)(&drawable, this);
  79. if (callback_object) callback_object->run(&drawable, this);
  80. if (!drawable_callback && !node_callback && !callback_object) callback->run(&drawable, this);
  81. }
  82. }
  83. handle_callbacks(drawable.getStateSet());
  84. }
  85. // The following overrides are technically redundant as the default implementation would eventually trickle down to
  86. // apply(osg::Node&); - however defining these explicitly should save a couple of virtual function calls
  87. virtual void apply(osg::Geode& node) { handle_callbacks_and_traverse(node); }
  88. virtual void apply(osg::Billboard& node) { handle_callbacks_and_traverse(node); }
  89. virtual void apply(osg::LightSource& node) { handle_callbacks_and_traverse(node); }
  90. virtual void apply(osg::Group& node) { handle_callbacks_and_traverse(node); }
  91. virtual void apply(osg::Transform& node) { handle_callbacks_and_traverse(node); }
  92. virtual void apply(osg::Projection& node) { handle_callbacks_and_traverse(node); }
  93. virtual void apply(osg::Switch& node) { handle_callbacks_and_traverse(node); }
  94. virtual void apply(osg::LOD& node) { handle_callbacks_and_traverse(node); }
  95. virtual void apply(osg::OccluderNode& node) { handle_callbacks_and_traverse(node); }
  96. protected:
  97. /** Prevent unwanted copy operator.*/
  98. EventVisitor& operator = (const EventVisitor&) { return *this; }
  99. inline void handle_callbacks(osg::StateSet* stateset)
  100. {
  101. if (stateset && stateset->requiresEventTraversal())
  102. {
  103. stateset->runEventCallbacks(this);
  104. }
  105. }
  106. inline void handle_callbacks_and_traverse(osg::Node& node)
  107. {
  108. handle_callbacks(node.getStateSet());
  109. osg::Callback* callback = node.getEventCallback();
  110. if (callback) callback->run(&node,this);
  111. else if (node.getNumChildrenRequiringEventTraversal()>0) traverse(node);
  112. }
  113. osgGA::GUIActionAdapter* _actionAdapter;
  114. osg::ref_ptr<GUIEventAdapter> _accumulateEventState;
  115. bool _handled;
  116. EventQueue::Events _events;
  117. };
  118. }
  119. #endif