Replace single method anonymous classes with lambdas in Godot Java code

This commit is contained in:
Marcel Admiraal 2021-07-02 18:07:54 +01:00
parent 710effdaad
commit a7825ca12a
6 changed files with 115 additions and 243 deletions

View file

@ -279,29 +279,20 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
editText.setView(mRenderView); editText.setView(mRenderView);
io.setEdit(editText); io.setEdit(editText);
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { view.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
@Override Point fullSize = new Point();
public void onGlobalLayout() { activity.getWindowManager().getDefaultDisplay().getSize(fullSize);
Point fullSize = new Point(); Rect gameSize = new Rect();
activity.getWindowManager().getDefaultDisplay().getSize(fullSize); mRenderView.getView().getWindowVisibleDisplayFrame(gameSize);
Rect gameSize = new Rect(); final int keyboardHeight = fullSize.y - gameSize.bottom;
mRenderView.getView().getWindowVisibleDisplayFrame(gameSize); GodotLib.setVirtualKeyboardHeight(keyboardHeight);
final int keyboardHeight = fullSize.y - gameSize.bottom;
GodotLib.setVirtualKeyboardHeight(keyboardHeight);
}
}); });
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(() -> {
@Override for (GodotPlugin plugin : pluginRegistry.getAllPlugins()) {
public void run() { plugin.onRegisterPluginWithGodotNative();
// Must occur after GodotLib.setup has completed.
for (GodotPlugin plugin : pluginRegistry.getAllPlugins()) {
plugin.onRegisterPluginWithGodotNative();
}
setKeepScreenOn("True".equals(GodotLib.getGlobal("display/window/energy_saving/keep_screen_on")));
} }
setKeepScreenOn("True".equals(GodotLib.getGlobal("display/window/energy_saving/keep_screen_on")));
}); });
// Include the returned non-null views in the Godot view hierarchy. // Include the returned non-null views in the Godot view hierarchy.
@ -314,14 +305,11 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
} }
public void setKeepScreenOn(final boolean p_enabled) { public void setKeepScreenOn(final boolean p_enabled) {
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override if (p_enabled) {
public void run() { getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
if (p_enabled) { } else {
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
} else {
getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
} }
}); });
} }
@ -368,21 +356,14 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
public void alert(final String message, final String title) { public void alert(final String message, final String title) {
final Activity activity = getActivity(); final Activity activity = getActivity();
runOnUiThread(new Runnable() { runOnUiThread(() -> {
@Override AlertDialog.Builder builder = new AlertDialog.Builder(activity);
public void run() { builder.setMessage(message).setTitle(title);
AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setPositiveButton(
builder.setMessage(message).setTitle(title); "OK",
builder.setPositiveButton( (dialog, id) -> dialog.cancel());
"OK", AlertDialog dialog = builder.create();
new DialogInterface.OnClickListener() { dialog.show();
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}); });
} }
@ -753,19 +734,16 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
public void UiChangeListener() { public void UiChangeListener() {
final View decorView = getActivity().getWindow().getDecorView(); final View decorView = getActivity().getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() { decorView.setOnSystemUiVisibilityChangeListener(visibility -> {
@Override if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
public void onSystemUiVisibilityChange(int visibility) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) { decorView.setSystemUiVisibility(
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
decorView.setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
} }
} }
}); });
@ -796,21 +774,18 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
final int typeOfSensor = event.sensor.getType(); final int typeOfSensor = event.sensor.getType();
if (mRenderView != null) { if (mRenderView != null) {
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(() -> {
@Override if (typeOfSensor == Sensor.TYPE_ACCELEROMETER) {
public void run() { GodotLib.accelerometer(-x, y, -z);
if (typeOfSensor == Sensor.TYPE_ACCELEROMETER) { }
GodotLib.accelerometer(-x, y, -z); if (typeOfSensor == Sensor.TYPE_GRAVITY) {
} GodotLib.gravity(-x, y, -z);
if (typeOfSensor == Sensor.TYPE_GRAVITY) { }
GodotLib.gravity(-x, y, -z); if (typeOfSensor == Sensor.TYPE_MAGNETIC_FIELD) {
} GodotLib.magnetometer(-x, y, -z);
if (typeOfSensor == Sensor.TYPE_MAGNETIC_FIELD) { }
GodotLib.magnetometer(-x, y, -z); if (typeOfSensor == Sensor.TYPE_GYROSCOPE) {
} GodotLib.gyroscope(x, -y, z);
if (typeOfSensor == Sensor.TYPE_GYROSCOPE) {
GodotLib.gyroscope(x, -y, z);
}
} }
}); });
} }
@ -845,12 +820,7 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
} }
if (shouldQuit && mRenderView != null) { if (shouldQuit && mRenderView != null) {
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(GodotLib::back);
@Override
public void run() {
GodotLib.back();
}
});
} }
} }
@ -927,16 +897,14 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
; ;
if (cnt == 0) if (cnt == 0)
return false; return false;
mRenderView.queueOnRenderThread(new Runnable() { // This method will be called on the rendering thread:
// This method will be called on the rendering thread: mRenderView.queueOnRenderThread(() -> {
public void run() { for (int i = 0, n = cc.length; i < n; i++) {
for (int i = 0, n = cc.length; i < n; i++) { int keyCode;
int keyCode; if ((keyCode = cc[i]) != 0) {
if ((keyCode = cc[i]) != 0) { // Simulate key down and up...
// Simulate key down and up... GodotLib.key(0, 0, keyCode, true);
GodotLib.key(0, 0, keyCode, true); GodotLib.key(0, 0, keyCode, false);
GodotLib.key(0, 0, keyCode, false);
}
} }
} }
}); });

