Ported: Migrate the Godot Editor java source file to Kotlin. - m4gr3d

5946b4bdc6
This commit is contained in:
Relintai 2022-07-29 09:00:31 +02:00
parent b829fff345
commit d6e3a9e014
3 changed files with 77 additions and 86 deletions

View File

@ -1,5 +1,5 @@
/*************************************************************************/ /*************************************************************************/
/* PandemoniumEditor.java */ /* PandemoniumEditor.kt */
/*************************************************************************/ /*************************************************************************/
/* This file is part of: */ /* This file is part of: */
/* GODOT ENGINE */ /* GODOT ENGINE */
@ -28,23 +28,20 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package net.relintai.pandemonium.editor; package net.relintai.pandemonium.editor
import net.relintai.pandemonium.pandemonium.FullScreenPandemoniumApp; import net.relintai.pandemonium.pandemonium.FullScreenPandemoniumApp
import net.relintai.pandemonium.pandemonium.utils.PermissionsUtil; import net.relintai.pandemonium.pandemonium.utils.PermissionsUtil
import android.content.Intent; import android.content.Intent
import android.os.Build; import android.os.Build
import android.os.Bundle; import android.os.Bundle
import android.os.Debug; import android.os.Debug
import androidx.annotation.Nullable;
import androidx.window.layout.WindowMetrics;
import androidx.window.layout.WindowMetricsCalculator; import androidx.window.layout.WindowMetricsCalculator;
import java.util.ArrayList; import java.util.*
import java.util.Arrays; import kotlin.math.min
import java.util.List;
/** /**
* Base class for the Pandemonium Android Editor activities. * Base class for the Pandemonium Android Editor activities.
@ -55,100 +52,95 @@ import java.util.List;
* *
* It also plays the role of the primary editor window. * It also plays the role of the primary editor window.
*/ */
public class PandemoniumEditor extends FullScreenPandemoniumApp { open class PandemoniumEditor : FullScreenPandemoniumApp() {
private static final boolean WAIT_FOR_DEBUGGER = false; companion object {
private static final String COMMAND_LINE_PARAMS = "command_line_params"; private const val WAIT_FOR_DEBUGGER = false
private const val COMMAND_LINE_PARAMS = "command_line_params"
private static final String EDITOR_ARG = "--editor"; private const val EDITOR_ARG = "--editor"
private static final String PROJECT_MANAGER_ARG = "--project-manager"; private const val PROJECT_MANAGER_ARG = "--project-manager"
}
private final List<String> commandLineParams = new ArrayList<>(); private val commandLineParams = ArrayList<String>()
@Override override fun onCreate(savedInstanceState : Bundle?) {
public void onCreate(Bundle savedInstanceState) {
PermissionsUtil.requestManifestPermissions(this); PermissionsUtil.requestManifestPermissions(this);
String[] params = getIntent().getStringArrayExtra(COMMAND_LINE_PARAMS); //String[]
val params = getIntent().getStringArrayExtra(COMMAND_LINE_PARAMS);
updateCommandLineParams(params); updateCommandLineParams(params);
if (BuildConfig.BUILD_TYPE.equals("debug") && WAIT_FOR_DEBUGGER) { if (BuildConfig.BUILD_TYPE == "debug" && WAIT_FOR_DEBUGGER) {
Debug.waitForDebugger(); Debug.waitForDebugger();
} }
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
private void updateCommandLineParams(@Nullable String[] args) { private fun updateCommandLineParams(args: Array<String>?) {
// Update the list of command line params with the new args // Update the list of command line params with the new args
commandLineParams.clear(); commandLineParams.clear()
if (args != null && args.length > 0) { if (args != null && args.isNotEmpty()) {
commandLineParams.addAll(Arrays.asList(args)); commandLineParams.addAll(listOf(*args))
} }
} }
@Override override fun onNewGodotInstanceRequested(args: Array<String>) {
public List<String> getCommandLine() {
return commandLineParams;
}
@Override
public void onNewPandemoniumInstanceRequested(String[] args) {
// Parse the arguments to figure out which activity to start. // Parse the arguments to figure out which activity to start.
Class<?> targetClass = PandemoniumGame.class; var targetClass: Class<*> = PandemoniumGame::class.java
// Whether we should launch the new godot instance in an adjacent window // Whether we should launch the new godot instance in an adjacent window
// https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_LAUNCH_ADJACENT // https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_LAUNCH_ADJACENT
boolean launchAdjacent = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (isInMultiWindowMode() || isLargeScreen()); var launchAdjacent =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && (isInMultiWindowMode || isLargeScreen)
for (String arg : args) { for (arg in args) {
if (EDITOR_ARG.equals(arg)) { if (EDITOR_ARG == arg) {
targetClass = PandemoniumEditor.class; targetClass = PandemoniumEditor::class.java
launchAdjacent = false; launchAdjacent = false
break; break
} }
if (PROJECT_MANAGER_ARG.equals(arg)) { if (PROJECT_MANAGER_ARG == arg) {
targetClass = PandemoniumProjectManager.class; targetClass = PandemoniumProjectManager::class.java
launchAdjacent = false; launchAdjacent = false
break; break
} }
} }
// Launch a new activity // Launch a new activity
Intent newInstance = new Intent(this, targetClass) val newInstance = Intent(this, targetClass)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.putExtra(COMMAND_LINE_PARAMS, args); .putExtra(COMMAND_LINE_PARAMS, args)
if (launchAdjacent) { if (launchAdjacent) {
newInstance.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT); newInstance.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT)
} }
startActivity(newInstance)
startActivity(newInstance);
} }
protected boolean isLargeScreen() {
WindowMetrics metrics =
WindowMetricsCalculator.getOrCreate().computeMaximumWindowMetrics(this);
// Get the screen's density scale // Get the screen's density scale
float scale = getResources().getDisplayMetrics().density; protected val isLargeScreen: Boolean
// Get the minimum window size // Correspond to the EXPANDED window size class.
get() {
val metrics = WindowMetricsCalculator.getOrCreate().computeMaximumWindowMetrics(this)
// Get the screen's density scale
val scale = resources.displayMetrics.density
// Get the minimum window size // Get the minimum window size
float minSize = Math.min(metrics.getBounds().width(), metrics.getBounds().height()); val minSize = min(metrics.bounds.width(), metrics.bounds.height()).toFloat()
float minSizeDp = minSize / scale; val minSizeDp = minSize / scale
return minSizeDp >= 840f; // Correspond to the EXPANDED window size class. return minSizeDp >= 840f // Correspond to the EXPANDED window size class.
} }
@Override override fun setRequestedOrientation(requestedOrientation: Int) {
public void setRequestedOrientation(int requestedOrientation) {
if (!overrideOrientationRequest()) { if (!overrideOrientationRequest()) {
super.setRequestedOrientation(requestedOrientation); super.setRequestedOrientation(requestedOrientation)
} }
} }
/** /**
* The Android Editor sets its own orientation via its AndroidManifest * The Android Editor sets its own orientation via its AndroidManifest
*/ */
protected boolean overrideOrientationRequest() { protected open fun overrideOrientationRequest() = true
return true;
}
} }

