EventHandler 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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_EVENTHANDLER
  14. #define OSGGA_EVENTHANDLER 1
  15. #include <vector>
  16. #include <osg/Drawable>
  17. #include <osg/ApplicationUsage>
  18. #include <osgGA/Export>
  19. #include <osgGA/GUIEventAdapter>
  20. #include <osgGA/GUIActionAdapter>
  21. namespace osgGA{
  22. /**
  23. EventHandler is base class for adding handling of events, either as node event callback, drawable event callback or an event handler attached directly to the view(er)
  24. */
  25. class OSGGA_EXPORT EventHandler : public osg::NodeCallback, public osg::DrawableEventCallback
  26. {
  27. public:
  28. EventHandler() {}
  29. EventHandler(const EventHandler& eh,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  30. osg::Object(eh, copyop),
  31. osg::Callback(eh, copyop),
  32. osg::NodeCallback(eh, copyop),
  33. osg::DrawableEventCallback(eh, copyop) {}
  34. META_Object(osgGA, EventHandler);
  35. virtual NodeCallback* asNodeCallback() { return osg::NodeCallback::asNodeCallback(); }
  36. virtual const NodeCallback* asNodeCallback() const { return osg::NodeCallback::asNodeCallback(); }
  37. virtual DrawableEventCallback* asDrawableEventCallback() { return osg::DrawableEventCallback::asDrawableEventCallback(); }
  38. virtual const DrawableEventCallback* asDrawableEventCallback() const { return osg::DrawableEventCallback::asDrawableEventCallback(); }
  39. virtual EventHandler* asEventHandler() { return this; }
  40. virtual const EventHandler* asEventHandler() const { return this; }
  41. virtual bool run(osg::Object* object, osg::Object* data)
  42. {
  43. osg::Node* node = object->asNode();
  44. osg::NodeVisitor* nv = data->asNodeVisitor();
  45. operator()(node, nv);
  46. return true;
  47. }
  48. /** Event traversal node callback method. There is no need to override this method in subclasses of EventHandler as this implementation calls handle(..) for you. */
  49. virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
  50. /** Event traversal drawable callback method. There is no need to override this method in subclasses of EventHandler as this implementation calls handle(..) for you. */
  51. virtual void event(osg::NodeVisitor* nv, osg::Drawable* drawable);
  52. /** Handle event. Override the handle(..) method in your event handlers to respond to events. */
  53. virtual bool handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv);
  54. /** Get the user interface usage of this event handler, i.e. keyboard and mouse bindings.*/
  55. virtual void getUsage(osg::ApplicationUsage&) const {}
  56. protected:
  57. };
  58. }
  59. #endif