From 546bbc3ea0f210fe3340a0a38b85e8f1abc86f42 Mon Sep 17 00:00:00 2001 From: pelya Date: Sun, 22 Mar 2015 22:21:35 +0200 Subject: [PATCH] SDL: move mouse with right gamepad stick, added bounds checking to moving mouse with arrow keys --- project/java/Globals.java | 2 +- project/java/Video.java | 4 +- .../openttd/AndroidAppSettings.cfg | 4 +- .../android/SDL_androidinput-queue-compat.c | 3 +- .../android/SDL_androidinput-queue-fast.c | 12 +- .../src/video/android/SDL_androidinput.c | 131 +++++++++++------- .../src/video/android/SDL_androidinput.h | 1 + 7 files changed, 102 insertions(+), 55 deletions(-) diff --git a/project/java/Globals.java b/project/java/Globals.java index 976aa2536..b58f2077e 100644 --- a/project/java/Globals.java +++ b/project/java/Globals.java @@ -102,7 +102,7 @@ class Globals public static int RightClickMethod = AppNeedsTwoButtonMouse ? Mouse.RIGHT_CLICK_WITH_MULTITOUCH : Mouse.RIGHT_CLICK_NONE; public static int RightClickKey = KeyEvent.KEYCODE_MENU; public static boolean MoveMouseWithJoystick = false; - public static int MoveMouseWithJoystickSpeed = 0; + public static int MoveMouseWithJoystickSpeed = 1; public static int MoveMouseWithJoystickAccel = 0; public static boolean MoveMouseWithGyroscope = false; public static int MoveMouseWithGyroscopeSpeed = 2; diff --git a/project/java/Video.java b/project/java/Video.java index da7963403..a782d4d2f 100644 --- a/project/java/Video.java +++ b/project/java/Video.java @@ -411,7 +411,7 @@ abstract class DifferentTouchInput DemoGLSurfaceView.nativeGamepadAnalogJoystickInput( event.getAxisValue(MotionEvent.AXIS_X), event.getAxisValue(MotionEvent.AXIS_Y), event.getAxisValue(MotionEvent.AXIS_Z), event.getAxisValue(MotionEvent.AXIS_RZ), - event.getAxisValue(MotionEvent.AXIS_RTRIGGER), event.getAxisValue(MotionEvent.AXIS_LTRIGGER), + event.getAxisValue(MotionEvent.AXIS_LTRIGGER), event.getAxisValue(MotionEvent.AXIS_RTRIGGER), (hatX == 0.0f && hatY == 0.0f) ? 0 : 1 ); return; } @@ -1053,6 +1053,6 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL { public static native void nativeHardwareMouseDetected( int detected ); public static native void nativeMouseButtonsPressed( int buttonId, int pressedState ); public static native void nativeMouseWheel( int scrollX, int scrollY ); - public static native void nativeGamepadAnalogJoystickInput( float stick1x, float stick1y, float stick2x, float stick2y, float rtrigger, float ltrigger, int usingHat ); + public static native void nativeGamepadAnalogJoystickInput( float stick1x, float stick1y, float stick2x, float stick2y, float ltrigger, float rtrigger, int usingHat ); public static native void nativeScreenVisibleRect( int x, int y, int w, int h ); } diff --git a/project/jni/application/openttd/AndroidAppSettings.cfg b/project/jni/application/openttd/AndroidAppSettings.cfg index 460cb2b96..e35d92b64 100644 --- a/project/jni/application/openttd/AndroidAppSettings.cfg +++ b/project/jni/application/openttd/AndroidAppSettings.cfg @@ -21,7 +21,7 @@ AppVersionName="1.4.4.37" AppDataDownloadUrl="!!Data files - 20 Mb|openttd-data-1.4.4-1.zip.xz|http://sourceforge.net/projects/libsdl-android/files/OpenTTD/openttd-data-1.4.4-1.zip.xz^!!Config file|:.openttd/openttd.cfg:openttd-1.4.0.30.cfg^!MIDI music support (18 Mb)|timidity.zip.xz|http://sourceforge.net/projects/libsdl-android/files/timidity.zip.xz^!!Internationalization files|icudt52l.zip.xz|http://sourceforge.net/projects/libsdl-android/files/icudt52l.zip.xz" # Reset SDL config when updating application to the new version (y) / (n) -ResetSdlConfigForThisVersion=n +ResetSdlConfigForThisVersion=y # Delete application data files when upgrading (specify file/dir paths separated by spaces) DeleteFilesOnUpgrade="" @@ -200,7 +200,7 @@ TouchscreenKeysTheme=2 # Redefine gamepad keys to SDL keysyms, button order is: # A B X Y L1 R1 L2 R2 LThumb RThumb -RedefinedKeysGamepad="LALT RETURN KP_PLUS KP_MINUS SPACE DELETE KP_PLUS KP_MINUS 1 2" +RedefinedKeysGamepad="LALT RETURN KP_PLUS KP_MINUS MOUSE_RIGHT MOUSE_LEFT SPACE MOUSE_RIGHT DELETE MOUSE_LEFT" # How long to show startup menu button, in msec, 0 to disable startup menu StartupMenuButtonTimeout=3000 diff --git a/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-compat.c b/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-compat.c index 424d1f61f..fd9ee5f1c 100644 --- a/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-compat.c +++ b/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-compat.c @@ -262,8 +262,9 @@ extern void SDL_ANDROID_MainThreadPushKeyboardKey(int pressed, SDL_scancode key, key == SDL_KEY(UP) || key == SDL_KEY(DOWN) || key == SDL_KEY(LEFT) || key == SDL_KEY(RIGHT) ) ) { - if( SDL_ANDROID_moveMouseWithKbX < 0 ) + if( !SDL_ANDROID_moveMouseWithKbActive ) { + SDL_ANDROID_moveMouseWithKbActive = 1; SDL_ANDROID_moveMouseWithKbX = SDL_ANDROID_currentMouseX; SDL_ANDROID_moveMouseWithKbY = SDL_ANDROID_currentMouseY; } diff --git a/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-fast.c b/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-fast.c index 4351329fd..40fe7e1c2 100644 --- a/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-fast.c +++ b/project/jni/sdl-1.2/src/video/android/SDL_androidinput-queue-fast.c @@ -104,8 +104,9 @@ extern void SDL_ANDROID_MainThreadPushKeyboardKey(int pressed, SDL_scancode key, key == SDL_KEY(UP) || key == SDL_KEY(DOWN) || key == SDL_KEY(LEFT) || key == SDL_KEY(RIGHT) ) ) { - if( SDL_ANDROID_moveMouseWithKbX < 0 ) + if( !SDL_ANDROID_moveMouseWithKbActive ) { + SDL_ANDROID_moveMouseWithKbActive = 1; SDL_ANDROID_moveMouseWithKbX = SDL_ANDROID_currentMouseX; SDL_ANDROID_moveMouseWithKbY = SDL_ANDROID_currentMouseY; } @@ -165,6 +166,15 @@ extern void SDL_ANDROID_MainThreadPushKeyboardKey(int pressed, SDL_scancode key, SDL_ANDROID_moveMouseWithKbX += SDL_ANDROID_moveMouseWithKbSpeedX; SDL_ANDROID_moveMouseWithKbY += SDL_ANDROID_moveMouseWithKbSpeedY; + if (SDL_ANDROID_moveMouseWithKbX < 0) + SDL_ANDROID_moveMouseWithKbX = 0; + if (SDL_ANDROID_moveMouseWithKbY < 0) + SDL_ANDROID_moveMouseWithKbY = 0; + if (SDL_ANDROID_moveMouseWithKbX >= SDL_ANDROID_sFakeWindowWidth) + SDL_ANDROID_moveMouseWithKbX = SDL_ANDROID_sFakeWindowWidth - 1; + if (SDL_ANDROID_moveMouseWithKbY >= SDL_ANDROID_sFakeWindowHeight) + SDL_ANDROID_moveMouseWithKbY = SDL_ANDROID_sFakeWindowHeight - 1; + SDL_ANDROID_MainThreadPushMouseMotion(SDL_ANDROID_moveMouseWithKbX, SDL_ANDROID_moveMouseWithKbY); return; } diff --git a/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c b/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c index 26b3c9a64..987ebecbc 100644 --- a/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c +++ b/project/jni/sdl-1.2/src/video/android/SDL_androidinput.c @@ -83,7 +83,8 @@ static int clickMouseWithDpadCenter = 0; int SDL_ANDROID_moveMouseWithArrowKeys = 0; int SDL_ANDROID_moveMouseWithKbSpeed = 0; int SDL_ANDROID_moveMouseWithKbAccel = 0; -int SDL_ANDROID_moveMouseWithKbX = -1, SDL_ANDROID_moveMouseWithKbY = -1; +int SDL_ANDROID_moveMouseWithKbActive = 0; +int SDL_ANDROID_moveMouseWithKbX = 0, SDL_ANDROID_moveMouseWithKbY = 0; int SDL_ANDROID_moveMouseWithKbSpeedX = 0, SDL_ANDROID_moveMouseWithKbSpeedY = 0; int SDL_ANDROID_moveMouseWithKbAccelX = 0, SDL_ANDROID_moveMouseWithKbAccelY = 0; int SDL_ANDROID_moveMouseWithKbAccelUpdateNeeded = 0; @@ -543,13 +544,14 @@ static void ProcessMouseUp( int x, int y ) if( SDL_ANDROID_ShowScreenUnderFinger == ZOOM_MAGNIFIER ) { // Move mouse by 1 pixel so it will force screen update and mouse-under-finger window will be removed - if( SDL_ANDROID_moveMouseWithKbX >= 0 ) + if( SDL_ANDROID_moveMouseWithKbActive ) SDL_ANDROID_MainThreadPushMouseMotion(SDL_ANDROID_moveMouseWithKbX > 0 ? SDL_ANDROID_moveMouseWithKbX-1 : 0, SDL_ANDROID_moveMouseWithKbY); else SDL_ANDROID_MainThreadPushMouseMotion(x > 0 ? x-1 : 0, y); } - SDL_ANDROID_moveMouseWithKbX = -1; - SDL_ANDROID_moveMouseWithKbY = -1; + SDL_ANDROID_moveMouseWithKbActive = 0; + SDL_ANDROID_moveMouseWithKbX = 0; + SDL_ANDROID_moveMouseWithKbY = 0; SDL_ANDROID_moveMouseWithKbSpeedX = 0; SDL_ANDROID_moveMouseWithKbSpeedY = 0; if( !deferredMouseTap ) @@ -562,7 +564,7 @@ static void ProcessMouseUp( int x, int y ) static int ProcessMouseDown( int x, int y ) { int action = MOUSE_DOWN; - if( (SDL_ANDROID_moveMouseWithKbX >= 0 || leftClickMethod == LEFT_CLICK_NEAR_CURSOR) && + if( (SDL_ANDROID_moveMouseWithKbActive || leftClickMethod == LEFT_CLICK_NEAR_CURSOR) && abs(SDL_ANDROID_currentMouseX - x) < SDL_ANDROID_sFakeWindowWidth / 10 && abs(SDL_ANDROID_currentMouseY - y) < SDL_ANDROID_sFakeWindowHeight / 10 ) { SDL_ANDROID_MainThreadPushMouseButton( SDL_PRESSED, SDL_BUTTON_LEFT ); @@ -637,7 +639,7 @@ static void ProcessMouseMove_Timeouts( int x, int y ) static void ProcessMouseMove( int x, int y, int force, int radius ) { - if( SDL_ANDROID_moveMouseWithKbX >= 0 ) + if( SDL_ANDROID_moveMouseWithKbActive ) { // Mouse lazily follows magnifying glass, not very intuitive for drag&drop /* @@ -657,8 +659,9 @@ static void ProcessMouseMove( int x, int y, int force, int radius ) if( abs(SDL_ANDROID_moveMouseWithKbX - x) >= SDL_ANDROID_sFakeWindowWidth / 10 || abs(SDL_ANDROID_moveMouseWithKbY - y) >= SDL_ANDROID_sFakeWindowHeight / 10 ) // || SDL_GetTicks() - leftButtonDownTime > 600 { - SDL_ANDROID_moveMouseWithKbX = -1; - SDL_ANDROID_moveMouseWithKbY = -1; + SDL_ANDROID_moveMouseWithKbActive = 0; + SDL_ANDROID_moveMouseWithKbX = 0; + SDL_ANDROID_moveMouseWithKbY = 0; SDL_ANDROID_moveMouseWithKbSpeedX = 0; SDL_ANDROID_moveMouseWithKbSpeedY = 0; SDL_ANDROID_MainThreadPushMouseMotion(x, y); @@ -794,7 +797,7 @@ static void ProcessMouseHover( jint *xx, jint *yy, int action, int distance ) static void AdjustMouseWithGyroscope( jint *xx, jint *yy ) { if( !moveMouseWithGyroscope || relativeMovement || - SDL_ANDROID_moveMouseWithKbX >= 0 || hardwareMouseDetected == MOUSE_HW_INPUT_MOUSE ) + SDL_ANDROID_moveMouseWithKbActive || hardwareMouseDetected == MOUSE_HW_INPUT_MOUSE ) return; static int oldX = 0, oldY = 0, count = 0; @@ -907,10 +910,18 @@ static void ProcessMoveMouseWithGyroscope(float gx, float gy, float gz) // TODO: mutex here? // If race condition happens, mouse will jump at random across the screen. Nothing serious. - if( SDL_ANDROID_moveMouseWithKbX >= 0 ) + if( SDL_ANDROID_moveMouseWithKbActive ) { SDL_ANDROID_moveMouseWithKbX += gx; SDL_ANDROID_moveMouseWithKbY += gy; + if (SDL_ANDROID_moveMouseWithKbX < 0) + SDL_ANDROID_moveMouseWithKbX = 0; + if (SDL_ANDROID_moveMouseWithKbY < 0) + SDL_ANDROID_moveMouseWithKbY = 0; + if (SDL_ANDROID_moveMouseWithKbX >= SDL_ANDROID_sFakeWindowWidth) + SDL_ANDROID_moveMouseWithKbX = SDL_ANDROID_sFakeWindowWidth - 1; + if (SDL_ANDROID_moveMouseWithKbY >= SDL_ANDROID_sFakeWindowHeight) + SDL_ANDROID_moveMouseWithKbY = SDL_ANDROID_sFakeWindowHeight - 1; SDL_ANDROID_MainThreadPushMouseMotion(SDL_ANDROID_moveMouseWithKbX, SDL_ANDROID_moveMouseWithKbY); return; } @@ -1367,7 +1378,7 @@ JAVA_EXPORT_NAME(Settings_nativeSetAccelerometerSettings) ( JNIEnv* env, jobjec JNIEXPORT void JNICALL JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeGamepadAnalogJoystickInput) (JNIEnv* env, jobject thiz, - jfloat stick1x, jfloat stick1y, jfloat stick2x, jfloat stick2y, jfloat rtrigger, jfloat ltrigger, + jfloat stick1x, jfloat stick1y, jfloat stick2x, jfloat stick2y, jfloat ltrigger, jfloat rtrigger, jint usingHat) { if( SDL_ANDROID_CurrentJoysticks[JOY_GAMEPAD1] ) @@ -1379,48 +1390,62 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeGamepadAnalogJoystickInput) (JNIEnv* en SDL_ANDROID_MainThreadPushJoystickAxis(JOY_GAMEPAD1, 4, NORMALIZE_FLOAT_32767(ltrigger)); SDL_ANDROID_MainThreadPushJoystickAxis(JOY_GAMEPAD1, 5, NORMALIZE_FLOAT_32767(rtrigger)); } - else if( !usingHat ) + else { - // Translate to up/down/left/right - if( stick1x < -0.5f ) + if( !usingHat ) { - if( !SDL_GetKeyboardState(NULL)[SDL_KEY(LEFT)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(LEFT), 0 ); + // Translate to up/down/left/right + if( stick1x < -0.5f ) + { + if( !SDL_GetKeyboardState(NULL)[SDL_KEY(LEFT)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(LEFT), 0 ); + } + else + { + if( SDL_GetKeyboardState(NULL)[SDL_KEY(LEFT)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(LEFT), 0 ); + } + if( stick1x > 0.5f ) + { + if( !SDL_GetKeyboardState(NULL)[SDL_KEY(RIGHT)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(RIGHT), 0 ); + } + else + { + if( SDL_GetKeyboardState(NULL)[SDL_KEY(RIGHT)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(RIGHT), 0 ); + } + if( stick1y < -0.5f ) + { + if( !SDL_GetKeyboardState(NULL)[SDL_KEY(UP)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(UP), 0 ); + } + else + { + if( SDL_GetKeyboardState(NULL)[SDL_KEY(UP)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(UP), 0 ); + } + if( stick1y > 0.5f ) + { + if( !SDL_GetKeyboardState(NULL)[SDL_KEY(DOWN)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(DOWN), 0 ); + } + else + { + if( SDL_GetKeyboardState(NULL)[SDL_KEY(DOWN)] ) + SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(DOWN), 0 ); + } + } + if( fabsf(stick2x) > 0.2 || fabsf(stick2y) > 0.2 ) + { + // Move mouse with right stick + SDL_ANDROID_moveMouseWithKbAccelUpdateNeeded |= 4; + SDL_ANDROID_moveMouseWithKbSpeedX = stick2x * 3 * SDL_ANDROID_moveMouseWithKbSpeed; + SDL_ANDROID_moveMouseWithKbSpeedY = stick2y * 3 * SDL_ANDROID_moveMouseWithKbSpeed; } else { - if( SDL_GetKeyboardState(NULL)[SDL_KEY(LEFT)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(LEFT), 0 ); - } - if( stick1x > 0.5f ) - { - if( !SDL_GetKeyboardState(NULL)[SDL_KEY(RIGHT)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(RIGHT), 0 ); - } - else - { - if( SDL_GetKeyboardState(NULL)[SDL_KEY(RIGHT)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(RIGHT), 0 ); - } - if( stick1y < -0.5f ) - { - if( !SDL_GetKeyboardState(NULL)[SDL_KEY(UP)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(UP), 0 ); - } - else - { - if( SDL_GetKeyboardState(NULL)[SDL_KEY(UP)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(UP), 0 ); - } - if( stick1y > 0.5f ) - { - if( !SDL_GetKeyboardState(NULL)[SDL_KEY(DOWN)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_PRESSED, SDL_KEY(DOWN), 0 ); - } - else - { - if( SDL_GetKeyboardState(NULL)[SDL_KEY(DOWN)] ) - SDL_ANDROID_MainThreadPushKeyboardKey( SDL_RELEASED, SDL_KEY(DOWN), 0 ); + SDL_ANDROID_moveMouseWithKbAccelUpdateNeeded &= ~4; } } } @@ -1518,6 +1543,16 @@ void SDL_ANDROID_processMoveMouseWithKeyboard() SDL_ANDROID_moveMouseWithKbX += SDL_ANDROID_moveMouseWithKbSpeedX; SDL_ANDROID_moveMouseWithKbY += SDL_ANDROID_moveMouseWithKbSpeedY; + + if (SDL_ANDROID_moveMouseWithKbX < 0) + SDL_ANDROID_moveMouseWithKbX = 0; + if (SDL_ANDROID_moveMouseWithKbY < 0) + SDL_ANDROID_moveMouseWithKbY = 0; + if (SDL_ANDROID_moveMouseWithKbX >= SDL_ANDROID_sFakeWindowWidth) + SDL_ANDROID_moveMouseWithKbX = SDL_ANDROID_sFakeWindowWidth - 1; + if (SDL_ANDROID_moveMouseWithKbY >= SDL_ANDROID_sFakeWindowHeight) + SDL_ANDROID_moveMouseWithKbY = SDL_ANDROID_sFakeWindowHeight - 1; + SDL_ANDROID_MainThreadPushMouseMotion(SDL_ANDROID_moveMouseWithKbX, SDL_ANDROID_moveMouseWithKbY); }; diff --git a/project/jni/sdl-1.2/src/video/android/SDL_androidinput.h b/project/jni/sdl-1.2/src/video/android/SDL_androidinput.h index 494fe6ae6..fbda36895 100644 --- a/project/jni/sdl-1.2/src/video/android/SDL_androidinput.h +++ b/project/jni/sdl-1.2/src/video/android/SDL_androidinput.h @@ -266,6 +266,7 @@ extern int SDL_ANDROID_currentMouseX, SDL_ANDROID_currentMouseY, SDL_ANDROID_cur extern int SDL_ANDROID_moveMouseWithArrowKeys; extern int SDL_ANDROID_moveMouseWithKbSpeed; extern int SDL_ANDROID_moveMouseWithKbAccel; +extern int SDL_ANDROID_moveMouseWithKbActive; extern int SDL_ANDROID_moveMouseWithKbX, SDL_ANDROID_moveMouseWithKbY; extern int SDL_ANDROID_moveMouseWithKbSpeedX, SDL_ANDROID_moveMouseWithKbSpeedY; extern int SDL_ANDROID_moveMouseWithKbAccelX, SDL_ANDROID_moveMouseWithKbAccelY;