root/OpenSceneGraph/trunk/src/osgPlugins/sdl/JoystickDevice.cpp @ 13429

Revision 13429, 5.4 kB (checked in by robert, 54 minutes ago)

From Jason Beverage, "It looks like the Callback header got accidentally removed from the CMakeLists.txt in the submission yesterday for the geometry instancing example."

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 <osgViewer/Viewer>
14
15#include "JoystickDevice.h"
16
17#include <SDL.h>
18#include <SDL_events.h>
19#include <SDL_joystick.h>
20
21#include <iostream>
22
23JoystickDevice::JoystickDevice()
24{
25    _verbose = false;
26
27    // init SDL
28    if ( SDL_Init(SDL_INIT_JOYSTICK) < 0 )
29    {
30        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
31        exit(1);
32    }
33    atexit(SDL_Quit);
34
35    int numJoysticks =  SDL_NumJoysticks();
36   
37    if (_verbose)
38    {
39        std::cout<<"number of joysticks "<<numJoysticks<<std::endl;
40        for(int i=0; i<numJoysticks; ++i)
41        {
42            std::cout<<"Joystick name '"<<SDL_JoystickName(i)<<"'"<<std::endl;
43        }
44    }
45   
46    _joystick = numJoysticks>0 ? SDL_JoystickOpen(0) : 0;
47   
48    _numAxes = _joystick ? SDL_JoystickNumAxes(_joystick) : 0;
49    _numBalls = _joystick ? SDL_JoystickNumBalls(_joystick) : 0;
50    _numHats = _joystick ? SDL_JoystickNumHats(_joystick) : 0;
51    _numButtons = _joystick ? SDL_JoystickNumButtons(_joystick) : 0;
52
53    if (_verbose)
54    {
55        std::cout<<"numAxes = "<<_numAxes<<std::endl;
56        std::cout<<"numBalls = "<<_numBalls<<std::endl;
57        std::cout<<"numHats = "<<_numHats<<std::endl;
58        std::cout<<"numButtons = "<<_numButtons<<std::endl;
59    }
60       
61    addMouseButtonMapping(4, 1); // left
62    addMouseButtonMapping(5, 3); // right
63    addMouseButtonMapping(6, 2); // middle
64
65    addKeyMapping(10, ' '); // R2
66   
67    addKeyMapping(0, '1'); // 1
68    addKeyMapping(1, '2'); // 2
69    addKeyMapping(2, '3'); // 3
70    addKeyMapping(4, '4'); // 4
71
72    addKeyMapping(7, ' '); // home
73
74    addKeyMapping(8, osgGA::GUIEventAdapter::KEY_Page_Up); // Start
75    addKeyMapping(9, osgGA::GUIEventAdapter::KEY_Page_Down); // Start
76    addKeyMapping(10, osgGA::GUIEventAdapter::KEY_Home); // Start
77
78
79    capture(_axisValues, _buttonValues);
80}
81
82JoystickDevice::~JoystickDevice()
83{
84}
85
86void JoystickDevice::capture(ValueList& axisValues, ValueList& buttonValues) const
87{
88    if (_joystick)
89    {
90        SDL_JoystickUpdate();
91       
92        axisValues.resize(_numAxes);
93        for(int ai=0; ai<_numAxes; ++ai)
94        {
95            axisValues[ai] = SDL_JoystickGetAxis(_joystick, ai);
96        }
97
98        buttonValues.resize(_numButtons);
99        for(int bi=0; bi<_numButtons; ++bi)
100        {
101            buttonValues[bi] = SDL_JoystickGetButton(_joystick, bi);
102        }
103    }
104}
105
106bool JoystickDevice::checkEvents()
107{
108    if (_joystick)
109    {
110
111        OSG_NOTICE<<"JoystickDevice::checkEvents()"<<std::endl;
112   
113        ValueList newAxisValues;
114        ValueList newButtonValues;
115       
116        capture(newAxisValues, newButtonValues);
117       
118        unsigned int mouseXaxis = 0;
119        unsigned int mouseYaxis = 1;
120       
121        float prev_mx = (float)_axisValues[mouseXaxis]/32767.0f;
122        float prev_my = -(float)_axisValues[mouseYaxis]/32767.0f;
123
124        float mx = (float)newAxisValues[mouseXaxis]/32767.0f;
125        float my = -(float)newAxisValues[mouseYaxis]/32767.0f;
126       
127
128        osgGA::EventQueue* eq = getEventQueue();
129        double time = eq ? eq->getTime() : 0.0;
130        osgGA::GUIEventAdapter* previous_event = eq->getCurrentEventState();
131        float projected_mx = previous_event->getXmin() + (mx+1.0)*0.5*(previous_event->getXmax()-previous_event->getXmin());
132        float projected_my = previous_event->getYmin() + (my+1.0)*0.5*(previous_event->getYmax()-previous_event->getYmin());
133
134        if (mx!=prev_mx || my!=prev_my)
135        {
136            eq->mouseMotion(projected_mx, projected_my, time);
137        }
138
139        OSG_NOTICE<<"mx="<<mx<<", my="<<my<<", projected_mx="<<projected_mx<<", projected_my="<<projected_my<<std::endl;
140
141
142        if (_verbose)
143        {
144            for(int ai=0; ai<_numAxes; ++ai)
145            {
146                if (newAxisValues[ai]!=_axisValues[ai])
147                {
148                    std::cout<<"axis "<<ai<<" moved to "<<newAxisValues[ai]<<std::endl;
149                }
150            }
151        }
152               
153        for(int bi=0; bi<_numButtons; ++bi)
154        {
155            if (newButtonValues[bi]!=_buttonValues[bi])
156            {
157                if (_verbose)
158                {
159                    std::cout<<"button "<<bi<<" changed to "<<newButtonValues[bi]<<std::endl;
160                }
161
162                int key =  getKeyMapping(bi);
163                int mouseButton =  getMouseButtonMapping(bi);
164
165                if (mouseButton>0)
166                {
167                    if (newButtonValues[bi]==0) eq->mouseButtonRelease(projected_mx,projected_my,mouseButton,time);
168                    else  eq->mouseButtonPress(projected_mx,projected_my,mouseButton,time);
169                }
170                else if (key>0)
171                {
172
173                    if (newButtonValues[bi]==0) eq->keyRelease(key,time);
174                    else eq->keyPress(key,time);
175                }
176            }
177        }
178       
179        _axisValues.swap(newAxisValues);
180        _buttonValues.swap(newButtonValues);
181       
182    }
183    return !(getEventQueue()->empty());
184}
185       
Note: See TracBrowser for help on using the browser.