Cocos2dx 3.2 solves the problem of Chinese garbled characters in the input box (TextField, TextFieldTTF), cocos2dxtextfield
Recently, cocos2dx was developed to create a small game. (I like to use the latest version)
Without system learning, there are many problems. For example, all opengl APIs must be called in the main thread,
This made me suffer a lot of losses in multi-threaded loading and wasted a lot of time.
This time, garbled characters are displayed when you enter Chinese characters in the input box. I searched many methods and did not find them. No way. But we know that cocos2dx 2.x does not have this problem, so we only need to follow up the code.
First, find the WM_CHAR sending message in 2.x:
You can find LRESULT CCEGLView: WindowProc (UINT message, WPARAM wParam, LPARAM lParam) in CCEGLView. cpp under platform/win32,
Leave it blank and paste the Code directly:
case WM_CHAR: { if (wParam < 0x20) { if (VK_BACK == wParam) { CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); } else if (VK_RETURN == wParam) { CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1); } else if (VK_TAB == wParam) { // tab input } else if (VK_ESCAPE == wParam) { // ESC input //CCDirector::sharedDirector()->end(); } } else if (wParam < 128) { // ascii char CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1); } else { char szUtf8[8] = {0}; int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL); CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); } if ( m_lpfnAccelerometerKeyHook!=NULL ) { (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam ); } } break;
As shown above: the most important thing is that,
else if (wParam < 128) { // ascii char CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1); } else { char szUtf8[8] = {0}; int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL); CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); }
Here we can see that the Chinese Garbled text processing method is as follows:
Now, go to our version 3.2:
First, check the start of the message event of the primary registration:
In GLView: initWithRect:
GlfwSetMouseButtonCallback (_ mainWindow, GLFWEventHandler: onGLFWMouseCallBack );
GlfwSetCursorPosCallback (_ mainWindow, GLFWEventHandler: onGLFWMouseMoveCallBack );
GlfwSetScrollCallback (_ mainWindow, GLFWEventHandler: onGLFWMouseScrollCallback );
GlfwSetCharCallback (_ mainWindow, GLFWEventHandler: onGLFWCharCallback );
GlfwSetKeyCallback (_ mainWindow, GLFWEventHandler: onGLFWKeyCallback );
GlfwSetWindowPosCallback (_ mainWindow, GLFWEventHandler: onGLFWWindowPosCallback );
GlfwSetFramebufferSizeCallback (_ mainWindow, GLFWEventHandler: onGLFWframebuffersize );
GlfwSetWindowSizeCallback (_ mainWindow, GLFWEventHandler: onGLFWWindowSizeFunCallback );
The above red is what we want:
Go to GLFWEventHandler: onGLFWCharCallback and follow up: _ view-> onGLFWCharCallback (window, character );
OK: Let's go to the most critical location: directly paste the Code:
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character){IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*)&character, 1);}
Is it similar to 2.x? Yes, we will use 2.x to handle Chinese Garbled text. We will not talk much about it. Let's look at the results directly.
void GLView::onGLFWCharCallback(GLFWwindow *window, unsigned int character){if (character < 128)IMEDispatcher::sharedDispatcher()->dispatchInsertText((const char*)&character, 1);else{char szUtf8[8] = { 0 };int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&character, 1, szUtf8, sizeof(szUtf8), NULL, NULL);IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen);}}
OK, complete! It's that simple. Now I can try it out. It supports Chinese input methods (mainly used on windows)
In fact, the next step is to collect GLView: onGLFWCharCallback in 3.x.
Then, add the above function content.
Learning and communication !!! For reference
In the struts tag, how does one add text behind the input box?
If you want to use struts 2.X to bring your own certificate, you can put some of your error prompts behind that tag. You can refer to the struts framework development book. It provides detailed descriptions.
Why is the parameter in textfield () the value 30 of textfield (30?
30 is the text width, that is, the textfield width can be 30 characters