root/OpenSceneGraph/trunk/src/osgGA/CameraViewSwitchManipulator.cpp @ 13041

Revision 13041, 3.0 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1#include <osgGA/CameraViewSwitchManipulator>
2#include <osg/Quat>
3#include <osg/Notify>
4#include <osg/BoundsChecking>
5
6
7using namespace osg;
8using namespace osgGA;
9
10class CollectCameraViewsNodeVisitor : public osg::NodeVisitor
11{
12public:
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
26void CameraViewSwitchManipulator::setNode(osg::Node* node)
27{
28    _node = node;
29
30    _cameraViews.clear();
31    CollectCameraViewsNodeVisitor visitor(&_cameraViews);
32
33    _node->accept(visitor);
34}
35
36void 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
42bool 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
72osg::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            // TODO take into account the position and attitude of the CameraView
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
92osg::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            // TODO take into account the position and attitude of the CameraView
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}
Note: See TracBrowser for help on using the browser.