aboutsummaryrefslogtreecommitdiff
path: root/platform/android/java/src/org/godotengine/godot/input
diff options
context:
space:
mode:
authorJuan Linietsky2016-01-08 17:53:00 -0300
committerJuan Linietsky2016-01-08 17:53:00 -0300
commit40ba22631bbb7fc4c6b88d01402e132dbaceaf2b (patch)
tree4629c457d390ba7c7eef4c3d31ebb803fc8179ce /platform/android/java/src/org/godotengine/godot/input
parent401622cc229317bd218f070dd07a3bd8db582f16 (diff)
downloadgodot-40ba22631bbb7fc4c6b88d01402e132dbaceaf2b.tar.gz
godot-40ba22631bbb7fc4c6b88d01402e132dbaceaf2b.tar.zst
godot-40ba22631bbb7fc4c6b88d01402e132dbaceaf2b.zip
Renamed godot domain from com.android.godot (which was incorrect) to org.godotengine.godot
Diffstat (limited to 'platform/android/java/src/org/godotengine/godot/input')
-rw-r--r--platform/android/java/src/org/godotengine/godot/input/GodotEditText.java133
-rw-r--r--platform/android/java/src/org/godotengine/godot/input/GodotTextInputWrapper.java154
2 files changed, 287 insertions, 0 deletions
diff --git a/platform/android/java/src/org/godotengine/godot/input/GodotEditText.java b/platform/android/java/src/org/godotengine/godot/input/GodotEditText.java
new file mode 100644
index 000000000..c8ffa74ec
--- /dev/null
+++ b/platform/android/java/src/org/godotengine/godot/input/GodotEditText.java
@@ -0,0 +1,133 @@
+package org.godotengine.godot.input;
+import android.content.Context;
+import android.util.AttributeSet;
+import android.view.KeyEvent;
+import android.widget.EditText;
+import org.godotengine.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
+ // ===========================================================
+}
diff --git a/platform/android/java/src/org/godotengine/godot/input/GodotTextInputWrapper.java b/platform/android/java/src/org/godotengine/godot/input/GodotTextInputWrapper.java
new file mode 100644
index 000000000..64d8826b4
--- /dev/null
+++ b/platform/android/java/src/org/godotengine/godot/input/GodotTextInputWrapper.java
@@ -0,0 +1,154 @@
+package org.godotengine.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 org.godotengine.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, true);
+ GodotLib.key(0, ch, false);
+ }
+ /*
+ if (BuildConfig.DEBUG) {
+ Log.d(TAG, "insertText(" + insertText + ")");
+ }
+ */
+ } else {
+ for (; nModified < 0; ++nModified) {
+ GodotLib.key(KeyEvent.KEYCODE_DEL, 0, true);
+ GodotLib.key(KeyEvent.KEYCODE_DEL, 0, false);
+ /*
+ 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, true);
+ GodotLib.key(KeyEvent.KEYCODE_DEL, 0, false);
+ /*
+ 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, true);
+ GodotLib.key(0, ch, false);
+ }
+ /*
+ if (BuildConfig.DEBUG) {
+ Log.d(TAG, "insertText(" + insertText + ")");
+ }
+ */
+ }
+
+ if (pActionID == EditorInfo.IME_ACTION_DONE) {
+ this.mView.requestFocus();
+ }
+ return false;
+ }
+
+ // ===========================================================
+ // Methods
+ // ===========================================================
+
+ // ===========================================================
+ // Inner and Anonymous Classes
+ // ===========================================================
+}