/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.lib;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
class TextInputWraper implements TextWatcher, OnEditorActionListener {
private static final Boolean debug = false;
private void LogD(String msg) {
if (debug) Log.d("TextInputFilter", msg);
}
private Cocos2dxGLSurfaceView mMainView;
private String mText;
private String mOriginText;
private Boolean isFullScreenEdit() {
InputMethodManager imm = (InputMethodManager)mMainView.getTextField().getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.isFullscreenMode();
}
public TextInputWraper(Cocos2dxGLSurfaceView view) {
mMainView = view;
}
public void setOriginText(String text) {
mOriginText = text;
}
@Override
public void afterTextChanged(Editable s) {
if (isFullScreenEdit()) {
return;
}
LogD("afterTextChanged: " + s);
int nModified = s.length() - mText.length();
if (nModified > 0) {
final String insertText = s.subSequence(mText.length(), s.length()).toString();
mMainView.insertText(insertText);
LogD("insertText(" + insertText + ")");
}
else {
for (; nModified < 0; ++nModified) {
mMainView.deleteBackward();
LogD("deleteBackward");
}
}
mText = s.toString();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
LogD("beforeTextChanged(" + s + ")start: " + start + ",count: " + count + ",after: " + after);
mText = s.toString();
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (mMainView.getTextField() == v && isFullScreenEdit()) {
// user press the action button, delete all old text and insert new text
for (int i = mOriginText.length(); i > 0; --i) {
mMainView.deleteBackward();
LogD("deleteBackward");
}
String text = v.getText().toString();
/*
* If user input nothing, translate "\n" to engine.
*/
if (text.compareTo("") == 0){
text = "\n";
}
if ('\n' != text.charAt(text.length() - 1)) {
text += '\n';
}
final String insertText = text;
mMainView.insertText(insertText);
LogD("insertText(" + insertText + ")");
}
return false;
}
}
public class Cocos2dxGLSurfaceView extends GLSurfaceView {
static private Cocos2dxGLSurfaceView mainView;
private static final String TAG = Cocos2dxGLSurfaceView.class.getCanonicalName();
private Cocos2dxRenderer mRenderer;
private static final boolean debug = false;
///////////////////////////////////////////////////////////////////////////
// for initialize
///////////////////////////////////////////////////////////////////////////
public Cocos2dxGLSurfaceView(Context context) {
super(context);
initView();
}
public Cocos2dxGLSurfaceView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public void setCocos2dxRenderer(Cocos2dxRenderer renderer){
mRenderer = renderer;
setRenderer(mRenderer);
}
protected void initView() {
setFocusableInTouchMode(true);
textInputWraper = new TextInputWraper(this);
handler = new Handler(){
public void handleMessage(Message msg){
switch(msg.what){
case HANDLER_OPEN_IME_KEYBOARD:
if (null != mTextField && mTextField.requestFocus()) {
mTextField.removeTextChangedListener(textInputWraper);
mTextField.setText("");
String text = (String)msg.obj;
mTextField.append(text);
textInputWraper.setOriginText(text);
mTextField.addTextChangedListener(textInputWraper);
InputMethodManager imm = (InputMethodManager)mainView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mTextField, 0);
Log.d("GLSurfaceView", "showSoftInput");
}
break;
case HANDLER_CLOSE_IME_KEYBOARD:
if (null != mTextField) {
mTextField.removeTextChangedListener(textInputWraper);
InputMethodManager imm = (InputMethodManager)mainView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mTextField.getWindowToken(), 0);
Log.d("GLSurfaceView", "HideSoftInput");
}
break;
}
}
};
mainView = this;
}
public void onPause(){
queueEvent(new Runnable() {
@Override
public void run() {
mRenderer.handleOnPause();
}
});
super.onPause();
}
public void onResume(){
super.onResume();
queueEvent(new Runnable() {
@Override
public void run() {
mRenderer.handleOnResume();
}
});
}
///////////////////////////////////////////////////////////////////////////
// for text input
///////////////////////////////////////////////////////////////////////////
private final static int HANDLER_OPEN_IME_KEYBOARD = 2;
private final static int HANDLER_CLOSE_IME_KEYBOARD = 3;
private static Handler handler;
private static TextInputWraper textInputWraper;
private Cocos2dxEditText mTextField;
public TextView getTextField() {
return mTextField;
}
public void setTextField(Cocos2dxEditText view) {
mTextField = view;
if (null != mTextField && null != textInputWraper) {
mTextField.setOnEditorActionListener(textInputWraper);
mTextField.setMainView(this);
this.requestFocus();
}
}
public static void openIMEKeyboard() {
Message msg = new Message();
msg.what = HANDLER_OPEN_IME_KEYBOARD;
msg.obj = mainView.getContentText();
handler.sendMessage(msg);
}
private String getContentText() {
return mRendere
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
cocos2d-x -2.0-rc2-x-2.0.1 (3214个子文件)
libjs_static.a 10.03MB
libjs_static.a 8.1MB
libcurl.a 6.54MB
libcurl.a 2.79MB
libxml2.a 2.75MB
libxml2.a 2.75MB
libxml2.a 2.71MB
libcurl.a 2.63MB
libcurl.a 2.6MB
libjpeg.a 1.56MB
libtiff.a 1.54MB
libtiff.a 1.54MB
libtiff.a 1.52MB
libjpeg.a 1.48MB
libjpeg.a 1.43MB
libpng.a 891KB
libpng.a 864KB
libpng.a 829KB
libandprof.a 35KB
libandprof.a 35KB
algorithm 116B
AUTHORS 3KB
create-android-project.bat 2KB
build-win32.bat 2KB
install-templates-msvc.bat 520B
bitset 113B
bitmapFontTest2.bmp 2MB
lparser.c 36KB
lstrlib.c 23KB
lvm.c 23KB
lapi.c 22KB
mat4.c 22KB
lcode.c 21KB
cpBBTree.c 20KB
lgc.c 20KB
tolua_map.c 19KB
loadlib.c 19KB
lauxlib.c 17KB
lbaselib.c 17KB
quaternion.c 17KB
ldebug.c 16KB
tolua_event.c 16KB
ltable.c 16KB
cpSpace.c 15KB
cpSpaceHash.c 15KB
tolua_is.c 15KB
ldo.c 15KB
liolib.c 13KB
cpSpaceStep.c 13KB
llex.c 12KB
cpCollision.c 11KB
cpSpaceComponent.c 11KB
mat3.c 10KB
lua.c 10KB
ldblib.c 10KB
cpShape.c 10KB
chipmunk.c 9KB
cpSpaceQuery.c 9KB
cpArbiter.c 9KB
cpBody.c 8KB
tolua_fix.c 8KB
vec3.c 7KB
ltablib.c 7KB
cpPolyShape.c 7KB
cpSweep1D.c 6KB
tolua_push.c 6KB
loslib.c 6KB
lmathlib.c 6KB
cpHashSet.c 6KB
matrix.c 6KB
lstate.c 6KB
lobject.c 5KB
ray2.c 5KB
vec4.c 5KB
print.c 5KB
cpGrooveJoint.c 5KB
plane.c 5KB
lundump.c 5KB
lfunc.c 5KB
cpSlideJoint.c 4KB
cpDampedSpring.c 4KB
cpPinJoint.c 4KB
cpRatchetJoint.c 4KB
cpDampedRotarySpring.c 4KB
cpPivotJoint.c 4KB
cpRotaryLimitJoint.c 4KB
cpGearJoint.c 3KB
tolua_to.c 3KB
ldump.c 3KB
vec2.c 3KB
neon_matrix_impl.c 3KB
lopcodes.c 3KB
lstring.c 3KB
cpSimpleMotor.c 3KB
cpArray.c 3KB
mat4stack.c 3KB
cpSpatialIndex.c 2KB
lmem.c 2KB
aabb.c 2KB
utility.c 2KB
共 3214 条
- 1
- 2
- 3
- 4
- 5
- 6
- 33
八_戒
- 粉丝: 39
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页