From 75260ec4c4fc8e8cf614fb9766fcffae8fc22b48 Mon Sep 17 00:00:00 2001 From: omigamedev Date: Tue, 29 Jan 2019 18:41:24 +0100 Subject: [PATCH] fix android egl errors, implement android backspace in text input --- android/src/main/cpp/keymap.h | 2 +- android/src/main/cpp/main.cpp | 49 +++++++++++++++++++++++++---------- src/event.h | 1 + src/node_text_input.cpp | 20 +++++++------- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/android/src/main/cpp/keymap.h b/android/src/main/cpp/keymap.h index 62ac6c5..663875a 100644 --- a/android/src/main/cpp/keymap.h +++ b/android/src/main/cpp/keymap.h @@ -70,7 +70,7 @@ kKey convert_key(int android_key) CASE(AKEYCODE_EXPLORER, kKey::Unknown); CASE(AKEYCODE_ENVELOPE, kKey::Unknown); CASE(AKEYCODE_ENTER, kKey::Unknown); - CASE(AKEYCODE_DEL, kKey::Unknown); + CASE(AKEYCODE_DEL, kKey::KeyBackspace); CASE(AKEYCODE_GRAVE, kKey::Unknown); CASE(AKEYCODE_MINUS, kKey::Unknown); CASE(AKEYCODE_EQUALS, kKey::Unknown); diff --git a/android/src/main/cpp/main.cpp b/android/src/main/cpp/main.cpp index 6763c18..1c26625 100755 --- a/android/src/main/cpp/main.cpp +++ b/android/src/main/cpp/main.cpp @@ -134,6 +134,16 @@ void android_async_lock(struct engine* engine) mutex_count++; } +bool android_async_trylock(struct engine* engine) +{ + if (!mutex.try_lock()) + return false; + if (mutex_count == 0) + eglMakeCurrent(engine->display, engine->surface, engine->surface, engine->context); + mutex_count++; + return true; +} + void android_async_swap(struct engine* engine) { eglSwapBuffers(engine->display, engine->surface); @@ -280,16 +290,18 @@ JNIEXPORT void JNICALL Java_com_omixlab_panopainter_MainActivity_pickExternalCal JNIEXPORT void JNICALL Java_com_omixlab_panopainter_MainActivity_contentRectChanged(JNIEnv *end, jobject, jint wnd_w, jint wnd_h, jint rect_left, jint rect_top, jint rect_right, jint rect_bottom) { + //if (!android_async_trylock(&g_engine)) + // return; LOG("resize wnd [%d %d] rect [%d %d %d %d]", wnd_w, wnd_h, rect_left, rect_top, rect_right, rect_bottom); - locker _lock(&g_engine); App::I.width = wnd_w; App::I.height = (rect_bottom - rect_top); App::I.off_x = 0; App::I.off_y = wnd_h - (rect_bottom - rect_top); - App::I.redraw = true; + //App::I.redraw = true; //App::I.resize(App::I.width, App::I.height); - g_engine.width = wnd_w; - g_engine.height = wnd_h; + //g_engine.width = wnd_w; + //g_engine.height = wnd_h; + //android_async_unlock(&g_engine); } } @@ -688,10 +700,10 @@ static void engine_draw_frame(struct engine* engine) { static float elapsed = 0; locker _lock(engine); - /* - if (engine->display == NULL) + if (engine->display == EGL_NO_DISPLAY) return; + /* int w, h; eglQuerySurface(engine->display, engine->surface, EGL_WIDTH, &w); eglQuerySurface(engine->display, engine->surface, EGL_HEIGHT, &h); @@ -711,7 +723,7 @@ static void engine_draw_frame(struct engine* engine) { elapsed += dt.count(); App::I.tick(dt.count()); - if (engine->display == NULL || !(App::I.redraw || App::I.animate)) + if (!(App::I.redraw || App::I.animate)) return; App::I.clear(); @@ -960,6 +972,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { //exit(0); break; case APP_CMD_GAINED_FOCUS: + /* // When our app gains focus, we start monitoring the accelerometer. if (engine->accelerometerSensor != NULL) { ASensorEventQueue_enableSensor(engine->sensorEventQueue, @@ -970,8 +983,10 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { (1000L/60)*1000); } engine->animating = 1; + */ break; case APP_CMD_LOST_FOCUS: + /* // When our app loses focus, we stop monitoring the accelerometer. // This is to avoid consuming battery while not being used. if (engine->accelerometerSensor != NULL) { @@ -981,6 +996,7 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { // Also stop animating. engine->animating = 0; engine_draw_frame(engine); + */ break; case APP_CMD_WINDOW_REDRAW_NEEDED: LOG("APP_CMD_WINDOW_REDRAW_NEEDED"); @@ -996,7 +1012,8 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { LOG("APP_CMD_CONTENT_RECT_CHANGED"); //App::I.width = engine->app->contentRect.right - engine->app->contentRect.left; //App::I.height = engine->app->contentRect.bottom - engine->app->contentRect.top; - LOG("content rect %f %f", App::I.width, App::I.height); + //LOG("content rect %f %f", App::I.width, App::I.height); + App::I.redraw = true; ALooper_wake(engine->app->looper); break; } @@ -1047,14 +1064,11 @@ void android_main(struct android_app* state) { // If animating, we loop until all events are read, then continue // to draw the next frame of animation. bool used = false; - while (!used && (ident=ALooper_pollOnce(100, NULL, &events, + int timeout = g_engine.display != EGL_NO_DISPLAY ? 100 : -1; + while (!used && (ident=ALooper_pollOnce(timeout, NULL, &events, (void**)&source)) != ALOOPER_POLL_ERROR) { // Process this event. - if (source != NULL) { - source->process(state, source); - used = true; - } // If a sensor has data, process it now. /* @@ -1069,8 +1083,15 @@ void android_main(struct android_app* state) { } } */ + if (source != NULL) { + source->process(state, source); + used = true; + } - if (ident == ALOOPER_POLL_TIMEOUT || ident == ALOOPER_POLL_CALLBACK){ + if (g_engine.display == EGL_NO_DISPLAY || ident == ALOOPER_POLL_CALLBACK) + continue; + + if (ident == ALOOPER_POLL_TIMEOUT){ App::I.redraw = true; engine_draw_frame(&g_engine); } diff --git a/src/event.h b/src/event.h index 5d1351b..17519c9 100644 --- a/src/event.h +++ b/src/event.h @@ -73,6 +73,7 @@ enum class kKey : uint8_t KeyShift, KeyTab, KeyEnter, + KeyBackspace, }; enum class kEventResult : uint8_t diff --git a/src/node_text_input.cpp b/src/node_text_input.cpp index 9554596..509f23c 100644 --- a/src/node_text_input.cpp +++ b/src/node_text_input.cpp @@ -82,14 +82,7 @@ kEventResult NodeTextInput::handle_event(Event* e) case kEventType::KeyUp: if (ke->m_key == kKey::KeyEnter && on_return) on_return(this); - break; - case kEventType::KeyChar: - if (m_cursor) - { - timer = 0; - m_cursor->m_display = true; - } - if (ke->m_char == '\b') // backspace + if (ke->m_key == kKey::KeyBackspace) { if (!m_string.empty()) { @@ -97,7 +90,16 @@ kEventResult NodeTextInput::handle_event(Event* e) m_text->set_text(m_string.c_str()); } } - else if (ke->m_char == 0x7f) // DEL + break; + case kEventType::KeyChar: + if (m_cursor) + { + timer = 0; + m_cursor->m_display = true; + } + if (ke->m_char == '\b' // backspace + || ke->m_char == 0x7f // DEL + ) { if (!m_string.empty()) {