Bläddra i källkod

Merge pull request #40791 from nekomatata/android-keyboard-cleaning-3.2

[3.2] Move PopupWindow logic to GodotEditText on Android
Rémi Verschelde 5 år sedan
förälder
incheckning
c49be9bf84

+ 5 - 40
platform/android/java/lib/src/org/godotengine/godot/Godot.java

@@ -53,8 +53,6 @@ import android.content.SharedPreferences.Editor;
 import android.content.pm.ConfigurationInfo;
 import android.content.pm.PackageManager;
 import android.content.pm.PackageManager.NameNotFoundException;
-import android.graphics.Point;
-import android.graphics.Rect;
 import android.hardware.Sensor;
 import android.hardware.SensorEvent;
 import android.hardware.SensorEventListener;
@@ -69,19 +67,16 @@ import android.os.VibrationEffect;
 import android.os.Vibrator;
 import android.provider.Settings.Secure;
 import android.view.Display;
-import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.MotionEvent;
 import android.view.Surface;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewGroup.LayoutParams;
-import android.view.ViewTreeObserver;
 import android.view.Window;
 import android.view.WindowManager;
 import android.widget.Button;
 import android.widget.FrameLayout;
-import android.widget.PopupWindow;
 import android.widget.ProgressBar;
 import android.widget.TextView;
 
@@ -250,7 +245,7 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
 	public GodotView mView;
 	private boolean godot_initialized = false;
 
-	private PopupWindow mKeyboardWindow;
+	private GodotEditText mEditText;
 
 	private SensorManager mSensorManager;
 	private Sensor mAccelerometer;
@@ -320,41 +315,11 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
 		layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
 		setContentView(layout);
 
