restored more gamepad code (hopefully cleaner but not tested)

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/irrlicht@3311 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
auria
2009-03-29 16:15:17 +00:00
parent 34f8e281d7
commit 174a753997
4 changed files with 65 additions and 8 deletions

View File

@@ -45,10 +45,11 @@ bool DeviceManager::mapInputToPlayerAndAction( Input::InputType type, int id0, i
}
else if(type == Input::IT_STICKMOTION)
{
// std::cout << "stick motion, ID=" <<id0 << " axis=" << id1 << " value=" << value << std::endl;
std::cout << "stick motion, ID=" <<id0 << " axis=" << id1 << " value=" << value << std::endl;
for(unsigned int n=0; n<m_gamepad_amount; n++)
{
if( /*m_gamepads[n].m_index == id0 &&*/ m_gamepads[n].hasBinding(id1 /* axis */, value, action) ) return true;
if( /*m_gamepads[n].m_index == id0 &&*/ m_gamepads[n].hasBinding(id1 /* axis */, value, *player, action /* out */) )
return true;
}
return false;
}

View File

@@ -1,5 +1,7 @@
#include "input/input_device.hpp"
#include "race_manager.hpp"
#include "modes/world.hpp"
InputDevice::InputDevice()
{
@@ -127,10 +129,64 @@ void GamePadDevice::loadDefaults()
*/
}
// -----------------------------------------------------------------------------
bool GamePadDevice::hasBinding(const int axis, const int value, PlayerAction* action /* out */) const
void GamePadDevice::resetAxisDirection(const int axis, Input::AxisDirection direction, const int player)
{
if(value > -m_deadzone && value < m_deadzone) return false; // within deadzone
for(int n=0; n<PA_COUNT; n++)
{
if(m_bindings[n].id == axis && m_bindings[n].dir == direction)
{
RaceManager::getWorld()->getLocalPlayerKart(player)->action((PlayerAction)n, 0);
return;
}
}
}
// -----------------------------------------------------------------------------
bool GamePadDevice::hasBinding(const int axis, const int value, const int player, PlayerAction* action /* out */)
{
// going to negative from positive
if (value < 0 && m_prevAxisDirections[axis] == Input::AD_POSITIVE)
{
// set positive axis to 0
resetAxisDirection(axis, Input::AD_POSITIVE, player);
m_prevAxisDirections[axis] = Input::AD_NEGATIVE;
}
// going to positive from negative
else if (value > 0 && m_prevAxisDirections[axis] == Input::AD_NEGATIVE)
{
// set negative axis to 0
resetAxisDirection(axis, Input::AD_NEGATIVE, player);
m_prevAxisDirections[axis] = Input::AD_POSITIVE;
}
// check if within deadzone
if(value > -m_deadzone && value < m_deadzone)
{
// Axis stands still: This is reported once for digital axes and
// can be called multipled times for analog ones. Uses the
// previous direction in which the axis was triggered to
// determine which one has to be brought into the released
// state. This allows us to regard two directions of an axis
// as completely independent input variants (as if they where
// two buttons).
if(m_prevAxisDirections[axis] == Input::AD_NEGATIVE)
{
// set negative axis to 0
resetAxisDirection(axis, Input::AD_NEGATIVE, player);
}
else if(m_prevAxisDirections[axis] == Input::AD_POSITIVE)
{
// set positive axis to 0
resetAxisDirection(axis, Input::AD_POSITIVE, player);
}
m_prevAxisDirections[axis] = Input::AD_NEUTRAL;
return false;
}
// find corresponding action and return it
for(int n=0; n<PA_COUNT; n++)
{
if(m_bindings[n].id == axis)
@@ -155,7 +211,7 @@ bool GamePadDevice::hasBinding(const int axis, const int value, PlayerAction* ac
*/
GamePadDevice::~GamePadDevice()
{
delete m_prevAxisDirections;
delete[] m_prevAxisDirections;
SDL_JoystickClose(m_sdlJoystick);
} // ~GamePadDevice

View File

@@ -49,6 +49,7 @@ public:
class GamePadDevice : public InputDevice
{
void resetAxisDirection(const int axis, Input::AxisDirection direction, const int player);
public:
SDL_Joystick *m_sdlJoystick;
std::string m_id;
@@ -57,7 +58,7 @@ public:
Input::AxisDirection *m_prevAxisDirections;
/** checks if this key belongs to this belongs. if yes, sets action and returns true; otherwise returns false */
bool hasBinding(const int axis, const int value, PlayerAction* action /* out */) const;
bool hasBinding(const int axis, const int value, const int player, PlayerAction* action /* out */);
void loadDefaults();

View File

@@ -579,8 +579,7 @@ void InputManager::input()
input(Input::IT_STICKMOTION, !m_mode ? 0 : stickIndex,
ev.jaxis.axis, Input::AD_POSITIVE, 0);
m_stick_infos[ev.jaxis.which]->m_prevAxisDirections[ev.jaxis.axis]
= Input::AD_NEUTRAL;
m_stick_infos[ev.jaxis.which]->m_prevAxisDirections[ev.jaxis.axis] = Input::AD_NEUTRAL;
}
#endif
break;