From 0950723da0b9a1a1a4b775f9b6a1350634af5fc7 Mon Sep 17 00:00:00 2001 From: pelya Date: Thu, 29 Jul 2010 14:49:16 +0300 Subject: [PATCH] More settings for application in ChangeAppSettings.sh --- alienblaster/AppSettings.cfg | 5 +- alienblaster/ChangeAppSettings.sh | 40 ++ .../application/alienblaster/surfaceDB.cpp | 14 +- alienblaster/project/sdl/sdl-1.3/Android.mk | 1 - .../sdl/sdl-1.3/include/SDL_config_android.h | 1 - .../src/video/android/SDL_androidinput.c | 569 ++++++++++++------ alienblaster/project/src/Accelerometer.java | 60 +- alienblaster/project/src/Globals.java | 9 +- alienblaster/project/src/Settings.java | 60 +- 9 files changed, 524 insertions(+), 235 deletions(-) diff --git a/alienblaster/AppSettings.cfg b/alienblaster/AppSettings.cfg index b72877731..aaf6275dc 100644 --- a/alienblaster/AppSettings.cfg +++ b/alienblaster/AppSettings.cfg @@ -5,9 +5,12 @@ ScreenOrientation=h AppDataDownloadUrl="http://sites.google.com/site/xpelyax/Home/alienblaster110_data.zip?attredirects=0&d=1|http://sitesproxy.goapk.com/site/xpelyax/Home/alienblaster110_data.zip" SdlVideoResize=y NeedDepthBuffer=n +AppUsesMouse=n +AppNeedsArrowKeys=y +AppUsesJoystick=n MultiABI=n AppVersionCode=110003 AppVersionName="1.1.0.p3" CompiledLibraries="sdl_mixer sdl_image" AppCflags='-finline-functions -O2' -ReadmeText='^Use accelerometer to navigate menus and control ship^Press "Menu" to select menu and for secondary fire^Press "Call" or touch screen for primary fire^Press "Volume Up/Down" to cycle through weapons' +ReadmeText='^You can press "Home" now - the data will be downloaded in background^In game press "Menu" for secondary fire, press "Volume Up/Down" to cycle through weapons' diff --git a/alienblaster/ChangeAppSettings.sh b/alienblaster/ChangeAppSettings.sh index f0c2b981d..882b252eb 100755 --- a/alienblaster/ChangeAppSettings.sh +++ b/alienblaster/ChangeAppSettings.sh @@ -48,6 +48,24 @@ if [ -n "$var" ] ; then NeedDepthBuffer="$var" fi +echo -n "\nApplication uses mouse (y) or (n), if (n) the screen touch will be mapped to Enter ($AppUsesMouse): " +read var +if [ -n "$var" ] ; then + AppUsesMouse="$var" +fi + +echo -n "\nApplication needs arrow keys (y) or (n), if (y) the accelerometer will be used\nas arrow keys if phone does not have dpad/trackball ($AppNeedsArrowKeys): " +read var +if [ -n "$var" ] ; then + AppNeedsArrowKeys="$var" +fi + +echo -n "\nApplication uses joystick (y) or (n), the accelerometer (2-axis) or orientation sensor (3-axis)\nwill be used as joystick if not used as arrow keys ($AppUsesJoystick): " +read var +if [ -n "$var" ] ; then + AppUsesJoystick="$var" +fi + echo -n "\nEnable multi-ABI binary, with hardware FPU support - \nit will also work on old devices, but .apk size is 2x bigger (y) or (n) ($MultiABI): " read var if [ -n "$var" ] ; then @@ -110,6 +128,9 @@ echo ScreenOrientation=$ScreenOrientation >> AppSettings.cfg echo AppDataDownloadUrl=\"$AppDataDownloadUrl\" >> AppSettings.cfg echo SdlVideoResize=$SdlVideoResize >> AppSettings.cfg echo NeedDepthBuffer=$NeedDepthBuffer >> AppSettings.cfg +echo AppUsesMouse=$AppUsesMouse >> AppSettings.cfg +echo AppNeedsArrowKeys=$AppNeedsArrowKeys >> AppSettings.cfg +echo AppUsesJoystick=$AppUsesJoystick >> AppSettings.cfg echo MultiABI=$MultiABI >> AppSettings.cfg echo AppVersionCode=$AppVersionCode >> AppSettings.cfg echo AppVersionName=\"$AppVersionName\" >> AppSettings.cfg @@ -138,6 +159,22 @@ if [ "$NeedDepthBuffer" = "y" ] ; then else NeedDepthBuffer=false fi +if [ "$AppUsesMouse" = "y" ] ; then + AppUsesMouse=true +else + AppUsesMouse=false +fi +if [ "$AppNeedsArrowKeys" = "y" ] ; then + AppNeedsArrowKeys=true +else + AppNeedsArrowKeys=false +fi +if [ "$AppUsesJoystick" = "y" ] ; then + AppUsesJoystick=true +else + AppUsesJoystick=false +fi + if [ "$MultiABI" = "y" ] ; then MultiABI="armeabi armeabi-v7a" else @@ -179,6 +216,9 @@ cat project/src/Globals.java | \ sed "s^public static String DataDownloadUrl = \".*\";^public static String DataDownloadUrl = \"$AppDataDownloadUrl1\";^" | \ sed "s/public static boolean NeedDepthBuffer = .*;/public static boolean NeedDepthBuffer = $NeedDepthBuffer;/" | \ sed "s/public static boolean HorizontalOrientation = .*;/public static boolean HorizontalOrientation = $HorizontalOrientation;/" | \ + sed "s/public static boolean AppUsesMouse = .*;/public static boolean AppUsesMouse = $AppUsesMouse;/" | \ + sed "s/public static boolean AppNeedsArrowKeys = .*;/public static boolean AppNeedsArrowKeys = $AppNeedsArrowKeys;/" | \ + sed "s/public static boolean AppUsesJoystick = .*;/public static boolean AppUsesJoystick = $AppUsesJoystick;/" | \ sed "s%public static String ReadmeText = .*%public static String ReadmeText = \"$ReadmeText\".replace(\"^\",\"\\\n\");%" | \ sed "s/public LoadLibrary() .*/public LoadLibrary() { $LibrariesToLoad };/" > \ project/src/Globals.java.1 diff --git a/alienblaster/project/jni/application/alienblaster/surfaceDB.cpp b/alienblaster/project/jni/application/alienblaster/surfaceDB.cpp index ed7a09f1d..73d501878 100644 --- a/alienblaster/project/jni/application/alienblaster/surfaceDB.cpp +++ b/alienblaster/project/jni/application/alienblaster/surfaceDB.cpp @@ -82,8 +82,6 @@ SdlCompat_AcceleratedSurface *SurfaceDB::loadSurface( string fn, bool alpha ) { #endif exit(1); } - SDL_SetColorKey( newSurface, SDL_SRCCOLORKEY, - SDL_MapRGB(newSurface->format, transR, transG, transB) ); SDL_Surface * hwSurface = SDL_DisplayFormat(newSurface); @@ -92,12 +90,16 @@ SdlCompat_AcceleratedSurface *SurfaceDB::loadSurface( string fn, bool alpha ) { newSurface = hwSurface; } - if ( alpha ) { - SDL_SetAlpha( newSurface, SDL_SRCALPHA, 128 ); - } - surfaceDB[ fn ] = SdlCompat_CreateAcceleratedSurface( newSurface ); SDL_FreeSurface(newSurface); + + SDL_SetColorKey( surfaceDB[ fn ], SDL_SRCCOLORKEY, + SDL_MapRGB(surfaceDB[ fn ]->format, transR, transG, transB) ); + + if ( alpha ) { + SDL_SetAlpha( surfaceDB[ fn ], SDL_SRCALPHA, 128 ); + } + return surfaceDB[ fn ]; } diff --git a/alienblaster/project/sdl/sdl-1.3/Android.mk b/alienblaster/project/sdl/sdl-1.3/Android.mk index 31c140e65..356012542 100644 --- a/alienblaster/project/sdl/sdl-1.3/Android.mk +++ b/alienblaster/project/sdl/sdl-1.3/Android.mk @@ -35,7 +35,6 @@ SDL_SRCS := \ src/audio/android/*.c \ src/cdrom/dummy/*.c \ src/video/android/*.c \ - src/joystick/dummy/*.c \ src/haptic/dummy/*.c \ src/loadso/dlopen/*.c \ src/atomic/dummy/*.c \ diff --git a/alienblaster/project/sdl/sdl-1.3/include/SDL_config_android.h b/alienblaster/project/sdl/sdl-1.3/include/SDL_config_android.h index b00791c5c..cca05efa6 100644 --- a/alienblaster/project/sdl/sdl-1.3/include/SDL_config_android.h +++ b/alienblaster/project/sdl/sdl-1.3/include/SDL_config_android.h @@ -39,7 +39,6 @@ #define SDL_CDROM_DISABLED 1 -#define SDL_JOYSTICK_DUMMY 1 // TODO: add joystick and remove that #define SDL_JOYSTICK_ANDROID 1 #define SDL_HAPTIC_DUMMY 1 // TODO: add vibrator and remove that diff --git a/alienblaster/project/sdl/sdl-1.3/src/video/android/SDL_androidinput.c b/alienblaster/project/sdl/sdl-1.3/src/video/android/SDL_androidinput.c index 6272cb938..0cc4fe603 100644 --- a/alienblaster/project/sdl/sdl-1.3/src/video/android/SDL_androidinput.c +++ b/alienblaster/project/sdl/sdl-1.3/src/video/android/SDL_androidinput.c @@ -47,6 +47,9 @@ #include "SDL_keysym.h" #include "../../events/SDL_events_c.h" #endif +#include "SDL_joystick.h" +#include "../../joystick/SDL_sysjoystick.h" +#include "../../joystick/SDL_joystick_c.h" #include "SDL_androidvideo.h" @@ -62,48 +65,6 @@ static SDLKey keymap[KEYCODE_LAST+1]; #define JAVA_EXPORT_NAME1(name,package) JAVA_EXPORT_NAME2(name,package) #define JAVA_EXPORT_NAME(name) JAVA_EXPORT_NAME1(name,SDL_JAVA_PACKAGE_PATH) - - -enum MOUSE_ACTION { MOUSE_DOWN = 0, MOUSE_UP=1, MOUSE_MOVE=2 }; - -JNIEXPORT void JNICALL -JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouse) ( JNIEnv* env, jobject thiz, jint x, jint y, jint action ) -{ -#if SDL_VIDEO_RENDER_RESIZE - // Translate mouse coordinates - -#if SDL_VERSION_ATLEAST(1,3,0) - SDL_Renderer *renderer; - renderer = SDL_GetCurrentRenderer(SDL_TRUE); - if( renderer && renderer->window ) { - x = x * renderer->window->w / renderer->window->display->desktop_mode.w; - y = y * renderer->window->h / renderer->window->display->desktop_mode.h; - } -#else - x = x * SDL_ANDROID_sFakeWindowWidth / SDL_ANDROID_sWindowWidth; - y = y * SDL_ANDROID_sFakeWindowHeight / SDL_ANDROID_sWindowHeight; -#endif - -#endif - - if( action == MOUSE_DOWN || action == MOUSE_UP ) - { -#if SDL_VERSION_ATLEAST(1,3,0) - SDL_SendMouseMotion(NULL, 0, x, y); - SDL_SendMouseButton(NULL, (action == MOUSE_DOWN) ? SDL_PRESSED : SDL_RELEASED, 1 ); -#else - SDL_PrivateMouseMotion(0, 0, x, y); - SDL_PrivateMouseButton( (action == MOUSE_DOWN) ? SDL_PRESSED : SDL_RELEASED, 1, x, y ); -#endif - } - if( action == MOUSE_MOVE ) -#if SDL_VERSION_ATLEAST(1,3,0) - SDL_SendMouseMotion(NULL, 0, x, y); -#else - SDL_PrivateMouseMotion(0, 0, x, y); -#endif -} - #if SDL_VERSION_ATLEAST(1,3,0) #define SDL_KEY(X) SDL_SCANCODE_ ## X @@ -189,17 +150,239 @@ static SDL_keysym *TranslateKey(int scancode, SDL_keysym *keysym) #endif +static int isTrackballUsed = 0; +static int isMouseUsed = 0; +static int isJoystickUsed = 0; +static SDL_Joystick *CurrentJoystick = NULL; + +enum MOUSE_ACTION { MOUSE_DOWN = 0, MOUSE_UP=1, MOUSE_MOVE=2 }; + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeMouse) ( JNIEnv* env, jobject thiz, jint x, jint y, jint action ) +{ + if( !isMouseUsed ) + { + SDL_keysym keysym; + if( action != MOUSE_MOVE ) + SDL_SendKeyboardKey( action == MOUSE_DOWN ? SDL_PRESSED : SDL_RELEASED, TranslateKey(KEYCODE_ENTER ,&keysym) ); + } +#if SDL_VIDEO_RENDER_RESIZE + // Translate mouse coordinates + +#if SDL_VERSION_ATLEAST(1,3,0) + SDL_Renderer *renderer; + renderer = SDL_GetCurrentRenderer(SDL_TRUE); + if( renderer && renderer->window ) { + x = x * renderer->window->w / renderer->window->display->desktop_mode.w; + y = y * renderer->window->h / renderer->window->display->desktop_mode.h; + } +#else + x = x * SDL_ANDROID_sFakeWindowWidth / SDL_ANDROID_sWindowWidth; + y = y * SDL_ANDROID_sFakeWindowHeight / SDL_ANDROID_sWindowHeight; +#endif + +#endif + + if( action == MOUSE_DOWN || action == MOUSE_UP ) + { +#if SDL_VERSION_ATLEAST(1,3,0) + SDL_SendMouseMotion(NULL, 0, x, y); + SDL_SendMouseButton(NULL, (action == MOUSE_DOWN) ? SDL_PRESSED : SDL_RELEASED, 1 ); +#else + SDL_PrivateMouseMotion(0, 0, x, y); + SDL_PrivateMouseButton( (action == MOUSE_DOWN) ? SDL_PRESSED : SDL_RELEASED, 1, x, y ); +#endif + } + if( action == MOUSE_MOVE ) +#if SDL_VERSION_ATLEAST(1,3,0) + SDL_SendMouseMotion(NULL, 0, x, y); +#else + SDL_PrivateMouseMotion(0, 0, x, y); +#endif +} + +static int processAndroidTrackball(int key, int action); JNIEXPORT void JNICALL JAVA_EXPORT_NAME(DemoGLSurfaceView_nativeKey) ( JNIEnv* env, jobject thiz, jint key, jint action ) { - //if( ! processAndroidTrackballKeyDelays(key, action) ) + if( isTrackballUsed ) + if( processAndroidTrackball(key, action) ) + return; SDL_keysym keysym; SDL_SendKeyboardKey( action ? SDL_PRESSED : SDL_RELEASED, TranslateKey(key ,&keysym) ); } -static void updateOrientation ( float accX, float accY, float accZ ) +static void updateOrientation ( float accX, float accY, float accZ ); + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobject thiz, jfloat accPosX, jfloat accPosY, jfloat accPosZ ) +{ + // Calculate two angles from three coordinates - TODO: this is faulty! + //float accX = atan2f(-accPosX, sqrtf(accPosY*accPosY+accPosZ*accPosZ) * ( accPosY > 0 ? 1.0f : -1.0f ) ) * M_1_PI * 180.0f; + //float accY = atan2f(accPosZ, accPosY) * M_1_PI; + float normal = sqrt(accPosX*accPosX+accPosY*accPosY+accPosZ*accPosZ); + if(normal <= 0.0000001f) + normal = 1.0f; + + updateOrientation (accPosX/normal, accPosY/normal, 0.0f); +} + + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat accX, jfloat accY, jfloat accZ ) +{ + updateOrientation (accX, accY, accZ); +} + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(Settings_nativeSetTrackballUsed) ( JNIEnv* env, jobject thiz) +{ + isTrackballUsed = 1; +} + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(Settings_nativeSetMouseUsed) ( JNIEnv* env, jobject thiz) +{ + isMouseUsed = 1; +} + +JNIEXPORT void JNICALL +JAVA_EXPORT_NAME(Settings_nativeSetJoystickUsed) ( JNIEnv* env, jobject thiz) +{ + isJoystickUsed = 1; +} + +void ANDROID_InitOSKeymap() +{ + int i; + +#if (SDL_VERSION_ATLEAST(1,3,0)) + SDLKey defaultKeymap[SDL_NUM_SCANCODES]; + SDL_GetDefaultKeymap(defaultKeymap); + SDL_SetKeymap(0, defaultKeymap, SDL_NUM_SCANCODES); +#endif + + // TODO: keys are mapped rather randomly + + for (i=0; i midY + dy*2 ) + midY = accY - dy*2; + if( accZ < midZ - dz*2 ) + midZ = accZ + dz*2; + if( accZ > midZ + dz*2 ) + midZ = accZ - dz*2; + return; + } + + if( accX < midX - dx ) { if( !pressLeft ) @@ -335,157 +537,150 @@ static void updateOrientation ( float accX, float accY, float accZ ) } -JNIEXPORT void JNICALL -JAVA_EXPORT_NAME(AccelerometerReader_nativeAccelerometer) ( JNIEnv* env, jobject thiz, jfloat accPosX, jfloat accPosY, jfloat accPosZ ) +int processAndroidTrackball(int key, int action) { - // Calculate two angles from three coordinates - TODO: this is faulty! - //float accX = atan2f(-accPosX, sqrtf(accPosY*accPosY+accPosZ*accPosZ) * ( accPosY > 0 ? 1.0f : -1.0f ) ) * M_1_PI * 180.0f; - //float accY = atan2f(accPosZ, accPosY) * M_1_PI; - float normal = sqrt(accPosX*accPosX+accPosY*accPosY+accPosZ*accPosZ); - if(normal <= 0.0000001f) - normal = 1.0f; - /* - static int count = 0; - count++; - if(count > 50) + static int leftPressed = 0, rightPressed = 0, upPressed = 0, downPressed = 0; + SDL_keysym keysym; + + if( key == KEYCODE_DPAD_UP ) { - __android_log_print(ANDROID_LOG_INFO, "libSDL", "Accel: %-1.3f %-1.3f %-1.3f", accPosX/normal, accPosY/normal, accPosZ/normal); + downPressed = 0; + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(KEYCODE_DPAD_DOWN ,&keysym) ); + if( !upPressed ) + { + if( action ) + { + upPressed = 1; + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + else + { + if( action ) + { + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(key ,&keysym) ); + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + return 1; } - */ - - updateOrientation (accPosX/normal, accPosY/normal, 0.0f); + + if( key == KEYCODE_DPAD_DOWN ) + { + upPressed = 0; + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(KEYCODE_DPAD_UP ,&keysym) ); + if( !upPressed ) + { + if( action ) + { + downPressed = 1; + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + else + { + if( action ) + { + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(key ,&keysym) ); + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + return 1; + } + + if( key == KEYCODE_DPAD_LEFT ) + { + rightPressed = 0; + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(KEYCODE_DPAD_RIGHT ,&keysym) ); + if( !leftPressed ) + { + if( action ) + { + leftPressed = 1; + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + else + { + if( action ) + { + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(key ,&keysym) ); + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + return 1; + } + + if( key == KEYCODE_DPAD_RIGHT ) + { + leftPressed = 0; + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(KEYCODE_DPAD_LEFT ,&keysym) ); + if( !rightPressed ) + { + if( action ) + { + rightPressed = 1; + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + else + { + if( action ) + { + SDL_SendKeyboardKey( SDL_RELEASED, TranslateKey(key ,&keysym) ); + SDL_SendKeyboardKey( SDL_PRESSED, TranslateKey(key ,&keysym) ); + } + } + return 1; + } + + return 0; } - -JNIEXPORT void JNICALL -JAVA_EXPORT_NAME(AccelerometerReader_nativeOrientation) ( JNIEnv* env, jobject thiz, jfloat accX, jfloat accY, jfloat accZ ) +int SDL_SYS_JoystickInit(void) { - updateOrientation (accX, accY, accZ); + SDL_numjoysticks = 1; + return(0); } -void ANDROID_InitOSKeymap() +/* Function to get the device-dependent name of a joystick */ +const char *SDL_SYS_JoystickName(int index) { - int i; - -#if (SDL_VERSION_ATLEAST(1,3,0)) - SDLKey defaultKeymap[SDL_NUM_SCANCODES]; - SDL_GetDefaultKeymap(defaultKeymap); - SDL_SetKeymap(0, defaultKeymap, SDL_NUM_SCANCODES); -#endif - - // TODO: keys are mapped rather randomly - - for (i=0; inbuttons = 1; // Ignored + joystick->naxes = 3; + CurrentJoystick = joystick; + return(0); +} + +/* Function to update the state of a joystick - called as a device poll. + * This function shouldn't update the joystick structure directly, + * but instead should call SDL_PrivateJoystick*() to deliver events + * and update joystick device state. + */ +void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick) +{ + return; +} + +/* Function to close a joystick after use */ +void SDL_SYS_JoystickClose(SDL_Joystick *joystick) +{ + CurrentJoystick = NULL; + return; +} + +/* Function to perform any system-specific joystick related cleanup */ +void SDL_SYS_JoystickQuit(void) +{ + return; +} diff --git a/alienblaster/project/src/Accelerometer.java b/alienblaster/project/src/Accelerometer.java index 6fa455f5b..f9b6a2fe0 100644 --- a/alienblaster/project/src/Accelerometer.java +++ b/alienblaster/project/src/Accelerometer.java @@ -10,27 +10,33 @@ import android.view.Window; import android.view.WindowManager; import android.os.Vibrator; import android.hardware.SensorManager; -import android.hardware.SensorListener; +import android.hardware.SensorEventListener; +import android.hardware.Sensor; +import android.hardware.SensorEvent; import android.widget.TextView; -// Accelerometer code partially ripped from http://karanar.net/ -class AccelerometerReader implements SensorListener { +class AccelerometerReader implements SensorEventListener { - private float [] v; - private SensorManager _manager = null; public AccelerometerReader(Activity context) { - v = new float[3]; _manager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE); if( _manager != null ) { - int mask = 0; - //mask |= SensorManager.SENSOR_ORIENTATION; - mask |= SensorManager.SENSOR_ACCELEROMETER; - _manager.registerListener(this, mask, SensorManager.SENSOR_DELAY_GAME); + if( Globals.AppNeedsArrowKeys ) + { + _manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); + } + else + { + if( Globals.AppUsesJoystick ) + { + if( ! _manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ORIENTATION), SensorManager.SENSOR_DELAY_GAME) ) + _manager.registerListener(this, _manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_GAME); + } + } } } @@ -41,40 +47,22 @@ class AccelerometerReader implements SensorListener { } } - public synchronized void onSensorChanged(int sensor, float[] values) { + public synchronized void onSensorChanged(SensorEvent event) { - v[0] = values[0]; - v[1] = values[1]; - v[2] = values[2]; - - if (sensor == SensorManager.SENSOR_ACCELEROMETER) { - /* - if( Globals.HorizontalOrientation ) - { - v[0] = values[2]; - v[1] = values[1]; - v[2] = values[0]; - } - */ - nativeAccelerometer(v[0], v[1], v[2]); + if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) + { + nativeAccelerometer(event.values[0], event.values[1], event.values[2]); } - if (sensor == SensorManager.SENSOR_ORIENTATION) { - nativeOrientation(v[0], v[1], v[2]); + else + { + nativeOrientation(event.values[0], event.values[1], event.values[2]); } } - public synchronized void onAccuracyChanged(int i, int i1) { + public synchronized void onAccuracyChanged(Sensor s, int a) { } - public synchronized float[] readAccelerometer() - { - float [] ret = new float[3]; - ret[0] = v[0]; - ret[1] = v[1]; - ret[2] = v[2]; - return ret; - }; private native void nativeAccelerometer(float accX, float accY, float accZ); private native void nativeOrientation(float accX, float accY, float accZ); diff --git a/alienblaster/project/src/Globals.java b/alienblaster/project/src/Globals.java index 2c7166386..f128f6dfa 100644 --- a/alienblaster/project/src/Globals.java +++ b/alienblaster/project/src/Globals.java @@ -23,7 +23,14 @@ class Globals { public static boolean HorizontalOrientation = true; // Readme text to be shown on download page - public static String ReadmeText = "^Use accelerometer to navigate menus and control ship^Press \"Menu\" to select menu and for secondary fire^Press \"Call\" or touch screen for primary fire^Press \"Volume Up/Down\" to cycle through weapons".replace("^","\n"); + public static String ReadmeText = "^You can press \"Home\" now - the data will be downloaded in background^In game press \"Menu\" for secondary fire, press \"Volume Up/Down\" to cycle through weapons".replace("^","\n"); + + public static boolean AppUsesMouse = false; + + // We have to use accelerometer as arrow keys + public static boolean AppNeedsArrowKeys = true; + + public static boolean AppUsesJoystick = false; } class LoadLibrary { diff --git a/alienblaster/project/src/Settings.java b/alienblaster/project/src/Settings.java index dc864596f..c991ba2e4 100644 --- a/alienblaster/project/src/Settings.java +++ b/alienblaster/project/src/Settings.java @@ -13,6 +13,7 @@ import android.util.Log; import java.io.*; import android.app.AlertDialog; import android.content.DialogInterface; +import android.content.res.Configuration; class Settings { @@ -22,6 +23,7 @@ class Settings try { ObjectInputStream settingsFile = new ObjectInputStream(new FileInputStream( p.getFilesDir().getAbsolutePath() + "/" + SettingsFileName )); Globals.DownloadToSdcard = settingsFile.readBoolean(); + Globals.AppNeedsArrowKeys = settingsFile.readBoolean(); startDownloader(p); return; @@ -29,6 +31,17 @@ class Settings } catch( SecurityException e ) { } catch ( IOException e ) {}; + Configuration c = new Configuration(); + c.setToDefaults(); + + if( c.navigation == Configuration.NAVIGATION_TRACKBALL || + c.navigation == Configuration.NAVIGATION_DPAD || + c.navigation == Configuration.NAVIGATION_WHEEL ) + { + Globals.AppNeedsArrowKeys = false; + } + + final CharSequence[] items = {"Phone storage", "SD card"}; AlertDialog.Builder builder = new AlertDialog.Builder(p); @@ -39,9 +52,8 @@ class Settings { Globals.DownloadToSdcard = (item == 1); - Save(p); dialog.dismiss(); - startDownloader(p); + showAccelermoeterConfig(p); } }); AlertDialog alert = builder.create(); @@ -50,11 +62,41 @@ class Settings }; + static void showAccelermoeterConfig(final MainActivity p) + { + if( Globals.AppNeedsArrowKeys || Globals.AppUsesJoystick ) + { + Save(p); + startDownloader(p); + return; + } + + final CharSequence[] items = {"Do not use accelerometer", "Use accelerometer as navigation keys"}; + + AlertDialog.Builder builder = new AlertDialog.Builder(p); + builder.setTitle("Your phone has navigation keys, you may optionally use accelerometer as another navigation keys"); + builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() + { + public void onClick(DialogInterface dialog, int item) + { + Globals.AppNeedsArrowKeys = (item == 1); + + Save(p); + dialog.dismiss(); + startDownloader(p); + } + }); + AlertDialog alert = builder.create(); + alert.setOwnerActivity(p); + alert.show(); + } + static void Save(final MainActivity p) { try { ObjectOutputStream out = new ObjectOutputStream(p.openFileOutput( SettingsFileName, p.MODE_WORLD_READABLE )); out.writeBoolean(Globals.DownloadToSdcard); + out.writeBoolean(Globals.AppNeedsArrowKeys); out.close(); } catch( FileNotFoundException e ) { } catch( SecurityException e ) { @@ -65,6 +107,17 @@ class Settings static void Apply() { nativeIsSdcardUsed( Globals.DownloadToSdcard ? 1 : 0 ); + Configuration c = new Configuration(); + c.setToDefaults(); + + if( c.navigation == Configuration.NAVIGATION_TRACKBALL ) + { + nativeSetTrackballUsed(); + } + if( Globals.AppUsesMouse ) + nativeSetMouseUsed(); + if( Globals.AppUsesJoystick && !Globals.AppNeedsArrowKeys ) + nativeSetJoystickUsed(); } static void startDownloader(MainActivity p) @@ -84,5 +137,8 @@ class Settings private static native int nativeIsSdcardUsed(int flag); + private static native int nativeSetTrackballUsed(); + private static native int nativeSetMouseUsed(); + private static native int nativeSetJoystickUsed(); }