root/OpenSceneGraph/trunk/src/osgPresentation/deprecated/KeyEventHandler.cpp @ 13721

Revision 13721, 4.8 kB (checked in by robert, 12 minutes ago)

From Paul Martz, fixed placement of OpenGL header so that it gets generated and placed in the build directory as per the Config file

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/deprecated/KeyEventHandler>
14#include <osgPresentation/deprecated/SlideEventHandler>
15
16#include <osgViewer/Viewer>
17#include <osg/Notify>
18#include <osgDB/FileUtils>
19
20#include <stdlib.h>
21
22using namespace osgPresentation;
23
24KeyEventHandler::KeyEventHandler(int key, osgPresentation::Operation operation, const JumpData& jumpData):
25    _key(key),
26    _operation(operation),
27    _jumpData(jumpData)
28{
29}
30
31KeyEventHandler::KeyEventHandler(int key, const std::string& str, osgPresentation::Operation operation, const JumpData& jumpData):
32    _key(key),
33    _command(str),
34    _operation(operation),
35    _jumpData(jumpData)
36{
37}
38
39KeyEventHandler::KeyEventHandler(int key, const osgPresentation::KeyPosition& keyPos, const JumpData& jumpData):
40    _key(key),
41    _keyPos(keyPos),
42    _operation(osgPresentation::EVENT),
43    _jumpData(jumpData)
44{
45}
46
47bool KeyEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& /*aa*/, osg::Object*, osg::NodeVisitor* /*nv*/)
48{
49    if (ea.getHandled()) return false;
50
51    switch(ea.getEventType())
52    {
53        case(osgGA::GUIEventAdapter::KEYDOWN):
54        {
55            if (ea.getKey()==_key)
56            {
57                doOperation();
58                return true;
59            }
60            break;
61        }
62        default:
63            break;
64    }
65    return false;
66}
67
68
69void KeyEventHandler::accept(osgGA::GUIEventHandlerVisitor& v)
70{
71    v.visit(*this);
72}
73
74void KeyEventHandler::getUsage(osg::ApplicationUsage& /*usage*/) const
75{
76}
77
78void KeyEventHandler::doOperation()
79{
80    switch(_operation)
81    {
82        case(osgPresentation::RUN):
83        {
84            OSG_NOTICE<<"Run "<<_command<<std::endl;
85
86#if 0
87            osgDB::FilePathList& paths = osgDB::getDataFilePathList();
88            if (!paths.empty())
89            {
90            #ifdef _WIN32
91                std::string delimintor(";");
92            #else
93                std::string delimintor(":");
94            #endif
95                std::string filepath("OSG_FILE_PATH=");
96
97                bool needDeliminator = false;
98                for(osgDB::FilePathList::iterator itr = paths.begin();
99                    itr != paths.end();
100                    ++itr)
101                {
102                    if (needDeliminator) filepath += delimintor;
103                    filepath += *itr;
104                    needDeliminator = true;
105                }
106                putenv( (char*) filepath.c_str());
107
108                std::string binpath("PATH=");
109                char* path = getenv("PATH");
110                if (path) binpath += path;
111
112                needDeliminator = true;
113                for(osgDB::FilePathList::iterator itr = paths.begin();
114                    itr != paths.end();
115                    ++itr)
116                {
117                    if (needDeliminator) binpath += delimintor;
118                    binpath += *itr;
119                    needDeliminator = true;
120                }
121                putenv( (char*) binpath.c_str());
122
123            }
124#endif
125
126            bool commandRunsInBackground = (_command.find("&")!=std::string::npos);
127
128            int result = system(_command.c_str());
129
130            OSG_INFO<<"system("<<_command<<") result "<<result<<std::endl;
131
132            if (commandRunsInBackground)
133            {
134                // Sleep breifly while command runs in background to give it a chance to open
135                // a window and obscure this present3D's window avoiding this present3D from
136                // rendering anything new before the new window opens.
137                OpenThreads::Thread::microSleep(500000); // half second sleep.
138            }
139
140            break;
141        }
142        case(osgPresentation::LOAD):
143        {
144            OSG_NOTICE<<"Load "<<_command<<std::endl;
145            break;
146        }
147        case(osgPresentation::EVENT):
148        {
149            OSG_INFO<<"Event "<<_keyPos._key<<" "<<_keyPos._x<<" "<<_keyPos._y<<std::endl;
150            if (SlideEventHandler::instance()) SlideEventHandler::instance()->dispatchEvent(_keyPos);
151            break;
152        }
153        case(osgPresentation::JUMP):
154        {
155            OSG_NOTICE<<"Requires jump "<<std::endl;
156            break;
157        }
158        case(osgPresentation::FORWARD_EVENT):
159            break;
160    }
161
162    if (_jumpData.requiresJump())
163    {
164        _jumpData.jump(SlideEventHandler::instance());
165    }
166    else
167    {
168        OSG_NOTICE<<"No jump required."<<std::endl;
169    }
170}
Note: See TracBrowser for help on using the browser.