diff --git a/native/cocos/engine/Engine.cpp b/native/cocos/engine/Engine.cpp index d6898d15e4..bfec718130 100644 --- a/native/cocos/engine/Engine.cpp +++ b/native/cocos/engine/Engine.cpp @@ -113,6 +113,9 @@ int32_t Engine::init() { platform->setHandleEventCallback( std::bind(&Engine::handleEvent, this, std::placeholders::_1)); // NOLINT(modernize-avoid-bind) + platform->setHandleTouchEventCallback( + std::bind(&Engine::handleTouchEvent, this, std::placeholders::_1)); // NOLINT(modernize-avoid-bind) + se::ScriptEngine::getInstance()->addRegisterCallback(setCanvasCallback); return 0; } @@ -284,6 +287,11 @@ bool Engine::handleEvent(const OSEvent& ev) { return isHandled; } +bool Engine::handleTouchEvent(const TouchEvent& ev) { // NOLINT(readability-convert-member-functions-to-static) + cc::EventDispatcher::dispatchTouchEvent(ev); + return true; +} + Engine::SchedulerPtr Engine::getScheduler() const { return _scheduler; } diff --git a/native/cocos/engine/Engine.h b/native/cocos/engine/Engine.h index 38f7739248..720f602c9d 100644 --- a/native/cocos/engine/Engine.h +++ b/native/cocos/engine/Engine.h @@ -95,11 +95,16 @@ public: void removeEventCallback(OSEventType evtype) override; /** @brief Event handling callback. - @param evtype:event type. - @param evtype:event information. + @param ev:Abstract event. @return whether it's been handled. */ bool handleEvent(const OSEvent& ev); + /** + @brief Touch event handling callback. + @param ev:Touch event. + @return whether it's been handled. + */ + bool handleTouchEvent(const TouchEvent& ev); /** @brief Get engine scheduler. */ diff --git a/native/cocos/platform/BasePlatform.h b/native/cocos/platform/BasePlatform.h index 3a769df23e..0a5ef151c7 100644 --- a/native/cocos/platform/BasePlatform.h +++ b/native/cocos/platform/BasePlatform.h @@ -82,6 +82,12 @@ public: virtual void setHandleEventCallback(HandleEventCallback cb) = 0; + /** + * @brief Set touch event handling callback function. + */ + using HandleTouchEventCallback = std::function; + virtual void setHandleTouchEventCallback(HandleTouchEventCallback cb) = 0; + /** * @brief Set default event handling callback function. */ diff --git a/native/cocos/platform/IEventDispatch.h b/native/cocos/platform/IEventDispatch.h index dda0be40bd..e40f6f2c77 100644 --- a/native/cocos/platform/IEventDispatch.h +++ b/native/cocos/platform/IEventDispatch.h @@ -42,7 +42,7 @@ public: /** * Dispatch touch event interface. */ - virtual void dispatchTouchEvent(const OSEvent& ev) = 0; + virtual void dispatchTouchEvent(const TouchEvent& ev) = 0; }; } // namespace cc diff --git a/native/cocos/platform/SDLHelper.cpp b/native/cocos/platform/SDLHelper.cpp index 069345a78a..0933f3ef8d 100644 --- a/native/cocos/platform/SDLHelper.cpp +++ b/native/cocos/platform/SDLHelper.cpp @@ -279,21 +279,21 @@ void SDLHelper::dispatchSDLEvent(const SDL_Event &sdlEvent, bool *quit) { const SDL_TouchFingerEvent &event = sdlEvent.tfinger; touch.type = TouchEvent::Type::ENDED; touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)}; - _delegate->dispatchEvent(touch); + _delegate->dispatchTouchEvent(touch); break; } case SDL_FINGERDOWN: { const SDL_TouchFingerEvent &event = sdlEvent.tfinger; touch.type = TouchEvent::Type::BEGAN; touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)}; - _delegate->dispatchEvent(touch); + _delegate->dispatchTouchEvent(touch); break; } case SDL_FINGERMOTION: { const SDL_TouchFingerEvent &event = sdlEvent.tfinger; touch.type = TouchEvent::Type::MOVED; touch.touches = {TouchInfo(event.x, event.y, (int)event.fingerId)}; - _delegate->dispatchEvent(touch); + _delegate->dispatchTouchEvent(touch); break; } case SDL_KEYDOWN: { diff --git a/native/cocos/platform/UniversalPlatform.cpp b/native/cocos/platform/UniversalPlatform.cpp index fb7e805320..7514a46682 100644 --- a/native/cocos/platform/UniversalPlatform.cpp +++ b/native/cocos/platform/UniversalPlatform.cpp @@ -51,7 +51,12 @@ void UniversalPlatform::dispatchEvent(const OSEvent& ev) { } } -void UniversalPlatform::dispatchTouchEvent(const OSEvent& ev) { +void UniversalPlatform::dispatchTouchEvent(const TouchEvent& ev) { + if (_handleTouchEventCallback) { + _handleTouchEventCallback(ev); + } else { + dispatchEvent(ev); + } } void UniversalPlatform::handleDefaultEvent(const OSEvent& ev) { @@ -62,6 +67,10 @@ void UniversalPlatform::setHandleEventCallback(HandleEventCallback cb) { _handleEventCallback = cb; } +void UniversalPlatform::setHandleTouchEventCallback(HandleTouchEventCallback cb) { + _handleTouchEventCallback = cb; +} + void UniversalPlatform::setHandleDefaultEventCallback(HandleEventCallback cb) { _handleDefaultEventCallback = cb; } diff --git a/native/cocos/platform/UniversalPlatform.h b/native/cocos/platform/UniversalPlatform.h index 5bd63e8ba9..283404fbe1 100644 --- a/native/cocos/platform/UniversalPlatform.h +++ b/native/cocos/platform/UniversalPlatform.h @@ -44,6 +44,12 @@ public: * @brief Set the event handling callback. */ void setHandleEventCallback(HandleEventCallback cb) override; + + /** + * @brief Set the touch event handling callback. + */ + void setHandleTouchEventCallback(HandleTouchEventCallback cb) override; + /** * @brief Set the event to handle callbacks by default. */ @@ -55,7 +61,7 @@ public: /** * @brief Implement dispatch touch event interface. */ - void dispatchTouchEvent(const OSEvent& ev) override; + void dispatchTouchEvent(const TouchEvent& ev) override; /** * @brief Implement handle default event interface. */ @@ -108,6 +114,7 @@ private: int32_t _fps{60}; HandleEventCallback _handleEventCallback{nullptr}; + HandleTouchEventCallback _handleTouchEventCallback{nullptr}; HandleEventCallback _handleDefaultEventCallback{nullptr}; }; diff --git a/native/cocos/platform/interfaces/OSInterface.cpp b/native/cocos/platform/interfaces/OSInterface.cpp index 2ee677f353..7891e61485 100644 --- a/native/cocos/platform/interfaces/OSInterface.cpp +++ b/native/cocos/platform/interfaces/OSInterface.cpp @@ -42,10 +42,10 @@ void OSInterface::dispatchEvent(const OSEvent& ev) { } } -void OSInterface::dispatchTouchEvent(const OSEvent& ev) { +void OSInterface::dispatchTouchEvent(const TouchEvent& ev) { if (_evDispatchInterface) { _evDispatchInterface->dispatchTouchEvent(ev); } } -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/interfaces/OSInterface.h b/native/cocos/platform/interfaces/OSInterface.h index 45f928f6b3..60c12c4db7 100644 --- a/native/cocos/platform/interfaces/OSInterface.h +++ b/native/cocos/platform/interfaces/OSInterface.h @@ -51,21 +51,16 @@ public: /** @brief Dispatch event. - @param type : event type - @param type : event information + @param type : Abstract event */ void dispatchEvent(const OSEvent& ev); /** @brief Dispatch touch event. - @param type : event type + @param type : Touch event */ - void dispatchTouchEvent(const OSEvent& ev); - - //enum class InterfaceType { - //}; + void dispatchTouchEvent(const TouchEvent& ev); protected: - // InterfaceType _type; IEventDispatch* _evDispatchInterface{nullptr}; private: diff --git a/native/cocos/platform/ios/AppDelegateBridge.mm b/native/cocos/platform/ios/AppDelegateBridge.mm index c67a581c8a..34317a8e73 100644 --- a/native/cocos/platform/ios/AppDelegateBridge.mm +++ b/native/cocos/platform/ios/AppDelegateBridge.mm @@ -110,7 +110,7 @@ cc::IOSPlatform *_platform = nullptr; static_cast([touch locationInView:[touch view]].y), static_cast((intptr_t)touch)}); } - _platform->dispatchEvent(touchEvent); + _platform->dispatchTouchEvent(touchEvent); touchEvent.touches.clear(); } diff --git a/native/cocos/platform/ios/View.mm b/native/cocos/platform/ios/View.mm index cc6fa04f04..4a43f202d1 100644 --- a/native/cocos/platform/ios/View.mm +++ b/native/cocos/platform/ios/View.mm @@ -48,7 +48,7 @@ namespace { } #endif -- (void)dispatchEvents:(cc::TouchEvent::Type)type withEvent:(NSSet*) touches { +- (void)dispatchTouchEvent:(cc::TouchEvent::Type)type withEvent:(NSSet*) touches { cc::TouchEvent touchEvent; touchEvent.type = type; for (UITouch *touch in touches) { @@ -57,7 +57,7 @@ namespace { static_cast((intptr_t)touch)}); } CCASSERT(_platform != nullptr, "Platform point cannot be null"); - _platform->dispatchEvent(touchEvent); + _platform->dispatchTouchEvent(touchEvent); } - (id)initWithFrame:(CGRect)frame { @@ -89,28 +89,28 @@ namespace { if (self.preventTouch) return; - [self dispatchEvents:cc::TouchEvent::Type::BEGAN withEvent:touches]; + [self dispatchTouchEvent:cc::TouchEvent::Type::BEGAN withEvent:touches]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { if (self.preventTouch) return; - [self dispatchEvents:cc::TouchEvent::Type::MOVED withEvent:touches]; + [self dispatchTouchEvent:cc::TouchEvent::Type::MOVED withEvent:touches]; } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (self.preventTouch) return; - [self dispatchEvents:cc::TouchEvent::Type::ENDED withEvent:touches]; + [self dispatchTouchEvent:cc::TouchEvent::Type::ENDED withEvent:touches]; } - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event { if (self.preventTouch) return; - [self dispatchEvents:cc::TouchEvent::Type::CANCELLED withEvent:touches]; + [self dispatchTouchEvent:cc::TouchEvent::Type::CANCELLED withEvent:touches]; } - (void)setPreventTouchEvent:(BOOL)flag { diff --git a/native/cocos/platform/java/jni/JniCocosTouchHandler.cpp b/native/cocos/platform/java/jni/JniCocosTouchHandler.cpp index 4f6ad29000..3eb5fe5a4b 100644 --- a/native/cocos/platform/java/jni/JniCocosTouchHandler.cpp +++ b/native/cocos/platform/java/jni/JniCocosTouchHandler.cpp @@ -37,17 +37,19 @@ JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionDown(JNI jfloat x, jfloat y) { touchEvent.type = cc::TouchEvent::Type::BEGAN; touchEvent.touches.emplace_back(x, y, id); - JNI_NATIVE_GLUE()->dispatchEvent(touchEvent); + JNI_NATIVE_GLUE()->dispatchTouchEvent(touchEvent); touchEvent.touches.clear(); } + //NOLINTNEXTLINE JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionUp(JNIEnv *env, jobject obj, jint id, jfloat x, jfloat y) { touchEvent.type = cc::TouchEvent::Type::ENDED; touchEvent.touches.emplace_back(x, y, id); - JNI_NATIVE_GLUE()->dispatchEvent(touchEvent); + JNI_NATIVE_GLUE()->dispatchTouchEvent(touchEvent); touchEvent.touches.clear(); } + //NOLINTNEXTLINE JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionMove(JNIEnv *env, jobject obj, jintArray ids, @@ -66,7 +68,7 @@ JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionMove(JNI touchEvent.touches.emplace_back(x[i], y[i], id[i]); } - JNI_NATIVE_GLUE()->dispatchEvent(touchEvent); + JNI_NATIVE_GLUE()->dispatchTouchEvent(touchEvent); touchEvent.touches.clear(); } @@ -87,7 +89,7 @@ JNIEXPORT void JNICALL Java_com_cocos_lib_CocosTouchHandler_handleActionCancel(J for (int i = 0; i < size; i++) { touchEvent.touches.emplace_back(x[i], y[i], id[i]); } - JNI_NATIVE_GLUE()->dispatchEvent(touchEvent); + JNI_NATIVE_GLUE()->dispatchTouchEvent(touchEvent); touchEvent.touches.clear(); } } diff --git a/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp b/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp index 5a9453f621..b2d47cd54b 100644 --- a/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp +++ b/native/cocos/platform/java/jni/glue/JniNativeGlue.cpp @@ -160,7 +160,7 @@ void JniNativeGlue::dispatchEvent(const OSEvent& ev) { } } -void JniNativeGlue::dispatchTouchEvent(const OSEvent& ev) { +void JniNativeGlue::dispatchTouchEvent(const TouchEvent& ev) { if (_eventDispatcher) { _eventDispatcher->dispatchTouchEvent(ev); } diff --git a/native/cocos/platform/java/jni/glue/JniNativeGlue.h b/native/cocos/platform/java/jni/glue/JniNativeGlue.h index 6270fab1a1..fb4a879a0b 100644 --- a/native/cocos/platform/java/jni/glue/JniNativeGlue.h +++ b/native/cocos/platform/java/jni/glue/JniNativeGlue.h @@ -53,6 +53,7 @@ namespace cc { class IEventDispatch; class OSEvent; +class TouchEvent; class CC_DLL JniNativeGlue { public: @@ -105,7 +106,7 @@ public: void setEventDispatch(IEventDispatch* eventDispatcher); void dispatchEvent(const OSEvent& ev); - void dispatchTouchEvent(const OSEvent& ev); + void dispatchTouchEvent(const TouchEvent& ev); void onPause(); void onResume(); diff --git a/native/cocos/platform/win32/modules/System.cpp b/native/cocos/platform/win32/modules/System.cpp index f78fdf8c1a..7ca1bd5428 100644 --- a/native/cocos/platform/win32/modules/System.cpp +++ b/native/cocos/platform/win32/modules/System.cpp @@ -134,13 +134,6 @@ std::string System::getSystemVersion() const { } } return buff; - //OSVERSIONINFO osvi; - //memset(&osvi, 0, sizeof(osvi)); - //osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - //GetVersionEx(&osvi); - //char buff[256] = {0}; - //snprintf(buff, sizeof(buff), "Windows version %d.%d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber); - //return buff; } bool System::openURL(const std::string& url) { @@ -151,4 +144,4 @@ bool System::openURL(const std::string& url) { delete[] temp; return (size_t)r > 32; } -} // namespace cc \ No newline at end of file +} // namespace cc