Cocos2dx engine 8-Event Processing
This article mainly introduces the processing of click events, and introduces Cocos2dx click events from win32, Android, and IOS systems.
1. Win32 System
AppDelegate::applicationDidFinishLaunching()->GLView::create(…)->GLView::initWithRect(…)bool GLView::initWithRect(conststd::string& viewName, Rect rect, float frameZoomFactor){ ………glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack);………}
In the win32 system, click an event as a mouse click event, and use glfwSetMouseButtonCallback to bind the time processing function of the mouse click event;
Static voidonGLFWMouseCallBack (GLFWwindow * window, int button, int action, int modify) {if (_ view) _ view-> onGLFWMouseCallBack (window, button, action, modify);} voidGLView:: onGLFWMouseCallBack (GLFWwindow * window, int button, int action, intmodify) {if (GLFW_MOUSE_BUTTON_LEFT = button) {// if (GLFW_PRESS = action) {// when the left mouse button is pressed, _ captured = true; if (this-> getViewPortRect (). equals (Rect: ZERO) | this-> g EtViewPortRect (). containsPoint (Vec2 (_ mouseX, _ mouseY) {intptr_t id = 0; this-> handleTouchesBegin (1, & id, & _ mouseX, & _ mouseY );}} elseif (GLFW_RELEASE = action) {// if (_ captured) {_ captured = false; intptr_t id = 0; this-> handleTouchesEnd (1, & id, & _ mouseX, & _ mouseY );}}}......... // Process mouse events}
The above code is located in cocos2d \ cocos \ platform \ win32 \ CCGLView. cpp;
2. Android system
Public class Cocos2dxGLSurfaceView extends GLSurfaceView {...... Public booleanonTouchEvent (final MotionEvent pMotionEvent ){.......... Switch (pMotionEvent. getAction () & MotionEvent. ACTION_MASK ){...... CaseMotionEvent. ACTION_DOWN: // press the event this. queueEvent (newRunnable () {// a new thread processes the event publicvoid run () {Cocos2dxGLSurfaceView. this. mCocos2dxRenderer. handleActionDown (idDown, xDown, yDown) ;}}); break ;................ CaseMotionEvent. ACTION_UP: // lift event this. queueEvent (newRunnable () {publicvoid run () {Cocos2dxGLSurfaceView. this. mCocos2dxRenderer. handleActionUp (idUp, xUp, yUp) ;}}); break ;............ } Return true ;}}
Cocos2dxGLSurfaceView inherits the View class to implement the onTouchEvent function in the View. When a touch screen event occurs, the onTouchEvent method in Cocos2dxGLSurfaceView is called. In the onTouchEvent method, Down & Up events are distinguished, and use a new thread to execute the handleActionDown & handleActionUp method in the Cocos2dxRenderer class;
public class Cocos2dxRenderer implements GLSurfaceView.Renderer{ private static native void nativeTouchesBegin(final int pID, final float pX, final float pY); private static native void nativeTouchesEnd(final int pID, final float pX, final float pY); public void handleActionDown(finalint pID, final float pX, final float pY) { Cocos2dxRenderer.nativeTouchesBegin(pID,pX, pY); } public voidhandleActionUp(final int pID, final float pX, final float pY) { Cocos2dxRenderer.nativeTouchesEnd(pID,pX, pY); }}
The handleActionDown & handleActionUp method in the Cocos2dxRenderer class calls the nativeTouchesBegin & nativeTouchesEnd method implemented by C ++ through JNI;
NativeTouchesBegin & nativeTouchesEnd correspond
In the cocos2d \ cocos \ platform \ android \ jni \ TouchesJni. cpp file:
JNIEXPORT voidJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesBegin(JNIEnv * env, jobject thiz, jint id, jfloatx, jfloat y) { cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesBegin(1,&id, &x, &y); }JNIEXPORT voidJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativeTouchesEnd(JNIEnv * env, jobject thiz, jint id, jfloatx, jfloat y) { cocos2d::Director::getInstance()->getOpenGLView()->handleTouchesEnd(1,&id, &x, &y);}
3. IOS system
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ …… glview->handleTouchesBegin(i,(intptr_t*)ids, xs, ys);}-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ …… glview->handleTouchesEnd(i,(intptr_t*)ids, xs, ys);}
The preceding method is implemented in cocos2d \ cocos \ platform \ ios \ CCEAGLView. mm;
4. Cocs2dx event processing
The handleTouchesBegin and handleTouchesEnd processes the same implementation in Win32, Android, and IOS. The analysis will continue;
VoidGLViewProtocol: handleTouchesBegin (int num, intptr_t ids [], float xs [], floatys []) {// execute intptr_t id = 0; float x = 0.0f; float y = 0.0f; int unusedIndex = 0; EventTouch touchEvent; for (int I = 0; I <num; ++ I) // traverses all the touch points, {id = ids [I]; x = xs [I]; y = ys [I]; // check whether the Touch already exists in the local touch dictionary: auto iter = g_touchIdReorderMap.find (id); // if it is a new Touch if (iter = g_touchIdReorderMap.end ()) {UnusedIndex = getUnUsedIndex (); // get the new Touch tag ...... // Collect Touch Information Touch * touch = g_touches [unusedIndex] = new Touch (); touch-> setTouchInfo (unusedIndex, (x-_ viewPortRect. origin. x)/_ scaleX, (y-_ viewPortRect. origin. y)/_ scaleY); CCLOGINFO ("x = % f y = % f", touch-> getLocationInView (). x, touch-> getLocationInView (). y); // Add the current Touch to the Touch query dictionary g_touchIdReorderMap.insert (std: make_pair (id, unusedIndex); touchEvent. _ touches. push_back (touch );}}...... TouchEvent. _ eventCode = EventTouch: EventCode: BEGAN; auto dispatcher = Director: getInstance ()-> getEventDispatcher (); dispatcher-> dispatchEvent (& touchEvent ); // distribute Touch events}
In handleTouchesBegin:
(1) check whether the click already exists
(2) If the click record does not contain the current click, collect the click information and distribute the TouchBegin event.
In Process (1), ids = {0} is input in single-click. Click events are stored in touchIdReorderMap [0, this prevents handleTouchesBegin from being called by clicking another location before clicking the same location is released;
VoidGLViewProtocol: handleTouchesEnd (int num, intptr_t ids [], float xs [], floatys []) {// execute handleTouchesOfEndOrCancel (EventTouch: EventCode: ENDED, num, ids, xs, ys);} void GLViewProtocol: handleTouchesOfEndOrCancel (EventTouch: EventCodeeventCode, int num, intptr_t ids [], float xs [], float ys []) {intptr_t id = 0; float x = 0.0f; float y = 0.0f; EventTouch touchEvent; for (int I = 0; I <num; ++ I) // Traverse all the touch points. If you touch multiple points, {id = ids [I]; x = xs [I]; y = ys [I] is required. // query whether the Click Event corresponding to the release event exists auto iter = g_touchIdReorderMap.find (id );...... Touch * touch = g_touches [iter-> second]; if (touch) {touch-> setTouchInfo (iter-> second, (x-_ viewPortRect. origin. x)/_ scaleX, (y-_ viewPortRect. origin. y)/_ scaleY); touchEvent. _ touches. push_back (touch); g_touches [iter-> second] = nullptr; removeUsedIndexBit (iter-> second); g_touchIdReorderMap.erase (id); // remove the released Click Event }...... }...... TouchEvent. _ eventCode = eventCode; auto dispatcher = Director: getInstance ()-> getEventDispatcher (); dispatcher-> dispatchEvent (& touchEvent ); // dispatch click release event for (auto & touch: touchEvent. _ touches) {// release click touch-> release ();}}
In handleTouchesOfEndOrCancel:
(1) check whether the click already exists
(2) If a click record exists, click information is collected and TouchEnd events are distributed.
Because ids = {0} passed in a single click, the release operation is performed only when the click event exists and the click event is removed from g_touchIdReorderMap, make sure that you have TouchBegin before calling ToucheEnd.