From 277914a8732800095da99ca3560c87b7e1c523f1 Mon Sep 17 00:00:00 2001 From: OpenSauce04 Date: Sat, 22 Jun 2024 20:47:54 +0100 Subject: [PATCH] android: Gamepads can now control the in-game menu --- .../android/activities/EmulationActivity.kt | 23 +++++++++++++------ .../android/fragments/EmulationFragment.kt | 10 ++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/android/app/src/main/java/io/github/lime3ds/android/activities/EmulationActivity.kt b/src/android/app/src/main/java/io/github/lime3ds/android/activities/EmulationActivity.kt index 4e88fa450..b5d8ec940 100644 --- a/src/android/app/src/main/java/io/github/lime3ds/android/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/io/github/lime3ds/android/activities/EmulationActivity.kt @@ -35,6 +35,7 @@ import io.github.lime3ds.android.display.ScreenAdjustmentUtil import io.github.lime3ds.android.features.hotkeys.HotkeyUtility import io.github.lime3ds.android.features.settings.model.SettingsViewModel import io.github.lime3ds.android.features.settings.model.view.InputBindingSetting +import io.github.lime3ds.android.fragments.EmulationFragment import io.github.lime3ds.android.fragments.MessageDialogFragment import io.github.lime3ds.android.utils.ControllerMappingHelper import io.github.lime3ds.android.utils.FileBrowserHelper @@ -49,14 +50,20 @@ class EmulationActivity : AppCompatActivity() { get() = PreferenceManager.getDefaultSharedPreferences(LimeApplication.appContext) private var foregroundService: Intent? = null var isActivityRecreated = false - - private val settingsViewModel: SettingsViewModel by viewModels() private val emulationViewModel: EmulationViewModel by viewModels() + private val settingsViewModel: SettingsViewModel by viewModels() private lateinit var binding: ActivityEmulationBinding private lateinit var screenAdjustmentUtil: ScreenAdjustmentUtil private lateinit var hotkeyUtility: HotkeyUtility + private val emulationFragment: EmulationFragment + get() { + val navHostFragment = + supportFragmentManager.findFragmentById(R.id.fragment_container) as NavHostFragment + return navHostFragment.getChildFragmentManager().fragments.last() as EmulationFragment + } + private var isEmulationRunning: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { @@ -198,6 +205,10 @@ class EmulationActivity : AppCompatActivity() { return false } + if (emulationFragment.isDrawerOpen()) { + return super.dispatchKeyEvent(event) + } + val button = preferences.getInt(InputBindingSetting.getInputButtonKey(event.keyCode), event.keyCode) val action: Int = when (event.action) { @@ -236,11 +247,9 @@ class EmulationActivity : AppCompatActivity() { override fun dispatchGenericMotionEvent(event: MotionEvent): Boolean { // TODO: Move this check into native code - prevents crash if input pressed before starting emulation - if (!NativeLibrary.isRunning()) { - return super.dispatchGenericMotionEvent(event) - } - - if (event.source and InputDevice.SOURCE_CLASS_JOYSTICK == 0) { + if (!NativeLibrary.isRunning() || + (event.source and InputDevice.SOURCE_CLASS_JOYSTICK == 0) || + emulationFragment.isDrawerOpen()) { return super.dispatchGenericMotionEvent(event) } diff --git a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/EmulationFragment.kt b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/EmulationFragment.kt index 44c7c5a71..de4ffb944 100644 --- a/src/android/app/src/main/java/io/github/lime3ds/android/fragments/EmulationFragment.kt +++ b/src/android/app/src/main/java/io/github/lime3ds/android/fragments/EmulationFragment.kt @@ -190,10 +190,16 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram override fun onDrawerOpened(drawerView: View) { binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) + binding.surfaceInputOverlay.isClickable = false + binding.surfaceInputOverlay.isFocusable = false + binding.surfaceInputOverlay.isFocusableInTouchMode = false } override fun onDrawerClosed(drawerView: View) { binding.drawerLayout.setDrawerLockMode(EmulationMenuSettings.drawerLockMode) + binding.surfaceInputOverlay.isClickable = true + binding.surfaceInputOverlay.isFocusable = true + binding.surfaceInputOverlay.isFocusableInTouchMode = true } override fun onDrawerStateChanged(newState: Int) { @@ -412,6 +418,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram setInsets() } + fun isDrawerOpen(): Boolean { + return binding.drawerLayout.isOpen + } + private fun togglePause() { if(emulationState.isPaused) { emulationState.unpause()