Skip to content

Cocos2d x 2.1.4 oh #20893

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

Merged
merged 9 commits into from
May 7, 2025
Merged
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 cocos2dx/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
51 changes: 51 additions & 0 deletions cocos2dx/platform/ohos/napi/modules/MouseNapi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Created on 2024/01/05.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include <js_native_api.h>
#include <js_native_api_types.h>
#include "MouseNapi.h"
#include "platform/ohos/napi/plugin_manager.h"
#include "../../CCLogOhos.h"
#include "ui/UIEditBox/UIEditBoxImpl-ohos.h"
#include "base/CCIMEDispatcher.h"
#include "platform/ohos/napi/render/plugin_render.h"
#include <string>

napi_value MouseNapi::mouseWheelCB(napi_env env, napi_callback_info info) {
napi_status status;
size_t argc = 2;
napi_value args[2];
NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr));
if (argc != 2) {
napi_throw_type_error(env, NULL, "Wrong number of arguments");
return nullptr;
}
napi_valuetype valuetype;
status = napi_typeof(env, args[0], &valuetype);
if (status != napi_ok) {
return nullptr;
}
if (valuetype != napi_string) {
napi_throw_type_error(env, NULL, "Wrong arguments");
return nullptr;
}

status = napi_typeof(env, args[1], &valuetype);
if (status != napi_ok) {
return nullptr;
}
if (valuetype != napi_number) {
napi_throw_type_error(env, NULL, "Wrong arguments");
return nullptr;
}
size_t pInt;
char eventType[256];
NAPI_CALL(env, napi_get_value_string_utf8(env, args[0], eventType, 256, &pInt));
double scrollY;
NAPI_CALL(env, napi_get_value_double(env, args[1], &scrollY));
PluginRender::MouseWheelCB(eventType, scrollY);
return nullptr;
}
18 changes: 18 additions & 0 deletions cocos2dx/platform/ohos/napi/modules/MouseNapi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Created on 2024/01/05.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef MyApplication_MouseNapi_H
#define MyApplication_MouseNapi_H

#include <string>
#include <napi/native_api.h>

class MouseNapi {
public:
static napi_value mouseWheelCB(napi_env env, napi_callback_info info);
};

#endif //MyApplication_MouseNapi_H
7 changes: 6 additions & 1 deletion cocos2dx/platform/ohos/napi/plugin_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 @@ -180,6 +184,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
2 changes: 1 addition & 1 deletion samples/Cpp/TestCpp/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 samples/Cpp/TestCpp/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_YsqqXgi_-vGu2MmS-XEvj0Qe99Edur1WB7XCSIQzA4g=.cer",
"storePassword": "0000001BED65E7E69E75A6E8718A9A34837AD311857FB4848103433F9D2E37E097304FD5B3F6F343EB52BB",
"certpath": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cC8pZN1-zB3QuF67fWoCo0LyOU0uiMb3BLLKA5aZWeE=.cer",
"keyAlias": "debugKey",
"keyPassword": "0000001B41912C35007D0C5131CB9693F5EB89D9AFDF428A10E358BF40774BEC08CE2E14BE6BF19BEDB97B",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_YsqqXgi_-vGu2MmS-XEvj0Qe99Edur1WB7XCSIQzA4g=.p7b",
"keyPassword": "0000001AE84C1F38E78F0D68D15FF84C5C34539A3902E3605BA1AA3E0716FC9C16BD2018DAD5296DF6FC",
"profile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cC8pZN1-zB3QuF67fWoCo0LyOU0uiMb3BLLKA5aZWeE=.p7b",
"signAlg": "SHA256withECDSA",
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_YsqqXgi_-vGu2MmS-XEvj0Qe99Edur1WB7XCSIQzA4g=.p12"
"storeFile": "C:\\Users\\cocos\\.ohos\\config\\default_proj.ohos_cC8pZN1-zB3QuF67fWoCo0LyOU0uiMb3BLLKA5aZWeE=.p12",
"storePassword": "0000001AC7FD8DE631ADEDF00A9231852223654FC07DD13938A89DE7BA4B92BD16B0B1C863D114CD5A55"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion samples/Cpp/TestCpp/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 @@ -12,7 +12,7 @@ export struct CocosVideoPlayer {
Video({
src: this.videoPlayerInfo.isUrl ? this.videoPlayerInfo.url : this.videoPlayerInfo.rawfile,
controller: this.videoPlayerInfo.controller
}).position({ x: this.videoPlayerInfo.x, y: this.videoPlayerInfo.y })
})
.width(this.videoPlayerInfo.w)
.height(this.videoPlayerInfo.h)
.visibility(this.videoPlayerInfo.visible ? Visibility.Visible : Visibility.None)
Expand Down
Loading