From fb28f9fd96e8a3370463263753daae778c99d381 Mon Sep 17 00:00:00 2001
From: Abandoned Cart <twistedumbrella@gmail.com>
Date: Mon, 12 Jun 2023 08:49:11 -0400
Subject: [PATCH] android: Set portrait default control params

---
 .../yuzu_emu/fragments/EmulationFragment.kt   |   4 +
 .../org/yuzu/yuzu_emu/overlay/InputOverlay.kt | 163 ++++++++++++++++--
 .../main/res/layout/fragment_emulation.xml    |   4 +-
 .../app/src/main/res/values/integers.xml      |  32 ++++
 4 files changed, 186 insertions(+), 17 deletions(-)

diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt
index 4da54c28f..c65db59f8 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt
@@ -334,6 +334,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
                     // Prevent touch regions from being displayed in the hinge
                     binding.overlayContainer.layoutParams.height = it.bounds.bottom - 48.toPx
                     binding.overlayContainer.updatePadding(0, 0, 0, 24.toPx)
+                    binding.inGameMenu.layoutParams.height = it.bounds.bottom
+                    refreshInputOverlay()
                 }
             }
             it.isSeparating
@@ -342,10 +344,12 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback {
             binding.emulationContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
             binding.overlayContainer.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
             binding.overlayContainer.updatePadding(0, 0, 0, 0)
+            binding.inGameMenu.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
             updateScreenLayout()
         }
         binding.emulationContainer.requestLayout()
         binding.overlayContainer.requestLayout()
