Skip to content

Cocos2d x 3.2 oh #20897

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 11 commits into
base: cocos2d-x-3.2-oh
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions tests/cpp-tests/proj.ohos/build-profile.json5
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"name": "default",
"signingConfig": "default",
"compatibleSdkVersion": "5.0.0(12)",
"compatibleSdkVersion": "5.0.3(15)",
"runtimeOS": "HarmonyOS"
}
],
Expand All @@ -13,13 +13,13 @@
"name": "default",
"type": "HarmonyOS",
"material": {
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_kQ3j-d7NCfHRs_dfTLf575Dy2229lzobHWxGKCkxbuM=.cer",
"storePassword": "0000001B2E09D50CB0EDD747304177695A9EEF9392F110FC0ADC059D010F84D48B8FBA6C27CB2EDC0BBB4C",
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_Ma-e8rGZRlL3pspfnvwOBKNCtMA6SdpY2I5Xq1ZqXq0=.cer",
"keyAlias": "debugKey",
"keyPassword": "0000001BE87BA242685DE9B38DF527A88FCBF03EB49744492B30807E743EAAFA0963BA86E5C92E31322D80",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_kQ3j-d7NCfHRs_dfTLf575Dy2229lzobHWxGKCkxbuM=.p7b",
"keyPassword": "0000001A1282ABAEE41837DB7D3730BE8226630F4EF6C6A82CFA6AB3E713E42B7EADFB2B35B84066E5BE",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_Ma-e8rGZRlL3pspfnvwOBKNCtMA6SdpY2I5Xq1ZqXq0=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_kQ3j-d7NCfHRs_dfTLf575Dy2229lzobHWxGKCkxbuM=.p12"
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_Ma-e8rGZRlL3pspfnvwOBKNCtMA6SdpY2I5Xq1ZqXq0=.p12",
"storePassword": "0000001A8D1860564E8789FA7D6EB0ECE6096168CA64586C290AE06E41D0F016D5C079FF9179BD8274CD"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import web_webview from '@ohos.web.webview';
import window from '@ohos.window';
import UIAbility from '@ohos.app.ability.UIAbility';
import nativeRender from "libnativerender.so";
import { ContextType } from "@ohos/libSysCapabilities"
import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities"
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'
import { window } from '@kit.ArkUI';
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);
// Initializes the webView kernel of the system. This parameter is optional if it is not used.
web_webview.WebviewController.initializeWebEngine();
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_WANT, this.context);
console.info('[LIFECYCLE-App] onCreate')
}

