From 0cb701bb2483d31f0318992b7bf73718585c4d96 Mon Sep 17 00:00:00 2001 From: pelya Date: Mon, 7 Feb 2011 17:49:14 +0000 Subject: [PATCH] Implemented clicking with tap and timeout --- project/java/Settings.java | 32 ++++- project/java/Video.java | 26 ++--- .../ballfield/AndroidAppSettings.cfg | 8 +- .../jni/application/ballfield/ballfield.cpp | 27 ++++- project/jni/application/ballfield/test.cpp | 18 --- project/jni/application/ballfield/test.h | 15 --- project/jni/application/ballfield/test2.cpp | 7 -- .../fheroes2/AndroidAppSettings.cfg | 4 +- project/jni/application/src | 2 +- .../src/video/android/SDL_androidinput.c | 109 ++++++++++++++++-- .../src/video/android/SDL_androidvideo.c | 2 +- .../src/video/android/SDL_androidvideo.h | 2 +- 12 files changed, 173 insertions(+), 79 deletions(-) delete mode 100644 project/jni/application/ballfield/test.cpp delete mode 100644 project/jni/application/ballfield/test.h delete mode 100644 project/jni/application/ballfield/test2.cpp diff --git a/project/java/Settings.java b/project/java/Settings.java index 7633ce9ae..87bd51388 100644 --- a/project/java/Settings.java +++ b/project/java/Settings.java @@ -1208,6 +1208,36 @@ class Settings alert.show(); } + static void showRelativeMouseMovementConfig(final MainActivity p) + { + final CharSequence[] items = { p.getResources().getString(R.string.accel_fast), + p.getResources().getString(R.string.accel_medium), + p.getResources().getString(R.string.accel_slow) }; + + AlertDialog.Builder builder = new AlertDialog.Builder(p); + builder.setTitle(R.string.pointandclick_relative_speed); + builder.setSingleChoiceItems(items, Globals.RelativeMouseMovementSpeed, new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int item) + { + Globals.RelativeMouseMovementSpeed = item; + + dialog.dismiss(); + showRelativeMouseMovementConfig1(p); + } + }); + builder.setOnCancelListener(new DialogInterface.OnCancelListener() + { + public void onCancel(DialogInterface dialog) + { + showConfigMainMenu(p); + } + }); + AlertDialog alert = builder.create(); + alert.setOwnerActivity(p); + alert.show(); + } + static void showRelativeMouseMovementConfig1(final MainActivity p) { final CharSequence[] items = { p.getResources().getString(R.string.accel_fast), @@ -1997,7 +2027,7 @@ class Settings Globals.RightClickKey, Globals.LeftClickTimeout, Globals.RightClickTimeout, - Globals.RelativeMouseMovement, + Globals.RelativeMouseMovement ? 1 : 0, Globals.RelativeMouseMovementSpeed, Globals.RelativeMouseMovementAccel ); if( Globals.AppUsesJoystick && (Globals.UseAccelerometerAsArrowKeys || Globals.UseTouchscreenKeyboard) ) diff --git a/project/java/Video.java b/project/java/Video.java index 1e64a9f6f..39eeb00a6 100644 --- a/project/java/Video.java +++ b/project/java/Video.java @@ -219,7 +219,9 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer { public int swapBuffers() // Called from native code { - this.notify(); + synchronized(this) { + this.notify(); + } if( ! super.SwapBuffers() && Globals.NonBlockingSwapBuffers ) return 0; if(mGlContextLost) { @@ -227,19 +229,6 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer { Settings.SetupTouchscreenKeyboardGraphics(context); // Reload on-screen buttons graphics } - /* - // Pass just one char per frame, many SDL games cannot handle multiple events in a single frame - synchronized(context.textInput) { - if( context.textInput.size() >= 2 ) - { - if( context.textInput.getFirst() != 0 ) - nativeTextInput( context.textInput.getFirst(), context.textInput.get(1) ); - context.textInput.removeFirst(); - context.textInput.removeFirst(); - } - } - */ - return 1; } @@ -256,7 +245,6 @@ class DemoRenderer extends GLSurfaceView_SDL.Renderer { Callback cb = new Callback(); cb.parent = context; context.runOnUiThread(cb); - //context.showScreenKeyboard(); } public void exitApp() { @@ -300,9 +288,11 @@ class DemoGLSurfaceView extends GLSurfaceView_SDL { touchInput.process(event); // Wait a bit, and try to synchronize to app framerate, or event thread will eat all CPU and we'll lose FPS if( event.getAction() == MotionEvent.ACTION_MOVE ) { - try { - mRenderer.wait(300L); - } catch (InterruptedException e) { } + synchronized(mRenderer) { + try { + mRenderer.wait(300L); + } catch (InterruptedException e) { } + } } return true; }; diff --git a/project/jni/application/ballfield/AndroidAppSettings.cfg b/project/jni/application/ballfield/AndroidAppSettings.cfg index b9a1f7f37..5a2c3aa1c 100644 --- a/project/jni/application/ballfield/AndroidAppSettings.cfg +++ b/project/jni/application/ballfield/AndroidAppSettings.cfg @@ -11,15 +11,15 @@ SdlVideoResizeKeepAspect=n NeedDepthBuffer=n AppUsesMouse=y AppNeedsTwoButtonMouse=y -AppNeedsArrowKeys=y -AppNeedsTextInput=y +AppNeedsArrowKeys=n +AppNeedsTextInput=n AppUsesJoystick=n AppHandlesJoystickSensitivity=n AppUsesMultitouch=n NonBlockingSwapBuffers=n RedefinedKeys="SPACE" -AppTouchscreenKeyboardKeysAmount=6 -AppTouchscreenKeyboardKeysAmountAutoFire=2 +AppTouchscreenKeyboardKeysAmount=0 +AppTouchscreenKeyboardKeysAmountAutoFire=0 RedefinedKeysScreenKb="1 2 3 4 5 6 1 2 3 4" MultiABI=n AppVersionCode=101 diff --git a/project/jni/application/ballfield/ballfield.cpp b/project/jni/application/ballfield/ballfield.cpp index 6b5840e3b..882a42dc8 100644 --- a/project/jni/application/ballfield/ballfield.cpp +++ b/project/jni/application/ballfield/ballfield.cpp @@ -16,7 +16,6 @@ #include "SDL.h" #include "SDL_image.h" -#include "test.h" #include "ballfield.h" @@ -361,8 +360,6 @@ int main(int argc, char* argv[]) int fps_start = 0; float x_speed, y_speed, z_speed; - __android_log_print(ANDROID_LOG_INFO, "==TEST==", "SDL_Main: test::initCount %d test::initCount2", test::initCount, test::initCount2); - SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); @@ -384,7 +381,7 @@ int main(int argc, char* argv[]) bpp = atoi(&argv[i][1]); } - screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, flags); + screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, bpp, 0 /*flags*/); if(!screen) { fprintf(stderr, "Failed to open screen!\n"); @@ -494,6 +491,28 @@ int main(int argc, char* argv[]) print_num(screen, font, screen->w-37, screen->h-12, fps); ++fps_count; + int mouseX, mouseY; + int mouseB = SDL_GetMouseState(&mouseX, &mouseY); + r.x = mouseX; + r.y = mouseY; + r.w = 10; + r.h = 1; + SDL_FillRect(screen, &r, 0xeeeeeeee); + + if( mouseB & SDL_BUTTON_LMASK ) + { + r.w = 20; + r.h = 5; + SDL_FillRect(screen, &r, 0xabcdaabb); + } + + if( mouseB & SDL_BUTTON_RMASK ) + { + r.w = 5; + r.h = 20; + SDL_FillRect(screen, &r, 0x67895566); + } + SDL_Flip(screen); /* Animate */ diff --git a/project/jni/application/ballfield/test.cpp b/project/jni/application/ballfield/test.cpp deleted file mode 100644 index accdef05a..000000000 --- a/project/jni/application/ballfield/test.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include "test.h" - -int test::initCount = 0; -int test::initCount2 = 12345; - -test::test() -{ - initCount++; - __android_log_print(ANDROID_LOG_INFO, "==TEST==", "test::test(): initCount %d initCount2 %d", initCount, initCount2); -} - -test::~test() -{ - initCount--; - __android_log_print(ANDROID_LOG_INFO, "==TEST==", "test::~test(): initCount %d initCount2 %d", initCount, initCount2); -} - diff --git a/project/jni/application/ballfield/test.h b/project/jni/application/ballfield/test.h deleted file mode 100644 index 8bb51f646..000000000 --- a/project/jni/application/ballfield/test.h +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include - -class test -{ - public: - test(); - ~test(); - - public: - static int initCount; - static int initCount2; -}; - - diff --git a/project/jni/application/ballfield/test2.cpp b/project/jni/application/ballfield/test2.cpp deleted file mode 100644 index 1ee3e8fba..000000000 --- a/project/jni/application/ballfield/test2.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include "test.h" - - -test t; - - diff --git a/project/jni/application/fheroes2/AndroidAppSettings.cfg b/project/jni/application/fheroes2/AndroidAppSettings.cfg index 777262774..2bc49947d 100644 --- a/project/jni/application/fheroes2/AndroidAppSettings.cfg +++ b/project/jni/application/fheroes2/AndroidAppSettings.cfg @@ -22,8 +22,8 @@ AppTouchscreenKeyboardKeysAmount=0 AppTouchscreenKeyboardKeysAmountAutoFire=0 RedefinedKeysScreenKb="LCTRL M T H E C SPACE C S L" MultiABI=n -AppVersionCode=224411 -AppVersionName="2244.11" +AppVersionCode=224812 +AppVersionName="2248.12" CompiledLibraries="sdl_net sdl_mixer sdl_image sdl_ttf png intl" CustomBuildScript=n AppCflags='-finline-functions -O2 -DWITH_ZLIB -DWITH_MIXER -DWITH_XML -DWITH_IMAGE -DWITH_TTF -DWITH_AI=simple' diff --git a/project/jni/application/src b/project/jni/application/src index 92362c4db..104f796a6 120000 --- a/project/jni/application/src +++ b/project/jni/application/src @@ -1 +1 @@ -ufoai \ No newline at end of file +ballfield \ No newline at end of file diff --git a/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c b/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c index 6556b236f..b7ae52fe2 100644 --- a/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c +++ b/project/jni/sdl-1.3/src/video/android/SDL_androidinput.c @@ -101,6 +101,15 @@ int SDL_ANDROID_TouchscreenCalibrationWidth = 480; int SDL_ANDROID_TouchscreenCalibrationHeight = 320; int SDL_ANDROID_TouchscreenCalibrationX = 0; int SDL_ANDROID_TouchscreenCalibrationY = 0; +int leftClickTimeout = 0; +int rightClickTimeout = 0; +int relativeMovement = 0; +int relativeMovementSpeed = 0; +int relativeMovementAccel = 0; +int mouseInitialX = -1; +int mouseInitialY = -1; +unsigned int mouseInitialTime = 0; +int deferredMouseTap = 0; static inline int InsideRect(const SDL_Rect * r, int x, int y) { @@ -356,10 +365,26 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouse) ( JNIEnv* env, jobject thiz, j SDL_GetMouseState( &oldX, &oldY ); if( action == MOUSE_UP ) { - if( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON(SDL_BUTTON_LEFT) ) - SDL_ANDROID_MainThreadPushMouseButton( SDL_RELEASED, SDL_BUTTON_LEFT ); - if( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON(SDL_BUTTON_RIGHT) ) - SDL_ANDROID_MainThreadPushMouseButton( SDL_RELEASED, SDL_BUTTON_RIGHT ); + if( mouseInitialX >= 0 && mouseInitialY >= 0 && ( + leftClickMethod == LEFT_CLICK_WITH_TAP || leftClickMethod == LEFT_CLICK_WITH_TAP_OR_TIMEOUT ) && + abs(mouseInitialX - x) < SDL_ANDROID_sFakeWindowHeight / 6 && + abs(mouseInitialY - y) < SDL_ANDROID_sFakeWindowHeight / 6 && + SDL_GetTicks() - mouseInitialTime < 300 ) + { + SDL_ANDROID_MainThreadPushMouseMotion(mouseInitialX, mouseInitialY); + SDL_ANDROID_MainThreadPushMouseButton( SDL_PRESSED, SDL_BUTTON_LEFT ); + deferredMouseTap = 2; + mouseInitialX = -1; + mouseInitialY = -1; + } + else + { + if( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON(SDL_BUTTON_LEFT) ) + SDL_ANDROID_MainThreadPushMouseButton( SDL_RELEASED, SDL_BUTTON_LEFT ); + if( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON(SDL_BUTTON_RIGHT) ) + SDL_ANDROID_MainThreadPushMouseButton( SDL_RELEASED, SDL_BUTTON_RIGHT ); + } + SDL_ANDROID_ShowScreenUnderFingerRect.w = SDL_ANDROID_ShowScreenUnderFingerRect.h = 0; SDL_ANDROID_ShowScreenUnderFingerRectSrc.w = SDL_ANDROID_ShowScreenUnderFingerRectSrc.h = 0; if( SDL_ANDROID_ShowScreenUnderFinger ) @@ -395,6 +420,9 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouse) ( JNIEnv* env, jobject thiz, j { SDL_ANDROID_MainThreadPushMouseMotion(x, y); action == MOUSE_MOVE; + mouseInitialX = x; + mouseInitialY = y; + mouseInitialTime = SDL_GetTicks(); } UpdateScreenUnderFingerRect(x, y); } @@ -438,6 +466,39 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouse) ( JNIEnv* env, jobject thiz, j if( ( (SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON(button)) != 0 ) != buttonState ) SDL_ANDROID_MainThreadPushMouseButton( buttonState ? SDL_PRESSED : SDL_RELEASED, button ); } + if( mouseInitialX >= 0 && mouseInitialY >= 0 && ( + leftClickMethod == LEFT_CLICK_WITH_TIMEOUT || leftClickMethod == LEFT_CLICK_WITH_TAP || + leftClickMethod == LEFT_CLICK_WITH_TAP_OR_TIMEOUT || rightClickMethod == RIGHT_CLICK_WITH_TIMEOUT ) ) + { + if( abs(mouseInitialX - x) >= SDL_ANDROID_sFakeWindowHeight / 6 || abs(mouseInitialY - y) >= SDL_ANDROID_sFakeWindowHeight / 6 ) + { + mouseInitialX = -1; + mouseInitialY = -1; + } + else + { + if( leftClickMethod == LEFT_CLICK_WITH_TIMEOUT || leftClickMethod == LEFT_CLICK_WITH_TAP_OR_TIMEOUT ) + { + if( SDL_GetTicks() - mouseInitialTime > leftClickTimeout ) + { + SDL_ANDROID_MainThreadPushMouseMotion(mouseInitialX, mouseInitialY); + SDL_ANDROID_MainThreadPushMouseButton( SDL_PRESSED, SDL_BUTTON_LEFT ); + mouseInitialX = -1; + mouseInitialY = -1; + } + } + if( rightClickMethod == RIGHT_CLICK_WITH_TIMEOUT ) + { + if( SDL_GetTicks() - mouseInitialTime > rightClickTimeout ) + { + SDL_ANDROID_MainThreadPushMouseMotion(mouseInitialX, mouseInitialY); + SDL_ANDROID_MainThreadPushMouseButton( SDL_PRESSED, SDL_BUTTON_RIGHT ); + mouseInitialX = -1; + mouseInitialY = -1; + } + } + } + } UpdateScreenUnderFingerRect(x, y); } } @@ -470,6 +531,16 @@ JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouse) ( JNIEnv* env, jobject thiz, j } } +void ProcessDeferredMouseTap() +{ + if( deferredMouseTap > 0 ) + { + deferredMouseTap--; + if( !deferredMouseTap ) + SDL_ANDROID_MainThreadPushMouseButton( SDL_RELEASED, SDL_BUTTON_LEFT ); + } +} + static int processAndroidTrackball(int key, int action); JNIEXPORT void JNICALL @@ -544,14 +615,27 @@ JAVA_EXPORT_NAME(Settings_nativeSetTrackballUsed) ( JNIEnv* env, jobject thiz) isTrackballUsed = 1; } -JNIEXPORT void JNICALL +static int getClickTimeout(int v) +{ + switch(v) + { + case 0: return 300; + case 1: return 500; + case 2: return 700; + case 3: return 1000; + case 4: return 1500; + } + return 1000; +} + +JNIEXPORT void JNICALL JAVA_EXPORT_NAME(Settings_nativeSetMouseUsed) ( JNIEnv* env, jobject thiz, jint RightClickMethod, jint ShowScreenUnderFinger, jint LeftClickMethod, jint MoveMouseWithJoystick, jint ClickMouseWithDpad, jint MaxForce, jint MaxRadius, jint MoveMouseWithJoystickSpeed, jint MoveMouseWithJoystickAccel, jint LeftClickKeycode, jint RightClickKeycode, - jint LeftClickTimeout, jint RghtClickTimeout, + jint LeftClickTimeout, jint RightClickTimeout, jint RelativeMovement, jint RelativeMovementSpeed, jint RelativeMovementAccel) { isMouseUsed = 1; @@ -566,6 +650,11 @@ JAVA_EXPORT_NAME(Settings_nativeSetMouseUsed) ( JNIEnv* env, jobject thiz, moveMouseWithKbAccel = MoveMouseWithJoystickAccel; leftClickKey = LeftClickKeycode; rightClickKey = RightClickKeycode; + leftClickTimeout = getClickTimeout(LeftClickTimeout); + rightClickTimeout = getClickTimeout(RightClickTimeout); + relativeMovement = RelativeMovement; + relativeMovementSpeed = RelativeMovementSpeed; + relativeMovementAccel = RelativeMovementAccel; } JNIEXPORT void JNICALL @@ -1272,7 +1361,7 @@ static int deferredTextIdx1 = 0; static int deferredTextIdx2 = 0; static SDL_mutex * deferredTextMutex = NULL; -extern void SDL_ANDROID_DeferredTextInput() +void SDL_ANDROID_DeferredTextInput() { int count = 2; if( !deferredTextMutex ) @@ -1397,6 +1486,12 @@ void SDL_ANDROID_processMoveMouseWithKeyboard() SDL_ANDROID_MainThreadPushMouseMotion(moveMouseWithKbX, moveMouseWithKbY); }; +extern void SDL_ANDROID_ProcessDeferredEvents() +{ + SDL_ANDROID_DeferredTextInput(); + ProcessDeferredMouseTap(); +}; + void ANDROID_InitOSKeymap() { #if (SDL_VERSION_ATLEAST(1,3,0)) diff --git a/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.c b/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.c index fd27392ce..46509a37b 100644 --- a/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.c +++ b/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.c @@ -118,7 +118,7 @@ int SDL_ANDROID_CallJavaSwapBuffers() showScreenKeyboardDeferred = 0; (*JavaEnv)->CallVoidMethod( JavaEnv, JavaRenderer, JavaShowScreenKeyboard ); } - SDL_ANDROID_DeferredTextInput(); + SDL_ANDROID_ProcessDeferredEvents(); return 1; } diff --git a/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.h b/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.h index d68f22379..f153f6b44 100644 --- a/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.h +++ b/project/jni/sdl-1.3/src/video/android/SDL_androidvideo.h @@ -52,7 +52,7 @@ extern void SDL_ANDROID_processAndroidTrackballDampening(); extern void SDL_ANDROID_processMoveMouseWithKeyboard(); extern int SDL_ANDROID_InsideVideoThread(); extern SDL_VideoDevice *ANDROID_CreateDevice_1_3(int devindex); -extern void SDL_ANDROID_DeferredTextInput(); +extern void SDL_ANDROID_ProcessDeferredEvents(); extern void SDL_ANDROID_initFakeStdout(); #if SDL_VERSION_ATLEAST(1,3,0)