root/OpenSceneGraph/trunk/src/osgPresentation/PickEventHandler.cpp @ 13041

Revision 13041, 6.9 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
2 *
3 * This software is open source and may be redistributed and/or modified under
4 * the terms of the GNU General Public License (GPL) version 2.0.
5 * The full license is in LICENSE.txt file included with this distribution,.
6 *
7 * This software is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * include LICENSE.txt for more details.
11*/
12
13#include <osgPresentation/PickEventHandler>
14#include <osgPresentation/SlideEventHandler>
15
16#include <osgViewer/Viewer>
17#include <osg/Notify>
18#include <osgDB/FileUtils>
19
20#include <stdlib.h>
21
22using namespace osgPresentation;
23
24PickEventHandler::PickEventHandler(osgPresentation::Operation operation,bool relativeJump, int slideNum, int layerNum):
25    _operation(operation),
26    _relativeJump(relativeJump),
27    _slideNum(slideNum),
28    _layerNum(layerNum)
29{
30}
31
32PickEventHandler::PickEventHandler(const std::string& str, osgPresentation::Operation operation,bool relativeJump, int slideNum, int layerNum):
33    _command(str),
34    _operation(operation),
35    _relativeJump(relativeJump),
36    _slideNum(slideNum),
37    _layerNum(layerNum)
38{
39}
40
41PickEventHandler::PickEventHandler(const osgPresentation::KeyPosition& keyPos,bool relativeJump, int slideNum, int layerNum):
42    _keyPos(keyPos),
43    _operation(osgPresentation::EVENT),
44    _relativeJump(relativeJump),
45    _slideNum(slideNum),
46    _layerNum(layerNum)
47{
48}
49
50
51bool PickEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv)
52{
53    switch(ea.getEventType())
54    {
55        case(osgGA::GUIEventAdapter::MOVE):
56        case(osgGA::GUIEventAdapter::PUSH):
57        case(osgGA::GUIEventAdapter::RELEASE):
58        {
59            osgViewer::Viewer* viewer = dynamic_cast<osgViewer::Viewer*>(&aa);
60            osgUtil::LineSegmentIntersector::Intersections intersections;
61            if (viewer->computeIntersections(ea.getX(),ea.getY(), nv->getNodePath(), intersections))
62            {
63                for(osgUtil::LineSegmentIntersector::Intersections::iterator hitr=intersections.begin();
64                    hitr!=intersections.end();
65                    ++hitr)
66                {
67                    if (ea.getEventType()==osgGA::GUIEventAdapter::MOVE)
68                    {
69                        OSG_INFO<<"Tooltip..."<<std::endl;
70                    }
71                    else if (ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
72                    {
73                        doOperation();
74                        return true;
75                    }
76                }
77            }
78            break;
79        }
80        case(osgGA::GUIEventAdapter::KEYDOWN):
81        {
82            //OSG_NOTICE<<"PickEventHandler KEYDOWN "<<(char)ea.getKey()<<std::endl;
83            //if (object) OSG_NOTICE<<"    "<<object->className()<<std::endl;
84            break;
85        }
86        default:
87            break;
88    }
89    return false;
90}
91
92
93void PickEventHandler::accept(osgGA::GUIEventHandlerVisitor& v)
94{
95    v.visit(*this);
96}
97
98void PickEventHandler::getUsage(osg::ApplicationUsage& /*usage*/) const
99{
100}
101
102void PickEventHandler::setRelativeJump(int slideNum, int layerNum)
103{
104    _relativeJump = true;
105    _slideNum = slideNum;
106    _layerNum = layerNum;
107}
108
109void PickEventHandler::setAbsoluteJump(int slideNum, int layerNum)
110{
111    _relativeJump = false;
112    _slideNum = slideNum;
113    _layerNum = layerNum;
114}
115
116
117void PickEventHandler::doOperation()
118{
119    switch(_operation)
120    {
121        case(osgPresentation::RUN):
122        {
123            OSG_NOTICE<<"Run "<<_command<<std::endl;
124
125#if 0
126            osgDB::FilePathList& paths = osgDB::getDataFilePathList();
127            if (!paths.empty())
128            {
129            #ifdef _WIN32
130                std::string delimintor(";");
131            #else
132                std::string delimintor(":");
133            #endif
134                std::string filepath("OSG_FILE_PATH=");
135
136                bool needDeliminator = false;
137                for(osgDB::FilePathList::iterator itr = paths.begin();
138                    itr != paths.end();
139                    ++itr)
140                {
141                    if (needDeliminator) filepath += delimintor;
142                    filepath += *itr;
143                    needDeliminator = true;
144                }
145                putenv( (char*) filepath.c_str());
146
147                std::string binpath("PATH=");
148                char* path = getenv("PATH");
149                if (path) binpath += path;
150
151                needDeliminator = true;
152                for(osgDB::FilePathList::iterator itr = paths.begin();
153                    itr != paths.end();
154                    ++itr)
155                {
156                    if (needDeliminator) binpath += delimintor;
157                    binpath += *itr;
158                    needDeliminator = true;
159                }
160                putenv( (char*) binpath.c_str());
161
162            }
163#endif
164
165            bool commandRunsInBackground = (_command.find("&")!=std::string::npos);
166
167            int result = system(_command.c_str());
168
169            OSG_INFO<<"system("<<_command<<") result "<<result<<std::endl;
170
171            if (commandRunsInBackground)
172            {
173                // Sleep breifly while command runs in background to give it a chance to open
174                // a window and obscure this present3D's window avoiding this present3D from
175                // rendering anything new before the new window opens.
176                OpenThreads::Thread::microSleep(500000); // half second sleep.
177            }
178
179            break;
180        }
181        case(osgPresentation::LOAD):
182        {
183            OSG_NOTICE<<"Load "<<_command<<std::endl;
184            break;
185        }
186        case(osgPresentation::EVENT):
187        {
188            OSG_INFO<<"Event "<<_keyPos._key<<" "<<_keyPos._x<<" "<<_keyPos._y<<std::endl;
189            if (SlideEventHandler::instance()) SlideEventHandler::instance()->dispatchEvent(_keyPos);
190            break;
191        }
192        case(osgPresentation::JUMP):
193        {
194            OSG_NOTICE<<"Requires jump "<<std::endl;
195            break;
196        }
197    }
198
199    if (requiresJump())
200    {
201        OSG_NOTICE<<"Requires jump "<<_relativeJump<<", "<<_slideNum<<", "<<_layerNum<<std::endl;
202
203        if (_relativeJump)
204        {
205            int previousSlide = SlideEventHandler::instance()->getActiveSlide();
206            int previousLayer = SlideEventHandler::instance()->getActiveLayer();
207            int newSlide = previousSlide + _slideNum;
208            int newLayer = previousLayer + _layerNum;
209            if (newLayer<0)
210            {
211                newLayer = 0;
212            }
213
214            OSG_NOTICE<<"   jump to "<<newSlide<<", "<<newLayer<<std::endl;
215
216            SlideEventHandler::instance()->selectSlide(newSlide, newLayer);
217        }
218        else
219        {
220            SlideEventHandler::instance()->selectSlide(_slideNum,_layerNum);
221        }
222    }
223    else
224    {
225        OSG_NOTICE<<"No jump required."<<std::endl;
226    }
227}
Note: See TracBrowser for help on using the browser.