File: osgdirectinput.cpp

package info (click to toggle)
openscenegraph 3.2.3%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 32,820 kB
  • ctags: 34,610
  • sloc: cpp: 370,040; ansic: 9,071; java: 1,020; yacc: 548; objc: 288; makefile: 285; xml: 155; lex: 151
file content (115 lines) | stat: -rw-r--r-- 4,383 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* OpenSceneGraph example, osgdirectinput.
*
*  Permission is hereby granted, free of charge, to any person obtaining a copy
*  of this software and associated documentation files (the "Software"), to deal
*  in the Software without restriction, including without limitation the rights
*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
*  copies of the Software, and to permit persons to whom the Software is
*  furnished to do so, subject to the following conditions:
*
*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
*  THE SOFTWARE.
*/

#include <osgDB/ReadFile>
#include <osgGA/StateSetManipulator>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <iostream>
#include "DirectInputRegistry"

class CustomViewer : public osgViewer::Viewer
{
public:
    CustomViewer() : osgViewer::Viewer() {}
    virtual ~CustomViewer() {}
    
    virtual void eventTraversal()
    {
        DirectInputRegistry::instance()->updateState( _eventQueue.get() );
        osgViewer::Viewer::eventTraversal();
    }
    
protected:
    virtual void viewerInit()
    {
        osgViewer::GraphicsWindowWin32* windowWin32 =
            dynamic_cast<osgViewer::GraphicsWindowWin32*>( _camera->getGraphicsContext() );
        if ( windowWin32 )
        {
            HWND hwnd = windowWin32->getHWND();
            DirectInputRegistry::instance()->initKeyboard( hwnd );
            //DirectInputRegistry::instance()->initMouse( hwnd );
            DirectInputRegistry::instance()->initJoystick( hwnd );
        }
        osgViewer::Viewer::viewerInit();
    }
};

class JoystickHandler : public osgGA::GUIEventHandler
{
public:
    JoystickHandler() {}
    
    bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
    {
        switch ( ea.getEventType() )
        {
        case osgGA::GUIEventAdapter::KEYDOWN:
            std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " down ***" << std::endl;
            break;
        case osgGA::GUIEventAdapter::KEYUP:
            std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " up ***" << std::endl;
            break;
        case osgGA::GUIEventAdapter::USER:
            {
                const JoystickEvent* event = dynamic_cast<const JoystickEvent*>( ea.getUserData() );
                if ( !event ) break;
                
                const DIJOYSTATE2& js = event->_js;
                for ( unsigned int i=0; i<128; ++i )
                {
                    if ( js.rgbButtons[i] )
                        std::cout << "*** Joystick Btn" << i << " = " << (int)js.rgbButtons[i] << std::endl;
                }
                
                if ( js.lX==0x0000 ) std::cout << "*** Joystick X-" << std::endl;
                else if ( js.lX==0xffff ) std::cout << "*** Joystick X+" << std::endl;
                
                if ( js.lY==0 ) std::cout << "*** Joystick Y-" << std::endl;
                else if ( js.lY==0xffff ) std::cout << "*** Joystick Y+" << std::endl;
            }
            return true;
        default:
            break;
        }
        return false;
    }
};

int main( int argc, char** argv )
{
    osg::ArgumentParser arguments( &argc, argv );
    osg::Node* model = osgDB::readNodeFiles( arguments );
    if ( !model ) model = osgDB::readNodeFile( "cow.osgt" );
    if ( !model ) 
    {
        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
        return 1;
    }
    
    CustomViewer viewer;
    viewer.addEventHandler( new JoystickHandler );
    viewer.addEventHandler( new osgViewer::StatsHandler );
    viewer.addEventHandler( new osgViewer::WindowSizeHandler );
    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
    viewer.setSceneData( model );
    viewer.setUpViewInWindow( 250, 50, 800, 600 );
    return viewer.run();
}