Commit a548df80 authored by Robert Menzel's avatar Robert Menzel
Browse files

added microsoft 360 controller

parent 474d97b0
...@@ -84,6 +84,15 @@ public: ...@@ -84,6 +84,15 @@ public:
//! define the mapping of one button //! define the mapping of one button
void setAxisMapping( GamePadAxis _axis, unsigned int _rawAxisNumber ); void setAxisMapping( GamePadAxis _axis, unsigned int _rawAxisNumber );
//! sets the minimal value an axis has to be pushed to trigger.
//! _sensitivity has to be >= 0.0 and < 1.0.
//! reported axis values will still be between -1..0..1
void setMinAxisSensitivity( float _sensitivity );
float getMinAxisSensitivity() { return mMinSensitivity; }
//! set and unset the invertation of an axis (1..-1 -> -1..1)
void invertAxis( int _axis, bool _invert = true );
//! print the button and axes state for debugging: //! print the button and axes state for debugging:
void printState(); void printState();
...@@ -97,6 +106,9 @@ private: ...@@ -97,6 +106,9 @@ private:
int mNumberOfAxes; int mNumberOfAxes;
int mButtonMap[GAMEPAD_BUTTON_ENUM_SIZE]; int mButtonMap[GAMEPAD_BUTTON_ENUM_SIZE];
int mAxisMap[GAMEPAD_AXIS_ENUM_SIZE]; int mAxisMap[GAMEPAD_AXIS_ENUM_SIZE];
float *mAxes;
float *mAxesMultiplier;
float mMinSensitivity;
// //
// GLFW specifics: replace this to support joysticks with other APIs // GLFW specifics: replace this to support joysticks with other APIs
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <ACGL/HardwareSupport/GamePad.hh> #include <ACGL/HardwareSupport/GamePad.hh>
#include <ACGL/Utils/Log.hh> #include <ACGL/Utils/Log.hh>
#include <ACGL/Math/Math.hh>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
...@@ -25,6 +26,9 @@ GamePad::GamePad( int _n ) ...@@ -25,6 +26,9 @@ GamePad::GamePad( int _n )
mGamePadOK = false; mGamePadOK = false;
mLastGLFWButtonState = NULL; mLastGLFWButtonState = NULL;
mAxes = NULL;
mAxesMultiplier = NULL;
mMinSensitivity = 0.0f;
#ifdef ACGL_COMPILE_WITH_GLFW #ifdef ACGL_COMPILE_WITH_GLFW
int numberOfJoysticksFound = 0; int numberOfJoysticksFound = 0;
...@@ -90,6 +94,34 @@ GamePad::GamePad( int _n ) ...@@ -90,6 +94,34 @@ GamePad::GamePad( int _n )
setAxisMapping( LEFT_ANALOG_STICK_Y , 1); setAxisMapping( LEFT_ANALOG_STICK_Y , 1);
setAxisMapping( RIGHT_ANALOG_STICK_X , 2); setAxisMapping( RIGHT_ANALOG_STICK_X , 2);
setAxisMapping( RIGHT_ANALOG_STICK_Y , 3); setAxisMapping( RIGHT_ANALOG_STICK_Y , 3);
setMinAxisSensitivity( 0.05f );
} else if (joystickName == "Microsoft X-Box 360 pad") {
setButtonMapping( SELECT, 6);
setButtonMapping( START, 7);
//setButtonMapping( LEFT_PAD_NORTH , 0);
//setButtonMapping( LEFT_PAD_EAST , 0);
//setButtonMapping( LEFT_PAD_SOUTH , 0);
//setButtonMapping( LEFT_PAD_WEST , 0);
setButtonMapping( RIGHT_PAD_NORTH , 3);
setButtonMapping( RIGHT_PAD_EAST , 1);
setButtonMapping( RIGHT_PAD_SOUTH , 0);
setButtonMapping( RIGHT_PAD_WEST , 2);
setButtonMapping( LEFT_SHOULDER , 4);
setButtonMapping( RIGHT_SHOULDER , 5);
//setButtonMapping( LEFT_TRIGGER , 0);
//setButtonMapping( RIGHT_TRIGGER , 0);
setAxisMapping( LEFT_ANALOG_TRIGGER , 2);
setAxisMapping( RIGHT_ANALOG_TRIGGER , 5);
setAxisMapping( LEFT_ANALOG_STICK_X , 0);
setAxisMapping( LEFT_ANALOG_STICK_Y , 1);
setAxisMapping( RIGHT_ANALOG_STICK_X , 3);
setAxisMapping( RIGHT_ANALOG_STICK_Y , 4);
setMinAxisSensitivity( 0.2f );
} else {
debug() << "unknown gamepad: " << joystickName << endl;
} }
} }
#endif #endif
...@@ -98,6 +130,8 @@ GamePad::GamePad( int _n ) ...@@ -98,6 +130,8 @@ GamePad::GamePad( int _n )
GamePad::~GamePad() GamePad::~GamePad()
{ {
delete[] mLastGLFWButtonState; delete[] mLastGLFWButtonState;
delete[] mAxes;
delete[] mAxesMultiplier;
} }
bool GamePad::isPressedRaw( unsigned int _button ) bool GamePad::isPressedRaw( unsigned int _button )
...@@ -133,7 +167,7 @@ bool GamePad::buttonStateChanged( GamePadButton _button ) ...@@ -133,7 +167,7 @@ bool GamePad::buttonStateChanged( GamePadButton _button )
float GamePad::getAxisRaw( unsigned int _axis ) float GamePad::getAxisRaw( unsigned int _axis )
{ {
if ( (int)_axis > mNumberOfAxes) return 0.0f; if ( (int)_axis > mNumberOfAxes) return 0.0f;
return mGLFWAxes[_axis]; return mAxes[_axis];
} }
float GamePad::getAxis( GamePadAxis _axis ) float GamePad::getAxis( GamePadAxis _axis )
...@@ -147,6 +181,19 @@ void GamePad::setAxisMapping( GamePadAxis _axis, unsigned int _rawAxisNumber ) ...@@ -147,6 +181,19 @@ void GamePad::setAxisMapping( GamePadAxis _axis, unsigned int _rawAxisNumber )
mAxisMap[_axis] = _rawAxisNumber; mAxisMap[_axis] = _rawAxisNumber;
} }
void GamePad::setMinAxisSensitivity( float _sensitivity )
{
assert( _sensitivity >= 0.0f && "sensitivity can't be negative" );
assert( _sensitivity < 1.0f && "sensitivity has to be smaller than one" );
mMinSensitivity = _sensitivity;
}
void GamePad::invertAxis( int _axis, bool _invert )
{
assert(_axis < GAMEPAD_AXIS_ENUM_SIZE);
mAxesMultiplier[_axis] = (_invert)? -1.0f : 1.0f;
}
void GamePad::update() void GamePad::update()
{ {
...@@ -158,12 +205,29 @@ void GamePad::update() ...@@ -158,12 +205,29 @@ void GamePad::update()
mGLFWAxes = glfwGetJoystickAxes( mGLFWGamePadNumber, &mNumberOfAxes ); mGLFWAxes = glfwGetJoystickAxes( mGLFWGamePadNumber, &mNumberOfAxes );
mLastGLFWButtonState = new unsigned char[mNumberOfButtons]; mLastGLFWButtonState = new unsigned char[mNumberOfButtons];
mAxes = new float[mNumberOfAxes];
mAxesMultiplier = new float[mNumberOfAxes];
for (int i = 0; i < mNumberOfAxes; ++i) {
mAxesMultiplier[i] = 1.0f;
}
} }
memcpy( mLastGLFWButtonState, mGLFWButtons, mNumberOfButtons ); memcpy( mLastGLFWButtonState, mGLFWButtons, mNumberOfButtons );
mGLFWButtons = glfwGetJoystickButtons( mGLFWGamePadNumber, &mNumberOfButtons ); mGLFWButtons = glfwGetJoystickButtons( mGLFWGamePadNumber, &mNumberOfButtons );
mGLFWAxes = glfwGetJoystickAxes( mGLFWGamePadNumber, &mNumberOfAxes ); mGLFWAxes = glfwGetJoystickAxes( mGLFWGamePadNumber, &mNumberOfAxes );
memcpy( mAxes, mGLFWAxes, mNumberOfAxes*sizeof(float) );
#endif #endif
for (int i = 0; i < mNumberOfAxes; ++i) {
float tmp = std::abs( mAxes[ i ] );
tmp -= mMinSensitivity;
if (tmp < 0.0f) tmp = 0.0f;
tmp /= (1.0f - mMinSensitivity); // rescaled to 0..1
float sign = ((mAxes[ i ] < 0.0f) && (tmp != 0.0f)) ? -1.0f : 1.0f;
mAxes[ i ] = tmp * sign;
}
} }
void GamePad::printState() void GamePad::printState()
...@@ -176,7 +240,7 @@ void GamePad::printState() ...@@ -176,7 +240,7 @@ void GamePad::printState()
} }
debug() << "| "; debug() << "| ";
for (int i = 0; i < mNumberOfAxes; ++i) { for (int i = 0; i < mNumberOfAxes; ++i) {
debug() << mGLFWAxes[i] << " "; debug() << mAxes[i] << " ";
} }
debug() << endl; debug() << endl;
#endif #endif
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment