Skip to content

Cocos2d js v3.6 oh #1862

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-JS-v3.6-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
12 changes: 6 additions & 6 deletions samples/js-tests/project/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_QXMC-gAJhIy09r-aR-O9R7q8nTHTumnKBesfb9edLwk=.cer",
"storePassword": "0000001B6A69302D4D111DCB74975A8165D5E23EA61A73FCCE30D42952F837C28C48131D9A1D6724675265",
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_8Fd3EcMM_RdXw_o1Qluil2Zs6rDDxSCb9KVybL2AofA=.cer",
"keyAlias": "debugKey",
"keyPassword": "0000001B8470462D8531EC86C7F826FE0041C5136295A7521B1EF44982FE9731397016A1E84BA83212AFDE",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_QXMC-gAJhIy09r-aR-O9R7q8nTHTumnKBesfb9edLwk=.p7b",
"keyPassword": "0000001A6E67CC5E817B52C8B8F1A155B53FCB113D9846F6430921A35A91F0C1FBA5081E337B95F52A2E",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_8Fd3EcMM_RdXw_o1Qluil2Zs6rDDxSCb9KVybL2AofA=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_QXMC-gAJhIy09r-aR-O9R7q8nTHTumnKBesfb9edLwk=.p12"
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_8Fd3EcMM_RdXw_o1Qluil2Zs6rDDxSCb9KVybL2AofA=.p12",
"storePassword": "0000001A8D23A0A8E834550BA7E84550FF7BF680D7129698185DB486564E8E45F50BEE6036BB86FE43D1"
}
}
]
Expand Down
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
Expand Up @@ -10,7 +10,7 @@ extern "C"

