2010-12-09 10:50:02 +08:00
|
|
|
/****************************************************************************
|
|
|
|
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"
|
2011-03-08 13:49:58 +08:00
|
|
|
#include "CCSet.h"
|
2010-12-09 10:50:02 +08:00
|
|
|
#include "CCDirector.h"
|
2011-04-01 15:27:06 +08:00
|
|
|
#include "CCKeypadDispatcher.h"
|
2010-12-09 10:50:02 +08:00
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCTouchDispatcher.h"
|
2011-03-08 13:49:58 +08:00
|
|
|
#include "CCFileUtils.h"
|
|
|
|
#include "CCGeometry.h"
|
2011-04-25 15:13:24 +08:00
|
|
|
#include "CCAccelerometer.h"
|
|
|
|
#include "CCApplication.h"
|
2011-04-22 16:33:41 +08:00
|
|
|
#include "CCIMEDispatcher.h"
|
2011-05-18 13:56:35 +08:00
|
|
|
#include "CCAccelerometer_android.h"
|
2010-12-09 10:50:02 +08:00
|
|
|
#include <android/log.h>
|
|
|
|
|
2011-03-29 14:20:05 +08:00
|
|
|
#if 0
|
2011-02-06 07:17:24 +08:00
|
|
|
#define LOG_TAG "Cocos2dJni"
|
|
|
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
|
2011-03-29 14:20:05 +08:00
|
|
|
#else
|
|
|
|
#define LOGD(...)
|
|
|
|
#endif
|
2011-02-06 07:17:24 +08:00
|
|
|
|
2011-02-26 15:52:57 +08:00
|
|
|
using namespace cocos2d;
|
|
|
|
|
2010-12-09 10:50:02 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// java vm helper function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2011-02-07 04:05:56 +08:00
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
JavaVM *gJavaVM = NULL;
|
2011-01-31 06:59:03 +08:00
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
jint JNI_OnLoad(JavaVM *vm, void *reserved)
|
|
|
|
{
|
|
|
|
gJavaVM = vm;
|
|
|
|
return JNI_VERSION_1_4;
|
|
|
|
}
|
2011-01-31 06:59:03 +08:00
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
struct TMethodJNI
|
|
|
|
{
|
|
|
|
JNIEnv * env;
|
|
|
|
jclass classID;
|
|
|
|
jmethodID methodID;
|
|
|
|
};
|
|
|
|
static bool getMethodID(struct TMethodJNI& t, const char *className, const char *methodName, const char *paramCode)
|
|
|
|
{
|
|
|
|
bool ret = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (gJavaVM->GetEnv((void**)&t.env, JNI_VERSION_1_4) != JNI_OK)
|
|
|
|
{
|
|
|
|
LOGD("Failed to get the environment using GetEnv()");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gJavaVM->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();
|
|
|
|
}
|
2011-01-31 06:59:03 +08:00
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
// handle touch event
|
2011-03-29 14:20:05 +08:00
|
|
|
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y)
|
2010-12-09 10:50:02 +08:00
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
|
|
|
|
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
|
2011-03-29 14:20:05 +08:00
|
|
|
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);
|
2011-02-07 04:05:56 +08:00
|
|
|
}
|
2010-12-09 10:50:02 +08:00
|
|
|
}
|
|
|
|
|
2011-03-29 14:20:05 +08:00
|
|
|
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv* env, jobject thiz, jint id, jfloat x, jfloat y)
|
2010-12-09 10:50:02 +08:00
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
|
|
|
|
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
|
2011-03-29 14:20:05 +08:00
|
|
|
CCSet set;
|
2011-02-18 10:23:36 +08:00
|
|
|
|
|
|
|
/* Add to the set to send to the director */
|
2011-03-29 14:20:05 +08:00
|
|
|
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);
|
|
|
|
}
|
2010-12-09 10:50:02 +08:00
|
|
|
}
|
|
|
|
|
2011-02-18 10:23:36 +08:00
|
|
|
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesMove(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
|
2010-12-09 10:50:02 +08:00
|
|
|
{
|
2011-02-18 10:23:36 +08:00
|
|
|
int size = env->GetArrayLength(ids);
|
|
|
|
jint id[size];
|
|
|
|
jfloat x[size];
|
|
|
|
jfloat y[size];
|
2011-03-08 13:49:58 +08:00
|
|
|
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
|
|
|
|
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
|
2011-03-29 14:20:05 +08:00
|
|
|
CCSet set;
|
2011-02-18 10:23:36 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2011-03-07 09:52:45 +08:00
|
|
|
pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor ,
|
|
|
|
(y[i] - rcRect.origin.y) / fScreenScaleFactor);
|
2011-03-29 14:20:05 +08:00
|
|
|
set.addObject(pTouch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is error, should return.
|
|
|
|
LOGD("Moving touches with id: %d error", id[i]);
|
|
|
|
return;
|
2011-02-18 10:23:36 +08:00
|
|
|
}
|
2011-02-07 04:05:56 +08:00
|
|
|
}
|
2011-03-07 09:52:45 +08:00
|
|
|
|
2011-03-29 14:20:05 +08:00
|
|
|
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesMoved(&set, NULL);
|
2010-12-09 10:50:02 +08:00
|
|
|
}
|
2011-02-07 04:05:56 +08:00
|
|
|
|
2011-02-18 10:23:36 +08:00
|
|
|
void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesCancel(JNIEnv* env, jobject thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
|
2010-12-09 10:50:02 +08:00
|
|
|
{
|
2011-02-18 10:23:36 +08:00
|
|
|
int size = env->GetArrayLength(ids);
|
|
|
|
jint id[size];
|
|
|
|
jfloat x[size];
|
|
|
|
jfloat y[size];
|
2011-03-08 13:49:58 +08:00
|
|
|
CCRect rcRect = CCEGLView::sharedOpenGLView().getViewPort();
|
|
|
|
float fScreenScaleFactor = CCEGLView::sharedOpenGLView().getScreenScaleFactor();
|
2011-03-29 14:20:05 +08:00
|
|
|
CCSet set;
|
2011-02-18 10:23:36 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2011-03-07 09:52:45 +08:00
|
|
|
pTouch->SetTouchInfo(0, (x[i] - rcRect.origin.x) / fScreenScaleFactor ,
|
|
|
|
(y[i] - rcRect.origin.y) / fScreenScaleFactor);
|
2011-03-29 14:20:05 +08:00
|
|
|
set.addObject(pTouch);
|
2011-03-07 09:52:45 +08:00
|
|
|
s_pTouches[id[i]] = NULL;
|
2011-03-29 14:20:05 +08:00
|
|
|
pTouch->release();
|
2011-02-18 10:23:36 +08:00
|
|
|
}
|
2011-02-07 04:05:56 +08:00
|
|
|
}
|
2011-03-07 09:52:45 +08:00
|
|
|
|
2011-03-29 14:20:05 +08:00
|
|
|
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->getDelegate()->touchesCancelled(&set, NULL);
|
2010-12-09 10:50:02 +08:00
|
|
|
}
|
2011-04-25 15:13:24 +08:00
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
2011-01-07 16:29:06 +08:00
|
|
|
|
2011-04-01 15:27:06 +08:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2011-01-07 16:29:06 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2010-12-09 10:50:02 +08:00
|
|
|
|
2011-02-06 07:17:24 +08:00
|
|
|
|
|
|
|
void enableAccelerometerJNI()
|
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
TMethodJNI t;
|
|
|
|
if (getMethodID(t
|
|
|
|
, "org/cocos2dx/lib/Cocos2dxActivity"
|
|
|
|
, "enableAccelerometer"
|
|
|
|
, "()V"))
|
2011-02-06 07:17:24 +08:00
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
t.env->CallStaticVoidMethod(t.classID, t.methodID);
|
2011-02-06 07:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void disableAccelerometerJNI()
|
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
TMethodJNI t;
|
2011-02-06 07:17:24 +08:00
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
if (getMethodID(t
|
|
|
|
, "org/cocos2dx/lib/Cocos2dxActivity"
|
|
|
|
, "disableAccelerometer"
|
|
|
|
, "()V"))
|
2011-02-06 07:17:24 +08:00
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
t.env->CallStaticVoidMethod(t.classID, t.methodID);
|
2011-02-06 07:17:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-01 11:35:26 +08:00
|
|
|
void showMessageBoxJNI(const char * pszMsg, const char * pszTitle)
|
|
|
|
{
|
|
|
|
if (! pszMsg)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
TMethodJNI t;
|
|
|
|
if (getMethodID(t
|
|
|
|
, "org/cocos2dx/lib/Cocos2dxActivity"
|
|
|
|
, "showMessageBox"
|
|
|
|
, "(Ljava/lang/String;Ljava/lang/String;)V"))
|
2011-04-01 11:35:26 +08:00
|
|
|
{
|
|
|
|
jstring StringArg1;
|
|
|
|
|
|
|
|
if (! pszTitle)
|
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
StringArg1 = t.env->NewStringUTF("");
|
2011-04-01 11:35:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-22 16:33:41 +08:00
|
|
|
StringArg1 = t.env->NewStringUTF(pszTitle);
|
2011-04-01 11:35:26 +08:00
|
|
|
}
|
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
jstring StringArg2 = t.env->NewStringUTF(pszMsg);
|
|
|
|
t.env->CallStaticVoidMethod(t.classID, t.methodID, StringArg1, StringArg2);
|
2011-04-01 11:35:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// handle IME message
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2010-12-09 10:50:02 +08:00
|
|
|
|
2011-04-22 16:33:41 +08:00
|
|
|
void setKeyboardStateJNI(int bOpen)
|
|
|
|
{
|
|
|
|
TMethodJNI t;
|
2011-06-10 17:51:37 +08:00
|
|
|
//jint open = bOpen;
|
2011-04-22 16:33:41 +08:00
|
|
|
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();
|
|
|
|
}
|
2011-05-12 18:32:13 +08:00
|
|
|
|
2011-05-30 16:36:12 +08:00
|
|
|
jstring Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeGetContentText()
|
|
|
|
{
|
|
|
|
JNIEnv * env = 0;
|
|
|
|
|
|
|
|
if (gJavaVM->GetEnv((void**)&env, JNI_VERSION_1_4) != JNI_OK || ! env)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
const char * pszText = cocos2d::CCIMEDispatcher::sharedDispatcher()->getContentText();
|
|
|
|
return env->NewStringUTF(pszText);
|
|
|
|
}
|
|
|
|
|
2011-05-12 18:32:13 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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)
|
|
|
|
{
|
2011-05-18 13:56:35 +08:00
|
|
|
rtn = new char[alen + 1];
|
2011-05-12 18:32:13 +08:00
|
|
|
memcpy(rtn, ba, alen);
|
|
|
|
rtn[alen] = 0;
|
|
|
|
}
|
|
|
|
env->ReleaseByteArrayElements(barr, ba, 0);
|
|
|
|
|
|
|
|
return rtn;
|
|
|
|
}
|
|
|
|
|
2011-05-12 18:55:32 +08:00
|
|
|
char* getPackageNameJNI()
|
2011-05-12 18:32:13 +08:00
|
|
|
{
|
|
|
|
TMethodJNI t;
|
2011-05-12 18:55:32 +08:00
|
|
|
char* ret = NULL;
|
2011-05-12 18:32:13 +08:00
|
|
|
|
2011-05-12 21:12:15 +08:00
|
|
|
if (getMethodID(t,
|
|
|
|
"org/cocos2dx/lib/Cocos2dxActivity",
|
|
|
|
"getCocos2dxPackageName",
|
|
|
|
"()Ljava/lang/String;"))
|
2011-05-12 18:32:13 +08:00
|
|
|
{
|
|
|
|
jstring str = (jstring)t.env->CallStaticObjectMethod(t.classID, t.methodID);
|
|
|
|
ret = jstringTostring(t.env, str);
|
|
|
|
|
|
|
|
LOGD("package name %s", ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2011-06-17 10:49:02 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// handle get current language
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
char* getCurrentLanguageJNI()
|
|
|
|
{
|
|
|
|
TMethodJNI 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;
|
|
|
|
}
|
2010-12-09 10:50:02 +08:00
|
|
|
}
|