android platform:fix EditBox not show input box and crash.

This commit is contained in:
Dhilan007 2013-09-17 23:16:32 +08:00
parent 3a283839bb
commit 509bea531a
3 changed files with 50 additions and 28 deletions

View File

@ -101,6 +101,7 @@ public class Cocos2dxHelper {
//Cocos2dxHelper.nativeSetAssetManager(sAssetManager); //Cocos2dxHelper.nativeSetAssetManager(sAssetManager);
Cocos2dxBitmap.setContext(activity); Cocos2dxBitmap.setContext(activity);
sActivity = activity;
} }
public static void initListener() { public static void initListener() {
@ -109,7 +110,6 @@ public class Cocos2dxHelper {
@Override @Override
public void showEditTextDialog(final String title, final String message, public void showEditTextDialog(final String title, final String message,
final int inputMode, final int inputFlag, final int returnType, final int maxLength) { final int inputMode, final int inputFlag, final int returnType, final int maxLength) {
sActivity.runOnUiThread(new Runnable() { sActivity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -286,7 +286,6 @@ public class Cocos2dxHelper {
public static void setEditTextDialogResult(final String pResult) { public static void setEditTextDialogResult(final String pResult) {
try { try {
final byte[] bytesUTF8 = pResult.getBytes("UTF8"); final byte[] bytesUTF8 = pResult.getBytes("UTF8");
Cocos2dxHelper.nativeSetEditTextDialogResult(bytesUTF8); Cocos2dxHelper.nativeSetEditTextDialogResult(bytesUTF8);
} catch (UnsupportedEncodingException pUnsupportedEncodingException) { } catch (UnsupportedEncodingException pUnsupportedEncodingException) {
/* Nothing. */ /* Nothing. */

View File

@ -6,14 +6,15 @@
#include "cocoa/CCString.h" #include "cocoa/CCString.h"
#include "Java_org_cocos2dx_lib_Cocos2dxHelper.h" #include "Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include <pthread.h>
#define LOG_TAG "Java_org_cocos2dx_lib_Cocos2dxHelper.cpp" #define LOG_TAG "Java_org_cocos2dx_lib_Cocos2dxHelper.cpp"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper" #define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
static EditTextCallback s_pfEditTextCallback = NULL; EditTextCallback s_pfEditTextCallback = NULL;
static void* s_ctx = NULL; void* s_ctx = NULL;
using namespace cocos2d; using namespace cocos2d;
using namespace std; using namespace std;
@ -25,26 +26,6 @@ extern "C" {
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath(JNIEnv* env, jobject thiz, jstring apkPath) { JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath(JNIEnv* env, jobject thiz, jstring apkPath) {
g_apkPath = JniHelper::jstring2string(apkPath); g_apkPath = JniHelper::jstring2string(apkPath);
} }
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetEditTextDialogResult(JNIEnv * env, jobject obj, jbyteArray text) {
jsize size = env->GetArrayLength(text);
if (size > 0) {
jbyte * data = (jbyte*)env->GetByteArrayElements(text, 0);
char* pBuf = (char*)malloc(size+1);
if (pBuf != NULL) {
memcpy(pBuf, data, size);
pBuf[size] = '\0';
// pass data to edittext's delegate
if (s_pfEditTextCallback) s_pfEditTextCallback(pBuf, s_ctx);
free(pBuf);
}
env->ReleaseByteArrayElements(text, data, 0);
} else {
if (s_pfEditTextCallback) s_pfEditTextCallback("", s_ctx);
}
}
} }
const char * getApkPath() { const char * getApkPath() {

View File

@ -27,6 +27,8 @@
#include "event_dispatcher/CCEventDispatcher.h" #include "event_dispatcher/CCEventDispatcher.h"
#include "event_dispatcher/CCAccelerationEvent.h" #include "event_dispatcher/CCAccelerationEvent.h"
#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "cocos2dx/nativeactivity.cpp", __VA_ARGS__)) #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "cocos2dx/nativeactivity.cpp", __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
@ -68,6 +70,36 @@ struct engine {
static struct engine engine; static struct engine engine;
static char* editboxText = NULL;
extern EditTextCallback s_pfEditTextCallback;
extern void* s_ctx;
extern "C" {
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetEditTextDialogResult(JNIEnv * env, jobject obj, jbyteArray text) {
jsize size = env->GetArrayLength(text);
pthread_mutex_lock(&(engine.app->mutex));
if (size > 0) {
jbyte * data = (jbyte*)env->GetByteArrayElements(text, 0);
char* pBuf = (char*)malloc(size+1);
if (pBuf != NULL) {
memcpy(pBuf, data, size);
pBuf[size] = '\0';
editboxText = pBuf;
}
env->ReleaseByteArrayElements(text, data, 0);
} else {
char* pBuf = (char*)malloc(1);
pBuf[0] = '\0';
editboxText = pBuf;
}
pthread_cond_broadcast(&engine.app->cond);
pthread_mutex_unlock(&(engine.app->mutex));
}
}
typedef struct cocos_dimensions { typedef struct cocos_dimensions {
int w; int w;
int h; int h;
@ -181,6 +213,8 @@ static cocos_dimensions engine_init_display(struct engine* engine) {
/** /**
* Just the current frame in the display. * Just the current frame in the display.
*/ */
int tmpCount = 0;
static void engine_draw_frame(struct engine* engine) { static void engine_draw_frame(struct engine* engine) {
LOG_RENDER_DEBUG("engine_draw_frame(...)"); LOG_RENDER_DEBUG("engine_draw_frame(...)");
pthread_t thisthread = pthread_self(); pthread_t thisthread = pthread_self();
@ -200,6 +234,13 @@ static void engine_draw_frame(struct engine* engine) {
/* ((float)engine->state.y)/engine->height, 1); */ /* ((float)engine->state.y)/engine->height, 1); */
/* glClear(GL_COLOR_BUFFER_BIT); */ /* glClear(GL_COLOR_BUFFER_BIT); */
if (s_pfEditTextCallback && editboxText)
{
s_pfEditTextCallback(editboxText, s_ctx);
free(editboxText);
editboxText = NULL;
}
eglSwapBuffers(engine->display, engine->surface); eglSwapBuffers(engine->display, engine->surface);
} }
@ -445,6 +486,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
case APP_CMD_GAINED_FOCUS: case APP_CMD_GAINED_FOCUS:
if (cocos2d::Director::getInstance()->getOpenGLView()) { if (cocos2d::Director::getInstance()->getOpenGLView()) {
cocos2d::Application::getInstance()->applicationWillEnterForeground(); cocos2d::Application::getInstance()->applicationWillEnterForeground();
engine->animating = 1;
} }
break; break;