-		// Create a popup window with an invisible layout for the virtual keyboard,
-		// so the view can be resized to get the vk height without resizing the main godot view.
-		final FrameLayout keyboardLayout = new FrameLayout(this);
-		keyboardLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
-		keyboardLayout.setVisibility(View.INVISIBLE);
-		mKeyboardWindow = new PopupWindow(keyboardLayout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
-		mKeyboardWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
-		mKeyboardWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
-		mKeyboardWindow.setFocusable(true); // for the text edit to work
-		mKeyboardWindow.setTouchable(false); // inputs need to go through
-
-		// GodotEditText layout
-		GodotEditText edittext = new GodotEditText(this);
-		edittext.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
-		edittext.setKeyboardView(keyboardLayout);
-		// ...add to keyboard layout
-		keyboardLayout.addView(edittext);
-
 		mView = new GodotView(this, xrMode, use_gl3, use_32_bits, use_debug_opengl);
 		layout.addView(mView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
-		edittext.setView(mView);
-		io.setEdit(edittext);
 
-		keyboardLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
-			@Override
-			public void onGlobalLayout() {
-				Point fullSize = new Point();
-				getWindowManager().getDefaultDisplay().getSize(fullSize);
-				Rect gameSize = new Rect();
-				mKeyboardWindow.getContentView().getWindowVisibleDisplayFrame(gameSize);
-
-				final int keyboardHeight = fullSize.y - gameSize.bottom;
-				GodotLib.setVirtualKeyboardHeight(keyboardHeight);
-			}
-		});
+		mEditText = new GodotEditText(this, mView);
+		io.setEdit(mEditText);
 
 		final String[] current_command_line = command_line;
 		mView.queueEvent(new Runnable() {
@@ -733,14 +698,14 @@ public abstract class Godot extends FragmentActivity implements SensorEventListe
 		mView.post(new Runnable() {
 			@Override
 			public void run() {
-				mKeyboardWindow.showAtLocation(getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, 0);
+				mEditText.onInitView();
 			}
 		});
 	}
 
 	@Override
 	protected void onDestroy() {
-		mKeyboardWindow.dismiss();
+		mEditText.onDestroyView();
 
 		for (int i = 0; i < singleton_count; i++) {
 			singletons[i].onMainDestroy();

+ 51 - 27
platform/android/java/lib/src/org/godotengine/godot/input/GodotEditText.java

@@ -32,17 +32,27 @@ package org.godotengine.godot.input;
 
 import org.godotengine.godot.*;
 
+import android.app.Activity;
 import android.content.Context;
+import android.graphics.Point;
+import android.graphics.Rect;
 import android.os.Handler;
 import android.os.Message;
 import android.text.InputFilter;
 import android.text.InputType;
 import android.util.AttributeSet;
+import android.view.Gravity;
 import android.view.KeyEvent;
 import android.view.View;
+import android.view.ViewGroup;
+import android.view.ViewGroup.LayoutParams;
+import android.view.ViewTreeObserver;
+import android.view.WindowManager;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.EditText;
+import android.widget.FrameLayout;
+import android.widget.PopupWindow;
 
 import java.lang.ref.WeakReference;
 
@@ -58,6 +68,7 @@ public class GodotEditText extends EditText {
 	// ===========================================================
 	private GodotView mView;
 	private View mKeyboardView;
+	private PopupWindow mKeyboardWindow;
 	private GodotTextInputWrapper mInputWrapper;
 	private EditHandler sHandler = new EditHandler(this);
 	private String mOriginText;
@@ -82,24 +93,52 @@ public class GodotEditText extends EditText {
 	// ===========================================================
 	// Constructors
 	// ===========================================================
-	public GodotEditText(final Context context) {
+	public GodotEditText(final Context context, final GodotView view) {
 		super(context);
-		this.initView();
-	}
 
-	public GodotEditText(final Context context, final AttributeSet attrs) {
-		super(context, attrs);
-		this.initView();
+		setPadding(0, 0, 0, 0);
+		setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI | EditorInfo.IME_ACTION_DONE);
+		setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
+
+		mView = view;
+		mInputWrapper = new GodotTextInputWrapper(mView, this);
+		setOnEditorActionListener(mInputWrapper);
+		view.requestFocus();
+
+		// Create a popup window with an invisible layout for the virtual keyboard,
+		// so the view can be resized to get the vk height without resizing the main godot view.
+		final FrameLayout keyboardLayout = new FrameLayout(context);
+		keyboardLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
+		keyboardLayout.setVisibility(View.INVISIBLE);
+		keyboardLayout.addView(this);
+		mKeyboardView = keyboardLayout;
+
+		mKeyboardWindow = new PopupWindow(keyboardLayout, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
+		mKeyboardWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
+		mKeyboardWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
+		mKeyboardWindow.setFocusable(true); // for the text edit to work
+		mKeyboardWindow.setTouchable(false); // inputs need to go through
+
+		keyboardLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
+			@Override
+			public void onGlobalLayout() {
+				Point fullSize = new Point();
+				((Activity)mView.getContext()).getWindowManager().getDefaultDisplay().getSize(fullSize);
+				Rect gameSize = new Rect();
+				mKeyboardWindow.getContentView().getWindowVisibleDisplayFrame(gameSize);
+
+				final int keyboardHeight = fullSize.y - gameSize.bottom;
+				GodotLib.setVirtualKeyboardHeight(keyboardHeight);
+			}
+		});
 	}
 
-	public GodotEditText(final Context context, final AttributeSet attrs, final int defStyle) {
-		super(context, attrs, defStyle);
-		this.initView();
+	public void onInitView() {
+		mKeyboardWindow.showAtLocation(mView, Gravity.NO_GRAVITY, 0, 0);
 	}
 
-	protected void initView() {
-		this.setPadding(0, 0, 0, 0);
-		this.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI | EditorInfo.IME_ACTION_DONE);
+	public void onDestroyView() {
+		mKeyboardWindow.dismiss();
 	}
 
 	public boolean isMultiline() {
@@ -153,21 +192,6 @@ public class GodotEditText extends EditText {
 		p_edit_text.setFilters(filters);
 	}
 
-	// ===========================================================
-	// Getter & Setter
-	// ===========================================================
-	public void setView(final GodotView view) {
-		this.mView = view;
-		if (mInputWrapper == null)
-			mInputWrapper = new GodotTextInputWrapper(mView, this);
-		this.setOnEditorActionListener(mInputWrapper);
-		view.requestFocus();
-	}
-
-	public void setKeyboardView(final View keyboardView) {
-		this.mKeyboardView = keyboardView;
-	}
-
 	// ===========================================================
 	// Methods for/from SuperClass/Interfaces
 	// ===========================================================