Browse Source

fix android can't input unicode characters
fix hide soft keyboard by press 'back' button,
then click current focus text edit/line edit control,
soft keyboard won't show again

add features:
press enter key with line edit control will hide soft keyboard

sanikoyes 11 years ago
parent
commit
51429bd8d6

+ 15 - 4
platform/android/java/src/com/android/godot/Godot.java

@@ -53,6 +53,8 @@ import java.lang.reflect.Method;
 import java.util.List;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import android.provider.Settings.Secure;
 import android.provider.Settings.Secure;
+import android.widget.FrameLayout;
+import com.android.godot.input.*;
 
 
 
 
 public class Godot extends Activity implements SensorEventListener
 public class Godot extends Activity implements SensorEventListener
@@ -117,7 +119,7 @@ public class Godot extends Activity implements SensorEventListener
 	private SensorManager mSensorManager;
 	private SensorManager mSensorManager;
 	private Sensor mAccelerometer;
 	private Sensor mAccelerometer;
 
 
-	public RelativeLayout layout;
+	public FrameLayout layout;
 
 
 
 
 	static public GodotIO io;
 	static public GodotIO io;
@@ -143,13 +145,22 @@ public class Godot extends Activity implements SensorEventListener
 //		mView = new GodotView(getApplication(),io,use_gl2);
 //		mView = new GodotView(getApplication(),io,use_gl2);
 //		setContentView(mView);
 //		setContentView(mView);
 
 
-		layout = new RelativeLayout(this);
+		layout = new FrameLayout(this);
 		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
 		layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
 		setContentView(layout);
 		setContentView(layout);
