| 1 | #include <osgGA/CameraViewSwitchManipulator> |
|---|
| 2 | #include <osg/Quat> |
|---|
| 3 | #include <osg/Notify> |
|---|
| 4 | #include <osg/BoundsChecking> |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | using namespace osg; |
|---|
| 8 | using namespace osgGA; |
|---|
| 9 | |
|---|
| 10 | class CollectCameraViewsNodeVisitor : public osg::NodeVisitor |
|---|
| 11 | { |
|---|
| 12 | public: |
|---|
| 13 | CollectCameraViewsNodeVisitor(CameraViewSwitchManipulator::CameraViewList* cameraViews): |
|---|
| 14 | osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN), |
|---|
| 15 | _cameraViews(cameraViews) |
|---|
| 16 | {} |
|---|
| 17 | |
|---|
| 18 | virtual void apply(CameraView& node) |
|---|
| 19 | { |
|---|
| 20 | _cameraViews->push_back(&node); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | CameraViewSwitchManipulator::CameraViewList* _cameraViews; |
|---|
| 24 | }; |
|---|
| 25 | |
|---|
| 26 | void CameraViewSwitchManipulator::setNode(osg::Node* node) |
|---|
| 27 | { |
|---|
| 28 | _node = node; |
|---|
| 29 | |
|---|
| 30 | _cameraViews.clear(); |
|---|
| 31 | CollectCameraViewsNodeVisitor visitor(&_cameraViews); |
|---|
| 32 | |
|---|
| 33 | _node->accept(visitor); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | void CameraViewSwitchManipulator::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 37 | { |
|---|
| 38 | usage.addKeyboardMouseBinding("CameraViewSwitcher: [","Decrease current camera number"); |
|---|
| 39 | usage.addKeyboardMouseBinding("CameraViewSwitcher: ]","Increase current camera number"); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | bool CameraViewSwitchManipulator::handle(const GUIEventAdapter& ea,GUIActionAdapter&) |
|---|
| 43 | { |
|---|
| 44 | if (ea.getHandled()) return false; |
|---|
| 45 | |
|---|
| 46 | switch(ea.getEventType()) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | case(GUIEventAdapter::KEYDOWN): |
|---|
| 50 | if (ea.getKey()=='[') |
|---|
| 51 | { |
|---|
| 52 | if (_currentView == 0) |
|---|
| 53 | _currentView = _cameraViews.size()-1; |
|---|
| 54 | else |
|---|
| 55 | _currentView--; |
|---|
| 56 | return true; |
|---|
| 57 | } |
|---|
| 58 | else if (ea.getKey()==']') |
|---|
| 59 | { |
|---|
| 60 | _currentView++; |
|---|
| 61 | if (_currentView >= _cameraViews.size()) |
|---|
| 62 | _currentView = 0; |
|---|
| 63 | return true; |
|---|
| 64 | } |
|---|
| 65 | return false; |
|---|
| 66 | |
|---|
| 67 | default: |
|---|
| 68 | return false; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | osg::Matrixd CameraViewSwitchManipulator::getMatrix() const |
|---|
| 73 | { |
|---|
| 74 | osg::Matrix mat; |
|---|
| 75 | if (_currentView < _cameraViews.size()) |
|---|
| 76 | { |
|---|
| 77 | NodePathList parentNodePaths = _cameraViews[_currentView]->getParentalNodePaths(); |
|---|
| 78 | |
|---|
| 79 | if (!parentNodePaths.empty()) |
|---|
| 80 | { |
|---|
| 81 | mat = osg::computeLocalToWorld(parentNodePaths[0]); |
|---|
| 82 | |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | OSG_NOTICE<<"CameraViewSwitchManipulator::getMatrix(): Unable to calculate matrix due to empty parental path."<<std::endl; |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | return mat; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | osg::Matrixd CameraViewSwitchManipulator::getInverseMatrix() const |
|---|
| 93 | { |
|---|
| 94 | osg::Matrix mat; |
|---|
| 95 | if (_currentView < _cameraViews.size()) |
|---|
| 96 | { |
|---|
| 97 | NodePathList parentNodePaths = _cameraViews[_currentView]->getParentalNodePaths(); |
|---|
| 98 | |
|---|
| 99 | if (!parentNodePaths.empty()) |
|---|
| 100 | { |
|---|
| 101 | mat = osg::computeWorldToLocal(parentNodePaths[0]); |
|---|
| 102 | |
|---|
| 103 | } |
|---|
| 104 | else |
|---|
| 105 | { |
|---|
| 106 | OSG_NOTICE<<"CameraViewSwitchManipulator::getInverseMatrix(): Unable to calculate matrix due to empty parental path."<<std::endl; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | return mat; |
|---|
| 110 | } |
|---|