From 429f51bd4a2a2f77ef9448781012fea449a45e0d Mon Sep 17 00:00:00 2001 From: duanaoqi Date: Mon, 15 Jan 2024 16:37:36 +0800 Subject: [PATCH 1/5] =?UTF-8?q?cocos2d-x-3.0=20openHarmony=E9=BC=A0?= =?UTF-8?q?=E6=A0=87=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: duanaoqi --- cocos/2d/CMakeLists.txt | 1 + .../platform/ohos/napi/WorkerMessageQueue.h | 2 + .../platform/ohos/napi/modules/MouseNapi.cpp | 49 ++++++++ .../2d/platform/ohos/napi/modules/MouseNapi.h | 18 +++ .../2d/platform/ohos/napi/plugin_manager.cpp | 11 ++ .../ohos/napi/render/plugin_render.cpp | 115 ++++++++++++++++++ .../platform/ohos/napi/render/plugin_render.h | 14 +++ .../src/main/cpp/types/libentry/index.d.ts | 1 + .../entry/src/main/ets/pages/Index.ets | 21 ++++ .../entry/src/main/ets/workers/CocosWorker.ts | 4 + .../src/main/ets/common/Constants.ts | 1 + 11 files changed, 237 insertions(+) create mode 100644 cocos/2d/platform/ohos/napi/modules/MouseNapi.cpp create mode 100644 cocos/2d/platform/ohos/napi/modules/MouseNapi.h diff --git a/cocos/2d/CMakeLists.txt b/cocos/2d/CMakeLists.txt index b1af86e2fb..3168fed102 100644 --- a/cocos/2d/CMakeLists.txt +++ b/cocos/2d/CMakeLists.txt @@ -41,6 +41,7 @@ elseif(OHOS) platform/ohos/napi/modules/RawFileUtils.cpp platform/ohos/napi/modules/TouchesNapi.cpp + platform/ohos/napi/modules/MouseNapi.cpp platform/ohos/napi/modules/InputNapi.cpp platform/ohos/napi/modules/WebViewNapi.cpp platform/ohos/napi/modules/SensorNapi.cpp diff --git a/cocos/2d/platform/ohos/napi/WorkerMessageQueue.h b/cocos/2d/platform/ohos/napi/WorkerMessageQueue.h index 35dece20ac..b8eded0332 100644 --- a/cocos/2d/platform/ohos/napi/WorkerMessageQueue.h +++ b/cocos/2d/platform/ohos/napi/WorkerMessageQueue.h @@ -34,6 +34,8 @@ enum class MessageType { WM_XCOMPONENT_SURFACE_CREATED = 0, WM_XCOMPONENT_TOUCH_EVENT, WM_XCOMPONENT_KEY_EVENT, + WM_XCOMPONENT_MOUSE_EVENT, + WM_XCOMPONENT_MOUSE_WHEEL_EVENT, WM_XCOMPONENT_SURFACE_CHANGED, WM_XCOMPONENT_SURFACE_DESTROY, WM_APP_SHOW, diff --git a/cocos/2d/platform/ohos/napi/modules/MouseNapi.cpp b/cocos/2d/platform/ohos/napi/modules/MouseNapi.cpp new file mode 100644 index 0000000000..e44a360e4a --- /dev/null +++ b/cocos/2d/platform/ohos/napi/modules/MouseNapi.cpp @@ -0,0 +1,49 @@ +// +// 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 +#include +#include "MouseNapi.h" +#include "platform/ohos/napi/plugin_manager.h" +#include "../../CCLogOhos.h" +#include "platform/ohos/napi/render/plugin_render.h" +#include + +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; +} \ No newline at end of file diff --git a/cocos/2d/platform/ohos/napi/modules/MouseNapi.h b/cocos/2d/platform/ohos/napi/modules/MouseNapi.h new file mode 100644 index 0000000000..39971a4da0 --- /dev/null +++ b/cocos/2d/platform/ohos/napi/modules/MouseNapi.h @@ -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 +#include + +class MouseNapi { +public: + static napi_value mouseWheelCB(napi_env env, napi_callback_info info); +}; + +#endif //MyApplication_MouseNapi_H \ No newline at end of file diff --git a/cocos/2d/platform/ohos/napi/plugin_manager.cpp b/cocos/2d/platform/ohos/napi/plugin_manager.cpp index 0db8247b69..a53457461e 100644 --- a/cocos/2d/platform/ohos/napi/plugin_manager.cpp +++ b/cocos/2d/platform/ohos/napi/plugin_manager.cpp @@ -21,6 +21,7 @@ #include "modules/RawFileUtils.h" #include "modules/InputNapi.h" +#include "modules/MouseNapi.h" #include "modules/WebViewNapi.h" #include "modules/SensorNapi.h" #include "modules/VideoPlayerNapi.h" @@ -41,6 +42,7 @@ enum ContextType { WORKER_INIT, NATIVE_API, INPUT_NAPI, + MOUSE_NAPI, WEBVIEW_NAPI, VIDEOPLAYER_NAPI, SENSOR_API @@ -145,6 +147,15 @@ napi_value NapiManager::GetContext(napi_env env, napi_callback_info info) NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); } break; + case MOUSE_NAPI: + { + OHOS_LOGD("NapiManager::GetContext MOUSE_NAPI"); + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("mouseWheelCB", MouseNapi::mouseWheelCB), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + } + break; case WEBVIEW_NAPI: { OHOS_LOGD("NapiManager::GetContext WEBVIEW_NAPI"); diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp index 0b3d902556..53341f9495 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp @@ -32,10 +32,19 @@ extern "C" { PluginRender* PluginRender::instance_ = nullptr; OH_NativeXComponent_Callback PluginRender::callback_; +OH_NativeXComponent_MouseEvent_Callback PluginRender::mouseCallback_; std::queue PluginRender::touchEventQueue_; std::queue PluginRender::keyEventQueue_; +std::queue PluginRender::mouseEventQueue_; +std::queue PluginRender::mouseWheelEventQueue_; uint64_t PluginRender::animationInterval_ = 16; uint64_t PluginRender::lastTime = 0; +// mouse event +float mousePositionX = -1; +float mousePositionY = -1; +bool isMouseLeftActive = false; +double scrollDistance = 0; +// key event const int keyCodeUnknownInOH = -1; const int keyActionUnknownInOH = -1; @@ -111,6 +120,23 @@ void OnSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) PluginRender::GetInstance()->sendMsgToWorker(MessageType::WM_XCOMPONENT_SURFACE_DESTROY, component, window); } +void DispatchMouseEventCB(OH_NativeXComponent* component, void* window) +{ + OH_NativeXComponent_MouseEvent mouseEvent; + int32_t ret = OH_NativeXComponent_GetMouseEvent(component, window, &mouseEvent); + if (ret == OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + PluginRender::mouseEventQueue_.push(mouseEvent); + PluginRender::GetInstance()->sendMsgToWorker(MessageType::WM_XCOMPONENT_MOUSE_EVENT, component, window); + } else { + OHOS_LOGE("OpenHarmonyPlatform::getMouseEventError"); + } +} + +void DispatchHoverEventCB(OH_NativeXComponent* component, bool isHover) +{ + OHOS_LOGD("OpenHarmonyPlatform::DispatchHoverEventCB"); +} + void DispatchKeyEventCB(OH_NativeXComponent* component, void* window) { OH_NativeXComponent_KeyEvent* keyEvent; @@ -185,6 +211,10 @@ void PluginRender::onMessageCallback(const uv_async_t* /* req */) { render->DispatchKeyEvent(nativexcomponet, msgData.window); } else if (msgData.type == MessageType::WM_XCOMPONENT_TOUCH_EVENT) { render->DispatchTouchEvent(nativexcomponet, msgData.window); + } else if (msgData.type == MessageType::WM_XCOMPONENT_MOUSE_EVENT) { + render->DispatchMouseEvent(nativexcomponet, msgData.window); + } else if (msgData.type == MessageType::WM_XCOMPONENT_MOUSE_WHEEL_EVENT) { + render->DispatchMouseWheelEvent(); } else if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_CHANGED) { render->OnSurfaceChanged(nativexcomponet, msgData.window); } else if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_DESTROY) { @@ -237,6 +267,10 @@ void PluginRender::SetNativeXComponent(OH_NativeXComponent* component) OH_NativeXComponent_RegisterCallback(component_, &PluginRender::callback_); // register keyEvent OH_NativeXComponent_RegisterKeyEventCallback(component_, DispatchKeyEventCB); + // register mouseEvent + PluginRender::mouseCallback_.DispatchMouseEvent = DispatchMouseEventCB; + PluginRender::mouseCallback_.DispatchHoverEvent = DispatchHoverEventCB; + OH_NativeXComponent_RegisterMouseEventCallback(component_, &mouseCallback_); } void PluginRender::workerInit(napi_env env, uv_loop_t* loop) { @@ -324,6 +358,70 @@ void PluginRender::DispatchKeyEvent(OH_NativeXComponent* component, void* window } } +void PluginRender::DispatchMouseEvent(OH_NativeXComponent* component, void* window) +{ + OH_NativeXComponent_MouseEvent mouseEvent; + while (!mouseEventQueue_.empty()) { + mouseEvent = mouseEventQueue_.front(); + mouseEventQueue_.pop(); + EventMouse::MouseEventType mouseAction; + mousePositionX = mouseEvent.x; + mousePositionY = mouseEvent.y; + switch (mouseEvent.action) { + case 1: + mouseAction = EventMouse::MouseEventType::MOUSE_DOWN; + break; + case 2: + mouseAction = EventMouse::MouseEventType::MOUSE_UP; + break; + case 3: + mouseAction = EventMouse::MouseEventType::MOUSE_MOVE; + break; + default: + mouseAction = EventMouse::MouseEventType::MOUSE_NONE; + break; + } + EventMouse::MouseButton mouseButton; + switch (mouseEvent.button) { + case 1: + mouseButton = EventMouse::MouseButton::BUTTON_LEFT; + break; + case 2: + mouseButton = EventMouse::MouseButton::BUTTON_RIGHT; + break; + case 4: + mouseButton = EventMouse::MouseButton::BUTTON_MIDDLE; + break; + default: + mouseButton = EventMouse::MouseButton::BUTTON_UNSET; + break; + } + if (mouseEvent.action == 1 && mouseEvent.button == 1) { + isMouseLeftActive = true; + } + if (mouseEvent.action == 2 && mouseEvent.button == 1) { + isMouseLeftActive = false; + } + EventMouse event(mouseAction); + event.setCursorPosition(mouseEvent.x, mouseEvent.y); + event.setMouseButton(mouseButton); + Director::getInstance()->getEventDispatcher()->dispatchEvent(&event); + } +} + +void PluginRender::DispatchMouseWheelEvent() +{ + EventMouseWheelData mouseWheelData; + while (!mouseWheelEventQueue_.empty()) { + mouseWheelData = mouseWheelEventQueue_.front(); + mouseWheelEventQueue_.pop(); + EventMouse mouseWheelEvent(cocos2d::EventMouse::MouseEventType::MOUSE_SCROLL); + mouseWheelEvent.setScrollData(0, -mouseWheelData.scrollY); + mouseWheelEvent.setCursorPosition(mouseWheelData.positonX, mouseWheelData.positonY); + Director::getInstance()->getEventDispatcher()->dispatchEvent(&mouseWheelEvent); + } +} + void PluginRender::DispatchTouchEvent(OH_NativeXComponent* component, void* window) { OH_NativeXComponent_TouchEvent* touchEvent; @@ -365,6 +463,23 @@ void PluginRender::DispatchTouchEvent(OH_NativeXComponent* component, void* wind } } +void PluginRender::MouseWheelCB(std::string eventType, double scrollY) +{ + if (isMouseLeftActive) { + return; + } + if (eventType == "actionEnd") { + scrollDistance = 0; + } + if (eventType == "actionUpdate") { + double moveScrollY = scrollY - scrollDistance; + scrollDistance = scrollY; + PluginRender::EventMouseWheelData mouseWheelData{mousePositionX, mousePositionY, moveScrollY}; + PluginRender::mouseWheelEventQueue_.push(mouseWheelData); + PluginRender::GetInstance()->sendMsgToWorker(MessageType::WM_XCOMPONENT_MOUSE_WHEEL_EVENT, nullptr, nullptr); + } +} + void PluginRender::OnCreateNative(napi_env env, uv_loop_t* loop) { OHOS_LOGD("PluginRender::OnCreateNative"); } diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.h b/cocos/2d/platform/ohos/napi/render/plugin_render.h index bf4eb67553..15d9308eaa 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.h +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.h @@ -56,6 +56,8 @@ public: static napi_value NapiChangeColor(napi_env env, napi_callback_info info); static napi_value NapiChangeColorWorker(napi_env env, napi_callback_info info); + static void MouseWheelCB(std::string eventType, double scrollY); + // Callback, called by ACE XComponent void OnSurfaceCreated(OH_NativeXComponent* component, void* window); @@ -63,20 +65,32 @@ public: void OnSurfaceDestroyed(OH_NativeXComponent* component, void* window); + void DispatchMouseEvent(OH_NativeXComponent* component, void* window); + void DispatchKeyEvent(OH_NativeXComponent* component, void* window); void DispatchTouchEvent(OH_NativeXComponent* component, void* window); + void DispatchMouseWheelEvent(); + void OnCreateNative(napi_env env, uv_loop_t* loop); void OnShowNative(); void OnHideNative(); void OnDestroyNative(); public: + struct EventMouseWheelData { + float positonX; + double scrollY; + float positonY; + }; static PluginRender* instance_; static OH_NativeXComponent_Callback callback_; + static OH_NativeXComponent_MouseEvent_Callback mouseCallback_; static std::queue touchEventQueue_; static std::queue keyEventQueue_; + static std::queue mouseEventQueue_; + static std::queue mouseWheelEventQueue_; OH_NativeXComponent* component_{nullptr}; uv_timer_t timerHandle_; diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts b/tests/cpp-tests/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts index a5bc5c8db6..8c059ca83e 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts +++ b/tests/cpp-tests/proj.ohos/entry/src/main/cpp/types/libentry/index.d.ts @@ -14,6 +14,7 @@ export interface CPPFunctions { nativeEngineStart: () => void; registerFunction: () => void; initAsyncInfo: () => void; + mouseWheelCB: (eventType: string, scrollY : number) => void; editBoxOnChangeCB: (viewTag: number, text: string) => void; editBoxOnEnterCB: (viewTag: number, text: string) => void; textFieldTTFOnChangeCB: (text: string) => void; diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets index 96759d9166..a61e911bfe 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -12,6 +12,7 @@ import { CocosVideoPlayer } from '../components/CocosVideoPlayer' import { TextInputDialog } from '../components/TextInputDialog' import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" const nativePageLifecycle:nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE); +import deviceInfo from '@ohos.deviceInfo' @Entry @Component @@ -42,6 +43,8 @@ struct Index { alignment: DialogAlignment.Bottom, customStyle: true, }) + // PanGesture + private panOption: PanGestureOptions = new PanGestureOptions({ direction: PanDirection.Up | PanDirection.Down }); onPageShow() { console.log('[LIFECYCLE-Page] onPageShow'); @@ -68,6 +71,10 @@ struct Index { return true; } + onMouseWheel(eventType: string, scrollY: number) { + cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); + } + build() { Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { XComponent({ @@ -77,6 +84,20 @@ struct Index { controller: this.xcomponentController }) .focusable(true) + .gesture( + PanGesture(this.panOption) + .onActionStart(() => { + this.onMouseWheel("actionStart", 0); + }) + .onActionUpdate((event: GestureEvent) => { + if (deviceInfo.deviceType === '2in1') { + this.onMouseWheel("actionUpdate", event.offsetY); + } + }) + .onActionEnd(() => { + this.onMouseWheel("actionEnd", 0); + }) + ) .onLoad((context) => { console.log('[cocos] XComponent.onLoad Callback'); this.cocosWorker.postMessage({ type: "onXCLoad", data: "XComponent" }); diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts index 327f0f94ad..d8fa6f5758 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/workers/CocosWorker.ts @@ -28,6 +28,7 @@ import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" const appLifecycle: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.APP_LIFECYCLE); const workerContext: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.WORKER_INIT); const inputNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.INPUT_NAPI); +const mouseNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.MOUSE_NAPI); const webViewNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.WEBVIEW_NAPI); const videoPlayNapi: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.VIDEOPLAYER_NAPI); const napiContext: nativeRender.CPPFunctions = nativeRender.getContext(ContextType.NATIVE_API); @@ -63,6 +64,9 @@ workerPort.onmessage = function(e) : void { case "textFieldTTFOnChange": inputNapi.textFieldTTFOnChangeCB(data.data); break; + case "onMouseWheel": + mouseNapi.mouseWheelCB(data.eventType, data.scrollY); + break; case "onPageBegin": webViewNapi.shouldStartLoading(data.viewTag, data.url); break; diff --git a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts index f6c4b10f63..0df8b27ed0 100644 --- a/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts +++ b/tests/cpp-tests/proj.ohos/libSysCapabilities/src/main/ets/common/Constants.ts @@ -19,6 +19,7 @@ export enum ContextType { WORKER_INIT, NATIVE_API, INPUT_NAPI, + MOUSE_NAPI, WEBVIEW_NAPI, VIDEOPLAYER_NAPI, SENSOR_API -- Gitee From 00093e6a8f5c0f58d88464c0e5d39db6dd8b9e43 Mon Sep 17 00:00:00 2001 From: duanaoqi Date: Mon, 15 Jan 2024 19:52:10 +0800 Subject: [PATCH 2/5] =?UTF-8?q?cocos2d-x-3.0=20openHarmony=E9=BC=A0?= =?UTF-8?q?=E6=A0=87=E9=80=82=E9=85=8D=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: duanaoqi --- cocos/2d/platform/ohos/napi/render/plugin_render.cpp | 10 +++++----- cocos/2d/platform/ohos/napi/render/plugin_render.h | 2 +- tests/cpp-tests/Classes/controller.cpp | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp index 53341f9495..ede94bb0e9 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp @@ -24,6 +24,7 @@ #include "../helper/NapiHelper.h" #include "2d/platform/ohos/CCLogOhos.h" #include "cocos2d.h" +#include "2d/CCEventMouse.h" using namespace cocos2d; #ifdef __cplusplus @@ -381,19 +382,18 @@ void PluginRender::DispatchMouseEvent(OH_NativeXComponent* component, void* wind mouseAction = EventMouse::MouseEventType::MOUSE_NONE; break; } - EventMouse::MouseButton mouseButton; + int mouseButton; switch (mouseEvent.button) { case 1: - mouseButton = EventMouse::MouseButton::BUTTON_LEFT; + mouseButton = MOUSE_BUTTON_LEFT; break; case 2: - mouseButton = EventMouse::MouseButton::BUTTON_RIGHT; + mouseButton = MOUSE_BUTTON_RIGHT; break; case 4: - mouseButton = EventMouse::MouseButton::BUTTON_MIDDLE; + mouseButton = MOUSE_BUTTON_MIDDLE; break; default: - mouseButton = EventMouse::MouseButton::BUTTON_UNSET; break; } if (mouseEvent.action == 1 && mouseEvent.button == 1) { diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.h b/cocos/2d/platform/ohos/napi/render/plugin_render.h index 15d9308eaa..cd85aed6c0 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.h +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.h @@ -81,8 +81,8 @@ public: public: struct EventMouseWheelData { float positonX; - double scrollY; float positonY; + double scrollY; }; static PluginRender* instance_; static OH_NativeXComponent_Callback callback_; diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index bc41a91c94..642d7cef51 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -230,7 +230,7 @@ void TestController::onTouchMoved(Touch* touch, Event *event) void TestController::onMouseScroll(Event *event) { auto mouseEvent = static_cast(event); - float nMoveY = mouseEvent->getScrollY() * 6; + float nMoveY = mouseEvent->getScrollY() * 0.2; auto curPos = _itemMenu->getPosition(); auto nextPos = Point(curPos.x, curPos.y + nMoveY); -- Gitee From e20ff3744a43a9e92040b5ccadfed3fab99b8316 Mon Sep 17 00:00:00 2001 From: duanaoqi Date: Mon, 15 Jan 2024 19:52:10 +0800 Subject: [PATCH 3/5] =?UTF-8?q?cocos2d-x-3.0=20openHarmony=E9=BC=A0?= =?UTF-8?q?=E6=A0=87=E9=80=82=E9=85=8D=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: duanaoqi --- cocos/2d/platform/ohos/napi/render/plugin_render.cpp | 10 +++++----- cocos/2d/platform/ohos/napi/render/plugin_render.h | 2 +- tests/cpp-tests/Classes/controller.cpp | 2 +- .../proj.ohos/entry/src/main/ets/pages/Index.ets | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp index 53341f9495..ede94bb0e9 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp @@ -24,6 +24,7 @@ #include "../helper/NapiHelper.h" #include "2d/platform/ohos/CCLogOhos.h" #include "cocos2d.h" +#include "2d/CCEventMouse.h" using namespace cocos2d; #ifdef __cplusplus @@ -381,19 +382,18 @@ void PluginRender::DispatchMouseEvent(OH_NativeXComponent* component, void* wind mouseAction = EventMouse::MouseEventType::MOUSE_NONE; break; } - EventMouse::MouseButton mouseButton; + int mouseButton; switch (mouseEvent.button) { case 1: - mouseButton = EventMouse::MouseButton::BUTTON_LEFT; + mouseButton = MOUSE_BUTTON_LEFT; break; case 2: - mouseButton = EventMouse::MouseButton::BUTTON_RIGHT; + mouseButton = MOUSE_BUTTON_RIGHT; break; case 4: - mouseButton = EventMouse::MouseButton::BUTTON_MIDDLE; + mouseButton = MOUSE_BUTTON_MIDDLE; break; default: - mouseButton = EventMouse::MouseButton::BUTTON_UNSET; break; } if (mouseEvent.action == 1 && mouseEvent.button == 1) { diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.h b/cocos/2d/platform/ohos/napi/render/plugin_render.h index 15d9308eaa..cd85aed6c0 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.h +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.h @@ -81,8 +81,8 @@ public: public: struct EventMouseWheelData { float positonX; - double scrollY; float positonY; + double scrollY; }; static PluginRender* instance_; static OH_NativeXComponent_Callback callback_; diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index bc41a91c94..642d7cef51 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -230,7 +230,7 @@ void TestController::onTouchMoved(Touch* touch, Event *event) void TestController::onMouseScroll(Event *event) { auto mouseEvent = static_cast(event); - float nMoveY = mouseEvent->getScrollY() * 6; + float nMoveY = mouseEvent->getScrollY() * 0.2; auto curPos = _itemMenu->getPosition(); auto nextPos = Point(curPos.x, curPos.y + nMoveY); diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets index a61e911bfe..25bbb0da5c 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -72,7 +72,7 @@ struct Index { } onMouseWheel(eventType: string, scrollY: number) { - cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); + this.cocosWorker.postMessage({ type: "onMouseWheel", eventType: eventType, scrollY: scrollY }); } build() { -- Gitee From 36e5c00577f6da231c54030d886ddbf87b9eed1d Mon Sep 17 00:00:00 2001 From: duanaoqi Date: Wed, 13 Nov 2024 16:54:50 +0800 Subject: [PATCH 4/5] =?UTF-8?q?#fix:=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: duanaoqi --- cocos/2d/platform/ohos/napi/modules/MouseNapi.h | 9 +-------- .../platform/ohos/napi/render/plugin_render.cpp | 16 ++++++++-------- tests/cpp-tests/Classes/controller.cpp | 7 ++++++- .../proj.ohos/entry/src/main/ets/pages/Index.ets | 4 ++-- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cocos/2d/platform/ohos/napi/modules/MouseNapi.h b/cocos/2d/platform/ohos/napi/modules/MouseNapi.h index 39971a4da0..59342ce600 100644 --- a/cocos/2d/platform/ohos/napi/modules/MouseNapi.h +++ b/cocos/2d/platform/ohos/napi/modules/MouseNapi.h @@ -1,11 +1,6 @@ // // 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 #include @@ -13,6 +8,4 @@ class MouseNapi { public: static napi_value mouseWheelCB(napi_env env, napi_callback_info info); -}; - -#endif //MyApplication_MouseNapi_H \ No newline at end of file +}; \ No newline at end of file diff --git a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp index ede94bb0e9..17e57024ef 100644 --- a/cocos/2d/platform/ohos/napi/render/plugin_render.cpp +++ b/cocos/2d/platform/ohos/napi/render/plugin_render.cpp @@ -369,13 +369,13 @@ void PluginRender::DispatchMouseEvent(OH_NativeXComponent* component, void* wind mousePositionX = mouseEvent.x; mousePositionY = mouseEvent.y; switch (mouseEvent.action) { - case 1: + case OH_NATIVEXCOMPONENT_MOUSE_PRESS: mouseAction = EventMouse::MouseEventType::MOUSE_DOWN; break; - case 2: + case OH_NATIVEXCOMPONENT_MOUSE_RELEASE: mouseAction = EventMouse::MouseEventType::MOUSE_UP; break; - case 3: + case OH_NATIVEXCOMPONENT_MOUSE_MOVE: mouseAction = EventMouse::MouseEventType::MOUSE_MOVE; break; default: @@ -384,22 +384,22 @@ void PluginRender::DispatchMouseEvent(OH_NativeXComponent* component, void* wind } int mouseButton; switch (mouseEvent.button) { - case 1: + case OH_NATIVEXCOMPONENT_LEFT_BUTTON: mouseButton = MOUSE_BUTTON_LEFT; break; - case 2: + case OH_NATIVEXCOMPONENT_RIGHT_BUTTON: mouseButton = MOUSE_BUTTON_RIGHT; break; - case 4: + case OH_NATIVEXCOMPONENT_MIDDLE_BUTTON: mouseButton = MOUSE_BUTTON_MIDDLE; break; default: break; } - if (mouseEvent.action == 1 && mouseEvent.button == 1) { + if (mouseEvent.action == OH_NATIVEXCOMPONENT_MOUSE_PRESS && mouseEvent.button == OH_NATIVEXCOMPONENT_LEFT_BUTTON) { isMouseLeftActive = true; } - if (mouseEvent.action == 2 && mouseEvent.button == 1) { + if (mouseEvent.action == OH_NATIVEXCOMPONENT_MOUSE_RELEASE && mouseEvent.button == OH_NATIVEXCOMPONENT_LEFT_BUTTON) { isMouseLeftActive = false; } EventMouse event(mouseAction); diff --git a/tests/cpp-tests/Classes/controller.cpp b/tests/cpp-tests/Classes/controller.cpp index 642d7cef51..a765446c67 100644 --- a/tests/cpp-tests/Classes/controller.cpp +++ b/tests/cpp-tests/Classes/controller.cpp @@ -230,7 +230,12 @@ void TestController::onTouchMoved(Touch* touch, Event *event) void TestController::onMouseScroll(Event *event) { auto mouseEvent = static_cast(event); - float nMoveY = mouseEvent->getScrollY() * 0.2; + float nMoveY = mouseEvent->getScrollY(); + #if (CC_TARGET_PLATFORM == CC_PLATFORM_OHOS) + nMoveY *= 0.2; + #else + nMoveY *= 6; + #endif auto curPos = _itemMenu->getPosition(); auto nextPos = Point(curPos.x, curPos.y + nMoveY); diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets index 25bbb0da5c..dd6f553719 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -12,7 +12,6 @@ import { CocosVideoPlayer } from '../components/CocosVideoPlayer' import { TextInputDialog } from '../components/TextInputDialog' import { GlobalContext,GlobalContextConstants} from "@ohos/libSysCapabilities" const nativePageLifecycle:nativeRender.CPPFunctions = nativeRender.getContext(ContextType.JSPAGE_LIFECYCLE); -import deviceInfo from '@ohos.deviceInfo' @Entry @Component @@ -84,13 +83,14 @@ struct Index { controller: this.xcomponentController }) .focusable(true) + .focusOnTouch(true) .gesture( PanGesture(this.panOption) .onActionStart(() => { this.onMouseWheel("actionStart", 0); }) .onActionUpdate((event: GestureEvent) => { - if (deviceInfo.deviceType === '2in1') { + if (event.source !== SourceType.Mouse) { this.onMouseWheel("actionUpdate", event.offsetY); } }) -- Gitee From d44f9edb5fd5edbbab648f8ca56ae32016eb341e Mon Sep 17 00:00:00 2001 From: duanaoqi Date: Wed, 13 Nov 2024 17:55:23 +0800 Subject: [PATCH 5/5] =?UTF-8?q?#fix:=20=E6=BB=9A=E8=BD=AE=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=80=BB=E8=BE=91=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: duanaoqi --- tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets index dd6f553719..16d25eb5f5 100644 --- a/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets +++ b/tests/cpp-tests/proj.ohos/entry/src/main/ets/pages/Index.ets @@ -90,7 +90,7 @@ struct Index { this.onMouseWheel("actionStart", 0); }) .onActionUpdate((event: GestureEvent) => { - if (event.source !== SourceType.Mouse) { + if (event.source === SourceType.Mouse) { this.onMouseWheel("actionUpdate", event.offsetY); } }) -- Gitee