浏览代码

Applied joystick index patch from Gunnar Kriik.

Lasse Öörni 12 年之前
父节点
当前提交
d90a9168cf
共有 4 个文件被更改,包括 36 次插入22 次删除
  1. 1 0
      Docs/Urho3D.dox
  2. 1 0
      Readme.txt
  3. 32 22
      Source/Engine/Input/Input.cpp
  4. 2 0
      Source/Engine/Input/Input.h

+ 1 - 0
Docs/Urho3D.dox

@@ -61,6 +61,7 @@ Urho3D development, contributions and bugfixes by:
 - Alex Fuller
 - Mika Heinonen
 - Jason Kinzer
+- Gunnar Kriik
 - Ali Kämäräinen
 - Pete Leigh
 - Paul Noome

+ 1 - 0
Readme.txt

@@ -21,6 +21,7 @@ Urho3D development, contributions and bugfixes by:
 - Alex Fuller
 - Mika Heinonen
 - Jason Kinzer
+- Gunnar Kriik
 - Ali Kämäräinen
 - Pete Leigh
 - Paul Noome

+ 32 - 22
Source/Engine/Input/Input.cpp

@@ -233,6 +233,10 @@ bool Input::OpenJoystick(unsigned index)
     SDL_Joystick* joystick = SDL_JoystickOpen(index);
     if (joystick)
     {
+        // Map SDL joystick index to internal index (which starts at 0)
+        int sdl_joy_instance_id = SDL_JoystickInstanceID(joystick);
+        joystickIDMap_[sdl_joy_instance_id] = index;
+        
         JoystickState& state = joysticks_[index];
         state.joystick_ = joystick;
         state.buttons_.Resize(SDL_JoystickNumButtons(joystick));
@@ -734,15 +738,16 @@ void Input::HandleSDLEvent(void* sdlEvent)
         {
             using namespace JoystickButtonDown;
 
+            unsigned button = evt.jbutton.button;
+            unsigned joystickIndex = joystickIDMap_[evt.jbutton.which];
+            
             VariantMap eventData;
-            eventData[P_JOYSTICK] = evt.jbutton.which;
-            eventData[P_BUTTON] = evt.jbutton.button;
-
-            if (evt.jbutton.which < joysticks_.Size() && evt.jbutton.button <
-                joysticks_[evt.jbutton.which].buttons_.Size())
-            {
-                joysticks_[evt.jbutton.which].buttons_[evt.jbutton.button] = true;
-                joysticks_[evt.jbutton.which].buttonPress_[evt.jbutton.button] = true;
+            eventData[P_JOYSTICK] = joystickIndex;
+            eventData[P_BUTTON] = button;
+            
+            if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
+                joysticks_[joystickIndex].buttons_[button] = true;
+                joysticks_[joystickIndex].buttonPress_[button] = true;
                 SendEvent(E_JOYSTICKBUTTONDOWN, eventData);
             }
         }
@@ -752,14 +757,15 @@ void Input::HandleSDLEvent(void* sdlEvent)
         {
             using namespace JoystickButtonUp;
 
+            unsigned button = evt.jbutton.button;
+            unsigned joystickIndex = joystickIDMap_[evt.jbutton.which];
+            
             VariantMap eventData;
-            eventData[P_JOYSTICK] = evt.jbutton.which;
-            eventData[P_BUTTON] = evt.jbutton.button;
+            eventData[P_JOYSTICK] = joystickIndex;
+            eventData[P_BUTTON] = button;
 
-            if (evt.jbutton.which < joysticks_.Size() && evt.jbutton.button <
-                joysticks_[evt.jbutton.which].buttons_.Size())
-            {
-                joysticks_[evt.jbutton.which].buttons_[evt.jbutton.button] = false;
+            if (joystickIndex < joysticks_.Size() && button < joysticks_[joystickIndex].buttons_.Size()) {
+                joysticks_[joystickIndex].buttons_[button] = false;
                 SendEvent(E_JOYSTICKBUTTONUP, eventData);
             }
         }
@@ -768,16 +774,18 @@ void Input::HandleSDLEvent(void* sdlEvent)
     case SDL_JOYAXISMOTION:
         {
             using namespace JoystickAxisMove;
+            
+            unsigned joystickIndex = joystickIDMap_[evt.jaxis.which];
 
             VariantMap eventData;
-            eventData[P_JOYSTICK] = evt.jaxis.which;
+            eventData[P_JOYSTICK] = joystickIndex;
             eventData[P_AXIS] = evt.jaxis.axis;
             eventData[P_POSITION] = Clamp((float)evt.jaxis.value / 32767.0f, -1.0f, 1.0f);
 
-            if (evt.jaxis.which < joysticks_.Size() && evt.jaxis.axis <
-                joysticks_[evt.jaxis.which].axes_.Size())
+            if (joystickIndex < joysticks_.Size() && evt.jaxis.axis <
+                joysticks_[joystickIndex].axes_.Size())
             {
-                joysticks_[evt.jaxis.which].axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
+                joysticks_[joystickIndex].axes_[evt.jaxis.axis] = eventData[P_POSITION].GetFloat();
                 SendEvent(E_JOYSTICKAXISMOVE, eventData);
             }
         }
@@ -786,16 +794,18 @@ void Input::HandleSDLEvent(void* sdlEvent)
     case SDL_JOYHATMOTION:
         {
             using namespace JoystickHatMove;
+            
+            unsigned joystickIndex = joystickIDMap_[evt.jaxis.which];
 
             VariantMap eventData;
-            eventData[P_JOYSTICK] = evt.jhat.which;
+            eventData[P_JOYSTICK] = joystickIndex;
             eventData[P_HAT] = evt.jhat.hat;
             eventData[P_POSITION] = evt.jhat.value;
 
-            if (evt.jhat.which < joysticks_.Size() && evt.jhat.hat <
-                joysticks_[evt.jhat.which].hats_.Size())
+            if (joystickIndex < joysticks_.Size() && evt.jhat.hat <
+                joysticks_[joystickIndex].hats_.Size())
             {
-                joysticks_[evt.jhat.which].hats_[evt.jhat.hat] = evt.jhat.value;
+                joysticks_[joystickIndex].hats_[evt.jhat.hat] = evt.jhat.value;
                 SendEvent(E_JOYSTICKHATMOVE, eventData);
             }
         }

+ 2 - 0
Source/Engine/Input/Input.h

@@ -244,6 +244,8 @@ private:
     bool suppressNextMouseMove_;
     /// Initialized flag.
     bool initialized_;
+    /// Map SDL joystick ID to internal index.
+    HashMap<int, unsigned> joystickIDMap_;
 };
 
 }