diff --git a/cocos/2d/CCTextFieldTTF.cpp b/cocos/2d/CCTextFieldTTF.cpp index 1a8ed91c01..98b0f54411 100644 --- a/cocos/2d/CCTextFieldTTF.cpp +++ b/cocos/2d/CCTextFieldTTF.cpp @@ -142,7 +142,11 @@ bool TextFieldTTF::attachWithIME() auto pGlView = Director::getInstance()->getOpenGLView(); if (pGlView) { +#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8 && CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) pGlView->setIMEKeyboardState(true); +#else + pGlView->setIMEKeyboardState(true, _inputText); +#endif } } return ret; @@ -157,7 +161,11 @@ bool TextFieldTTF::detachWithIME() auto glView = Director::getInstance()->getOpenGLView(); if (glView) { +#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8 && CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) glView->setIMEKeyboardState(false); +#else + glView->setIMEKeyboardState(false, ""); +#endif } } return ret; @@ -187,6 +195,7 @@ void TextFieldTTF::insertText(const char * text, size_t len) if (len > 0) { +#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8 && CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len)) { // delegate doesn't want to insert text @@ -197,6 +206,15 @@ void TextFieldTTF::insertText(const char * text, size_t len) std::string sText(_inputText); sText.append(insert); setString(sText); +#else + size_t existlen = _inputText.length(); + if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str() + existlen, len - existlen)) + { + // delegate doesn't want to insert text + return; + } + setString(insert); +#endif } if ((int)insert.npos == pos) { diff --git a/cocos/platform/CCGLView.h b/cocos/platform/CCGLView.h index 030def1ee8..6671030c52 100644 --- a/cocos/platform/CCGLView.h +++ b/cocos/platform/CCGLView.h @@ -105,6 +105,10 @@ public: /** Open or close IME keyboard , subclass must implement this method. */ virtual void setIMEKeyboardState(bool open) = 0; + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8 || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) + virtual void setIMEKeyboardState(bool open, std::string str) = 0; +#endif virtual bool windowShouldClose() { return false; }; diff --git a/cocos/platform/winrt/InputEvent.cpp b/cocos/platform/winrt/InputEvent.cpp index 62f0629afd..cfa1b4b6d6 100644 --- a/cocos/platform/winrt/InputEvent.cpp +++ b/cocos/platform/winrt/InputEvent.cpp @@ -86,11 +86,12 @@ void KeyboardEvent::execute() { case Cocos2dKeyEvent::Text: { - char szUtf8[8] = { 0 }; - int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) m_text.Get()->Data(), 1, szUtf8, sizeof(szUtf8), NULL, NULL); - IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen); + char szUtf8[256] = { 0 }; + int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR) m_text.Get()->Data(), -1, szUtf8, sizeof(szUtf8), NULL, NULL); + IMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen - 1); break; } + default: switch (m_type) { diff --git a/cocos/platform/winrt/InputEvent.h b/cocos/platform/winrt/InputEvent.h index 64f8ed64df..4ca3f3cb25 100644 --- a/cocos/platform/winrt/InputEvent.h +++ b/cocos/platform/winrt/InputEvent.h @@ -33,7 +33,7 @@ THE SOFTWARE. namespace PhoneDirect3DXamlAppComponent { - public delegate void Cocos2dEventDelegate(Cocos2dEvent event); + public delegate void Cocos2dEventDelegate(Cocos2dEvent event, Platform::String^ text); public delegate void Cocos2dMessageBoxDelegate(Platform::String^ title, Platform::String^ text); public delegate void Cocos2dEditBoxDelegate(Platform::String^ strPlaceHolder, Platform::String^ strText, int maxLength, int inputMode, int inputFlag, Windows::Foundation::EventHandler^ receiveHandler); } diff --git a/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp b/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp index e14858b33d..7fbefac3e5 100644 --- a/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp +++ b/cocos/platform/wp8-xaml/cpp/Direct3DInterop.cpp @@ -177,9 +177,10 @@ void Direct3DInterop::SetCocos2dEditBoxDelegate(Cocos2dEditBoxDelegate ^ delegat bool Direct3DInterop::SendCocos2dEvent(Cocos2dEvent event) { + Platform::String^ str; if(m_delegate) { - m_delegate->Invoke(event); + m_delegate->Invoke(event, str); return true; } return false; diff --git a/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs b/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs index 70d50eb5ee..4610a773e3 100644 --- a/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs +++ b/cocos/platform/wp8-xaml/xaml/MainPage.xaml.cs @@ -134,6 +134,11 @@ namespace PhoneDirect3DXamlAppInterop m_textBox.Text = ""; } + public void OnTextChanged(object sender, TextChangedEventArgs e) + { + m_d3dInterop.OnCocos2dKeyEvent(Cocos2dKeyEvent.Text, m_textBox.Text); + } + // Called by the Cocos2d-x C++ engine to display a MessageBox public void OnCocos2dMessageBoxEvent(String title, String text) { @@ -144,7 +149,7 @@ namespace PhoneDirect3DXamlAppInterop } // events called by the Cocos2d-x C++ engine to be handled by C# - public void OnCocos2dEvent(Cocos2dEvent theEvent) + public void OnCocos2dEvent(Cocos2dEvent theEvent, String text) { Dispatcher.BeginInvoke(() => { @@ -161,11 +166,13 @@ namespace PhoneDirect3DXamlAppInterop m_textBox.Opacity = 0.0; m_textBox.Width = 1; m_textBox.Height = 1; - m_textBox.MaxLength = 1; m_textBox.KeyDown += OnKeyDown; - m_textBox.KeyUp += OnKeyUp; + m_textBox.TextChanged += OnTextChanged; DrawingSurfaceBackground.Children.Add(m_textBox); } + m_textBox.Text = text; + m_textBox.SelectionLength = 0; + m_textBox.SelectionStart = int.MaxValue; m_textBox.Focus(); break; diff --git a/cocos/platform/wp8/CCGLViewImpl-wp8.cpp b/cocos/platform/wp8/CCGLViewImpl-wp8.cpp index 4a35e08cbf..8eaa3f7314 100644 --- a/cocos/platform/wp8/CCGLViewImpl-wp8.cpp +++ b/cocos/platform/wp8/CCGLViewImpl-wp8.cpp @@ -133,20 +133,38 @@ void GLViewImpl::UpdateDevice(EGLDisplay eglDisplay, EGLContext eglContext, EGLS } void GLViewImpl::setIMEKeyboardState(bool bOpen) +{ + std::string str; + setIMEKeyboardState(bOpen, str); +} + +void GLViewImpl::setIMEKeyboardState(bool bOpen, std::string str) { if(m_delegate) { if(bOpen) { - m_delegate->Invoke(Cocos2dEvent::ShowKeyboard); + m_delegate->Invoke(Cocos2dEvent::ShowKeyboard, stringToPlatformString(str)); } else { - m_delegate->Invoke(Cocos2dEvent::HideKeyboard); + m_delegate->Invoke(Cocos2dEvent::HideKeyboard, stringToPlatformString(str)); } } } +Platform::String^ GLViewImpl::stringToPlatformString(std::string strSrc) +{ + // to wide char + int strLen = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, NULL, 0); + wchar_t* wstr = new wchar_t[strLen + 1]; + memset(wstr, 0, strLen + 1); + MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(), -1, wstr, strLen); + Platform::String^ strDst = ref new Platform::String(wstr); + delete[] wstr; + return strDst; +} + void GLViewImpl::swapBuffers() { eglSwapBuffers(m_eglDisplay, m_eglSurface); @@ -176,9 +194,10 @@ void GLViewImpl::OnResuming(Platform::Object^ sender, Platform::Object^ args) // user pressed the Back Key on the phone void GLViewImpl::OnBackKeyPress() { + std::string str; if(m_delegate) { - m_delegate->Invoke(Cocos2dEvent::TerminateApp); + m_delegate->Invoke(Cocos2dEvent::TerminateApp, stringToPlatformString(str)); } } diff --git a/cocos/platform/wp8/CCGLViewImpl-wp8.h b/cocos/platform/wp8/CCGLViewImpl-wp8.h index c65b9f9cad..706bbd7e3a 100644 --- a/cocos/platform/wp8/CCGLViewImpl-wp8.h +++ b/cocos/platform/wp8/CCGLViewImpl-wp8.h @@ -68,8 +68,10 @@ public: Size getRenerTargetSize() const { return Size(m_width, m_height); } virtual void setIMEKeyboardState(bool bOpen); - void ShowKeyboard(Windows::Foundation::Rect r); - void HideKeyboard(Windows::Foundation::Rect r); + virtual void setIMEKeyboardState(bool bOpen, std::string str); + Platform::String^ stringToPlatformString(std::string strSrc); + void ShowKeyboard(Windows::Foundation::Rect r); + void HideKeyboard(Windows::Foundation::Rect r); // WP8 XAML app virtual bool Create(EGLDisplay eglDisplay, EGLContext eglContext, EGLSurface eglSurface, float width, float height diff --git a/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs b/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs index 70d50eb5ee..4610a773e3 100644 --- a/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs +++ b/templates/cpp-template-default/proj.wp8-xaml/App/MainPage.xaml.cs @@ -134,6 +134,11 @@ namespace PhoneDirect3DXamlAppInterop m_textBox.Text = ""; } + public void OnTextChanged(object sender, TextChangedEventArgs e) + { + m_d3dInterop.OnCocos2dKeyEvent(Cocos2dKeyEvent.Text, m_textBox.Text); + } + // Called by the Cocos2d-x C++ engine to display a MessageBox public void OnCocos2dMessageBoxEvent(String title, String text) { @@ -144,7 +149,7 @@ namespace PhoneDirect3DXamlAppInterop } // events called by the Cocos2d-x C++ engine to be handled by C# - public void OnCocos2dEvent(Cocos2dEvent theEvent) + public void OnCocos2dEvent(Cocos2dEvent theEvent, String text) { Dispatcher.BeginInvoke(() => { @@ -161,11 +166,13 @@ namespace PhoneDirect3DXamlAppInterop m_textBox.Opacity = 0.0; m_textBox.Width = 1; m_textBox.Height = 1; - m_textBox.MaxLength = 1; m_textBox.KeyDown += OnKeyDown; - m_textBox.KeyUp += OnKeyUp; + m_textBox.TextChanged += OnTextChanged; DrawingSurfaceBackground.Children.Add(m_textBox); } + m_textBox.Text = text; + m_textBox.SelectionLength = 0; + m_textBox.SelectionStart = int.MaxValue; m_textBox.Focus(); break; diff --git a/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp b/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp index e14858b33d..3f3797078a 100644 --- a/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp +++ b/templates/cpp-template-default/proj.wp8-xaml/AppComponent/src/Direct3DInterop.cpp @@ -177,9 +177,10 @@ void Direct3DInterop::SetCocos2dEditBoxDelegate(Cocos2dEditBoxDelegate ^ delegat bool Direct3DInterop::SendCocos2dEvent(Cocos2dEvent event) { + std::string str; if(m_delegate) { - m_delegate->Invoke(event); + m_delegate->Invoke(event, stringToPlatformString(str)); return true; } return false; diff --git a/tests/cpp-tests/Classes/TextInputTest/TextInputTest.cpp b/tests/cpp-tests/Classes/TextInputTest/TextInputTest.cpp index d41e951171..58db1c670a 100644 --- a/tests/cpp-tests/Classes/TextInputTest/TextInputTest.cpp +++ b/tests/cpp-tests/Classes/TextInputTest/TextInputTest.cpp @@ -250,6 +250,14 @@ void TextFieldTTFDefaultTest::onEnter() // add TextFieldTTF auto s = Director::getInstance()->getWinSize(); + std::string strSubtitle = subtitle(); + TTFConfig ttfConfig; + ttfConfig.fontFilePath = FONT_NAME; + ttfConfig.fontSize = 16; + auto subTitle = Label::createWithTTF(ttfConfig, strSubtitle.c_str()); + addChild(subTitle, 9999); + subTitle->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60); + auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("", FONT_NAME, FONT_SIZE); @@ -310,6 +318,14 @@ void TextFieldTTFActionTest::onEnter() // add TextFieldTTF auto s = Director::getInstance()->getWinSize(); + std::string strSubtitle = subtitle(); + TTFConfig ttfConfig; + ttfConfig.fontFilePath = FONT_NAME; + ttfConfig.fontSize = 16; + auto subTitle = Label::createWithTTF(ttfConfig, strSubtitle.c_str()); + addChild(subTitle, 9999); + subTitle->setPosition(VisibleRect::center().x, VisibleRect::top().y - 60); + _textField = TextFieldTTF::textFieldWithPlaceHolder("", FONT_NAME, FONT_SIZE);