+        binding.inGameMenu.requestLayout()
     }
 
     override fun surfaceCreated(holder: SurfaceHolder) {
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt
index ed84ba5f0..ae5d935d0 100644
--- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt
+++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt
@@ -3,7 +3,6 @@
 
 package org.yuzu.yuzu_emu.overlay
 
-import android.annotation.SuppressLint
 import android.app.Activity
 import android.content.Context
 import android.content.SharedPreferences
@@ -16,14 +15,12 @@ import android.graphics.drawable.Drawable
 import android.graphics.drawable.VectorDrawable
 import android.os.Build
 import android.util.AttributeSet
-import android.util.Rational
 import android.view.HapticFeedbackConstants
 import android.view.MotionEvent
 import android.view.SurfaceView
 import android.view.View
 import android.view.View.OnTouchListener
 import android.view.WindowInsets
-import android.view.WindowManager
 import androidx.core.content.ContextCompat
 import androidx.preference.PreferenceManager
 import androidx.window.layout.WindowMetricsCalculator
@@ -36,7 +33,6 @@ import org.yuzu.yuzu_emu.features.settings.model.Settings
 import org.yuzu.yuzu_emu.utils.EmulationMenuSettings
 import kotlin.math.max
 import kotlin.math.min
-import kotlin.math.roundToInt
 
 /**
  * Draws the interactive input overlay on top of the
@@ -237,11 +233,6 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context
         val fingerPositionX = event.getX(pointerIndex).toInt()
         val fingerPositionY = event.getY(pointerIndex).toInt()
 
-        val orientation = if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT)
-            "-Portrait"
-        else
-            ""
-
         for (button in overlayButtons) {
             // Determine the button state to apply based on the MotionEvent action flag.
             when (event.action and MotionEvent.ACTION_MASK) {
@@ -538,10 +529,6 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context
         overlayButtons.clear()
         overlayDpads.clear()
         overlayJoysticks.clear()
-        val orientation = if (resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT)
-            "-Portrait"
-        else
-            ""
 
         // Add all the enabled overlay items back to the HashSet.
         if (EmulationMenuSettings.showOverlay) {
@@ -566,7 +553,10 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context
 
     private fun defaultOverlay() {
         if (!preferences.getBoolean(Settings.PREF_OVERLAY_INIT, false)) {
-            defaultOverlayLandscape()
+            if (orientation == portrait)
+                defaultOverlayPortrait()
+            else
+                defaultOverlayLandscape()
         }
 
         resetButtonPlacement()
@@ -576,10 +566,141 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context
     }
 
     fun resetButtonPlacement() {
-        defaultOverlayLandscape()
+        if (orientation == portrait)
+            defaultOverlayPortrait()
+        else
+            defaultOverlayLandscape()
         refreshControls()
     }
 
+    private fun defaultOverlayPortrait() {
+        // Each value represents the position of the button in relation to the screen size without insets.
+        preferences.edit()
+            .putFloat(
+                ButtonType.BUTTON_A.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_A_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_A.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_A_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_B.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_B_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_B.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_B_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_X.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_X_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_X.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_X_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_Y.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_Y_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_Y.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_Y_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_ZL.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_ZL_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_ZL.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_ZL_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_ZR.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_ZR_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_ZR.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_ZR_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.DPAD_UP.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_DPAD_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.DPAD_UP.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_DPAD_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_L.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_L_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_L.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_L_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_R.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_R_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.TRIGGER_R.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_TRIGGER_R_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_PLUS.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_PLUS_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_PLUS.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_PLUS_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_MINUS.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_MINUS_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_MINUS.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_MINUS_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_HOME.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_HOME_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_HOME.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_HOME_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_CAPTURE.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_BUTTON_CAPTURE_PORTRAIT_X)
+                    .toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.BUTTON_CAPTURE.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_BUTTON_CAPTURE_PORTRAIT_Y)
+                    .toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.STICK_R.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_STICK_R_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.STICK_R.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_STICK_R_PORTRAIT_Y).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.STICK_L.toString() + "$portrait-X",
+                resources.getInteger(R.integer.SWITCH_STICK_L_PORTRAIT_X).toFloat() / 1000
+            )
+            .putFloat(
+                ButtonType.STICK_L.toString() + "$portrait-Y",
+                resources.getInteger(R.integer.SWITCH_STICK_L_PORTRAIT_Y).toFloat() / 1000
+            )
+            .apply()
+    }
+
     private fun defaultOverlayLandscape() {
         // Each value represents the position of the button in relation to the screen size without insets.
         preferences.edit()
@@ -712,10 +833,22 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context
         return inEditMode
     }
 
+    override fun onConfigurationChanged(newConfig: Configuration?) {
+        super.onConfigurationChanged(newConfig)
+        orientation =
+            if (newConfig?.orientation == Configuration.ORIENTATION_PORTRAIT)
+                portrait
+            else
+                ""
+    }
+
     companion object {
         private val preferences: SharedPreferences =
             PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext)
 
+        private const val portrait = "-Portrait"
+        private var orientation = ""
+
         /**
          * Resizes a [Bitmap] by a given scale factor
          *
diff --git a/src/android/app/src/main/res/layout/fragment_emulation.xml b/src/android/app/src/main/res/layout/fragment_emulation.xml
index ccd0f4c50..01f6b9fe4 100644
--- a/src/android/app/src/main/res/layout/fragment_emulation.xml
+++ b/src/android/app/src/main/res/layout/fragment_emulation.xml
@@ -39,7 +39,7 @@
                 android:id="@+id/surface_input_overlay"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"
-                android:layout_gravity="bottom"
+                android:layout_gravity="center"
                 android:focusable="true"
                 android:focusableInTouchMode="true" />
 
@@ -72,7 +72,7 @@
         android:id="@+id/in_game_menu"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
-        android:layout_gravity="start"
+        android:layout_gravity="start|bottom"
         app:headerLayout="@layout/header_in_game"
         app:menu="@menu/menu_in_game" />
 
diff --git a/src/android/app/src/main/res/values/integers.xml b/src/android/app/src/main/res/values/integers.xml
index bc614b81d..326867664 100644
--- a/src/android/app/src/main/res/values/integers.xml
+++ b/src/android/app/src/main/res/values/integers.xml
@@ -2,6 +2,38 @@
 <resources>
     <integer name="game_title_lines">2</integer>
 
+    <!-- Default SWITCH portrait layout -->
+    <integer name="SWITCH_BUTTON_A_PORTRAIT_X">830</integer>
+    <integer name="SWITCH_BUTTON_A_PORTRAIT_Y">820</integer>
+    <integer name="SWITCH_BUTTON_B_PORTRAIT_X">730</integer>
+    <integer name="SWITCH_BUTTON_B_PORTRAIT_Y">870</integer>
+    <integer name="SWITCH_BUTTON_X_PORTRAIT_X">730</integer>
+    <integer name="SWITCH_BUTTON_X_PORTRAIT_Y">770</integer>
+    <integer name="SWITCH_BUTTON_Y_PORTRAIT_X">630</integer>
+    <integer name="SWITCH_BUTTON_Y_PORTRAIT_Y">820</integer>
+    <integer name="SWITCH_STICK_L_PORTRAIT_X">170</integer>
+    <integer name="SWITCH_STICK_L_PORTRAIT_Y">640</integer>
+    <integer name="SWITCH_STICK_R_PORTRAIT_X">820</integer>
+    <integer name="SWITCH_STICK_R_PORTRAIT_Y">640</integer>
+    <integer name="SWITCH_TRIGGER_L_PORTRAIT_X">140</integer>
+    <integer name="SWITCH_TRIGGER_L_PORTRAIT_Y">240</integer>
+    <integer name="SWITCH_TRIGGER_R_PORTRAIT_X">860</integer>
+    <integer name="SWITCH_TRIGGER_R_PORTRAIT_Y">240</integer>
+    <integer name="SWITCH_TRIGGER_ZL_PORTRAIT_X">140</integer>
+    <integer name="SWITCH_TRIGGER_ZL_PORTRAIT_Y">180</integer>
+    <integer name="SWITCH_TRIGGER_ZR_PORTRAIT_X">860</integer>
+    <integer name="SWITCH_TRIGGER_ZR_PORTRAIT_Y">180</integer>
+    <integer name="SWITCH_BUTTON_MINUS_PORTRAIT_X">440</integer>
+    <integer name="SWITCH_BUTTON_MINUS_PORTRAIT_Y">950</integer>
+    <integer name="SWITCH_BUTTON_PLUS_PORTRAIT_X">560</integer>
+    <integer name="SWITCH_BUTTON_PLUS_PORTRAIT_Y">950</integer>
+    <integer name="SWITCH_BUTTON_HOME_PORTRAIT_X">600</integer>
+    <integer name="SWITCH_BUTTON_HOME_PORTRAIT_Y">950</integer>
+    <integer name="SWITCH_BUTTON_CAPTURE_PORTRAIT_X">400</integer>
+    <integer name="SWITCH_BUTTON_CAPTURE_PORTRAIT_Y">950</integer>
+    <integer name="SWITCH_BUTTON_DPAD_PORTRAIT_X">240</integer>
+    <integer name="SWITCH_BUTTON_DPAD_PORTRAIT_Y">820</integer>
+
     <!-- Default SWITCH landscape layout -->
     <integer name="SWITCH_BUTTON_A_X">760</integer>
     <integer name="SWITCH_BUTTON_A_Y">790</integer>