From a1e67a1a6cf677a1eb937fc12954a06aa4a41c34 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 24 Oct 2009 14:08:10 +0000 Subject: make joystick axes configurable --- src/client/input.cc | 84 ++++++++++++++++++++++++++++++-------------------- src/client/joystick.cc | 26 ++++++++++++++-- src/client/joystick.h | 12 ++++++++ 3 files changed, 87 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/client/input.cc b/src/client/input.cc index 790af91..87fa0b0 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -591,47 +591,65 @@ void key_released(Key *key) void axis_event(int axis, int value) { - // value is in range -32768 to 32767 - switch (axis) { - case 0: // direction - if (value >= 0) { - local_direction = (float) value / 32767.0f; - } else { - local_direction = (float) value / 32768.0f; - } + float axis_direction = 0; + if (Joystick::input_axisdirection) { + axis_direction = truncf(Joystick::input_axisdirection->value()); + } - local_direction *= -1; - break; + float axis_pitch = 0; + if (Joystick::input_axispitch) { + axis_pitch = truncf(Joystick::input_axispitch->value()); + } - case 1: // pitch - if (value >= 0) { - local_pitch = (float) value / 32767.0f; - } else { - local_pitch = (float) value / 32768.0f; - } + float axis_roll = 0; + if (Joystick::input_axisroll) { + axis_roll = truncf(Joystick::input_axisroll->value()); + } + + float axis_throttle = 0; + if (Joystick::input_axisthrottle) { + axis_throttle = (int) truncf(Joystick::input_axisthrottle->value()); + } + // value is in range -32768 to 32767 + if (axis + 1 == math::absf(axis_direction)) { + + if (value >= 0) { + local_direction = (float) value / 32767.0f; + } else { + local_direction = (float) value / 32768.0f; + } - break; + local_direction *= math::sgnf(axis_direction) * -1.0f; - case 2: // roll - if (value >= 0) { - local_roll = (float) value / 32767.0f; - } else { - local_roll = (float) value / 32768.0f; - } + } else if (axis + 1 == math::absf(axis_pitch)) { + + if (value >= 0) { + local_pitch = (float) value / 32767.0f; + } else { + local_pitch = (float) value / 32768.0f; + } + local_pitch *= math::sgnf(axis_pitch); - local_roll *= -1; - break; + } else if (axis + 1 == math::absf(axis_roll)) { + + if (value >= 0) { + local_roll = (float) value / 32767.0f; + } else { + local_roll = (float) value / 32768.0f; + } + local_roll *= math::sgnf(axis_roll) * -1.0f; - case 3: // throttle - if (value >= 0) { - local_thrust = (float) value / 32767.0f; - } else { - local_thrust = (float) value / 32768.0f; - } + } else if (axis + 1 == math::absf(axis_throttle)) { + + if (value >= 0) { + local_thrust = (float) value / 32767.0f; + } else { + local_thrust = (float) value / 32768.0f; + } - local_thrust = -0.5f * local_thrust + 0.5f; + local_thrust = -0.5f * local_thrust + 0.5f; + local_thrust *= math::sgnf(axis_throttle); } - } void reset() diff --git a/src/client/joystick.cc b/src/client/joystick.cc index c8f7f9d..0ea05a8 100644 --- a/src/client/joystick.cc +++ b/src/client/joystick.cc @@ -8,14 +8,24 @@ #include "auxiliary/functions.h" #include "client/joystick.h" -#include "core/cvar.h" + #include "sys/sys.h" namespace client { -core::Cvar *input_joystick = 0; +// configured joystick number +core::Cvar *Joystick::input_joystick = 0; + +// joystick axes +core::Cvar *Joystick::input_axisdirection = 0; +core::Cvar *Joystick::input_axispitch = 0; +core::Cvar *Joystick::input_axisroll = 0; +core::Cvar *Joystick::input_axisthrottle = 0; + +// current joystick number int current_joystick_number = 0; + SDL_Joystick *current_joystick = 0; void Joystick::init() @@ -40,6 +50,18 @@ void Joystick::init() input_joystick = core::Cvar::get("input_joystick", 0.0f, core::Cvar::Archive); input_joystick->set_info("[int] set joystick number to use"); + input_axisdirection = core::Cvar::get("input_axisdirection", 1.0f, core::Cvar::Archive); + input_axisdirection->set_info("[int] joystick axis for direction control, 0 disables"); + + input_axispitch = core::Cvar::get("input_axispitch", 2.0f, core::Cvar::Archive); + input_axispitch->set_info("[int] joystick axis for pitch control, 0 disables"); + + input_axisroll = core::Cvar::get("input_axisroll", 3.0f, core::Cvar::Archive); + input_axisroll->set_info("[int] joystick axis for roll control, 0 disables"); + + input_axisthrottle = core::Cvar::get("input_axisthrottle", 4.0f, core::Cvar::Archive); + input_axisthrottle->set_info("[int] joystick axis for throttle control, 0 disables"); + current_joystick_number = 0; SDL_JoystickEventState(SDL_IGNORE); } diff --git a/src/client/joystick.h b/src/client/joystick.h index 9b1925d..4695003 100644 --- a/src/client/joystick.h +++ b/src/client/joystick.h @@ -7,6 +7,8 @@ #ifndef __INCLUDED_CLIENT_JOYSTICK_H__ #define __INCLUDED_CLIENT_JOYSTICK_H__ +#include "core/cvar.h" + namespace client { @@ -22,6 +24,16 @@ public: static void frame(); static bool is_enabled(); + + // configured joystick number + static core::Cvar *input_joystick; + + // joystick axes + static core::Cvar *input_axisdirection; + static core::Cvar *input_axispitch; + static core::Cvar *input_axisroll; + static core::Cvar *input_axisthrottle; + }; } -- cgit v1.2.3