Applied Robert's joystick steering fix patch.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1197 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
coz 2007-08-05 03:37:29 +00:00
parent f6ca3b5155
commit 25a007cefd
6 changed files with 65 additions and 76 deletions

View File

@ -45,6 +45,7 @@ void BaseGUI::input(InputType type, int id0, int id1, int id2, int value)
break;
case IT_MOUSEBUTTON:
if (!value) // Act on button release only.
switch (id0)
{
case SDL_BUTTON_LEFT:
@ -69,7 +70,7 @@ void BaseGUI::input(InputType type, int id0, int id1, int id2, int value)
break;
case IT_STICKBUTTON:
if( value)
if( !value) // act on button release only
switch (id1) // Button no
{
case 0:
@ -97,7 +98,8 @@ void BaseGUI::input(InputType type, int id0, int id1, int id2, int value)
//-----------------------------------------------------------------------------
void BaseGUI::inputKeyboard(int key, int pressed)
{
if (!pressed)
// Skip on keypress, act on keyrelease only.
if (pressed)
return;
switch ( key )

View File

@ -120,14 +120,13 @@ void RaceGUI::putEntry(PlayerKart *kart, KartActions kc)
}
//-----------------------------------------------------------------------------
bool RaceGUI::handleInput(InputType type, int id0, int id1, int id2, int value,
bool isKeyboard)
bool RaceGUI::handleInput(InputType type, int id0, int id1, int id2, int value)
{
PlayerKart *k = m_input_map[type][id0][id1][id2].kart;
if (k)
{
k->action(m_input_map[type][id0][id1][id2].action, value, isKeyboard);
k->action(m_input_map[type][id0][id1][id2].action, value);
return true;
}
else
@ -148,12 +147,13 @@ void RaceGUI::input(InputType type, int id0, int id1, int id2, int value)
switch (type)
{
case IT_KEYBOARD:
// Set flag that this event is a keyboard event.
if (!handleInput(type, id0, id1, id2, value, true))
// Stuff that handleInput() does not care about are
// internal keyboard actions.
if (!handleInput(type, id0, id1, id2, value))
inputKeyboard(id0, value);
break;
default: // no keyboard event
handleInput(type, id0, id1, id2, value, false);
handleInput(type, id0, id1, id2, value);
break;
}

View File

@ -103,8 +103,7 @@ private:
float ratio_x, float ratio_y );
void UpdateKeyboardMappings();
void putEntry(PlayerKart *kart, KartActions ka);
bool handleInput(InputType type, int id0, int id1, int id2, int value,
bool isKeyboard=false);
bool handleInput(InputType type, int id0, int id1, int id2, int value);
void inputKeyboard(int key, int pressed);
void drawPlayerIcons ();
void oldDrawPlayerIcons ();

View File

@ -29,36 +29,25 @@
#include "gui/race_gui.hpp"
#include "translation.hpp"
void PlayerKart::action(KartActions action, int value, bool isKeyboard)
void PlayerKart::action(KartActions action, int value)
{
if(isKeyboard) m_action_keys_values[action]=value;
switch (action)
{
case KC_LEFT:
// Special case: when releasing a key on the keyboard, take the
// current value of the opposite steering direction. If the opposite
// key is not pressed, this value is zero, otherwise it will
// pick up the previous direction (e.g. consider: press right,
// press left, release left --> will steer right; or:
// press left, press right, release left --> will steer right)
if(isKeyboard && value==0)
{
m_steer_val = m_action_keys_values[KC_RIGHT];
}
m_steer_val_l = -value;
if (value)
m_steer_val = -value;
else
{
m_steer_val = -value;
}
m_steer_val = m_steer_val_r;
break;
case KC_RIGHT:
if(isKeyboard && value==0)
{
m_steer_val = -m_action_keys_values[KC_LEFT];
}
m_steer_val_r = value;
if (value)
m_steer_val = value;
else
{
m_steer_val = value;
}
m_steer_val = m_steer_val_l;
break;
case KC_ACCEL:
m_accel_val = value;
@ -86,15 +75,23 @@ void PlayerKart::action(KartActions action, int value, bool isKeyboard)
}
//-----------------------------------------------------------------------------
void PlayerKart::smoothSteer(float dt, bool left, bool right)
void PlayerKart::steer(float dt, int steer_val)
{
const float STEER_CHANGE = dt/getTimeFullSteer(); // amount the steering is changed
if (left)
if (steer_val < 0)
{
// If we got analog values do not cumulate.
if (steer_val > -32767)
m_controls.lr = -steer_val/32767.0f;
else
m_controls.lr += STEER_CHANGE;
}
else if(right)
else if(steer_val > 0)
{
// If we got analog values do not cumulate.
if (steer_val < 32767)
m_controls.lr = -steer_val/32767.0f;
else
m_controls.lr -= STEER_CHANGE;
}
else
@ -113,14 +110,14 @@ void PlayerKart::smoothSteer(float dt, bool left, bool right)
m_controls.lr = std::min(1.0f, std::max(-1.0f, m_controls.lr));
} // smoothSteer
} // steer
//-----------------------------------------------------------------------------
void PlayerKart::update(float dt)
{
smoothSteer(dt, m_steer_val == -1, m_steer_val == 1);
steer(dt, m_steer_val);
m_controls.accel = m_accel_val;
m_controls.accel = m_accel_val / 32768.0f;
if(world->getPhase()==World::START_PHASE)
{
@ -182,6 +179,8 @@ void PlayerKart::collectedHerring(Herring* herring)
//-----------------------------------------------------------------------------
void PlayerKart::reset()
{
m_steer_val_l = 0;
m_steer_val_r = 0;
m_steer_val = 0;
m_accel_val = 0;
m_controls.accel = 0.0;
@ -190,10 +189,6 @@ void PlayerKart::reset()
m_controls.wheelie = false;
m_controls.jump = false;
m_penalty_time = 0;
for(int i=KC_LEFT; i<=KC_FIRE; i++)
{
m_action_keys_values[i]=false;
}
Kart::reset();
}
//-----------------------------------------------------------------------------

View File

@ -32,16 +32,12 @@ class Player;
class PlayerKart : public Kart
{
private:
int m_steer_val, m_accel_val;
int m_steer_val, m_steer_val_l, m_steer_val_r, m_accel_val;
Player *m_player;
float m_penalty_time;
// This field saves all currently pressed keys for each action. This is used
// to solve problems like: press left, press right, release right -->
// the kart should in this case steer left.
int m_action_keys_values[KC_FIRE+1];
void smoothSteer(float dt, bool left, bool right);
void steer(float, int);
public:
PlayerKart(const KartProperties *kart_properties,
int position, Player *_player,
@ -53,7 +49,7 @@ public:
Player* getPlayer () {return m_player; }
void update (float);
void addMessages ();
void action (KartActions action, int value, bool isKeyboard);
void action (KartActions action, int value);
void forceCrash ();
void handleZipper ();
void collectedHerring (Herring* herring);

View File

@ -40,6 +40,7 @@ long flags;
SDL_Joystick **sticks;
#define DEADZONE_MOUSE 1
#define DEADZONE_JOYSTICK 1000
//-----------------------------------------------------------------------------
void drv_init()
@ -158,13 +159,12 @@ void drv_loop()
game_manager->abort();
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
if(user_config->m_keyboard_debug)
{
printf("keyboard: %d %d\n",ev.key.keysym.sym,ev.key.state);
}
input(IT_KEYBOARD, ev.key.keysym.sym, ev.key.keysym.unicode, 0, ev.key.state);
input(IT_KEYBOARD, ev.key.keysym.sym, ev.key.keysym.unicode, 0, 0);
break;
case SDL_KEYDOWN:
input(IT_KEYBOARD, ev.key.keysym.sym, ev.key.keysym.unicode, 0, 32768);
break;
case SDL_MOUSEMOTION:
@ -194,36 +194,33 @@ void drv_loop()
#endif
break;
case SDL_MOUSEBUTTONUP:
input(IT_MOUSEBUTTON, ev.button.button, 0, 0, 0);
break;
case SDL_MOUSEBUTTONDOWN:
input(IT_MOUSEBUTTON, ev.button.button, 0, 0, ev.button.state);
input(IT_MOUSEBUTTON, ev.button.button, 0, 0, 32768);
break;
case SDL_JOYAXISMOTION:
if(ev.jaxis.value <= -1000)
{
#ifdef ALT_JOY_HANDLING
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, 0);
#endif
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, 1);
}
else if(ev.jaxis.value >= 1000)
{
#ifdef ALT_JOY_HANDLING
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, 0);
#endif
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, 1);
}
else
{
if(ev.jaxis.value <= -DEADZONE_JOYSTICK)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, -ev.jaxis.value);
else if(ev.jaxis.value <= 0)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_NEGATIVE, 0);
if(ev.jaxis.value >= DEADZONE_JOYSTICK)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, ev.jaxis.value);
else if(ev.jaxis.value >= 0)
input(IT_STICKMOTION, ev.jaxis.which, ev.jaxis.axis, AD_POSITIVE, 0);
}
break;
case SDL_JOYBUTTONDOWN:
break;
case SDL_JOYBUTTONUP:
input(IT_STICKBUTTON, ev.jbutton.which, ev.jbutton.button, 0,
ev.jbutton.state);
0);
break;
case SDL_JOYBUTTONDOWN:
input(IT_STICKBUTTON, ev.jbutton.which, ev.jbutton.button, 0,
32768);
break;
} // switch
} // while (SDL_PollEvent())