Merge pull request #8141 from huangshiwu/v3_wp8test

fix Class TextFieldTTF's keyboard input bug on WP8 platform
This commit is contained in:
minggo 2014-09-25 09:40:10 +08:00
commit 04aa44aeea
11 changed files with 93 additions and 17 deletions

View File

@ -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) {

View File

@ -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; };

View File

@ -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)
{

View File

@ -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<Platform::String^>^ receiveHandler);
}

View File

@ -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;

View File

@ -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;

View File

@ -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));
}
}

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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("<click here for input>",
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("<click here for input>",
FONT_NAME,
FONT_SIZE);