Browse Source

joystick handling refactoried; all platforms should be as flexible as expected

Pal Lockheart 6 years ago
parent
commit
1313f23883
5 changed files with 112 additions and 43 deletions
  1. 107 40
      input.c
  2. 3 0
      input.h
  3. 1 1
      ios/SDLPal/SDLPal.xcodeproj/project.pbxproj
  4. 1 1
      macos/Pal.xcodeproj/project.pbxproj
  5. 0 1
      macos/pal_config.h

+ 107 - 40
input.c

@@ -528,32 +528,17 @@ PAL_JoystickEventFilter(
          //
          // X axis
          //
-         if (lpEvent->jaxis.value > 20000)
+         if (lpEvent->jaxis.value > 3200)
          {
-            if (g_InputState.dir != kDirEast)
-            {
-               g_InputState.dwKeyPress |= kKeyRight;
-            }
-            g_InputState.prevdir = g_InputState.dir;
-            g_InputState.dir = kDirEast;
+            g_InputState.axisX = 1;
          }
-         else if (lpEvent->jaxis.value < -20000)
+         else if (lpEvent->jaxis.value < -3200)
          {
-            if (g_InputState.dir != kDirWest)
-            {
-               g_InputState.dwKeyPress |= kKeyLeft;
-            }
-            g_InputState.prevdir = g_InputState.dir;
-            g_InputState.dir = kDirWest;
+            g_InputState.axisX = -1;
          }
          else
          {
-            if (g_InputState.prevdir != kDirEast &&
-               g_InputState.prevdir != kDirWest)
-            {
-               g_InputState.dir = g_InputState.prevdir;
-            }
-            g_InputState.prevdir = kDirUnknown;
+            g_InputState.axisX = 0;
          }
          break;
 
@@ -561,37 +546,64 @@ PAL_JoystickEventFilter(
          //
          // Y axis
          //
-         if (lpEvent->jaxis.value > 20000)
+         if (lpEvent->jaxis.value > 3200)
          {
-            if (g_InputState.dir != kDirSouth)
-            {
-               g_InputState.dwKeyPress |= kKeyDown;
-            }
-            g_InputState.prevdir = g_InputState.dir;
-            g_InputState.dir = kDirSouth;
+            g_InputState.axisY = 1;
          }
-         else if (lpEvent->jaxis.value < -20000)
+         else if (lpEvent->jaxis.value < -3200)
          {
-            if (g_InputState.dir != kDirNorth)
-            {
-               g_InputState.dwKeyPress |= kKeyUp;
-            }
-            g_InputState.prevdir = g_InputState.dir;
-            g_InputState.dir = kDirNorth;
+            g_InputState.axisY = -1;
          }
          else
          {
-            if (g_InputState.prevdir != kDirNorth &&
-               g_InputState.prevdir != kDirSouth)
-            {
-               g_InputState.dir = g_InputState.prevdir;
-            }
-            g_InputState.prevdir = kDirUnknown;
+            g_InputState.axisY = 0;
          }
          break;
       }
       break;
 
+   case SDL_JOYHATMOTION:
+      //
+      // Pressed the joystick hat button
+      //
+      switch (lpEvent->jhat.value)
+      {
+         case SDL_HAT_LEFT:
+         case SDL_HAT_LEFTUP:
+            g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
+            g_InputState.dir = kDirWest;
+            g_InputState.dwKeyPress = kKeyLeft;
+            break;
+
+         case SDL_HAT_RIGHT:
+         case SDL_HAT_RIGHTDOWN:
+            g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
+            g_InputState.dir = kDirEast;
+            g_InputState.dwKeyPress = kKeyRight;
+            break;
+
+         case SDL_HAT_UP:
+         case SDL_HAT_RIGHTUP:
+            g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
+            g_InputState.dir = kDirNorth;
+            g_InputState.dwKeyPress = kKeyUp;
+            break;
+
+         case SDL_HAT_DOWN:
+         case SDL_HAT_LEFTDOWN:
+            g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
+            g_InputState.dir = kDirSouth;
+            g_InputState.dwKeyPress = kKeyDown;
+            break;
+
+         case SDL_HAT_CENTERED:
+            g_InputState.prevdir = (gpGlobals->fInBattle ? kDirUnknown : g_InputState.dir);
+            g_InputState.dir = kDirUnknown;
+            g_InputState.dwKeyPress = kKeyNone;
+            break;
+      }
+      break;
+
    case SDL_JOYBUTTONDOWN:
       //
       // Pressed the joystick button
@@ -611,6 +623,58 @@ PAL_JoystickEventFilter(
 #endif
 }
 
+static VOID
+PAL_UpdateJoyStickState(
+VOID
+)
+/*++
+ Purpose:
+ 
+ Poll & update joystick state.
+ 
+ Parameters:
+ 
+ None.
+ 
+ Return value:
+ 
+ None.
+ 
+ --*/
+{
+   if( g_InputState.axisX == 1 && g_InputState.axisY >= 0 )
+   {
+      g_InputState.prevdir = g_InputState.dir;
+      g_InputState.dir = kDirEast;
+      g_InputState.dwKeyPress |= kKeyRight;
+   }
+   else if( g_InputState.axisX == -1 && g_InputState.axisY <= 0 )
+   {
+      g_InputState.prevdir = g_InputState.dir;
+      g_InputState.dir = kDirWest;
+      g_InputState.dwKeyPress |= kKeyLeft;
+   }
+   else if( g_InputState.axisY == 1 && g_InputState.axisX <= 0 )
+   {
+      g_InputState.prevdir = g_InputState.dir;
+      g_InputState.dir = kDirSouth;
+      g_InputState.dwKeyPress |= kKeyDown;
+   }
+   else if( g_InputState.axisY == -1 && g_InputState.axisX >= 0 )
+   {
+      g_InputState.prevdir = g_InputState.dir;
+      g_InputState.dir = kDirNorth;
+      g_InputState.dwKeyPress |= kKeyUp;
+   }
+   else
+   {
+      g_InputState.prevdir = g_InputState.dir;
+      g_InputState.dir = kDirUnknown;
+      if(!input_event_filter)
+         g_InputState.dwKeyPress = kKeyNone;
+   }
+}
+
 #if PAL_HAS_TOUCH
 
 #define  TOUCH_NONE    0
@@ -1115,6 +1179,9 @@ PAL_ProcessEvent(
    while (PAL_PollEvent(NULL));
 
    PAL_UpdateKeyboardState();
+#if PAL_HAS_JOYSTICKS
+   PAL_UpdateJoyStickState();
+#endif
 #if PAL_HAS_TOUCH
    PAL_TouchRepeatCheck();
 #endif

+ 3 - 0
input.h

@@ -30,6 +30,9 @@ typedef struct tagPALINPUTSTATE
 {
    PALDIRECTION           dir, prevdir;
    DWORD                  dwKeyPress;
+#if PAL_HAS_JOYSTICKS
+   int                    axisX,axisY;
+#endif
 } PALINPUTSTATE;
 
 enum PALKEY

+ 1 - 1
ios/SDLPal/SDLPal.xcodeproj/project.pbxproj

@@ -269,7 +269,7 @@
 		71655209195BB6DA006E1227 /* game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = game.h; path = ../../../game.h; sourceTree = "<group>"; };
 		7165520C195BB6DA006E1227 /* global.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = global.c; path = ../../../global.c; sourceTree = "<group>"; };
 		7165520D195BB6DA006E1227 /* global.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = global.h; path = ../../../global.h; sourceTree = "<group>"; };
-		7165520E195BB6DA006E1227 /* input.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = input.c; path = ../../../input.c; sourceTree = "<group>"; };
+		7165520E195BB6DA006E1227 /* input.c */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.c; name = input.c; path = ../../../input.c; sourceTree = "<group>"; tabWidth = 3; };
 		7165520F195BB6DA006E1227 /* input.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = input.h; path = ../../../input.h; sourceTree = "<group>"; };
 		71655210195BB6DA006E1227 /* itemmenu.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = itemmenu.c; path = ../../../itemmenu.c; sourceTree = "<group>"; };
 		71655211195BB6DA006E1227 /* itemmenu.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = itemmenu.h; path = ../../../itemmenu.h; sourceTree = "<group>"; };

+ 1 - 1
macos/Pal.xcodeproj/project.pbxproj

@@ -267,7 +267,7 @@
 		7104FD400D772F6300A97E53 /* game.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = game.h; sourceTree = "<group>"; };
 		7104FD430D772F6300A97E53 /* global.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = global.c; sourceTree = "<group>"; };
 		7104FD440D772F6300A97E53 /* global.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = global.h; sourceTree = "<group>"; };
-		7104FD450D772F6300A97E53 /* input.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = input.c; sourceTree = "<group>"; };
+		7104FD450D772F6300A97E53 /* input.c */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.c; path = input.c; sourceTree = "<group>"; tabWidth = 3; };
 		7104FD460D772F6300A97E53 /* input.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = input.h; sourceTree = "<group>"; };
 		7104FD470D772F6300A97E53 /* main.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = main.h; sourceTree = "<group>"; };
 		7104FD480D772F6300A97E53 /* map.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = map.c; sourceTree = "<group>"; };

+ 0 - 1
macos/pal_config.h

@@ -58,7 +58,6 @@
 #endif
 
 #define PAL_HAS_MOUSE 0
-#define PAL_HAS_JOYSTICK 1
 
 #define PAL_HAS_CONFIG_PAGE 0