From 73a3f776f0d88e68dc020bb2deb625e6c1690b29 Mon Sep 17 00:00:00 2001 From: yangws Date: Wed, 27 Apr 2011 17:01:27 +0800 Subject: [PATCH] android lanscope keyboard ok. --- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 50 ++++++++++++++----- tests/tests/TextInputTest/TextInputTest.cpp | 43 +++++++++++++--- 2 files changed, 75 insertions(+), 18 deletions(-) diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index 9a3ad96000..de3397b954 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -16,18 +16,34 @@ import android.view.inputmethod.InputMethodManager; class Cocos2dxInputConnection implements InputConnection { + private boolean mDebug = false; + void LogD(String msg) { + if (mDebug) { + Log.d("Cocos2dxInputConnection", msg); + } + } + + private Cocos2dxGLSurfaceView mView; + + Cocos2dxInputConnection(Cocos2dxGLSurfaceView view) { + mView = view; + } + @Override public boolean beginBatchEdit() { + LogD("beginBatchEdit"); return false; } @Override public boolean clearMetaKeyStates(int states) { + LogD("clearMetaKeyStates: " + states); return false; } @Override public boolean commitCompletion(CompletionInfo text) { + LogD("commitCompletion: " + text.getText().toString()); return false; } @@ -36,53 +52,63 @@ class Cocos2dxInputConnection implements InputConnection { if (null != mView) { final String insertText = text.toString(); mView.insertText(insertText); + LogD("commitText: " + insertText); } return false; } @Override public boolean deleteSurroundingText(int leftLength, int rightLength) { + LogD("deleteSurroundingText: " + leftLength + "," + rightLength); return false; } @Override public boolean endBatchEdit() { + LogD("endBatchEdit"); return false; } @Override public boolean finishComposingText() { + LogD("finishComposingText"); return false; } @Override public int getCursorCapsMode(int reqModes) { + LogD("getCursorCapsMode: " + reqModes); return 0; } @Override public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) { - return null; + LogD("getExtractedText: " + request.toString() + "," + flags); + return new ExtractedText(); } @Override public CharSequence getTextAfterCursor(int n, int flags) { + LogD("getTextAfterCursor: " + n + "," + flags); return null; } @Override public CharSequence getTextBeforeCursor(int n, int flags) { + LogD("getTextBeforeCursor: " + n + "," + flags); return null; } @Override public boolean performContextMenuAction(int id) { + Log.d("Cocos2dxInputConnection", "performContextMenuAction"); return false; } @Override public boolean performEditorAction(int editorAction) { + LogD("performEditorAction: " + editorAction); if (null != mView) { final String insertText = "\n"; mView.insertText(insertText); @@ -92,16 +118,19 @@ class Cocos2dxInputConnection implements InputConnection { @Override public boolean performPrivateCommand(String action, Bundle data) { + LogD("performPrivateCommand: " + action + "," + data.toString()); return false; } @Override public boolean reportFullscreenMode(boolean enabled) { + LogD("reportFullscreenMode: " + enabled); return false; } @Override public boolean sendKeyEvent(KeyEvent event) { + LogD("sendKeyEvent: " + event.toString()); if (null != mView) { switch (event.getKeyCode()) { @@ -115,19 +144,15 @@ class Cocos2dxInputConnection implements InputConnection { @Override public boolean setComposingText(CharSequence text, int newCursorPosition) { + LogD("setComposingText: " + text.toString() + "," + newCursorPosition); return false; } @Override public boolean setSelection(int start, int end) { + LogD("setSelection: " + start + "," + end); return false; } - - public void setGLSurfaceView(Cocos2dxGLSurfaceView view) { - mView = view; - } - - private Cocos2dxGLSurfaceView mView; } public class Cocos2dxGLSurfaceView extends GLSurfaceView { @@ -192,7 +217,8 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { if (imm == null) { return; } - imm.showSoftInput(mainView, InputMethodManager.SHOW_IMPLICIT); + boolean ret = imm.showSoftInput(mainView, 0); + Log.d("openIMEKeboard", (ret)? "true" : "false"); } public static void closeIMEKeyboard() { @@ -221,15 +247,14 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { if (onCheckIsTextEditor()) { outAttrs.inputType = EditorInfo.TYPE_CLASS_TEXT; - outAttrs.imeOptions = EditorInfo.IME_NULL; + outAttrs.imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI; outAttrs.initialSelStart = -1; outAttrs.initialSelEnd = -1; - outAttrs.initialCapsMode = 0; + outAttrs.initialCapsMode = 1; if (null == ic) { - ic = new Cocos2dxInputConnection(); - ic.setGLSurfaceView(this); + ic = new Cocos2dxInputConnection(this); } return ic; } @@ -253,6 +278,7 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { } }); } + /////////////////////////////////////////////////////////////////////////// // for touch event /////////////////////////////////////////////////////////////////////////// diff --git a/tests/tests/TextInputTest/TextInputTest.cpp b/tests/tests/TextInputTest/TextInputTest.cpp index 3a79c033dd..66e5dce35c 100644 --- a/tests/tests/TextInputTest/TextInputTest.cpp +++ b/tests/tests/TextInputTest/TextInputTest.cpp @@ -1,3 +1,5 @@ +//#define COCOS2D_DEBUG 1 + #include "TextInputTest.h" ////////////////////////////////////////////////////////////////////////// @@ -150,19 +152,29 @@ void KeyboardNotificationLayer::registerWithTouchDispatcher() void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo& info) { + CCLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)", + info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height); + if (! m_pTrackNode) { return; } CCRect rectTracked = getRect(m_pTrackNode); + CCLOG("TextInputTest:trackingNodeAt(origin:%f,%f, size:%f,%f)", + rectTracked.origin.x, rectTracked.origin.y, rectTracked.size.width, rectTracked.size.height); + + // if the keyboard area doesn't intersect with the tracking node area, nothing need to do. if (! CCRect::CCRectIntersectsRect(rectTracked, info.end)) { return; } + // assume keyboard at the bottom of screen, calculate the vertical adjustment. float adjustVert = CCRect::CCRectGetMaxY(info.end) - CCRect::CCRectGetMinY(rectTracked); + CCLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert); + // move all the children node of KeyboardNotificationLayer CCArray * children = getChildren(); CCNode * node = 0; int count = children->count(); @@ -226,27 +238,46 @@ void TextFieldTTFTest::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) return; } + // decide the index of CCTextFieldTTF which is clicked. int index = 0; + CCRect rect; + CCPoint point = convertTouchToNodeSpaceAR(pTouch); + CCLOG("TextFieldTTFTest:clickedAt(%f,%f)", point.x, point.y); + for (; index < sizeof(m_pTextField) / sizeof(CCTextFieldTTF *); ++index) { - if (CCRect::CCRectContainsPoint(getRect(m_pTextField[index]), convertTouchToNodeSpaceAR(pTouch))) + rect = getRect(m_pTextField[index]); + CCLOG("TextFieldTTFTest:CCTextFieldTTF[%d]at(origin:%f,%f, size:%f,%f)", + index, rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); + + if (CCRect::CCRectContainsPoint(rect, point)) { break; } } - if (index < sizeof(m_pTextField) / sizeof(CCTextFieldTTF *)) + if (index == m_nSelected) { - m_nSelected = index; - m_pTrackNode = m_pTextField[index]; - m_pTextField[index]->attachWithIME(); + CCLOG("TextFieldTTFTest:noCCTextFieldTTFClicked."); + return; } - else if (m_nSelected >= 0) + + if (m_nSelected >= 0) { + // hide the keyboard + CCLOG("TextFieldTTFTest:CCTextFieldTTF[%d]detachWithIME", m_nSelected); m_pTextField[m_nSelected]->detachWithIME(); m_nSelected = -1; m_pTrackNode = 0; } + + if (index < sizeof(m_pTextField) / sizeof(CCTextFieldTTF *)) + { + CCLOG("TextFieldTTFTest:CCTextFieldTTF[%d]attachWithIME", index); + m_nSelected = index; + m_pTrackNode = m_pTextField[index]; + m_pTextField[index]->attachWithIME(); + } } //////////////////////////////////////////////////////////////////////////