Skip to content

Cocos2d x 3.13 oh #20904

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 3 commits into
base: cocos2d-x-3.13-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
83 changes: 7 additions & 76 deletions cocos/platform/ohos/CCGLViewImpl-ohos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,82 +106,13 @@ void GLViewImpl::setIMEKeyboardState(bool bOpen) {
}

Rect GLViewImpl::getSafeAreaRect() const {
Rect safeAreaRect = GLView::getSafeAreaRect();
float deviceAspectRatio = 0;
if(safeAreaRect.size.height > safeAreaRect.size.width) {
deviceAspectRatio = safeAreaRect.size.height / safeAreaRect.size.width;
} else {
deviceAspectRatio = safeAreaRect.size.width / safeAreaRect.size.height;
}

float marginX = DEFAULT_MARGIN_OHOS / _scaleX;
float marginY = DEFAULT_MARGIN_OHOS / _scaleY;

bool isScreenRound = JSFunction::getFunction("DeviceUtils.isRoundScreen").invoke<bool>();
bool hasSoftKeys = JSFunction::getFunction("DeviceUtils.hasSoftKeys").invoke<bool>();
bool isCutoutEnabled = JSFunction::getFunction("DeviceUtils.isCutoutEnable").invoke<bool>();

if(isScreenRound) {
// edge screen
if(safeAreaRect.size.width < safeAreaRect.size.height) {
safeAreaRect.origin.y += marginY * 2.f;
safeAreaRect.size.height -= (marginY * 2.f);

safeAreaRect.origin.x += marginX;
safeAreaRect.size.width -= (marginX * 2.f);
} else {
safeAreaRect.origin.y += marginY;
safeAreaRect.size.height -= (marginY * 2.f);

// landscape: no changes with X-coords
}
} else if (deviceAspectRatio >= WIDE_SCREEN_ASPECT_RATIO_OHOS) {
// almost all devices on the market have round corners
float bottomMarginIfPortrait = 0;
if(hasSoftKeys) {
bottomMarginIfPortrait = marginY * 2.f;
}

if(safeAreaRect.size.width < safeAreaRect.size.height) {
// portrait: double margin space if device has soft menu
safeAreaRect.origin.y += bottomMarginIfPortrait;
safeAreaRect.size.height -= (bottomMarginIfPortrait + marginY);
} else {
// landscape: ignore double margin at the bottom in any cases
// prepare signle margin for round corners
safeAreaRect.origin.y += marginY;
safeAreaRect.size.height -= (marginY * 2.f);
}
} else {
if(hasSoftKeys && (safeAreaRect.size.width < safeAreaRect.size.height)) {
// portrait: preserve only for soft system menu
safeAreaRect.origin.y += marginY * 2.f;
safeAreaRect.size.height -= (marginY * 2.f);
}
}

if (isCutoutEnabled) {
// screen with enabled cutout area
int orientation = JSFunction::getFunction("DeviceUtils.getOrientation").invoke<int>();

if(static_cast<int>(GLViewImpl::Orientation::PORTRAIT) == orientation) {
double height = JSFunction::getFunction("DeviceUtils.getCutoutHeight").invoke<int>() / _scaleY;
safeAreaRect.origin.y += height;
safeAreaRect.size.height -= height;
} else if(static_cast<int>(GLViewImpl::Orientation::PORTRAIT_INVERTED) == orientation) {
double height =JSFunction::getFunction("DeviceUtils.getCutoutHeight").invoke<int>() / _scaleY;
safeAreaRect.size.height -= height;
} else if(static_cast<int>(GLViewImpl::Orientation::LANDSCAPE) == orientation) {
double width = JSFunction::getFunction("DeviceUtils.getCutoutWidth").invoke<int>() / _scaleX;
safeAreaRect.size.width -= width;
} else if(static_cast<int>(GLViewImpl::Orientation::LANDSCAPE_INVERTED) == orientation) {
double width = JSFunction::getFunction("DeviceUtils.getCutoutWidth").invoke<int>() / _scaleX;
safeAreaRect.origin.x += width;
safeAreaRect.size.width -= width;
}
}

return safeAreaRect;
Rect safeAreaRect1;
safeAreaRect1.origin.x = JSFunction::getFunction("DeviceUtils.getSafeAreaLeft").invoke<int>() / _scaleX;
safeAreaRect1.origin.y = JSFunction::getFunction("DeviceUtils.getSafeAreaTop").invoke<int>() / _scaleY;
safeAreaRect1.size.width = JSFunction::getFunction("DeviceUtils.getSafeAreaWidth").invoke<int>() / _scaleX;
safeAreaRect1.size.height = JSFunction::getFunction("DeviceUtils.getSafeAreaHeight").invoke<int>() / _scaleX;
OHOS_LOGD("GLViewImpl getsafeAreaRect1, x:%{public}f, y:%{public}f, width:%{public}f, height:%{public}f", safeAreaRect1.origin.x, safeAreaRect1.origin.y, safeAreaRect1.size.width, safeAreaRect1.size.height);
return safeAreaRect1;
}
NS_CC_END

Expand Down
2 changes: 1 addition & 1 deletion cocos/platform/ohos/CCGLViewImpl-ohos.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,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__
1 change: 0 additions & 1 deletion cocos/platform/ohos/napi/helper/Js_Cocos2dxHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ napi_value Js_Cocos2dxHelper::initJsCocos2dxHelper(napi_env env, napi_callback_i
* If you have more information that can be obtained asynchronously, add it here.
*/
napi_value Js_Cocos2dxHelper::initAsyncInfo(napi_env env, napi_callback_info info) {
JSFunction::getFunction("DeviceUtils.initScreenInfo").invoke<void>();
return nullptr;
}

Expand Down
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 @@ -116,7 +116,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 @@ -138,6 +142,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 @@ -169,6 +178,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 @@ -189,6 +203,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 @@ -59,7 +59,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 @@ -160,6 +164,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