+		
+		// GodotEditText layout
+		GodotEditText edittext = new GodotEditText(this); 
+        edittext.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
+        // ...add to FrameLayout
+        layout.addView(edittext);
+		
 		mView = new GodotView(getApplication(),io,use_gl2, this);
 		mView = new GodotView(getApplication(),io,use_gl2, this);
 		layout.addView(mView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
 		layout.addView(mView,new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
 		mView.setKeepScreenOn(true);
 		mView.setKeepScreenOn(true);
-
+		
+        edittext.setView(mView);
+        io.setEdit(edittext);
 	}
 	}
 
 
 	@Override protected void onCreate(Bundle icicle) {
 	@Override protected void onCreate(Bundle icicle) {
@@ -172,7 +183,7 @@ public class Godot extends Activity implements SensorEventListener
 		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
 		mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
 
 
 		result_callback = null;
 		result_callback = null;
-
+		
 	//	instanceSingleton( new GodotFacebook(this) );
 	//	instanceSingleton( new GodotFacebook(this) );
 
 
 
 

+ 16 - 6
platform/android/java/src/com/android/godot/GodotIO.java

@@ -47,6 +47,7 @@ import android.media.*;
 import android.hardware.*;
 import android.hardware.*;
 import android.content.*;
 import android.content.*;
 import android.content.pm.ActivityInfo;
 import android.content.pm.ActivityInfo;
+import com.android.godot.input.*;
 //android.os.Build
 //android.os.Build
 
 
 // Wrapper for native library
 // Wrapper for native library
@@ -55,7 +56,8 @@ public class GodotIO {
 
 
 
 
 	AssetManager am;
 	AssetManager am;
-	Activity activity;
+	Godot activity;
+	GodotEditText edit;
 
 
 	final int SCREEN_LANDSCAPE=0;
 	final int SCREEN_LANDSCAPE=0;
 	final int SCREEN_PORTRAIT=1;
 	final int SCREEN_PORTRAIT=1;
@@ -320,7 +322,7 @@ public class GodotIO {
 
 
 
 
 
 
-	GodotIO(Activity p_activity) {
+	GodotIO(Godot p_activity) {
 
 
 		am=p_activity.getAssets();
 		am=p_activity.getAssets();
 		activity=p_activity;
 		activity=p_activity;
@@ -462,15 +464,19 @@ public class GodotIO {
 	}
 	}
 
 
 	public void showKeyboard(String p_existing_text) {
 	public void showKeyboard(String p_existing_text) {
+		if(edit != null)
+			edit.showKeyboard(p_existing_text);
 
 
-		InputMethodManager inputMgr = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
-		inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
+		//InputMethodManager inputMgr = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
+		//inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
 	};
 	};
 
 
 	public void hideKeyboard() {
 	public void hideKeyboard() {
+		if(edit != null)
+			edit.hideKeyboard();
 
 
-		InputMethodManager inputMgr = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
-		inputMgr.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
+		//InputMethodManager inputMgr = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
+		//inputMgr.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
 	};
 	};
 
 
 	public void setScreenOrientation(int p_orientation) {
 	public void setScreenOrientation(int p_orientation) {
@@ -501,6 +507,10 @@ public class GodotIO {
 
 
 		}
 		}
 	};
 	};
+	
+	public void setEdit(GodotEditText _edit) {
+		edit = _edit;
+	}
 
 
 	protected static final String PREFS_FILE = "device_id.xml";
 	protected static final String PREFS_FILE = "device_id.xml";
 	protected static final String PREFS_DEVICE_ID = "device_id";
 	protected static final String PREFS_DEVICE_ID = "device_id";

+ 1 - 1
platform/android/java/src/com/android/godot/GodotView.java

@@ -61,7 +61,7 @@ import javax.microedition.khronos.opengles.GL10;
  *   that matches it exactly (with regards to red/green/blue/alpha channels
  *   that matches it exactly (with regards to red/green/blue/alpha channels
  *   bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
  *   bit depths). Failure to do so would result in an EGL_BAD_MATCH error.
  */
  */
-class GodotView extends GLSurfaceView {
+public class GodotView extends GLSurfaceView {
     private static String TAG = "GodotView";
     private static String TAG = "GodotView";
     private static final boolean DEBUG = false;
     private static final boolean DEBUG = false;
     private static Context ctx;
     private static Context ctx;

+ 133 - 0
platform/android/java/src/com/android/godot/input/GodotEditText.java

@@ -0,0 +1,133 @@
+package com.android.godot.input;
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.KeyEvent;
+import android.widget.EditText;
+import com.android.godot.*;
+import android.os.Handler;
+import android.os.Message;
+import android.view.inputmethod.InputMethodManager;
+import android.view.inputmethod.EditorInfo;
+
+public class GodotEditText extends EditText {
+	// ===========================================================
+	// Constants
+	// ===========================================================
+	private final static int HANDLER_OPEN_IME_KEYBOARD = 2;
+	private final static int HANDLER_CLOSE_IME_KEYBOARD = 3;
+
+	// ===========================================================
+	// Fields
+	// ===========================================================
+	private GodotView mView;
+	private GodotTextInputWrapper mInputWrapper;
+	private static Handler sHandler;
+	private String mOriginText;
+
+	// ===========================================================
+	// Constructors
+	// ===========================================================
+	public GodotEditText(final Context context) {
+		super(context);
+		this.initView();
+	}
+
+	public GodotEditText(final Context context, final AttributeSet attrs) {
+		super(context, attrs);
+		this.initView();
+	}
+
+	public GodotEditText(final Context context, final AttributeSet attrs, final int defStyle) {
+		super(context, attrs, defStyle);
+		this.initView();
+	}
+	
+	protected void initView() {
+		this.setPadding(0,  0, 0, 0);
+		this.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
+		
+		sHandler = new Handler() {
+			@Override
+			public void handleMessage(final Message msg) {
+				switch (msg.what) {
+					case HANDLER_OPEN_IME_KEYBOARD:
+						{
+							GodotEditText edit = (GodotEditText) msg.obj;
+							String text = edit.mOriginText;
+							if (edit.requestFocus())
+							{
+								edit.removeTextChangedListener(edit.mInputWrapper);
+								edit.setText("");
+								edit.append(text);
+								edit.mInputWrapper.setOriginText(text);
+								edit.addTextChangedListener(edit.mInputWrapper);
+								final InputMethodManager imm = (InputMethodManager) mView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+								imm.showSoftInput(edit, 0);
+							}
+						}
+						break;
+
+					case HANDLER_CLOSE_IME_KEYBOARD:
+						{
+							GodotEditText edit = (GodotEditText) msg.obj;
+							
+							edit.removeTextChangedListener(mInputWrapper);
+							final InputMethodManager imm = (InputMethodManager) mView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+							imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
+							edit.mView.requestFocus();
+						}
+						break;
+				}
+			}
+		};
+	}
+
+	// ===========================================================
+	// Getter & Setter
+	// ===========================================================
+	public void setView(final GodotView view) {
+		this.mView = view;
+		if(mInputWrapper == null)
+			mInputWrapper = new GodotTextInputWrapper(mView, this);
+		this.setOnEditorActionListener(mInputWrapper);
+		view.requestFocus();
+	}
+
+	// ===========================================================
+	// Methods for/from SuperClass/Interfaces
+	// ===========================================================
+    @Override
+	public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {
+		super.onKeyDown(keyCode, keyEvent);
+
+		/* Let GlSurfaceView get focus if back key is input. */
+		if (keyCode == KeyEvent.KEYCODE_BACK) {
+			this.mView.requestFocus();
+		}
+
+		return true;
+	}
+
+	// ===========================================================
+	// Methods
+	// ===========================================================
+	public void showKeyboard(String p_existing_text) {
+		this.mOriginText = p_existing_text;
+		
+		final Message msg = new Message();
+		msg.what = HANDLER_OPEN_IME_KEYBOARD;
+		msg.obj = this;
+		sHandler.sendMessage(msg);
+	}
+
+	public void hideKeyboard() {
+		final Message msg = new Message();
+		msg.what = HANDLER_CLOSE_IME_KEYBOARD;
+		msg.obj = this;
+		sHandler.sendMessage(msg);
+	}
+	
+	// ===========================================================
+	// Inner and Anonymous Classes
+	// ===========================================================
+}

+ 154 - 0
platform/android/java/src/com/android/godot/input/GodotTextInputWrapper.java

@@ -0,0 +1,154 @@
+package com.android.godot.input;
+import android.content.Context;
+import android.text.Editable;
+import android.text.TextWatcher;
+import android.util.Log;
+import android.view.KeyEvent;
+import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.TextView;
+import android.widget.TextView.OnEditorActionListener;
+import com.android.godot.*;
+
+public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListener {
+	// ===========================================================
+	// Constants
+	// ===========================================================
+	private static final String TAG = GodotTextInputWrapper.class.getSimpleName();
+
+	// ===========================================================
+	// Fields
+	// ===========================================================
+	private final GodotView mView;
+	private final GodotEditText mEdit;
+	private String mText;
+	private String mOriginText;
+
+	// ===========================================================
+	// Constructors
+	// ===========================================================
+
+	public GodotTextInputWrapper(final GodotView view, final GodotEditText edit) {
+		this.mView = view;
+		this.mEdit = edit;
+	}
+
+	// ===========================================================
+	// Getter & Setter
+	// ===========================================================
+
+	private boolean isFullScreenEdit() {
+		final TextView textField = this.mEdit;
+		final InputMethodManager imm = (InputMethodManager) textField.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+		return imm.isFullscreenMode();
+	}
+
+	public void setOriginText(final String originText) {
+		this.mOriginText = originText;
+	}
+
+	// ===========================================================
+	// Methods for/from SuperClass/Interfaces
+	// ===========================================================
+
+	@Override
+	public void afterTextChanged(final Editable s) {
+		if (this.isFullScreenEdit()) {
+			return;
+		}
+
+		//if (BuildConfig.DEBUG) {
+			//Log.d(TAG, "afterTextChanged: " + s);
+		//}
+		int nModified = s.length() - this.mText.length();
+		if (nModified > 0) {
+			final String insertText = s.subSequence(this.mText.length(), s.length()).toString();
+			for(int i = 0; i < insertText.length(); i++) {
+				int ch = insertText.codePointAt(i);
+				GodotLib.key(0, ch, false);
+				GodotLib.key(0, ch, true);
+			}
+			/*
+			if (BuildConfig.DEBUG) {
+				Log.d(TAG, "insertText(" + insertText + ")");
+			}
+			*/
+		} else {
+			for (; nModified < 0; ++nModified) {
+				GodotLib.key(KeyEvent.KEYCODE_DEL, 0, false);
+				GodotLib.key(KeyEvent.KEYCODE_DEL, 0, true);
+				/*
+				if (BuildConfig.DEBUG) {
+					Log.d(TAG, "deleteBackward");
+				}
+				*/
+			}
+		}
+		this.mText = s.toString();
+	}
+
+	@Override
+	public void beforeTextChanged(final CharSequence pCharSequence, final int start, final int count, final int after) {
+		/*
+		if (BuildConfig.DEBUG) {
+			Log.d(TAG, "beforeTextChanged(" + pCharSequence + ")start: " + start + ",count: " + count + ",after: " + after);
+		}
+		*/
+		this.mText = pCharSequence.toString();
+	}
+
+	@Override
+	public void onTextChanged(final CharSequence pCharSequence, final int start, final int before, final int count) {
+
+	}
+
+	@Override
+	public boolean onEditorAction(final TextView pTextView, final int pActionID, final KeyEvent pKeyEvent) {
+		if (this.mEdit == pTextView && this.isFullScreenEdit()) {
+			// user press the action button, delete all old text and insert new text
+			for (int i = this.mOriginText.length(); i > 0; i--) {
+				GodotLib.key(KeyEvent.KEYCODE_DEL, 0, false);
+				GodotLib.key(KeyEvent.KEYCODE_DEL, 0, true);
+				/*
+				if (BuildConfig.DEBUG) {
+					Log.d(TAG, "deleteBackward");
+				}
+				*/
+			}
+			String text = pTextView.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';
+			}
+
+			for(int i = 0; i < text.length(); i++) {
+				int ch = text.codePointAt(i);
+				GodotLib.key(0, ch, false);
+				GodotLib.key(0, ch, true);
+			}
+			/*
+			if (BuildConfig.DEBUG) {
+				Log.d(TAG, "insertText(" + insertText + ")");
+			}
+			*/
+		}
+		
+		if (pActionID == EditorInfo.IME_ACTION_DONE) {
+			this.mView.requestFocus();
+		}
+		return false;
+	}
+
+	// ===========================================================
+	// Methods
+	// ===========================================================
+
+	// ===========================================================
+	// Inner and Anonymous Classes
+	// ===========================================================
+}

+ 5 - 0
scene/gui/line_edit.cpp

@@ -79,6 +79,9 @@ void LineEdit::_input_event(InputEvent p_event) {
 				}
 				}
 				selection.creating=false;
 				selection.creating=false;
 				selection.doubleclick=false;
 				selection.doubleclick=false;
+
+                // notify to show soft keyboard
+                notification(NOTIFICATION_FOCUS_ENTER);
 			}
 			}
 			
 			
 			update();				
 			update();				
@@ -208,6 +211,8 @@ void LineEdit::_input_event(InputEvent p_event) {
 					case KEY_RETURN: {
 					case KEY_RETURN: {
 
 
 						emit_signal( "text_entered",text );
 						emit_signal( "text_entered",text );
+                        // notify to hide soft keyboard
+                        notification(NOTIFICATION_FOCUS_EXIT);
 						return;
 						return;
 					} break;
 					} break;
 
 

+ 2 - 0
scene/gui/text_edit.cpp

@@ -857,6 +857,8 @@ void TextEdit::_input_event(const InputEvent& p_input_event) {
 			} else {
 			} else {
 
 
 				selection.selecting_mode=Selection::MODE_NONE;
 				selection.selecting_mode=Selection::MODE_NONE;
+                // notify to show soft keyboard
+                notification(NOTIFICATION_FOCUS_ENTER);
 			}
 			}
 
 
 		} break;
 		} break;