From b105b6005e15548016ed039583eae5c1f9a03410 Mon Sep 17 00:00:00 2001 From: minggo Date: Mon, 25 Jul 2011 14:12:30 +0800 Subject: [PATCH] [android] fixed #623: refactor Cocos2dJni.cpp --- cocos2dx/Android.mk | 8 +- cocos2dx/platform/CCCommon.cpp | 3 +- .../android/CCAccelerometer_android.cpp | 1 + .../android/CCAccelerometer_android.h | 1 - .../android/CCApplication_android.cpp | 2 +- .../platform/android/CCEGLView_android.cpp | 4 +- .../platform/android/CCFileUtils_android.cpp | 2 +- cocos2dx/platform/android/CCImage_android.cpp | 1 - cocos2dx/platform/android/Cocos2dJni.cpp | 463 ------------------ cocos2dx/platform/android/jni/IMEJni.cpp | 87 ++++ cocos2dx/platform/android/jni/IMEJni.h | 33 ++ cocos2dx/platform/android/jni/MessageJni.cpp | 114 +++++ .../{Cocos2dJni.h => jni/MessageJni.h} | 13 +- cocos2dx/platform/android/jni/SensorJni.cpp | 99 ++++ cocos2dx/platform/android/jni/SensorJni.h | 34 ++ .../platform/android/jni/SystemInfoJni.cpp | 82 ++++ cocos2dx/platform/android/jni/SystemInfoJni.h | 36 ++ cocos2dx/platform/android/jni/TouchesJni.cpp | 184 +++++++ 18 files changed, 685 insertions(+), 482 deletions(-) delete mode 100644 cocos2dx/platform/android/Cocos2dJni.cpp create mode 100644 cocos2dx/platform/android/jni/IMEJni.cpp create mode 100644 cocos2dx/platform/android/jni/IMEJni.h create mode 100644 cocos2dx/platform/android/jni/MessageJni.cpp rename cocos2dx/platform/android/{Cocos2dJni.h => jni/MessageJni.h} (78%) create mode 100644 cocos2dx/platform/android/jni/SensorJni.cpp create mode 100644 cocos2dx/platform/android/jni/SensorJni.h create mode 100644 cocos2dx/platform/android/jni/SystemInfoJni.cpp create mode 100644 cocos2dx/platform/android/jni/SystemInfoJni.h create mode 100644 cocos2dx/platform/android/jni/TouchesJni.cpp diff --git a/cocos2dx/Android.mk b/cocos2dx/Android.mk index 63a8baa9a1..f806b8c242 100644 --- a/cocos2dx/Android.mk +++ b/cocos2dx/Android.mk @@ -65,8 +65,12 @@ platform/platform.cpp \ platform/android/CCEGLView_android.cpp \ platform/android/CCAccelerometer_android.cpp \ platform/android/CCApplication_android.cpp \ -platform/android/Cocos2dJni.cpp \ platform/android/jni/JniHelper.cpp \ +platform/android/jni/IMEJni.cpp \ +platform/android/jni/MessageJni.cpp \ +platform/android/jni/SensorJni.cpp \ +platform/android/jni/SystemInfoJni.cpp \ +platform/android/jni/TouchesJni.cpp \ script_support/CCScriptSupport.cpp \ sprite_nodes/CCAnimation.cpp \ sprite_nodes/CCAnimationCache.cpp \ @@ -98,7 +102,7 @@ tileMap_parallax_nodes/CCTMXTiledMap.cpp \ tileMap_parallax_nodes/CCTMXXMLParser.cpp \ tileMap_parallax_nodes/CCTileMapAtlas.cpp \ touch_dispatcher/CCTouchDispatcher.cpp \ -touch_dispatcher/CCTouchHandler.cpp \ +touch_dispatcher/CCTouchHandler.cpp diff --git a/cocos2dx/platform/CCCommon.cpp b/cocos2dx/platform/CCCommon.cpp index 8fd1e146ce..e9f212515c 100644 --- a/cocos2dx/platform/CCCommon.cpp +++ b/cocos2dx/platform/CCCommon.cpp @@ -159,8 +159,9 @@ NS_CC_END; #include #include +#include -#include "android/Cocos2dJni.h" +#include "android/jni/MessageJni.h" NS_CC_BEGIN; diff --git a/cocos2dx/platform/android/CCAccelerometer_android.cpp b/cocos2dx/platform/android/CCAccelerometer_android.cpp index 5f525b3c74..4d04327b0a 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.cpp +++ b/cocos2dx/platform/android/CCAccelerometer_android.cpp @@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "CCAccelerometer_android.h" +#include "jni/SensorJni.h" #include #include diff --git a/cocos2dx/platform/android/CCAccelerometer_android.h b/cocos2dx/platform/android/CCAccelerometer_android.h index 407d0850c8..3bb75a4fad 100644 --- a/cocos2dx/platform/android/CCAccelerometer_android.h +++ b/cocos2dx/platform/android/CCAccelerometer_android.h @@ -27,7 +27,6 @@ THE SOFTWARE. #include "CCCommon.h" #include "CCAccelerometerDelegate.h" -#include "Cocos2dJni.h" #include namespace cocos2d { diff --git a/cocos2dx/platform/android/CCApplication_android.cpp b/cocos2dx/platform/android/CCApplication_android.cpp index f0f9cce43b..b25f930bed 100644 --- a/cocos2dx/platform/android/CCApplication_android.cpp +++ b/cocos2dx/platform/android/CCApplication_android.cpp @@ -2,7 +2,7 @@ #include "jni/JniHelper.h" #include "CCDirector.h" #include "CCEGLView.h" -#include "Cocos2dJni.h" +#include "android/jni/SystemInfoJni.h" #include #include diff --git a/cocos2dx/platform/android/CCEGLView_android.cpp b/cocos2dx/platform/android/CCEGLView_android.cpp index 59ea027246..2a63b60863 100644 --- a/cocos2dx/platform/android/CCEGLView_android.cpp +++ b/cocos2dx/platform/android/CCEGLView_android.cpp @@ -23,12 +23,12 @@ THE SOFTWARE. ****************************************************************************/ #include "CCEGLView_android.h" #include "GLES/gl.h" - #include "CCSet.h" #include "CCDirector.h" #include "ccMacros.h" #include "CCTouchDispatcher.h" -#include "Cocos2dJni.h" +#include "jni/IMEJni.h" +#include "jni/MessageJni.h" #include diff --git a/cocos2dx/platform/android/CCFileUtils_android.cpp b/cocos2dx/platform/android/CCFileUtils_android.cpp index 4fa7c5f356..04a21d52fc 100644 --- a/cocos2dx/platform/android/CCFileUtils_android.cpp +++ b/cocos2dx/platform/android/CCFileUtils_android.cpp @@ -25,7 +25,7 @@ THE SOFTWARE. NS_CC_BEGIN; #include "CCCommon.h" -#include "Cocos2dJni.h" +#include "jni/SystemInfoJni.h" #define MAX_PATH 256 diff --git a/cocos2dx/platform/android/CCImage_android.cpp b/cocos2dx/platform/android/CCImage_android.cpp index 6a84e80d84..745cd975f2 100644 --- a/cocos2dx/platform/android/CCImage_android.cpp +++ b/cocos2dx/platform/android/CCImage_android.cpp @@ -29,7 +29,6 @@ THE SOFTWARE. #include #include "CCPlatformMacros.h" -#include "Cocos2dJni.h" #include "CCImage.h" #include "jni/JniHelper.h" diff --git a/cocos2dx/platform/android/Cocos2dJni.cpp b/cocos2dx/platform/android/Cocos2dJni.cpp deleted file mode 100644 index 6c41fdbda9..0000000000 --- a/cocos2dx/platform/android/Cocos2dJni.cpp +++ /dev/null @@ -1,463 +0,0 @@ -/**************************************************************************** -Copyright (c) 2010 cocos2d-x.org - -http://www.cocos2d-x.org - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in -all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. -****************************************************************************/ -#include "Cocos2dJni.h" -#include "CCSet.h" -#include "CCDirector.h" -#include "CCKeypadDispatcher.h" -#include "CCTouch.h" -#include "CCTouchDispatcher.h" -#include "CCFileUtils.h" -#include "CCGeometry.h" -#include "CCAccelerometer.h" -#include "CCApplication.h" -#include "CCIMEDispatcher.h" -#include "CCAccelerometer_android.h" -#include - -#include "jni/JniHelper.h" - -#if 0 -#define LOG_TAG "Cocos2dJni" -#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) -#else -#define LOGD(...) -#endif - -using namespace cocos2d; - -extern "C" -{ - ////////////////////////////////////////////////////////////////////////// - // java vm helper function - ////////////////////////////////////////////////////////////////////////// - - static bool getMethodID(JniMethodInfo& t, const char *className, const char *methodName, const char *paramCode) - { - bool ret = 0; - do - { - if (JniHelper::getJavaVM()->GetEnv((void**)&t.env, JNI_VERSION_1_4) != JNI_OK) - { - LOGD("Failed to get the environment using GetEnv()"); - break; - } - - if (JniHelper::getJavaVM()->AttachCurrentThread(&t.env, 0) < 0) - { - LOGD("Failed to get the environment using AttachCurrentThread()"); - break; - } - - t.classID = t.env->FindClass(className); - if (! t.classID) - { - LOGD("Failed to find class of %s", className); - break; - } - - t.methodID = t.env->GetStaticMethodID(t.classID, methodName, paramCode); - if (! t.methodID) - { - LOGD("Failed to find method id of %s", methodName); - break; - } - ret = true; - } while (0); - - return ret; - } - - ////////////////////////////////////////////////////////////////////////// - // native renderer - ////////////////////////////////////////////////////////////////////////// - - #define MAX_TOUCHES 5 - static CCTouch *s_pTouches[MAX_TOUCHES] = { NULL }; - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env) - { - cocos2d::CCDirector::sharedDirector()->mainLoop(); - } - - // handle touch event - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y) - { - CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); - float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); - CCSet set; - - CCTouch *pTouch = s_pTouches[id]; - if (! pTouch) - { - LOGD("Beginning touches with id: %d, x=%f, y=%f", id, x, y); - - pTouch = new CCTouch(); - pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor, (y - rcRect.origin.y) / fScreenScaleFactor); - s_pTouches[id] = pTouch; - set.addObject(pTouch); - - cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesBegan(&set, NULL); - } - else - { - LOGD("Beginnig touches with id: %d error", id); - } - } - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y) - { - CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); - float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); - CCSet set; - - /* Add to the set to send to the director */ - CCTouch* pTouch = s_pTouches[id]; - if (pTouch) - { - LOGD("Ending touches with id: %d, x=%f, y=%f", id, x, y); - - pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor , (y - rcRect.origin.y) / fScreenScaleFactor); - set.addObject(pTouch); - - // release the object - pTouch->release(); - s_pTouches[id] = NULL; - - cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesEnded(&set, NULL); - } else { - LOGD("Ending touches with id: %d error", id); - } - } - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) - { - int size = env->GetArrayLength(ids); - jint id[size]; - jfloat x[size]; - jfloat y[size]; - CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); - float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); - CCSet set; - - env->GetIntArrayRegion(ids, 0, size, id); - env->GetFloatArrayRegion(xs, 0, size, x); - env->GetFloatArrayRegion(ys, 0, size, y); - - for( int i = 0 ; i < size ; i++ ) { - LOGD("Moving touches with id: %d, x=%f, y=%f", id[i], x[i], y[i]); - cocos2d::CCTouch* pTouch = s_pTouches[id[i]]; - if (pTouch) - { - pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor , - (y[i] - rcRect.origin.y) / fScreenScaleFactor); - set.addObject(pTouch); - } - else - { - // It is error, should return. - LOGD("Moving touches with id: %d error", id[i]); - return; - } - } - - cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesMoved(&set, NULL); - } - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) - { - int size = env->GetArrayLength(ids); - jint id[size]; - jfloat x[size]; - jfloat y[size]; - CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); - float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); - CCSet set; - - env->GetIntArrayRegion(ids, 0, size, id); - env->GetFloatArrayRegion(xs, 0, size, x); - env->GetFloatArrayRegion(ys, 0, size, y); - - for( int i = 0 ; i < size ; i++ ) { - cocos2d::CCTouch* pTouch = s_pTouches[id[i]]; - if (pTouch) - { - pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor , - (y[i] - rcRect.origin.y) / fScreenScaleFactor); - set.addObject(pTouch); - s_pTouches[id[i]] = NULL; - pTouch->release(); - } - } - - cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesCancelled(&set, NULL); - } - - // handle onPause and onResume - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause() - { - CCApplication::sharedApplication().applicationDidEnterBackground(); - } - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume() - { - // Shared OpenGL View instance doesn't exist yet when Activity.onResume is first called - if (CCDirector::sharedDirector()->getOpenGLView()) - CCApplication::sharedApplication().applicationWillEnterForeground(); - } - -#define KEYCODE_BACK 0x04 -#define KEYCODE_MENU 0x52 - - // handle keydown event - - jboolean Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv* env, jobject thiz, jint keyCode) - { - switch (keyCode) - { - case KEYCODE_BACK: - if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeBackClicked)) - return JNI_TRUE; - break; - case KEYCODE_MENU: - if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeMenuClicked)) - return JNI_TRUE; - break; - default: - return JNI_FALSE; - } - return JNI_FALSE; - } - - ////////////////////////////////////////////////////////////////////////// - // handle accelerometer changes - ////////////////////////////////////////////////////////////////////////// - - void Java_org_cocos2dx_lib_Cocos2dxAccelerometer_onSensorChanged(JNIEnv* env, jobject thiz, jfloat x, jfloat y, jfloat z, jlong timeStamp) - { - // We need to invert to make it compatible with iOS. - CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); - float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); - cocos2d::CCAccelerometer::sharedAccelerometer()->update((x - rcRect.origin.x) / fScreenScaleFactor, - (y - rcRect.origin.y) / fScreenScaleFactor, - z, - timeStamp); - } - - void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeSetPaths(JNIEnv* env, jobject thiz, jstring apkPath) - { - const char* str; - jboolean isCopy; - str = env->GetStringUTFChars(apkPath, &isCopy); - if (isCopy) { - cocos2d::CCFileUtils::setResourcePath(str); - env->ReleaseStringUTFChars(apkPath, str); - } - } - - - void enableAccelerometerJNI() - { - JniMethodInfo t; - if (getMethodID(t - , "org/cocos2dx/lib/Cocos2dxActivity" - , "enableAccelerometer" - , "()V")) - { - t.env->CallStaticVoidMethod(t.classID, t.methodID); - } - } - - void disableAccelerometerJNI() - { - JniMethodInfo t; - - if (getMethodID(t - , "org/cocos2dx/lib/Cocos2dxActivity" - , "disableAccelerometer" - , "()V")) - { - t.env->CallStaticVoidMethod(t.classID, t.methodID); - } - } - - void showMessageBoxJNI(const char * pszMsg, const char * pszTitle) - { - if (! pszMsg) - { - return; - } - - JniMethodInfo t; - if (getMethodID(t - , "org/cocos2dx/lib/Cocos2dxActivity" - , "showMessageBox" - , "(Ljava/lang/String;Ljava/lang/String;)V")) - { - jstring StringArg1; - - if (! pszTitle) - { - StringArg1 = t.env->NewStringUTF(""); - } - else - { - StringArg1 = t.env->NewStringUTF(pszTitle); - } - - jstring StringArg2 = t.env->NewStringUTF(pszMsg); - t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1, StringArg2); - } - } - - ////////////////////////////////////////////////////////////////////////// - // handle IME message - ////////////////////////////////////////////////////////////////////////// - - void setKeyboardStateJNI(int bOpen) - { - JniMethodInfo t; - //jint open = bOpen; - if (getMethodID(t - , "org/cocos2dx/lib/Cocos2dxGLSurfaceView" - , (bOpen) ? "openIMEKeyboard" : "closeIMEKeyboard" - , "()V")) - { - t.env->CallStaticVoidMethod(t.classID, t.methodID); - } - } - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) - { - jboolean isCopy = 0; - const char* pszText = env->GetStringUTFChars(text, &isCopy); - if (isCopy) - { - cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText)); - env->ReleaseStringUTFChars(text, pszText); - } - } - - void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) - { - cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); - } - - jstring Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeGetContentText() - { - JNIEnv * env = 0; - - if (JniHelper::getJavaVM()->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || ! env) - { - return 0; - } - const char * pszText = cocos2d::CCIMEDispatcher::sharedDispatcher()->getContentText(); - return env->NewStringUTF(pszText); - } - - ////////////////////////////////////////////////////////////////////////// - // get package name - ////////////////////////////////////////////////////////////////////////// - - static char* jstringTostring(JNIEnv* env, jstring jstr) - { - char* rtn = NULL; - - // convert jstring to byte array - jclass clsstring = env->FindClass("java/lang/String"); - jstring strencode = env->NewStringUTF("utf-8"); - jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); - jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode); - jsize alen = env->GetArrayLength(barr); - jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE); - - // copy byte array into char[] - if (alen > 0) - { - rtn = new char[alen + 1]; - memcpy(rtn, ba, alen); - rtn[alen] = 0; - } - env->ReleaseByteArrayElements(barr, ba, 0); - - return rtn; - } - - char* getPackageNameJNI() - { - JniMethodInfo t; - char* ret = NULL; - - if (getMethodID(t, - "org/cocos2dx/lib/Cocos2dxActivity", - "getCocos2dxPackageName", - "()Ljava/lang/String;")) - { - jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); - ret = jstringTostring(t.env, str); - - LOGD("package name %s", ret); - } - - return ret; - } - - ////////////////////////////////////////////////////////////////////////// - // handle get current language - ////////////////////////////////////////////////////////////////////////// - char* getCurrentLanguageJNI() - { - JniMethodInfo t; - char* ret = NULL; - - if (getMethodID(t - , "org/cocos2dx/lib/Cocos2dxActivity" - , "getCurrentLanguage" - , "()Ljava/lang/String;")) - { - jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); - ret = jstringTostring(t.env, str); - - LOGD("language name %s", ret); - } - - return ret; - } - - ////////////////////////////////////////////////////////////////////////// - // terminate the process - ////////////////////////////////////////////////////////////////////////// - void terminateProcessJNI() - { - JniMethodInfo t; - - if (getMethodID(t - , "org/cocos2dx/lib/Cocos2dxActivity" - , "terminateProcess" - , "()V")) - { - t.env->CallStaticObjectMethod(t.classID, t.methodID); - } - } -} diff --git a/cocos2dx/platform/android/jni/IMEJni.cpp b/cocos2dx/platform/android/jni/IMEJni.cpp new file mode 100644 index 0000000000..b6d1a5b9bd --- /dev/null +++ b/cocos2dx/platform/android/jni/IMEJni.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "IMEJni.h" +#include "CCIMEDispatcher.h" +#include "JniHelper.h" + +#include +#include +#include + +#if 0 +#define LOG_TAG "IMEJni" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) +#else +#define LOGD(...) +#endif + +using namespace cocos2d; + +extern "C" +{ + ////////////////////////////////////////////////////////////////////////// + // handle IME message + ////////////////////////////////////////////////////////////////////////// + + void setKeyboardStateJNI(int bOpen) + { + JniMethodInfo t; + + if (JniHelper::getMethodInfo(t, + "org/cocos2dx/lib/Cocos2dxGLSurfaceView", + (bOpen) ? "openIMEKeyboard" : "closeIMEKeyboard", + "()V")) + { + t.env->CallStaticVoidMethod(t.classID, t.methodID); + } + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInsertText(JNIEnv* env, jobject thiz, jstring text) + { + jboolean isCopy = 0; + const char* pszText = env->GetStringUTFChars(text, &isCopy); + if (isCopy) + { + cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(pszText, strlen(pszText)); + env->ReleaseStringUTFChars(text, pszText); + } + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeDeleteBackward(JNIEnv* env, jobject thiz) + { + cocos2d::CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); + } + + jstring Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeGetContentText() + { + JNIEnv * env = 0; + + if (JniHelper::getJavaVM()->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || ! env) + { + return 0; + } + const char * pszText = cocos2d::CCIMEDispatcher::sharedDispatcher()->getContentText(); + return env->NewStringUTF(pszText); + } +} diff --git a/cocos2dx/platform/android/jni/IMEJni.h b/cocos2dx/platform/android/jni/IMEJni.h new file mode 100644 index 0000000000..75dfad756e --- /dev/null +++ b/cocos2dx/platform/android/jni/IMEJni.h @@ -0,0 +1,33 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#ifndef __ANDROID_IME_JNI_H__ +#define __ANDROID_IME_JNI_H__ + +extern "C" + +{ + extern void setKeyboardStateJNI(int bOpen); +} + +#endif // __ANDROID_IME_JNI_H__ diff --git a/cocos2dx/platform/android/jni/MessageJni.cpp b/cocos2dx/platform/android/jni/MessageJni.cpp new file mode 100644 index 0000000000..b77a5ff1e8 --- /dev/null +++ b/cocos2dx/platform/android/jni/MessageJni.cpp @@ -0,0 +1,114 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "MessageJni.h" +#include "CCDirector.h" +#include "JniHelper.h" +#include "CCApplication.h" + +#include +#include + + +#if 0 +#define LOG_TAG "MessageJni" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) +#else +#define LOGD(...) +#endif + +using namespace cocos2d; + +extern "C" +{ + ////////////////////////////////////////////////////////////////////////// + // native renderer + ////////////////////////////////////////////////////////////////////////// + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeRender(JNIEnv* env) + { + cocos2d::CCDirector::sharedDirector()->mainLoop(); + } + + // handle onPause and onResume + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnPause() + { + CCApplication::sharedApplication().applicationDidEnterBackground(); + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeOnResume() + { + // Shared OpenGL View instance doesn't exist yet when Activity.onResume is first called + if (CCDirector::sharedDirector()->getOpenGLView()) + { + CCApplication::sharedApplication().applicationWillEnterForeground(); + } + } + + void showMessageBoxJNI(const char * pszMsg, const char * pszTitle) + { + if (! pszMsg) + { + return; + } + + JniMethodInfo t; + if (JniHelper::getMethodInfo(t + , "org/cocos2dx/lib/Cocos2dxActivity" + , "showMessageBox" + , "(Ljava/lang/String;Ljava/lang/String;)V")) + { + jstring StringArg1; + + if (! pszTitle) + { + StringArg1 = t.env->NewStringUTF(""); + } + else + { + StringArg1 = t.env->NewStringUTF(pszTitle); + } + + jstring StringArg2 = t.env->NewStringUTF(pszMsg); + t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1, StringArg2); + } + } + + + ////////////////////////////////////////////////////////////////////////// + // terminate the process + ////////////////////////////////////////////////////////////////////////// + void terminateProcessJNI() + { + JniMethodInfo t; + + if (JniHelper::getMethodInfo(t + , "org/cocos2dx/lib/Cocos2dxActivity" + , "terminateProcess" + , "()V")) + { + t.env->CallStaticObjectMethod(t.classID, t.methodID); + } + } +} diff --git a/cocos2dx/platform/android/Cocos2dJni.h b/cocos2dx/platform/android/jni/MessageJni.h similarity index 78% rename from cocos2dx/platform/android/Cocos2dJni.h rename to cocos2dx/platform/android/jni/MessageJni.h index 4a4ee8ca06..fbc20b7c47 100644 --- a/cocos2dx/platform/android/Cocos2dJni.h +++ b/cocos2dx/platform/android/jni/MessageJni.h @@ -21,21 +21,14 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ -#ifndef __ANDROID_COCOS2D_JNI_H__ -#define __ANDROID_COCOS2D_JNI_H__ - -#include +#ifndef __ANDROID_MESSAGE_JNI_H__ +#define __ANDROID_MESSAGE_JNI_H__ extern "C" { - extern void enableAccelerometerJNI(); - extern void disableAccelerometerJNI(); extern void showMessageBoxJNI(const char * pszMsg, const char * pszTitle); - extern void setKeyboardStateJNI(int bOpen); - extern char* getCurrentLanguageJNI(); - extern char* getPackageNameJNI(); extern void terminateProcessJNI(); } -#endif // __ANDROID_COCOS2D_JNI_H__ +#endif // __ANDROID_MESSAGE_JNI_H__ diff --git a/cocos2dx/platform/android/jni/SensorJni.cpp b/cocos2dx/platform/android/jni/SensorJni.cpp new file mode 100644 index 0000000000..1fe073bcd5 --- /dev/null +++ b/cocos2dx/platform/android/jni/SensorJni.cpp @@ -0,0 +1,99 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "SensorJni.h" +#include "CCGeometry.h" +#include "CCAccelerometer.h" +#include "platform/android/CCAccelerometer_android.h" +#include "CCEGLView.h" +#include "CCFileUtils.h" +#include "JniHelper.h" +#include +#include + + +#if 0 +#define LOG_TAG "SensorJni" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) +#else +#define LOGD(...) +#endif + +using namespace cocos2d; + +extern "C" +{ + + ////////////////////////////////////////////////////////////////////////// + // handle accelerometer changes + ////////////////////////////////////////////////////////////////////////// + + void Java_org_cocos2dx_lib_Cocos2dxAccelerometer_onSensorChanged(JNIEnv* env, jobject thiz, jfloat x, jfloat y, jfloat z, jlong timeStamp) + { + // We need to invert to make it compatible with iOS. + CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); + float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); + cocos2d::CCAccelerometer::sharedAccelerometer()->update((x - rcRect.origin.x) / fScreenScaleFactor, + (y - rcRect.origin.y) / fScreenScaleFactor, + z, + timeStamp); + } + + void Java_org_cocos2dx_lib_Cocos2dxActivity_nativeSetPaths(JNIEnv* env, jobject thiz, jstring apkPath) + { + const char* str; + jboolean isCopy; + str = env->GetStringUTFChars(apkPath, &isCopy); + if (isCopy) { + cocos2d::CCFileUtils::setResourcePath(str); + env->ReleaseStringUTFChars(apkPath, str); + } + } + + + void enableAccelerometerJNI() + { + JniMethodInfo t; + + if (JniHelper::getMethodInfo(t, + "org/cocos2dx/lib/Cocos2dxActivity", + "enableAccelerometer", + "()V")) + { + t.env->CallStaticVoidMethod(t.classID, t.methodID); + } + } + + void disableAccelerometerJNI() + { + JniMethodInfo t; + + if (JniHelper::getMethodInfo(t, + "org/cocos2dx/lib/Cocos2dxActivity", + "disableAccelerometer", + "()V")) + { + t.env->CallStaticVoidMethod(t.classID, t.methodID); + } + } +} diff --git a/cocos2dx/platform/android/jni/SensorJni.h b/cocos2dx/platform/android/jni/SensorJni.h new file mode 100644 index 0000000000..90a85f7ff7 --- /dev/null +++ b/cocos2dx/platform/android/jni/SensorJni.h @@ -0,0 +1,34 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#ifndef __ANDROID_SENSOR_JNI_H__ +#define __ANDROID_SENSOR_JNI_H__ + +extern "C" + +{ + extern void enableAccelerometerJNI(); + extern void disableAccelerometerJNI(); +} + +#endif // __ANDROID_SENSOR_JNI_H__ diff --git a/cocos2dx/platform/android/jni/SystemInfoJni.cpp b/cocos2dx/platform/android/jni/SystemInfoJni.cpp new file mode 100644 index 0000000000..b25b04f3a7 --- /dev/null +++ b/cocos2dx/platform/android/jni/SystemInfoJni.cpp @@ -0,0 +1,82 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "SystemInfoJni.h" +#include "JniHelper.h" + +#include +#include + + +#if 0 +#define LOG_TAG "SystemInfoJni" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) +#else +#define LOGD(...) +#endif + +using namespace cocos2d; + +extern "C" +{ + char* getPackageNameJNI() + { + JniMethodInfo t; + char* ret = 0; + + if (JniHelper::getMethodInfo(t, + "org/cocos2dx/lib/Cocos2dxActivity", + "getCocos2dxPackageName", + "()Ljava/lang/String;")) + { + jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); + ret = JniHelper::jstringToChar(str); + + LOGD("package name %s", ret); + } + + return ret; + } + + ////////////////////////////////////////////////////////////////////////// + // handle get current language + ////////////////////////////////////////////////////////////////////////// + char* getCurrentLanguageJNI() + { + JniMethodInfo t; + char* ret = 0; + + if (JniHelper::getMethodInfo(t + , "org/cocos2dx/lib/Cocos2dxActivity" + , "getCurrentLanguage" + , "()Ljava/lang/String;")) + { + jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID); + ret = JniHelper::jstringToChar(str); + + LOGD("language name %s", ret); + } + + return ret; + } +} diff --git a/cocos2dx/platform/android/jni/SystemInfoJni.h b/cocos2dx/platform/android/jni/SystemInfoJni.h new file mode 100644 index 0000000000..dfcccb67e1 --- /dev/null +++ b/cocos2dx/platform/android/jni/SystemInfoJni.h @@ -0,0 +1,36 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#ifndef __ANDROID_SYSTEM_INFO_JNI_H__ +#define __ANDROID_SYSTEM_INFO_JNI_H__ + +#include + +extern "C" + +{ + extern char* getCurrentLanguageJNI(); + extern char* getPackageNameJNI(); +} + +#endif // __ANDROID_SYSTEM_INFO_JNI_H__ diff --git a/cocos2dx/platform/android/jni/TouchesJni.cpp b/cocos2dx/platform/android/jni/TouchesJni.cpp new file mode 100644 index 0000000000..ca0e0b8425 --- /dev/null +++ b/cocos2dx/platform/android/jni/TouchesJni.cpp @@ -0,0 +1,184 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ +#include "CCSet.h" +#include "CCDirector.h" +#include "CCKeypadDispatcher.h" +#include "CCTouch.h" +#include "CCEGLView.h" +#include "CCTouchDispatcher.h" + +#include +#include + +#if 0 +#define LOG_TAG "NativeTouchesJni" +#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) +#else +#define LOGD(...) +#endif + +using namespace cocos2d; + +extern "C" +{ + +#define MAX_TOUCHES 5 +static CCTouch *s_pTouches[MAX_TOUCHES] = { NULL }; + + // handle touch event + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y) + { + CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); + float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); + CCSet set; + + CCTouch *pTouch = s_pTouches[id]; + if (! pTouch) + { + LOGD("Beginning touches with id: %d, x=%f, y=%f", id, x, y); + + pTouch = new CCTouch(); + pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor, (y - rcRect.origin.y) / fScreenScaleFactor); + s_pTouches[id] = pTouch; + set.addObject(pTouch); + + cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesBegan(&set, NULL); + } + else + { + LOGD("Beginnig touches with id: %d error", id); + } + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y) + { + CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); + float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); + CCSet set; + + /* Add to the set to send to the director */ + CCTouch* pTouch = s_pTouches[id]; + if (pTouch) + { + LOGD("Ending touches with id: %d, x=%f, y=%f", id, x, y); + + pTouch->SetTouchInfo(0, (x - rcRect.origin.x) / fScreenScaleFactor , (y - rcRect.origin.y) / fScreenScaleFactor); + set.addObject(pTouch); + + // release the object + pTouch->release(); + s_pTouches[id] = NULL; + + cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesEnded(&set, NULL); + } else { + LOGD("Ending touches with id: %d error", id); + } + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) + { + int size = env->GetArrayLength(ids); + jint id[size]; + jfloat x[size]; + jfloat y[size]; + CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); + float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); + CCSet set; + + env->GetIntArrayRegion(ids, 0, size, id); + env->GetFloatArrayRegion(xs, 0, size, x); + env->GetFloatArrayRegion(ys, 0, size, y); + + for( int i = 0 ; i < size ; i++ ) { + LOGD("Moving touches with id: %d, x=%f, y=%f", id[i], x[i], y[i]); + cocos2d::CCTouch* pTouch = s_pTouches[id[i]]; + if (pTouch) + { + pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor , + (y[i] - rcRect.origin.y) / fScreenScaleFactor); + set.addObject(pTouch); + } + else + { + // It is error, should return. + LOGD("Moving touches with id: %d error", id[i]); + return; + } + } + + cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesMoved(&set, NULL); + } + + void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys) + { + int size = env->GetArrayLength(ids); + jint id[size]; + jfloat x[size]; + jfloat y[size]; + CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort(); + float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor(); + CCSet set; + + env->GetIntArrayRegion(ids, 0, size, id); + env->GetFloatArrayRegion(xs, 0, size, x); + env->GetFloatArrayRegion(ys, 0, size, y); + + for( int i = 0 ; i < size ; i++ ) { + cocos2d::CCTouch* pTouch = s_pTouches[id[i]]; + if (pTouch) + { + pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor , + (y[i] - rcRect.origin.y) / fScreenScaleFactor); + set.addObject(pTouch); + s_pTouches[id[i]] = NULL; + pTouch->release(); + } + } + + cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesCancelled(&set, NULL); + } + +#define KEYCODE_BACK 0x04 +#define KEYCODE_MENU 0x52 + + // handle keydown event + + jboolean Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeKeyDown(JNIEnv* env, jobject thiz, jint keyCode) + { + switch (keyCode) + { + case KEYCODE_BACK: + if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeBackClicked)) + return JNI_TRUE; + break; + case KEYCODE_MENU: + if (CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeMenuClicked)) + return JNI_TRUE; + break; + default: + return JNI_FALSE; + } + return JNI_FALSE; + } +}