From 4a22e007f61304f600e2f61352ec6093cbb0609f Mon Sep 17 00:00:00 2001 From: Anish Mishra Date: Thu, 13 Feb 2025 13:54:40 +0530 Subject: [PATCH] Android Editor: Fix embed mode orientation handling --- .../editor/embed/EmbeddedGodotGame.kt | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/platform/android/java/editor/src/main/java/org/godotengine/editor/embed/EmbeddedGodotGame.kt b/platform/android/java/editor/src/main/java/org/godotengine/editor/embed/EmbeddedGodotGame.kt index 4383b0b816b..b9ee9afed78 100644 --- a/platform/android/java/editor/src/main/java/org/godotengine/editor/embed/EmbeddedGodotGame.kt +++ b/platform/android/java/editor/src/main/java/org/godotengine/editor/embed/EmbeddedGodotGame.kt @@ -30,6 +30,7 @@ package org.godotengine.editor.embed +import android.content.pm.ActivityInfo import android.os.Bundle import android.view.Gravity import android.view.MotionEvent @@ -63,6 +64,9 @@ class EmbeddedGodotGame : GodotGame() { private var layoutWidthInPx = 0 private var layoutHeightInPx = 0 + private var gameRequestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + private var isFullscreen = false + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -81,13 +85,26 @@ class EmbeddedGodotGame : GodotGame() { window.attributes = layoutParams } + override fun setRequestedOrientation(requestedOrientation: Int) { + // Allow orientation change only if fullscreen mode is active + // or if the requestedOrientation is landscape (i.e switching to default). + if (isFullscreen || requestedOrientation == ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE) { + super.setRequestedOrientation(requestedOrientation) + } else { + // Cache the requestedOrientation to apply when switching to fullscreen. + gameRequestedOrientation = requestedOrientation + } + } + override fun dispatchTouchEvent(event: MotionEvent): Boolean { when (event.actionMasked) { MotionEvent.ACTION_OUTSIDE -> { - if (gameMenuFragment?.isAlwaysOnTop() == true) { - enterPiPMode() - } else { - minimizeGameWindow() + if (!isFullscreen) { + if (gameMenuFragment?.isAlwaysOnTop() == true) { + enterPiPMode() + } else { + minimizeGameWindow() + } } } @@ -127,12 +144,18 @@ class EmbeddedGodotGame : GodotGame() { override fun onFullScreenUpdated(enabled: Boolean) { godot?.enableImmersiveMode(enabled) + isFullscreen = enabled if (enabled) { layoutWidthInPx = FULL_SCREEN_WIDTH layoutHeightInPx = FULL_SCREEN_HEIGHT + requestedOrientation = gameRequestedOrientation } else { layoutWidthInPx = defaultWidthInPx layoutHeightInPx = defaultHeightInPx + + // Cache the last used orientation in fullscreen to reapply when re-entering fullscreen. + gameRequestedOrientation = requestedOrientation + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_LANDSCAPE } updateWindowDimensions(layoutWidthInPx, layoutHeightInPx) }