View File

@ -1,5 +1,5 @@
/*************************************************************************/ /*************************************************************************/
/* PandemoniumGame.java */ /* PandemoniumGame.kt */
/*************************************************************************/ /*************************************************************************/
/* This file is part of: */ /* This file is part of: */
/* GODOT ENGINE */ /* GODOT ENGINE */
@ -28,13 +28,11 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package net.relintai.pandemonium.editor; package net.relintai.pandemonium.editor
/** /**
* Drives the 'run project' window of the Pandemonium Editor. * Drives the 'run project' window of the Pandemonium Editor.
*/ */
public class PandemoniumGame extends PandemoniumEditor { class PandemoniumGame : PandemoniumEditor {
protected boolean overrideOrientationRequest() { override fun overrideOrientationRequest() = false
return false;
}
} }

View File

@ -1,5 +1,5 @@
/*************************************************************************/ /*************************************************************************/
/* PandemoniumProjectManager.java */ /* PandemoniumProjectManager.kt */
/*************************************************************************/ /*************************************************************************/
/* This file is part of: */ /* This file is part of: */
/* GODOT ENGINE */ /* GODOT ENGINE */
@ -28,14 +28,15 @@
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/ /*************************************************************************/
package net.relintai.pandemonium.editor; package net.relintai.pandemonium.editor
/** /**
* Launcher activity for the Pandemonium Android Editor. * Launcher activity for the Pandemonium Android Editor.
* *
* It presents the user with the project manager interface. * It presents the user with the project manager interface.
* Upon selection of a project, this activity (via its parent logic) starts the * Upon selection of a project, this activity (via its parent logic) starts the
* {@link PandemoniumEditor} activity. * [PandemoniumEditor] activity.
*/ */
public class PandemoniumProjectManager extends PandemoniumEditor {
} class PandemoniumProjectManager : PandemoniumEditor()