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

@ -57,7 +57,7 @@ public class Cocos2dxHelper {
private static String sFileDirectory;
private static Activity sActivity = null;
private static Cocos2dxHelperListener sCocos2dxHelperListener;
/**
* Optional meta-that can be in the manifest for this component, specifying
* the name of the native shared library to load. If not specified,
@ -101,6 +101,7 @@ public class Cocos2dxHelper {
//Cocos2dxHelper.nativeSetAssetManager(sAssetManager);
Cocos2dxBitmap.setContext(activity);
sActivity = activity;
}
public static void initListener() {
@ -108,8 +109,7 @@ public class Cocos2dxHelper {
@Override
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() {
@Override
public void run() {
@ -286,8 +286,7 @@ public class Cocos2dxHelper {
public static void setEditTextDialogResult(final String pResult) {
try {
final byte[] bytesUTF8 = pResult.getBytes("UTF8");
Cocos2dxHelper.nativeSetEditTextDialogResult(bytesUTF8);
Cocos2dxHelper.nativeSetEditTextDialogResult(bytesUTF8);
} catch (UnsupportedEncodingException pUnsupportedEncodingException) {
/* Nothing. */
}

View File

@ -6,14 +6,15 @@
#include "cocoa/CCString.h"
#include "Java_org_cocos2dx_lib_Cocos2dxHelper.h"
#include <pthread.h>
#define LOG_TAG "Java_org_cocos2dx_lib_Cocos2dxHelper.cpp"
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define CLASS_NAME "org/cocos2dx/lib/Cocos2dxHelper"
static EditTextCallback s_pfEditTextCallback = NULL;
static void* s_ctx = NULL;
EditTextCallback s_pfEditTextCallback = NULL;
void* s_ctx = NULL;
using namespace cocos2d;
using namespace std;
@ -25,26 +26,6 @@ extern "C" {
JNIEXPORT void JNICALL Java_org_cocos2dx_lib_Cocos2dxHelper_nativeSetApkPath(JNIEnv* env, jobject thiz, jstring 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() {

View File

@ -27,6 +27,8 @@
#include "event_dispatcher/CCEventDispatcher.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 LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "cocos2dx/nativeactivity.cpp", __VA_ARGS__))
@ -68,6 +70,36 @@ struct 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 {
int w;
int h;
@ -181,6 +213,8 @@ static cocos_dimensions engine_init_display(struct engine* engine) {
/**
* Just the current frame in the display.
*/
int tmpCount = 0;
static void engine_draw_frame(struct engine* engine) {
LOG_RENDER_DEBUG("engine_draw_frame(...)");
pthread_t thisthread = pthread_self();
@ -198,7 +232,14 @@ static void engine_draw_frame(struct engine* engine) {
/* // Just fill the screen with a color. */
/* glClearColor(((float)engine->state.x)/engine->width, engine->state.angle, */
/* ((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);
}
@ -445,6 +486,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
case APP_CMD_GAINED_FOCUS:
if (cocos2d::Director::getInstance()->getOpenGLView()) {
cocos2d::Application::getInstance()->applicationWillEnterForeground();
engine->animating = 1;
}
break;