Skip to content

Cocos2d x 3.1 oh #20896

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.1-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
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 @@ -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 @@ -30,7 +31,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 @@ -60,7 +61,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 @@ -80,8 +97,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
2 changes: 1 addition & 1 deletion tests/cpp-tests/proj.ohos/.clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Language: Cpp
# BasedOnStyle: LLVM
ColumnLimit: 120
SortIncludes: false
SortIncludes: CaseSensitive
TabWidth: 4
IndentWidth: 4
UseTab: Never
Expand Down
13 changes: 6 additions & 7 deletions tests/cpp-tests/proj.ohos/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
{
"name": "default",
"signingConfig": "default",
"compatibleSdkVersion": "5.0.0(12)",
"targetSdkVersion": "5.0.0(12)",
"compatibleSdkVersion": "5.0.3(15)",
"runtimeOS": "HarmonyOS"
}
],
Expand All @@ -14,13 +13,13 @@
"name": "default",
"type": "HarmonyOS",
"material": {
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_bDUQ21cu36Hb_T3jW1UCUliZUhDOg-slWObojAZ01aE=.cer",
"storePassword": "0000001B42DE88211EE39411DFDEC8296FC1F89A1E03E0A0E155C6E2D9282E6FDCEA4C39E34EB9C3D38AA3",
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_Sn00X7CKuPjypPJX7h9NSE93VEZdoZLgQH01I4cTSMw=.cer",
"keyAlias": "debugKey",
"keyPassword": "0000001B85EB0DEC53BFE80C21BD062A5180A3CAA3DFBBB1B13B8F29130E2CF6DD6A2B2E4EABBABA095CC6",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_bDUQ21cu36Hb_T3jW1UCUliZUhDOg-slWObojAZ01aE=.p7b",
"keyPassword": "0000001A142B209FF7D2A7268F3273851288B08854AD9E09D57A0F77AEA8C1488ACD59F3BF5F880ABE47",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_Sn00X7CKuPjypPJX7h9NSE93VEZdoZLgQH01I4cTSMw=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_bDUQ21cu36Hb_T3jW1UCUliZUhDOg-slWObojAZ01aE=.p12"
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_Sn00X7CKuPjypPJX7h9NSE93VEZdoZLgQH01I4cTSMw=.p12",
"storePassword": "0000001A11A855F8A13953CDE22713662FC55157E78C6847EDF97963892A7BDC05A0C5B554C6528FB69D"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion tests/cpp-tests/proj.ohos/entry/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
"sourceOption": {
"workers": [
'./src/main/ets/workers/CocosWorker.ts'
'./src/main/ets/workers/CocosWorker.ets'
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import window from '@ohos.window';
import UIAbility from '@ohos.app.ability.UIAbility';
import nativeRender from "libnativerender.so";
import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities"
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"
import { BusinessError } from '@kit.BasicServicesKit';
import { WorkerManager } from '../workers/WorkerManager';
import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant';

const nativeAppLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE);
const rawFileUtils: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.RAW_FILE_UTILS);
let cocosWorker = WorkerManager.getInstance().getWorker();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
export default class MainAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
nativeAppLifecycle.onCreate();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, this.context);
console.info('[LIFECYCLE-App] onCreate')
}

onDestroy() {
nativeAppLifecycle.onDestroy();
console.info('[LIFECYCLE-App] onDestroy')
}

onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
windowStage.loadContent('pages/Index', (err:BusinessError, data) => {
if (err.code) {
return;
}
rawFileUtils.nativeResourceManagerInit(this.context.resourceManager);
rawFileUtils.writablePathInit(this.context.filesDir);
});

windowStage.getMainWindow().then((windowIns: window.Window) => {
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW, windowIns);
// Set whether to display the status bar and navigation bar. If they are not displayed, [] is displayed.
let systemBarPromise = windowIns.setWindowSystemBarEnable([]);
// Whether the window layout is displayed in full screen mode
let fullScreenPromise = windowIns.setWindowLayoutFullScreen(true);
// Sets whether the screen is always on.
let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true);
Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => {
console.info('Succeeded in setting the window');
}).catch((err: BusinessError) => {
console.error('Failed to set the window, cause ', err.code, err.message);
});

try {
DeviceUtils.calculateSafeArea(cocosWorker, windowIns.getWindowAvoidArea(window.AvoidAreaType.TYPE_CUTOUT), windowIns.getWindowProperties().windowRect);
windowIns.on('avoidAreaChange', (data) => {
console.info('getSafeAreaRect Succeeded in enabling the listener for system avoid area changes. type:' +
JSON.stringify(data.type) + ', area: ' + JSON.stringify(data.area));

if(data.type == window.AvoidAreaType.TYPE_SYSTEM_GESTURE || data.type == window.AvoidAreaType.TYPE_KEYBOARD) {
return;
}

let mainWindow: window.Window = GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_MAIN_WINDOW);
DeviceUtils.calculateSafeArea(cocosWorker, data.area, mainWindow.getWindowProperties().windowRect);
});
} catch (exception) {
console.error(`Failed to enable the listener for system avoid area changes. Cause code: ${exception.code}, message: ${exception.message}`);
}
})

windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => {
let stageEventType: window.WindowStageEventType = data;
switch (stageEventType) {
case window.WindowStageEventType.RESUMED:
console.info('[LIFECYCLE-App] onShow_RESUMED')
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
nativeAppLifecycle.onShow();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
}
break;
case window.WindowStageEventType.PAUSED:
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
console.info('[LIFECYCLE-App] onHide_PAUSED')
nativeAppLifecycle.onHide();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
}
break;
default:
break;
}
});
}

onWindowStageDestroy() {
// Main window is destroyed, release UI related resources
}

onForeground() {
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG)){
// Ability has brought to foreground
console.info('[LIFECYCLE-App] onShow')
nativeAppLifecycle.onShow();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, true);
}
}

onBackground() {
if(GlobalContext.loadGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG)){
// Ability has back to background
console.info('[LIFECYCLE-App] onHide')
nativeAppLifecycle.onHide();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_HIDE_FLAG, false);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_SHOW_FLAG, true);
}
}
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ struct Index {
}

build() {
Stack(){
Stack() {
XComponent({
id: 'xcomponentId',
type: 'surface',
Expand Down
Loading