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; break;
case IT_MOUSEBUTTON: case IT_MOUSEBUTTON:
if (!value) // Act on button release only.
switch (id0) switch (id0)
{ {
case SDL_BUTTON_LEFT: case SDL_BUTTON_LEFT:
@ -69,7 +70,7 @@ void BaseGUI::input(InputType type, int id0, int id1, int id2, int value)
break; break;
case IT_STICKBUTTON: case IT_STICKBUTTON:
if( value) if( !value) // act on button release only
switch (id1) // Button no switch (id1) // Button no
{ {
case 0: 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) void BaseGUI::inputKeyboard(int key, int pressed)
{ {
if (!pressed) // Skip on keypress, act on keyrelease only.
if (pressed)
return; return;
switch ( key ) 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 RaceGUI::handleInput(InputType type, int id0, int id1, int id2, int value)
bool isKeyboard)
{ {
PlayerKart *k = m_input_map[type][id0][id1][id2].kart; PlayerKart *k = m_input_map[type][id0][id1][id2].kart;
if (k) 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; return true;
} }
else else
@ -148,12 +147,13 @@ void RaceGUI::input(InputType type, int id0, int id1, int id2, int value)
switch (type) switch (type)
{ {
case IT_KEYBOARD: case IT_KEYBOARD:
// Set flag that this event is a keyboard event. // Stuff that handleInput() does not care about are
if (!handleInput(type, id0, id1, id2, value, true)) // internal keyboard actions.
if (!handleInput(type, id0, id1, id2, value))
inputKeyboard(id0, value); inputKeyboard(id0, value);
break; break;
default: // no keyboard event default: // no keyboard event
handleInput(type, id0, id1, id2, value, false); handleInput(type, id0, id1, id2, value);
break; break;
} }

View File

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

View File

@ -29,36 +29,25 @@
#include "gui/race_gui.hpp" #include "gui/race_gui.hpp"
#include "translation.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) switch (action)
{ {
case KC_LEFT: case KC_LEFT:
// Special case: when releasing a key on the keyboard, take the m_steer_val_l = -value;
// current value of the opposite steering direction. If the opposite if (value)
// key is not pressed, this value is zero, otherwise it will m_steer_val = -value;
// 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];
}
else else
{ m_steer_val = m_steer_val_r;
m_steer_val = -value;
}
break; break;
case KC_RIGHT: case KC_RIGHT:
if(isKeyboard && value==0) m_steer_val_r = value;
{ if (value)
m_steer_val = -m_action_keys_values[KC_LEFT]; m_steer_val = value;
}
else else
{ m_steer_val = m_steer_val_l;
m_steer_val = value;
}
break; break;
case KC_ACCEL: case KC_ACCEL:
m_accel_val = value; 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 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; 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; m_controls.lr -= STEER_CHANGE;
} }
else 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)); m_controls.lr = std::min(1.0f, std::max(-1.0f, m_controls.lr));
} // smoothSteer } // steer
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void PlayerKart::update(float dt) 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) if(world->getPhase()==World::START_PHASE)
{ {
@ -182,6 +179,8 @@ void PlayerKart::collectedHerring(Herring* herring)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void PlayerKart::reset() void PlayerKart::reset()
{ {
m_steer_val_l = 0;
m_steer_val_r = 0;
m_steer_val = 0; m_steer_val = 0;
m_accel_val = 0; m_accel_val = 0;
m_controls.accel = 0.0; m_controls.accel = 0.0;
@ -190,10 +189,6 @@ void PlayerKart::reset()
m_controls.wheelie = false; m_controls.wheelie = false;
m_controls.jump = false; m_controls.jump = false;
m_penalty_time = 0; m_penalty_time = 0;
for(int i=KC_LEFT; i<=KC_FIRE; i++)
{
m_action_keys_values[i]=false;
}
Kart::reset(); Kart::reset();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

@ -32,16 +32,12 @@ class Player;
class PlayerKart : public Kart class PlayerKart : public Kart
{ {
private: 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; Player *m_player;
float m_penalty_time; 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: public:
PlayerKart(const KartProperties *kart_properties, PlayerKart(const KartProperties *kart_properties,
int position, Player *_player, int position, Player *_player,
@ -53,7 +49,7 @@ public:
Player* getPlayer () {return m_player; } Player* getPlayer () {return m_player; }
void update (float); void update (float);
void addMessages (); void addMessages ();
void action (KartActions action, int value, bool isKeyboard); void action (KartActions action, int value);
void forceCrash (); void forceCrash ();
void handleZipper (); void handleZipper ();
void collectedHerring (Herring* herring); void collectedHerring (Herring* herring);

View File

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