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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/* -*-c++-*- Present3D - Copyright (C) 1999-2006 Robert Osfield
*
* This software is open source and may be redistributed and/or modified under
* the terms of the GNU General Public License (GPL) version 2.0.
* The full license is in LICENSE.txt file included with this distribution,.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* include LICENSE.txt for more details.
*/
#include <osgViewer/Viewer>
#include "SDLIntegration.h"
#include <SDL.h>
#include <SDL_events.h>
#include <SDL_joystick.h>
#include <iostream>
SDLIntegration::SDLIntegration()
{
_verbose = false;
// init SDL
if ( SDL_Init(SDL_INIT_JOYSTICK) < 0 )
{
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
int numJoysticks = SDL_NumJoysticks();
if (_verbose)
{
std::cout<<"number of joysticks "<<numJoysticks<<std::endl;
for(int i=0; i<numJoysticks; ++i)
{
std::cout<<"Joystick name '"<<SDL_JoystickName(i)<<"'"<<std::endl;
}
}
_joystick = numJoysticks>0 ? SDL_JoystickOpen(0) : 0;
_numAxes = _joystick ? SDL_JoystickNumAxes(_joystick) : 0;
_numBalls = _joystick ? SDL_JoystickNumBalls(_joystick) : 0;
_numHats = _joystick ? SDL_JoystickNumHats(_joystick) : 0;
_numButtons = _joystick ? SDL_JoystickNumButtons(_joystick) : 0;
if (_verbose)
{
std::cout<<"numAxes = "<<_numAxes<<std::endl;
std::cout<<"numBalls = "<<_numBalls<<std::endl;
std::cout<<"numHats = "<<_numHats<<std::endl;
std::cout<<"numButtons = "<<_numButtons<<std::endl;
}
addMouseButtonMapping(4, 1); // left
addMouseButtonMapping(5, 3); // right
addMouseButtonMapping(6, 2); // middle
addKeyMapping(10, ' '); // R2
addKeyMapping(0, '1'); // 1
addKeyMapping(1, '2'); // 2
addKeyMapping(2, '3'); // 3
addKeyMapping(4, '4'); // 4
addKeyMapping(7, ' '); // home
addKeyMapping(8, osgGA::GUIEventAdapter::KEY_Page_Up); // Start
addKeyMapping(9, osgGA::GUIEventAdapter::KEY_Page_Down); // Start
addKeyMapping(10, osgGA::GUIEventAdapter::KEY_Home); // Start
capture(_axisValues, _buttonValues);
}
SDLIntegration::~SDLIntegration()
{
}
void SDLIntegration::capture(ValueList& axisValues, ValueList& buttonValues) const
{
if (_joystick)
{
SDL_JoystickUpdate();
axisValues.resize(_numAxes);
for(int ai=0; ai<_numAxes; ++ai)
{
axisValues[ai] = SDL_JoystickGetAxis(_joystick, ai);
}
buttonValues.resize(_numButtons);
for(int bi=0; bi<_numButtons; ++bi)
{
buttonValues[bi] = SDL_JoystickGetButton(_joystick, bi);
}
}
}
void SDLIntegration::update(osgViewer::Viewer& viewer)
{
if (_joystick)
{
ValueList newAxisValues;
ValueList newButtonValues;
capture(newAxisValues, newButtonValues);
unsigned int mouseXaxis = 0;
unsigned int mouseYaxis = 1;
float prev_mx = (float)_axisValues[mouseXaxis]/32767.0f;
float prev_my = -(float)_axisValues[mouseYaxis]/32767.0f;
float mx = (float)newAxisValues[mouseXaxis]/32767.0f;
float my = -(float)newAxisValues[mouseYaxis]/32767.0f;
osgGA::EventQueue* eq = viewer.getEventQueue();
double time = eq ? eq->getTime() : 0.0;
osgGA::GUIEventAdapter* previous_event = eq->getCurrentEventState();
float projected_mx = previous_event->getXmin() + (mx+1.0)*0.5*(previous_event->getXmax()-previous_event->getXmin());
float projected_my = previous_event->getYmin() + (my+1.0)*0.5*(previous_event->getYmax()-previous_event->getYmin());
if (mx!=prev_mx || my!=prev_my)
{
eq->mouseMotion(projected_mx, projected_my, time);
}
if (_verbose)
{
for(int ai=0; ai<_numAxes; ++ai)
{
if (newAxisValues[ai]!=_axisValues[ai])
{
std::cout<<"axis "<<ai<<" moved to "<<newAxisValues[ai]<<std::endl;
}
}
}
for(int bi=0; bi<_numButtons; ++bi)
{
if (newButtonValues[bi]!=_buttonValues[bi])
{
if (_verbose)
{
std::cout<<"button "<<bi<<" changed to "<<newButtonValues[bi]<<std::endl;
}
int key = getKeyMapping(bi);
int mouseButton = getMouseButtonMapping(bi);
if (mouseButton>0)
{
if (newButtonValues[bi]==0) eq->mouseButtonRelease(projected_mx,projected_my,mouseButton,time);
else eq->mouseButtonPress(projected_mx,projected_my,mouseButton,time);
}
else if (key>0)
{
if (newButtonValues[bi]==0) eq->keyRelease(key,time);
else eq->keyPress(key,time);
}
}
}
_axisValues.swap(newAxisValues);
_buttonValues.swap(newButtonValues);
}
}
|