View file

@ -232,13 +232,10 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
queueEvent(new Runnable() { queueEvent(() -> {
@Override // Resume the renderer
public void run() { godotRenderer.onActivityResumed();
// Resume the renderer GodotLib.focusin();
godotRenderer.onActivityResumed();
GodotLib.focusin();
}
}); });
} }
@ -246,13 +243,10 @@ public class GodotGLRenderView extends GLSurfaceView implements GodotRenderView
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
queueEvent(new Runnable() { queueEvent(() -> {
@Override GodotLib.focusout();
public void run() { // Pause the renderer
GodotLib.focusout(); godotRenderer.onActivityPaused();
// Pause the renderer
godotRenderer.onActivityPaused();
}
}); });
} }
} }

View file

@ -149,13 +149,10 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
queueOnVkThread(new Runnable() { queueOnVkThread(() -> {
@Override // Resume the renderer
public void run() { mRenderer.onVkResume();
// Resume the renderer GodotLib.focusin();
mRenderer.onVkResume();
GodotLib.focusin();
}
}); });
} }
@ -163,13 +160,10 @@ public class GodotVulkanRenderView extends VkSurfaceView implements GodotRenderV
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
queueOnVkThread(new Runnable() { queueOnVkThread(() -> {
@Override GodotLib.focusout();
public void run() { // Pause the renderer
GodotLib.focusout(); mRenderer.onVkPause();
// Pause the renderer
mRenderer.onVkPause();
}
}); });
} }
} }

View file

@ -75,12 +75,7 @@ public class GodotGestureHandler extends GestureDetector.SimpleOnGestureListener
final int x = Math.round(event.getX()); final int x = Math.round(event.getX());
final int y = Math.round(event.getY()); final int y = Math.round(event.getY());
final int buttonMask = event.getButtonState(); final int buttonMask = event.getButtonState();
queueEvent(new Runnable() { queueEvent(() -> GodotLib.doubleTap(buttonMask, x, y));
@Override
public void run() {
GodotLib.doubleTap(buttonMask, x, y);
}
});
return true; return true;
} }
@ -89,12 +84,7 @@ public class GodotGestureHandler extends GestureDetector.SimpleOnGestureListener
//Log.i("GodotGesture", "onScroll"); //Log.i("GodotGesture", "onScroll");
final int x = Math.round(distanceX); final int x = Math.round(distanceX);
final int y = Math.round(distanceY); final int y = Math.round(distanceY);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.scroll(x, y));
@Override
public void run() {
GodotLib.scroll(x, y);
}
});
return true; return true;
} }

View file

