source: osgVisual/trunk/src/core/core_manipulator.cpp @ 231

Last change on this file since 231 was 231, checked in by Torben Dannhauer, 13 years ago

Reloaced eventhandler and manipulator in a dedicated manipulator and tracking class (preparation for automatic tracking by extLink)

File size: 6.0 KB
Line 
1/* -*-c++-*- osgVisual - Copyright (C) 2009-2011 Torben Dannhauer
2 *
3 * This library is based on OpenSceneGraph, 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 * osgVisual requires for some proprietary modules a license from the correspondig manufacturer.
9 * You have to aquire licenses for all used proprietary modules.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * OpenSceneGraph Public License for more details.
15*/
16
17#include <core_manipulator.h>
18
19using namespace osgVisual;
20
21core_manipulator::core_manipulator()
22{
23#ifdef USE_SPACENAVIGATOR
24        _mouse = NULL;
25#endif
26        _currentTrackingID = -1;
27
28}
29
30bool core_manipulator::init(osgViewer::Viewer* viewer, osg::ArgumentParser& arguments, std::string configFilename, osg::Node* rootNode)
31{
32        _rootNode = rootNode;
33
34        // Setup manipulators
35        if(!visual_dataIO::getInstance()->isSlave()) // set up the camera manipulators if not slave.
36    {
37        osg::ref_ptr<osgGA::KeySwitchMatrixManipulator> keyswitchManipulator = new osgGA::KeySwitchMatrixManipulator;
38
39        keyswitchManipulator->addMatrixManipulator( '1', "Trackball", new osgGA::TrackballManipulator() );
40        keyswitchManipulator->addMatrixManipulator( '2', "Flight", new osgGA::FlightManipulator() );
41        keyswitchManipulator->addMatrixManipulator( '3', "Terrain", new osgGA::TerrainManipulator() );
42                _nt = new osgGA::NodeTrackerManipulator();
43                _nt->setTrackNode(NULL);
44                keyswitchManipulator->addMatrixManipulator( '4', "NodeTrackerManipulator", _nt );
45               
46#ifdef USE_SPACENAVIGATOR
47                // SpaceNavigator manipulator
48                _mouse = new SpaceMouse();
49                _mouse->initialize();
50                _mouseTrackerManip = new NodeTrackerSpaceMouse(_mouse);
51                _mouseTrackerManip->setTrackerMode(NodeTrackerSpaceMouse::NODE_CENTER);
52                _mouseTrackerManip->setRotationMode(NodeTrackerSpaceMouse::ELEVATION_AZIM);
53                _mouseTrackerManip->setAutoComputeHomePosition( true );
54                keyswitchManipulator->addMatrixManipulator( '5', "Spacemouse Node Tracker", _mouseTrackerManip );
55                keyswitchManipulator->addMatrixManipulator( '6', "Spacemouse Free (Airplane)", new FreeManipulator(_mouse) );
56#endif
57
58                // objectMounted Manipulator for Camera control by Nodes
59                _objectMountedCameraManip = new objectMountedManipulator();
60                keyswitchManipulator->addMatrixManipulator( '7', "Object mounted Camera", _objectMountedCameraManip );
61
62                // Animation path manipulator
63                std::string pathfile = util::getAnimationPathFromXMLConfig(configFilename);
64        char keyForAnimationPath = '8';
65                if( pathfile != "" )
66        {
67            osgGA::AnimationPathManipulator* apm = new osgGA::AnimationPathManipulator(pathfile);
68            if (apm || !apm->valid()) 
69            {
70                unsigned int num = keyswitchManipulator->getNumMatrixManipulators();
71                keyswitchManipulator->addMatrixManipulator( keyForAnimationPath, "Path", apm );
72                keyswitchManipulator->selectMatrixManipulator(num);
73                ++keyForAnimationPath;
74            }
75        }
76
77        viewer->setCameraManipulator( keyswitchManipulator.get() );
78    }   // If not Slave END
79
80    viewer->addEventHandler( new osgGA::StateSetManipulator(_rootNode->getOrCreateStateSet()) );                // add the state manipulator
81    viewer->addEventHandler(new osgViewer::ThreadingHandler);                           // add the thread model handler
82    viewer->addEventHandler(new osgViewer::WindowSizeHandler);                          // add the window size toggle handler
83    viewer->addEventHandler(new osgViewer::StatsHandler);                                       // add the stats handler
84    viewer->addEventHandler(new osgViewer::HelpHandler(arguments.getApplicationUsage()));                       // add the help handler
85    viewer->addEventHandler(new osgViewer::RecordCameraPathHandler);            // add the record camera path handler
86    viewer->addEventHandler(new osgViewer::LODScaleHandler);                            // add the LOD Scale handler
87    viewer->addEventHandler(new osgViewer::ScreenCaptureHandler);                       // add the screen capture handler
88
89
90
91        return true;
92}
93
94void core_manipulator::shutdown()
95{
96#ifdef USE_SPACENAVIGATOR
97        //Delete SpaceMouse driver
98        if(_mouse)
99        {
100                _mouse->shutdown();
101                delete _mouse;
102        }
103#endif
104
105        _objectMountedCameraManip = NULL;
106        _mouseTrackerManip = NULL;
107        _nt = NULL;
108        _rootNode = NULL;
109}
110
111void core_manipulator::trackNode( int trackingID )
112{
113        osg::ref_ptr<osg::Node> tmp = visual_object::findNodeByTrackingID(trackingID, _rootNode);
114        if(tmp.valid())
115        {
116                _currentTrackingID = trackingID;
117                trackNode(tmp);
118        }
119}
120
121void core_manipulator::trackNode( osg::Node* node_ )
122{
123        if(!node_)
124                return;
125
126        osg::Node* node = NULL;
127        // Check if tracked node is a visual_object
128        osgVisual::visual_object* trackedObject = dynamic_cast<osgVisual::visual_object*>(node_);
129        if(trackedObject)
130        {
131                node = trackedObject->getGeometry();
132
133                // Object mounted manipulator ( Only working with visual_object, not with osg::Node )
134                if (_objectMountedCameraManip.valid())
135                        _objectMountedCameraManip->setAttachedObject( trackedObject );
136        }
137        else
138                node = node_;
139
140        // Spacemouse Node Tracker
141#ifdef USE_SPACENAVIGATOR
142        if (_mouseTrackerManip.valid())
143        {
144                _mouseTrackerManip->setTrackNode( node );
145                _mouseTrackerManip->setMinimumDistance( 100 );
146        }
147#endif
148
149        // Classical OSG Nodetracker
150        if(_nt.valid())
151        {
152                osgGA::NodeTrackerManipulator::TrackerMode trackerMode = osgGA::NodeTrackerManipulator::NODE_CENTER;
153                osgGA::NodeTrackerManipulator::RotationMode rotationMode = osgGA::NodeTrackerManipulator::ELEVATION_AZIM;
154                _nt->setTrackerMode(trackerMode);
155                _nt->setRotationMode(rotationMode);
156                _nt->setMinimumDistance( 100 );
157                _nt->setTrackNode( node );
158                _nt->setAutoComputeHomePosition( true );
159                _nt->setDistance( 250 );
160        }
161}
Note: See TracBrowser for help on using the repository browser.