Skip to content

Cocos2d x 3.4 oh #20912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: cocos2d-x-3.4-oh
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cocos/platform/ohos/CCGLViewImpl-ohos.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ class CC_DLL GLViewImpl : public GLView

NS_CC_END
#endif // end of CC_TARGET_PLATFORM == CC_PLATFORM_OHOS
#endif // end of __CC_EGLVIEW_ANDROID_H__
#endif // end of __CC_EGLVIEWIMPL_OHOS_H__
21 changes: 20 additions & 1 deletion cocos/platform/ohos/napi/helper/NapiHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ class JSFunction {
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
napi_value return_val;
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);

if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
ReturnType value;
if (!NapiValueConverter::ToCppValue(env, return_val, value)) {
// Handle error here
Expand All @@ -136,6 +140,11 @@ class JSFunction {
napi_value jsArgs[sizeof...(Args)] = {NapiValueConverter::ToNapiValue(env, args)...};
napi_value return_val;
status = napi_call_function(env, global, func, sizeof...(Args), jsArgs, &return_val);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}

static void callFunctionWithParams(WorkParam *param) {
Expand Down Expand Up @@ -167,6 +176,11 @@ class JSFunction {
}
if (status != napi_ok) {
LOGI("XXXXXX:napi_call_function getClassObject != napi_ok %{public}d", status);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}

napi_value thenFunc = nullptr;
Expand All @@ -187,6 +201,11 @@ class JSFunction {
status = napi_call_function(env, promise, thenFunc, 1, &successFunc, &ret);
if (status != napi_ok) {
LOGI("XXXXXX:napi_call_function thenFunc failed, ret: %{public}d", status);
if (status == napi_pending_exception) {
LOGI("Caught invoke exception: napi_pending_exception");
napi_value exception;
napi_get_and_clear_last_exception(env, &exception);
}
}
}
// Callback Function Type
Expand Down
7 changes: 6 additions & 1 deletion cocos/platform/ohos/napi/plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) {

int64_t value;
NAPI_CALL(env, napi_get_value_int64(env, args[0], &value));

napi_handle_scope scope = nullptr;
NAPI_CALL(env, napi_open_handle_scope(env, &scope));
if(scope == nullptr){
return nullptr;
}
NAPI_CALL(env, napi_create_object(env, &exports));

switch (value) {
Expand Down Expand Up @@ -161,6 +165,7 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) {
default:
OHOS_LOGE("unknown type");
}
NAPI_CALL(env, napi_close_handle_scope(env, scope));
return exports;
}

Expand Down
26 changes: 22 additions & 4 deletions cocos/platform/ohos/napi/render/plugin_render.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <ace/xcomponent/native_xcomponent_key_event.h>
#include <stdint.h>
#include <unistd.h>

Expand Down Expand Up @@ -31,7 +32,7 @@ float mousePositionY = -1;
bool isMouseLeftActive = false;
double scrollDistance = 0;

std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
const std::unordered_map<OH_NativeXComponent_KeyCode, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
{KEY_ESCAPE, cocos2d::EventKeyboard::KeyCode::KEY_ESCAPE},
{KEY_GRAVE, cocos2d::EventKeyboard::KeyCode::KEY_GRAVE},
{KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS},
Expand Down Expand Up @@ -61,7 +62,23 @@ std::unordered_map<int, cocos2d::EventKeyboard::KeyCode> ohKeyMap = {
{KEY_DPAD_UP, cocos2d::EventKeyboard::KeyCode::KEY_DPAD_UP},
{KEY_SYSRQ, cocos2d::EventKeyboard::KeyCode::KEY_PRINT},
{KEY_INSERT, cocos2d::EventKeyboard::KeyCode::KEY_INSERT},
{KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE}
{KEY_FORWARD_DEL, cocos2d::EventKeyboard::KeyCode::KEY_DELETE},
{KEY_SCROLL_LOCK, cocos2d::EventKeyboard::KeyCode::KEY_SCROLL_LOCK},
{KEY_MINUS, cocos2d::EventKeyboard::KeyCode::KEY_MINUS},
{KEY_AT, cocos2d::EventKeyboard::KeyCode::KEY_AT},
{KEY_PLUS, cocos2d::EventKeyboard::KeyCode::KEY_PLUS},
{KEY_MENU, cocos2d::EventKeyboard::KeyCode::KEY_MENU},
{KEY_BREAK, cocos2d::EventKeyboard::KeyCode::KEY_PAUSE},
{KEY_MOVE_HOME, cocos2d::EventKeyboard::KeyCode::KEY_HOME},
{KEY_MOVE_END, cocos2d::EventKeyboard::KeyCode::KEY_END},
{KEY_PAGE_UP, cocos2d::EventKeyboard::KeyCode::KEY_PG_UP},
{KEY_PAGE_DOWN, cocos2d::EventKeyboard::KeyCode::KEY_PG_DOWN},
{KEY_NUMPAD_ADD, cocos2d::EventKeyboard::KeyCode::KEY_KP_PLUS},
{KEY_NUMPAD_SUBTRACT, cocos2d::EventKeyboard::KeyCode::KEY_KP_MINUS},
{KEY_NUMPAD_MULTIPLY, cocos2d::EventKeyboard::KeyCode::KEY_KP_MULTIPLY},
{KEY_NUMPAD_DIVIDE, cocos2d::EventKeyboard::KeyCode::KEY_KP_DIVIDE},
{KEY_NUMPAD_ENTER, cocos2d::EventKeyboard::KeyCode::KEY_KP_ENTER}

};

cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyCode ohKeyCode)
Expand All @@ -81,8 +98,9 @@ cocos2d::EventKeyboard::KeyCode ohKeyCodeToCocosKeyCode(OH_NativeXComponent_KeyC
if (ohKeyCode >= KEY_A && ohKeyCode <= KEY_Z) {
// A - Z
return cocos2d::EventKeyboard::KeyCode(int(cocos2d::EventKeyboard::KeyCode::KEY_A) + (ohKeyCode - KEY_A));
}
return cocos2d::EventKeyboard::KeyCode(ohKeyCode);
}
OHOS_LOGW("Unmapped OH key code: %d", ohKeyCode);
return cocos2d::EventKeyboard::KeyCode::KEY_NONE;
}

void OnSurfaceCreatedCB(OH_NativeXComponent* component, void* window)
Expand Down