From 7ac353a7895f1ab30d09268968bc85ef9394cf41 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 5 Aug 2008 00:39:09 +0000 Subject: initial joystick control support --- src/client/input.cc | 60 ++++++++++++++++++++++++++++++++++++++++++++------ src/client/joystick.cc | 5 +++++ src/client/joystick.h | 2 ++ 3 files changed, 60 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/client/input.cc b/src/client/input.cc index 6778e9c..2f6fb27 100644 --- a/src/client/input.cc +++ b/src/client/input.cc @@ -66,6 +66,8 @@ float mouse_control_override_time = 0; bool mouse_control_override = false; bool mouse_control = false; +bool joystick_control = false; + const float thruster_offset = 0.05f; const float mouse_timeout = 0.200f; // 200 ms mouse timout @@ -533,6 +535,42 @@ 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; + } + + local_direction *= -1; + break; + + case 1: // pitch + if (value >= 0) { + local_pitch = (float) value / 32767.0f; + } else { + local_pitch = (float) value / 32768.0f; + } + + break; + + case 2: // roll + if (value >= 0) { + local_roll = (float) value / 32767.0f; + } else { + local_roll = (float) value / 32768.0f; + } + + local_roll *= -1; + break; + } + +} + void reset() { local_direction = 0.0f; @@ -586,9 +624,9 @@ void frame(float seconds) render::reset(); } - /* -- detect joystick changes --------------------- */ + /* -- detect joystick stat changes ---------------- */ Joystick::frame(); - + joystick_control = Joystick::is_enabled(); /* -- poll SDL keyboard events -------------------- */ SDL_Event event; @@ -632,6 +670,10 @@ void frame(float seconds) pressed = false; break; + case SDL_JOYAXISMOTION: + axis_event((int) event.jaxis.axis, (int) event.jaxis.value); + break; + case SDL_KEYDOWN: keyboard_modifiers = event.key.keysym.mod; key = keyboard->press(event.key.keysym.sym); @@ -700,10 +742,13 @@ void frame(float seconds) mouse_deadzone = false; if (core::application()->connected() && core::localcontrol()) { - - mouse_control = !console()->visible() && ((input_mousecontrol->value() > 0) - || (mouse_control_override && (mouse_control_override_time + mouse_timeout < core::application()->time()))); - //core::localcontrol()->set_autolevel(!mouse_control); + mouse_control = !console()->visible() && ((input_mousecontrol->value() > 0) || (mouse_control_override && (mouse_control_override_time + mouse_timeout < core::application()->time()))); + + if (mouse_control && joystick_control && ((render::Camera::mode() == render::Camera::Track) || (render::Camera::mode() == render::Camera::Cockpit))) { + if (!(mouse_control_override && (mouse_control_override_time + mouse_timeout < core::application()->time()))) { + mouse_control = false; + } + } if (mouse_control) { // the mouse will not react if it is in the deadzone @@ -735,6 +780,7 @@ void frame(float seconds) } if ((render::Camera::mode() == render::Camera::Track) || (render::Camera::mode() == render::Camera::Cockpit)) { + local_direction = mouse_direction * math::absf(mouse_direction); local_pitch = mouse_pitch * math::absf(mouse_pitch); @@ -745,7 +791,7 @@ void frame(float seconds) } } - + math::clamp(local_direction, -1.0f, 1.0f); math::clamp(local_pitch, -1.0f, 1.0f); math::clamp(local_roll, -1.0f, 1.0f); diff --git a/src/client/joystick.cc b/src/client/joystick.cc index cf2270f..5f09280 100644 --- a/src/client/joystick.cc +++ b/src/client/joystick.cc @@ -56,6 +56,11 @@ void Joystick::shutdown() SDL_QuitSubSystem(SDL_INIT_JOYSTICK); } +bool Joystick::is_enabled() +{ + return (current_joystick != 0); +} + void Joystick::frame() { if (current_joystick_number != (int) input_joystick->value()) diff --git a/src/client/joystick.h b/src/client/joystick.h index e21a60c..9404cb3 100644 --- a/src/client/joystick.h +++ b/src/client/joystick.h @@ -18,6 +18,8 @@ public: static void shutdown(); static void frame(); + + static bool is_enabled(); }; } -- cgit v1.2.3