Expand All @@ -26,7 +27,7 @@ export default class MainAbility extends UIAbility {

onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
windowStage.loadContent('pages/Index', (err, data) => {
windowStage.loadContent('pages/Index', (err:BusinessError, data) => {
if (err.code) {
return;
}
Expand All @@ -35,6 +36,7 @@ export default class MainAbility extends UIAbility {
});

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
Expand All @@ -46,16 +48,43 @@ export default class MainAbility extends UIAbility {
}).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) => {
windowStage.on("windowStageEvent", (data:window.WindowStageEventType) => {
let stageEventType: window.WindowStageEventType = data;
switch (stageEventType) {
case window.WindowStageEventType.RESUMED:
nativeAppLifecycle.onShow();
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:
nativeAppLifecycle.onHide();
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;
Expand All @@ -68,14 +97,22 @@ export default class MainAbility extends UIAbility {
}

onForeground() {
// Ability has brought to foreground
console.info('[LIFECYCLE-App] onShow')
nativeAppLifecycle.onShow();
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() {
// Ability has back to background
console.info('[LIFECYCLE-App] onDestroy')
nativeAppLifecycle.onHide();
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);
}
}
};
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import worker, { ThreadWorkerGlobalScope, MessageEvents } from '@ohos.worker';
import nativeRender from "libnativerender.so";
import { ContextType } from "@ohos/libSysCapabilities"
import { ContextType, DeviceUtils } from "@ohos/libSysCapabilities"
import { VideoPlayer } from "@ohos/libSysCapabilities"
import { ApplicationManager } from "@ohos/libSysCapabilities"
import { CocosEditBox } from "@ohos/libSysCapabilities"
import { Dialog } from "@ohos/libSysCapabilities"
import { WebView } from "@ohos/libSysCapabilities"
import { JumpManager } from "@ohos/libSysCapabilities"
import { NapiHelper } from "@ohos/libSysCapabilities"
import { ApplicationManager } from "@ohos/libSysCapabilities"
import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities"

const appLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE);
Expand All @@ -32,8 +32,8 @@ workerPort.onmessage = (e: MessageEvents) => {
CocosEditBox.init(workerPort);
JumpManager.init(workerPort);
WebView.init(workerPort);
VideoPlayer.init(workerPort);
ApplicationManager.init(workerPort);
VideoPlayer.init(workerPort);
napiContext.initAsyncInfo();
break;
case "abilityContextInit":
Expand Down Expand Up @@ -72,6 +72,9 @@ workerPort.onmessage = (e: MessageEvents) => {
case "exit":
appLifecycle.onBackPress();
break;
case "updateSafeArea":
DeviceUtils.updateWorkerSafeArea(e.data.safeArea);
break;
default:
console.error("cocos worker: message type unknown")
}
Expand Down
15 changes: 1 addition & 14 deletions tests/cpp-tests/proj.ohos/entry/src/main/module.json5
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,10 @@
}
],
// https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/quick-start/module-configuration-file.md/
//������ģʽ����Ϊֻ֧��ȫ��
"supportWindowMode": ["fullscreen"],
// ��Ϸ�����ڹ̶����ڻ�
// "supportWindowMode": ["floating"],
// �����ȫ�����󣬿���ͬʱ����fullscreen��floating�������������ڻ�����ֻ������ʾ��ȫ����ȫ��������ʾ
//"supportWindowMode": ["fullscreen","floating"],
//�����߱�
// "maxWindowRatio": 3.5,
//��С���߱�
// "minWindowRatio": 0.5,
//������Ϊ1080
"supportWindowMode": ["fullscreen"],
"maxWindowWidth": 1080,
//��С����Ϊ1080
"minWindowWidth": 1080,
//���߶�Ϊ720
"maxWindowHeight": 720,
//��С�߶�Ϊ720
"minWindowHeight": 720
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ export class AppPermissionConsts {
static readonly REQUEST_CODE_REQUIRED: number = 1000;

static readonly REQUEST_CODE_CUSTOM: number = 1001;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ export class GlobalContextConstants {
static readonly COCOS2DX_ABILITY_CONTEXT = "Cocos2dx.abilityContext";
static readonly COCOS2DX_ABILITY_WANT = "Cocos2dx.abilityWant";
static readonly COCOS2DX_WEB_RESULT= "Cocos2dx.webResult";
static readonly COCOS2DX_MAIN_WINDOW = "Cocos2dx.mainWindow";
static readonly COCOS2DX_HIDE_FLAG = "Cocos2dx.hideFlag";
static readonly COCOS2DX_SHOW_FLAG = "Cocos2dx.showFlag";
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Color4B, EditBoxMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity';
import type { ThreadWorkerGlobalScope } from '@ohos.worker';
import { Color4B, EditBoxMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity';

export class CocosEditBox {
static MODULE_NAME : string = 'EditBox';

private static workerPort: ThreadWorkerGlobalScope;
private static workerPort : ThreadWorkerGlobalScope;

static init(workerPort: ThreadWorkerGlobalScope) : void {
static init(workerPort : ThreadWorkerGlobalScope) : void {
CocosEditBox.workerPort = workerPort;
}

Expand Down Expand Up @@ -108,4 +109,4 @@ export class CocosEditBox {
let editBoxMsgEntity: EditBoxMsgEntity = new EditBoxMsgEntity(CocosEditBox.MODULE_NAME, 'hideAllEditBox');
CocosEditBox.workerPort.postMessage(editBoxMsgEntity);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { VideoPlayMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity';
import type { ThreadWorkerGlobalScope } from '@ohos.worker';

export class VideoPlayer {
static MODULE_NAME: string = 'VideoPlay';

private static workerPort: ThreadWorkerGlobalScope;
private static workerPort : ThreadWorkerGlobalScope;

static init(workerPort: ThreadWorkerGlobalScope) : void {
static init(workerPort : ThreadWorkerGlobalScope) : void {
VideoPlayer.workerPort = workerPort;
}

Expand Down
Loading