KeySwitchMatrixManipulator 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 OSGUTIL_KEYSWITCMATRIXMANIPULATOR
  14. #define OSGUTIL_KEYSWITCMATRIXMANIPULATOR 1
  15. #include <osgGA/Export>
  16. #include <osgGA/CameraManipulator>
  17. #include <osgGA/GUIEventHandler>
  18. namespace osgGA{
  19. class GUIActionAdapter;
  20. /**
  21. KeySwitchMatrixManipulator is a decorator which allows the type of camera manipulator
  22. being used to be switched by pressing a key. E.g. '1' for a TrackballManipulator,
  23. '2' for a DriveManipulator, '3' for a FlightManipulator. The manipulators available,
  24. and the associated switch keys, can be configured.
  25. */
  26. class OSGGA_EXPORT KeySwitchMatrixManipulator : public CameraManipulator
  27. {
  28. public:
  29. typedef std::pair<std::string, osg::ref_ptr<CameraManipulator> > NamedManipulator;
  30. typedef std::map<int, NamedManipulator> KeyManipMap;
  31. virtual const char* className() const { return "KeySwitchMatrixManipulator"; }
  32. /**
  33. Add a camera manipulator with an associated name, and a key to
  34. trigger the switch,
  35. */
  36. void addMatrixManipulator(int key, std::string name, CameraManipulator *cm);
  37. /**
  38. Add a camera manipulator with an autogenerated keybinding which is '1' + previous number of camera's registered.
  39. */
  40. void addNumberedMatrixManipulator(CameraManipulator *cm);
  41. unsigned int getNumMatrixManipulators() const { return _manips.size(); }
  42. void selectMatrixManipulator(unsigned int num);
  43. /** Get the complete list of manipulators attached to this keyswitch manipulator.*/
  44. KeyManipMap& getKeyManipMap() { return _manips; }
  45. /** Get the const complete list of manipulators attached to this keyswitch manipulator.*/
  46. const KeyManipMap& getKeyManipMap() const { return _manips; }
  47. /** Get the current active manipulators.*/
  48. CameraManipulator* getCurrentMatrixManipulator() { return _current.get(); }
  49. /** Get the const current active manipulators.*/
  50. const CameraManipulator* getCurrentMatrixManipulator() const { return _current.get(); }
  51. /** Get manipulator assigned to a specified index.*/
  52. CameraManipulator* getMatrixManipulatorWithIndex(unsigned int key);
  53. /** Get const manipulator assigned to a specified index.*/
  54. const CameraManipulator* getMatrixManipulatorWithIndex(unsigned int key) const;
  55. /** Get manipulator assigned to a specified key.*/
  56. CameraManipulator* getMatrixManipulatorWithKey(unsigned int key);
  57. /** Get const manipulator assigned to a specified key.*/
  58. const CameraManipulator* getMatrixManipulatorWithKey(unsigned int key) const;
  59. // Overrides from CameraManipulator...
  60. /** set the coordinate frame which callback tells the manipulator which way is up, east and north.*/
  61. virtual void setCoordinateFrameCallback(CoordinateFrameCallback* cb);
  62. /** Set the position of the matrix manipulator using a 4x4 Matrix.*/
  63. virtual void setByMatrix(const osg::Matrixd& matrix) { _current->setByMatrix(matrix); }
  64. /** set the position of the matrix manipulator using a 4x4 Matrix.*/
  65. virtual void setByInverseMatrix(const osg::Matrixd& matrix) { _current->setByInverseMatrix(matrix); }
  66. /** get the position of the manipulator as 4x4 Matrix.*/
  67. virtual osg::Matrixd getMatrix() const { return _current->getMatrix(); }
  68. /** get the position of the manipulator as a inverse matrix of the manipulator, typically used as a model view matrix.*/
  69. virtual osg::Matrixd getInverseMatrix() const { return _current->getInverseMatrix(); }
  70. /** Get the FusionDistanceMode. Used by SceneView for setting up stereo convergence.*/
  71. virtual osgUtil::SceneView::FusionDistanceMode getFusionDistanceMode() const { return _current->getFusionDistanceMode(); }
  72. /** Get the FusionDistanceValue. Used by SceneView for setting up stereo convergence.*/
  73. virtual float getFusionDistanceValue() const { return _current->getFusionDistanceValue(); }
  74. virtual void setNode(osg::Node* n);
  75. virtual const osg::Node* getNode() const { return _current->getNode(); }
  76. virtual osg::Node* getNode() { return _current->getNode(); }
  77. virtual void setHomePosition(const osg::Vec3d& eye, const osg::Vec3d& center, const osg::Vec3d& up, bool autoComputeHomePosition=false);
  78. virtual void setAutoComputeHomePosition(bool flag);
  79. virtual void computeHomePosition();
  80. virtual void finishAnimation();
  81. virtual void home(const GUIEventAdapter& ee,GUIActionAdapter& aa);
  82. virtual void init(const GUIEventAdapter& ee,GUIActionAdapter& aa) { if (_current.valid()) _current->init(ee,aa); }
  83. virtual bool handle(const GUIEventAdapter& ea,GUIActionAdapter& us);
  84. /** Get the keyboard and mouse usage of this manipulator.*/
  85. virtual void getUsage(osg::ApplicationUsage& usage) const;
  86. private:
  87. KeyManipMap _manips;
  88. osg::ref_ptr<CameraManipulator> _current;
  89. };
  90. }
  91. #endif