Add an option to copy global settings to per-game ones

This commit is contained in:
lynxnb 2023-02-24 12:37:36 +01:00 committed by Niccolò Betto
parent 485bd2031c
commit aa1da257f8
4 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,40 @@
/*
* SPDX-License-Identifier: MPL-2.0
* Copyright © 2023 Skyline Team and Contributors (https://github.com/skyline-emu/)
*/
package emu.skyline.preference
import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import androidx.preference.Preference
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import emu.skyline.R
import emu.skyline.settings.EmulationSettings
/**
* Copies global emulation settings into the current shared preferences, showing a dialog to confirm the action
* This preference recreates the activity to update the UI after modifying shared preferences
*/
class CopyGlobalSettingsPreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = androidx.preference.R.attr.preferenceStyle) : Preference(context, attrs, defStyleAttr) {
init {
setOnPreferenceClickListener {
MaterialAlertDialogBuilder(context)
.setTitle(title)
.setMessage(R.string.copy_global_settings_warning)
.setPositiveButton(android.R.string.ok) { _, _ ->
// Copy global settings to the current ones
val emulationSettings = EmulationSettings.forPrefName(preferenceManager.sharedPreferencesName)
emulationSettings.copyFromGlobal()
// Recreate the activity to update the UI after modifying shared preferences
(context as? Activity)?.recreate()
}
.setNegativeButton(android.R.string.cancel, null)
.show()
true
}
}
}

View File

@ -10,6 +10,8 @@ import android.content.pm.ActivityInfo
import emu.skyline.R
import emu.skyline.SkylineApplication
import emu.skyline.utils.sharedPreferences
import kotlin.reflect.KMutableProperty
import kotlin.reflect.full.memberProperties
/**
* Settings used during emulation. Use [forTitleId] to retrieve settings for a given `titleId`.
@ -56,6 +58,23 @@ class EmulationSettings private constructor(context : Context, prefName : String
// Debug
var validationLayer by sharedPreferences(context, false, prefName = prefName)
/**
* Copies all settings from the global settings to this instance.
* This is a no-op if the instance this is called on is the global one
*/
fun copyFromGlobal() {
if (isGlobal)
return
for (prop in EmulationSettings::class.memberProperties) {
if (prop.name == "useCustomSettings")
continue
if (prop is KMutableProperty<*>)
prop.setter.call(this, prop.get(global))
}
}
companion object {
const val SYSTEM_GPU_DRIVER = "system"
@ -71,6 +90,11 @@ class EmulationSettings private constructor(context : Context, prefName : String
*/
fun forTitleId(titleId : String) = EmulationSettings(SkylineApplication.context, prefNameForTitle(titleId))
/**
* Returns emulation settings for the given preferences name
*/
fun forPrefName(prefName : String) = EmulationSettings(SkylineApplication.context, prefName)
/**
* Returns emulation settings to be used during emulation of the given `titleId`.
* Global settings are returned if custom settings are disabled for this title

View File

@ -57,6 +57,9 @@
<string name="reset_custom_settings">Reset Custom Settings</string>
<string name="reset_custom_settings_desc">Reset custom settings to the default values</string>
<string name="reset_settings_warning">Are you sure you want to reset all settings to the default values? <b>Current settings will be lost</b></string>
<string name="copy_global_settings">Copy Global Settings</string>
<string name="copy_global_settings_desc">Copy global settings to this game</string>
<string name="copy_global_settings_warning">Are you sure you want to copy global settings to this game? <b>Current settings will be lost</b></string>
<!-- Settings - System -->
<string name="system">System</string>
<string name="use_docked">Use Docked Mode</string>

View File

@ -13,5 +13,9 @@
android:summary="@string/reset_custom_settings_desc"
app:key="reset_custom_settings"
app:title="@string/reset_custom_settings" />
<emu.skyline.preference.CopyGlobalSettingsPreference
android:summary="@string/copy_global_settings_desc"
app:key="copy_global_settings"
app:title="@string/copy_global_settings" />
</PreferenceCategory>
</androidx.preference.PreferenceScreen>