@ -97,22 +97,12 @@ public class GodotInputHandler implements InputDeviceListener {
final int button = getGodotButton(keyCode); final int button = getGodotButton(keyCode);
final int godotJoyId = mJoystickIds.get(deviceId); final int godotJoyId = mJoystickIds.get(deviceId);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.joybutton(godotJoyId, button, false));
@Override
public void run() {
GodotLib.joybutton(godotJoyId, button, false);
}
});
} }
} else { } else {
final int scanCode = event.getScanCode(); final int scanCode = event.getScanCode();
final int chr = event.getUnicodeChar(0); final int chr = event.getUnicodeChar(0);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.key(keyCode, scanCode, chr, false));
@Override
public void run() {
GodotLib.key(keyCode, scanCode, chr, false);
}
});
} }
return true; return true;
@ -143,22 +133,12 @@ public class GodotInputHandler implements InputDeviceListener {
final int button = getGodotButton(keyCode); final int button = getGodotButton(keyCode);
final int godotJoyId = mJoystickIds.get(deviceId); final int godotJoyId = mJoystickIds.get(deviceId);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.joybutton(godotJoyId, button, true));
@Override
public void run() {
GodotLib.joybutton(godotJoyId, button, true);
}
});
} }
} else { } else {
final int scanCode = event.getScanCode(); final int scanCode = event.getScanCode();
final int chr = event.getUnicodeChar(0); final int chr = event.getUnicodeChar(0);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.key(keyCode, scanCode, chr, true));
@Override
public void run() {
GodotLib.key(keyCode, scanCode, chr, true);
}
});
} }
return true; return true;
@ -190,19 +170,16 @@ public class GodotInputHandler implements InputDeviceListener {
final int action = event.getActionMasked(); final int action = event.getActionMasked();
final int pointer_idx = event.getPointerId(event.getActionIndex()); final int pointer_idx = event.getPointerId(event.getActionIndex());
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(() -> {
@Override switch (action) {
public void run() { case MotionEvent.ACTION_DOWN:
switch (action) { case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_POINTER_DOWN: {
case MotionEvent.ACTION_POINTER_UP: GodotLib.touch(event.getSource(), action, pointer_idx, evcount, arr);
case MotionEvent.ACTION_POINTER_DOWN: { } break;
GodotLib.touch(event.getSource(), action, pointer_idx, evcount, arr);
} break;
}
} }
}); });
} }
@ -228,13 +205,7 @@ public class GodotInputHandler implements InputDeviceListener {
// save value to prevent repeats // save value to prevent repeats
joystick.axesValues.put(axis, value); joystick.axesValues.put(axis, value);
final int godotAxisIdx = i; final int godotAxisIdx = i;
queueEvent(new Runnable() { queueEvent(() -> GodotLib.joyaxis(godotJoyId, godotAxisIdx, value));
@Override
public void run() {
GodotLib.joyaxis(godotJoyId, godotAxisIdx, value);
//Log.i(tag, "GodotLib.joyaxis("+godotJoyId+", "+godotAxisIdx+", "+value+");");
}
});
} }
} }
@ -244,13 +215,7 @@ public class GodotInputHandler implements InputDeviceListener {
if (joystick.hatX != hatX || joystick.hatY != hatY) { if (joystick.hatX != hatX || joystick.hatY != hatY) {
joystick.hatX = hatX; joystick.hatX = hatX;
joystick.hatY = hatY; joystick.hatY = hatY;
queueEvent(new Runnable() { queueEvent(() -> GodotLib.joyhat(godotJoyId, hatX, hatY));
@Override
public void run() {
GodotLib.joyhat(godotJoyId, hatX, hatY);
//Log.i(tag, "GodotLib.joyhat("+godotJoyId+", "+hatX+", "+hatY+");");
}
});
} }
} }
return true; return true;
@ -259,12 +224,7 @@ public class GodotInputHandler implements InputDeviceListener {
final float x = event.getX(); final float x = event.getX();
final float y = event.getY(); final float y = event.getY();
final int type = event.getAction(); final int type = event.getAction();
queueEvent(new Runnable() { queueEvent(() -> GodotLib.hover(type, x, y));
@Override
public void run() {
GodotLib.hover(type, x, y);
}
});
return true; return true;
} else if (event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)) { } else if (event.isFromSource(InputDevice.SOURCE_MOUSE) || event.isFromSource(InputDevice.SOURCE_MOUSE_RELATIVE)) {
@ -356,12 +316,7 @@ public class GodotInputHandler implements InputDeviceListener {
} }
mJoysticksDevices.put(deviceId, joystick); mJoysticksDevices.put(deviceId, joystick);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.joyconnectionchanged(id, true, joystick.name));
@Override
public void run() {
GodotLib.joyconnectionchanged(id, true, joystick.name);
}
});
} }
@Override @Override
@ -374,12 +329,7 @@ public class GodotInputHandler implements InputDeviceListener {
mJoystickIds.delete(deviceId); mJoystickIds.delete(deviceId);
mJoysticksDevices.delete(deviceId); mJoysticksDevices.delete(deviceId);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.joyconnectionchanged(godotJoyId, false, ""));
@Override
public void run() {
GodotLib.joyconnectionchanged(godotJoyId, false, "");
}
});
} }
@Override @Override
@ -468,12 +418,7 @@ public class GodotInputHandler implements InputDeviceListener {
final float x = event.getX(); final float x = event.getX();
final float y = event.getY(); final float y = event.getY();
final int type = event.getAction(); final int type = event.getAction();
queueEvent(new Runnable() { queueEvent(() -> GodotLib.hover(type, x, y));
@Override
public void run() {
GodotLib.hover(type, x, y);
}
});
return true; return true;
} }
case MotionEvent.ACTION_BUTTON_PRESS: case MotionEvent.ACTION_BUTTON_PRESS:
@ -483,12 +428,7 @@ public class GodotInputHandler implements InputDeviceListener {
final float y = event.getY(); final float y = event.getY();
final int buttonsMask = event.getButtonState(); final int buttonsMask = event.getButtonState();
final int action = event.getAction(); final int action = event.getAction();
queueEvent(new Runnable() { queueEvent(() -> GodotLib.touch(event.getSource(), action, 0, 1, new float[] { 0, x, y }, buttonsMask));
@Override
public void run() {
GodotLib.touch(event.getSource(), action, 0, 1, new float[] { 0, x, y }, buttonsMask);
}
});
return true; return true;
} }
case MotionEvent.ACTION_SCROLL: { case MotionEvent.ACTION_SCROLL: {
@ -498,12 +438,7 @@ public class GodotInputHandler implements InputDeviceListener {
final int action = event.getAction(); final int action = event.getAction();
final float verticalFactor = event.getAxisValue(MotionEvent.AXIS_VSCROLL); final float verticalFactor = event.getAxisValue(MotionEvent.AXIS_VSCROLL);
final float horizontalFactor = event.getAxisValue(MotionEvent.AXIS_HSCROLL); final float horizontalFactor = event.getAxisValue(MotionEvent.AXIS_HSCROLL);
queueEvent(new Runnable() { queueEvent(() -> GodotLib.touch(event.getSource(), action, 0, 1, new float[] { 0, x, y }, buttonsMask, verticalFactor, horizontalFactor));
@Override
public void run() {
GodotLib.touch(event.getSource(), action, 0, 1, new float[] { 0, x, y }, buttonsMask, verticalFactor, horizontalFactor);
}
});
} }
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP: { case MotionEvent.ACTION_UP: {

View file

@ -94,17 +94,14 @@ public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListene
public void beforeTextChanged(final CharSequence pCharSequence, final int start, final int count, final int after) { public void beforeTextChanged(final CharSequence pCharSequence, final int start, final int count, final int after) {
//Log.d(TAG, "beforeTextChanged(" + pCharSequence + ")start: " + start + ",count: " + count + ",after: " + after); //Log.d(TAG, "beforeTextChanged(" + pCharSequence + ")start: " + start + ",count: " + count + ",after: " + after);
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(() -> {
@Override for (int i = 0; i < count; ++i) {
public void run() { GodotLib.key(KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_DEL, 0, true);
for (int i = 0; i < count; ++i) { GodotLib.key(KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_DEL, 0, false);
GodotLib.key(KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_DEL, 0, true);
GodotLib.key(KeyEvent.KEYCODE_DEL, KeyEvent.KEYCODE_DEL, 0, false);
if (mHasSelection) { if (mHasSelection) {
mHasSelection = false; mHasSelection = false;
break; break;
}
} }
} }
}); });
@ -118,18 +115,15 @@ public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListene
for (int i = start; i < start + count; ++i) { for (int i = start; i < start + count; ++i) {
newChars[i - start] = pCharSequence.charAt(i); newChars[i - start] = pCharSequence.charAt(i);
} }
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(() -> {
@Override for (int i = 0; i < count; ++i) {
public void run() { int key = newChars[i];
for (int i = 0; i < count; ++i) { if ((key == '\n') && !mEdit.isMultiline()) {
int key = newChars[i]; // Return keys are handled through action events
if ((key == '\n') && !mEdit.isMultiline()) { continue;
// Return keys are handled through action events
continue;
}
GodotLib.key(0, 0, key, true);
GodotLib.key(0, 0, key, false);
} }
GodotLib.key(0, 0, key, true);
GodotLib.key(0, 0, key, false);
} }
}); });
} }
@ -139,14 +133,11 @@ public class GodotTextInputWrapper implements TextWatcher, OnEditorActionListene
if (mEdit == pTextView && isFullScreenEdit()) { if (mEdit == pTextView && isFullScreenEdit()) {
final String characters = pKeyEvent.getCharacters(); final String characters = pKeyEvent.getCharacters();
mRenderView.queueOnRenderThread(new Runnable() { mRenderView.queueOnRenderThread(() -> {
@Override for (int i = 0; i < characters.length(); i++) {
public void run() { final int ch = characters.codePointAt(i);
for (int i = 0; i < characters.length(); i++) { GodotLib.key(0, 0, ch, true);
final int ch = characters.codePointAt(i); GodotLib.key(0, 0, ch, false);
GodotLib.key(0, 0, ch, true);
GodotLib.key(0, 0, ch, false);
}
} }
}); });
} }