diff --git a/native/cocos/platform/openharmony/OpenHarmonyPlatform.cpp b/native/cocos/platform/openharmony/OpenHarmonyPlatform.cpp index dd4ebf2665..9e1857aa68 100644 --- a/native/cocos/platform/openharmony/OpenHarmonyPlatform.cpp +++ b/native/cocos/platform/openharmony/OpenHarmonyPlatform.cpp @@ -46,11 +46,11 @@ #include namespace { -void sendMsgToWorker(const cc::MessageType& type, OH_NativeXComponent* component, void* window) { +void sendMsgToWorker(const cc::MessageType& type, void* data, void* window) { cc::OpenHarmonyPlatform* platform = dynamic_cast(cc::BasePlatform::getPlatform()); CC_ASSERT(platform != nullptr); - cc::WorkerMessageData data{type, static_cast(component), window}; - platform->enqueue(data); + cc::WorkerMessageData msg{type, static_cast(data), window}; + platform->enqueue(msg); } void onSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { @@ -70,15 +70,35 @@ void onSurfaceCreatedCB(OH_NativeXComponent* component, void* window) { } void dispatchTouchEventCB(OH_NativeXComponent* component, void* window) { - sendMsgToWorker(cc::MessageType::WM_XCOMPONENT_TOUCH_EVENT, component, window); + OH_NativeXComponent_TouchEvent touchEvent; + int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent); + if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { + return; + } + // TODO(qgh):Is it possible to find an efficient way to do this, I thought about using a cache queue but it requires locking. + cc::TouchEvent* ev = new cc::TouchEvent; + ev->windowId = 1; + if (touchEvent.type == OH_NATIVEXCOMPONENT_DOWN) { + ev->type = cc::TouchEvent::Type::BEGAN; + } else if (touchEvent.type == OH_NATIVEXCOMPONENT_MOVE) { + ev->type = cc::TouchEvent::Type::MOVED; + } else if (touchEvent.type == OH_NATIVEXCOMPONENT_UP) { + ev->type = cc::TouchEvent::Type::ENDED; + } else if (touchEvent.type == OH_NATIVEXCOMPONENT_CANCEL) { + ev->type = cc::TouchEvent::Type::CANCELLED; + } + for(int i = 0; i < touchEvent.numPoints; ++i) { + ev->touches.emplace_back(touchEvent.touchPoints[i].x, touchEvent.touchPoints[i].y, touchEvent.touchPoints[i].id); + } + sendMsgToWorker(cc::MessageType::WM_XCOMPONENT_TOUCH_EVENT, reinterpret_cast(ev), window); } void onSurfaceChangedCB(OH_NativeXComponent* component, void* window) { - sendMsgToWorker(cc::MessageType::WM_XCOMPONENT_SURFACE_CHANGED, component, window); + sendMsgToWorker(cc::MessageType::WM_XCOMPONENT_SURFACE_CHANGED, reinterpret_cast(component), window); } void onSurfaceDestroyedCB(OH_NativeXComponent* component, void* window) { - sendMsgToWorker(cc::MessageType::WM_XCOMPONENT_SURFACE_DESTROY, component, window); + sendMsgToWorker(cc::MessageType::WM_XCOMPONENT_SURFACE_DESTROY, reinterpret_cast(component), window); } } // namespace @@ -157,16 +177,23 @@ void OpenHarmonyPlatform::onMessageCallback(const uv_async_t* /* req */) { } if ((msgData.type >= MessageType::WM_XCOMPONENT_SURFACE_CREATED) && (msgData.type <= MessageType::WM_XCOMPONENT_SURFACE_DESTROY)) { - OH_NativeXComponent* nativexcomponet = reinterpret_cast(msgData.data); - CC_ASSERT(nativexcomponet != nullptr); - - if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_CREATED) { + if (msgData.type == MessageType::WM_XCOMPONENT_TOUCH_EVENT) { + TouchEvent* ev = reinterpret_cast(msgData.data); + CC_ASSERT(ev != nullptr); + events::Touch::broadcast(*ev); + delete ev; + ev = nullptr; + } else if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_CREATED) { + OH_NativeXComponent* nativexcomponet = reinterpret_cast(msgData.data); + CC_ASSERT(nativexcomponet != nullptr); platform->onSurfaceCreated(nativexcomponet, msgData.window); - } else if (msgData.type == MessageType::WM_XCOMPONENT_TOUCH_EVENT) { - platform->dispatchTouchEvent(nativexcomponet, msgData.window); } else if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_CHANGED) { + OH_NativeXComponent* nativexcomponet = reinterpret_cast(msgData.data); + CC_ASSERT(nativexcomponet != nullptr); platform->onSurfaceChanged(nativexcomponet, msgData.window); } else if (msgData.type == MessageType::WM_XCOMPONENT_SURFACE_DESTROY) { + OH_NativeXComponent* nativexcomponet = reinterpret_cast(msgData.data); + CC_ASSERT(nativexcomponet != nullptr); platform->onSurfaceDestroyed(nativexcomponet, msgData.window); } else { CC_ASSERT(false); @@ -248,31 +275,6 @@ void OpenHarmonyPlatform::onSurfaceDestroyed(OH_NativeXComponent* component, voi systemWindowIntf->setWindowHandle(nullptr); } -void OpenHarmonyPlatform::dispatchTouchEvent(OH_NativeXComponent* component, void* window) { - OH_NativeXComponent_TouchEvent touchEvent; - int32_t ret = OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent); - if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) { - return; - } - - TouchEvent ev; - ev.windowId = 1; - if (touchEvent.type == OH_NATIVEXCOMPONENT_DOWN) { - ev.type = cc::TouchEvent::Type::BEGAN; - } else if (touchEvent.type == OH_NATIVEXCOMPONENT_MOVE) { - ev.type = cc::TouchEvent::Type::MOVED; - } else if (touchEvent.type == OH_NATIVEXCOMPONENT_UP) { - ev.type = cc::TouchEvent::Type::ENDED; - } else if (touchEvent.type == OH_NATIVEXCOMPONENT_CANCEL) { - ev.type = cc::TouchEvent::Type::CANCELLED; - } - for(int i = 0; i < touchEvent.numPoints; ++i) { - ev.touches.emplace_back(touchEvent.touchPoints[i].x, touchEvent.touchPoints[i].y, touchEvent.touchPoints[i].id); - } - - events::Touch::broadcast(ev); -} - ISystemWindow *OpenHarmonyPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) { SystemWindow* window = ccnew SystemWindow(windowId, externalHandle); uint64_t width = 0; diff --git a/native/cocos/platform/openharmony/OpenHarmonyPlatform.h b/native/cocos/platform/openharmony/OpenHarmonyPlatform.h index a22eb23140..fdbba01164 100644 --- a/native/cocos/platform/openharmony/OpenHarmonyPlatform.h +++ b/native/cocos/platform/openharmony/OpenHarmonyPlatform.h @@ -67,7 +67,6 @@ public: void onSurfaceCreated(OH_NativeXComponent* component, void* window); void onSurfaceChanged(OH_NativeXComponent* component, void* window); void onSurfaceDestroyed(OH_NativeXComponent* component, void* window); - void dispatchTouchEvent(OH_NativeXComponent* component, void* window); static void onMessageCallback(const uv_async_t* req); static void timerCb(uv_timer_t* handle);