V3.5.0 Distribute touch events independently (#10712)

* Distribute touch events independently

* remove  touch event cast

* Fix the error of clang-tidy

Co-authored-by: qiuguohua <862332094@qq.com>
This commit is contained in:
qiuguohua
2022-04-10 22:10:15 +08:00
committed by GitHub
parent a7f48be180
commit e20e4b889e
15 changed files with 65 additions and 39 deletions
+8
View File
@@ -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;
}
+7 -2
View File
@@ -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.
*/
+6
View File
@@ -82,6 +82,12 @@ public:
virtual void setHandleEventCallback(HandleEventCallback cb) = 0;
/**
* @brief Set touch event handling callback function.
*/
using HandleTouchEventCallback = std::function<bool(const TouchEvent&)>;
virtual void setHandleTouchEventCallback(HandleTouchEventCallback cb) = 0;
/**
* @brief Set default event handling callback function.
*/
+1 -1
View File
@@ -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
+3 -3
View File
@@ -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: {
+10 -1
View File
@@ -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;
}
+8 -1
View File
@@ -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};
};
@@ -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
} // namespace cc
@@ -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:
@@ -110,7 +110,7 @@ cc::IOSPlatform *_platform = nullptr;
static_cast<float>([touch locationInView:[touch view]].y),
static_cast<int>((intptr_t)touch)});
}
_platform->dispatchEvent(touchEvent);
_platform->dispatchTouchEvent(touchEvent);
touchEvent.touches.clear();
}
+6 -6
View File
@@ -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<int>((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 {
@@ -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();
}
}
@@ -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);
}
@@ -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();
@@ -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
} // namespace cc