void Cocos2dxRenderer_nativeInit(int w, int h)
{
OHOS_LOGD("Cocos2dxRenderer_nativeInit() - window width:[%d], height:[%d]", w, h);
OHOS_LOGD("Cocos2dxRenderer_nativeInit() - window width:[%{public}d], height:[%{public}d]", w, h);
if (!CCDirector::sharedDirector()->getOpenGLView())
{
OHOS_LOGD("Cocos2dxRenderer_nativeInit() - branch 1");
Expand All @@ -33,4 +33,4 @@ void Cocos2dxRenderer_nativeInit(int w, int h)
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "platform/ohos/CCLogOhos.h"
#include "plugin_manager.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export interface CPPFunctions {
failLoading: (viewTag: number, url: string) => void;
jsCallback: () => void;
onVideoCallBack: (viewTag: number, event: number) => void;
onAccelerometerCallBack: (x: number, y: number, z: number, interval: number) => void;
}

export const getContext: (a: number) => CPPFunctions;
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import window from '@ohos.window';
import UIAbility from '@ohos.app.ability.UIAbility';
import web_webview from '@ohos.web.webview';
import nativeRender from "libnativerender.so";
import { ContextType } from "@ohos/libSysCapabilities"
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();

export default class MainAbility extends UIAbility {
onCreate(want, launchParam) {
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 @@ -23,9 +24,9 @@ export default class MainAbility extends UIAbility {
console.info('[LIFECYCLE-App] onDestroy')
}

onWindowStageCreate(windowStage) {
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 @@ -34,6 +35,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 @@ -42,12 +44,29 @@ export default class MainAbility extends UIAbility {
let keepScreenOnPromise = windowIns.setWindowKeepScreenOn(true);
Promise.all([systemBarPromise, fullScreenPromise, keepScreenOnPromise]).then(() => {
console.info('Succeeded in setting the window');
}).catch((err) => {
console.error('Failed to set the window, cause ' + JSON.stringify(err));
}).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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { WorkerManager } from '../workers/WorkerManager';

@Component
export struct CocosWebView {
viewInfo: WebViewInfo = new WebViewInfo(0, 0, 0, 0, 0);
@ObjectLink viewInfo: WebViewInfo ;
cocosWorker: worker.ThreadWorker = WorkerManager.getInstance().getWorker();

build() {
Expand Down Expand Up @@ -40,4 +40,4 @@ export struct CocosWebView {
this.cocosWorker.postMessage({ type: "onErrorReceive", viewTag: this.viewInfo.viewTag, url: this.viewInfo.url })
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import worker, { ThreadWorkerGlobalScope } from '@ohos.worker';
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 @@ -24,56 +24,60 @@ NapiHelper.registerFunctions(napiContext.registerFunction)

const workerPort: ThreadWorkerGlobalScope = worker.workerPort;

workerPort.onmessage = function(e) : void {
let data = e.data;
switch(data.type) {
workerPort.onmessage = (e: MessageEvents) => {
switch(e.data.type) {
case "onXCLoad":
console.log("[cocos] onXCLoad Callback");
Dialog.init(workerPort);
CocosEditBox.init(workerPort);
JumpManager.init(workerPort);
WebView.init(workerPort);
VideoPlayer.init(workerPort);
ApplicationManager.init(workerPort);
VideoPlayer.init(workerPort);
napiContext.initAsyncInfo();
break;
case "abilityContextInit":
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, data.data);
GlobalContext.storeGlobalThis(GlobalContextConstants.COCOS2DX_ABILITY_CONTEXT, e.data.data);
break;
case "editBoxOnFocus":
inputNapi.editBoxOnFocusCB(data.viewTag);
inputNapi.editBoxOnFocusCB(e.data.viewTag);
break;
case "editBoxOnChange":
inputNapi.editBoxOnChangeCB(data.viewTag, data.value);
inputNapi.editBoxOnChangeCB(e.data.viewTag, e.data.value);
break;
case "editBoxOnEnter":
inputNapi.editBoxOnEnterCB(data.viewTag, data.text);
inputNapi.editBoxOnEnterCB(e.data.viewTag, e.data.text);
break;
case "textFieldTTFOnChange":
inputNapi.textFieldTTFOnChangeCB(data.data);
inputNapi.textFieldTTFOnChangeCB(e.data.data);
break;
case "onMouseWheel":
mouseNapi.mouseWheelCB(data.eventType, data.scrollY);
mouseNapi.mouseWheelCB(e.data.eventType, e.data.scrollY);
break;
case "onPageBegin":
webViewNapi.shouldStartLoading(data.viewTag, data.url);
webViewNapi.shouldStartLoading(e.data.viewTag, e.data.url);
break;
case "onPageEnd":
webViewNapi.finishLoading(data.viewTag, data.url);
webViewNapi.finishLoading(e.data.viewTag, e.data.url);
break;
case "onJsCallBack":
webViewNapi.jsCallback();
break;
case "onErrorReceive":
webViewNapi.failLoading(data.viewTag, data.url);
webViewNapi.failLoading(e.data.viewTag, e.data.url);
break;
case "onVideoCallBack":
videoPlayNapi.onVideoCallBack(data.viewTag, data.event);
videoPlayNapi.onVideoCallBack(e.data.viewTag, e.data.event);
break;
case "exit":
appLifecycle.onBackPress();
break;
case "updateSafeArea":
DeviceUtils.updateWorkerSafeArea(e.data.safeArea);
break;
default:
console.error("cocos worker: message type unknown")
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class WorkerManager {
private cocosWorker: worker.ThreadWorker;

private constructor() {
this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ts", {
this.cocosWorker = new worker.ThreadWorker("entry/ets/workers/CocosWorker.ets", {
type: "classic",
name: "CocosWorker"
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"abilities": [
{
"name": "MainAbility",
"srcEntry": "./ets/MainAbility/MainAbility.ts",
"srcEntry": "./ets/MainAbility/MainAbility.ets",
"description": "$string:MainAbility_desc",
"icon": "$media:icon",
"label": "$string:MainAbility_label",
Expand All @@ -33,7 +33,7 @@
}
],
// https://docs.openharmony.cn/pages/v4.0/zh-cn/application-dev/quick-start/module-configuration-file.md/
"supportWindowMode": ["fullscreen"],
"supportWindowMode": ["fullscreen"],
"maxWindowWidth": 1080,
"minWindowWidth": 1080,
"maxWindowHeight": 720,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ export { NapiHelper } from './src/main/ets/napi/NapiHelper'

export { JumpManager } from './src/main/ets/system/appJump/JumpManager'
export { DeviceUtils } from './src/main/ets/system/device/DeviceUtils'
export { ApplicationManager } from './src/main/ets/system/application/ApplicationManager'
export { ApplicationManager } from './src/main/ets/system/application/ApplicationManager'
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"ohos": {
"org": ""
},
"main": "index.ts",
"main": "index.ets",
"type": "module",
"version": "1.0.0",
"dependencies": {}
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,5 @@ 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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function showDialog(dialogTitle: string, dialogMessage: string) {
color: '#000000'
},
],
success: function(data) {
success: (data) => {
log.debug("handling callback, data:%{public}s", data);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { DialogMsgEntity } from '../../entity/WorkerMsgEntity';

export class Dialog {
static MODULE_NAME : string = 'Dialog';
static workerPort;
static workerPort: ThreadWorkerGlobalScope;

static init(workerPort: ThreadWorkerGlobalScope) : void {
Dialog.workerPort = workerPort;
Expand All @@ -26,4 +26,4 @@ export class Dialog {
let dialogMsgEntity: DialogMsgEntity = new DialogMsgEntity(Dialog.MODULE_NAME, 'hideTextInputDialog');
Dialog.workerPort.postMessage(dialogMsgEntity);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { ThreadWorkerGlobalScope } from '@ohos.worker';
import { Color4B, EditBoxMsgEntity, ViewRect } from '../../entity/WorkerMsgEntity';

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

private static workerPort;
private static workerPort : ThreadWorkerGlobalScope;

static init(workerPort) : 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;
private static workerPort : ThreadWorkerGlobalScope;

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

Expand Down
Loading