From 4d16b3fcf80aa376076469867e51033f83aa1f43 Mon Sep 17 00:00:00 2001 From: SomeTroglodyte <63000004+SomeTroglodyte@users.noreply.github.com> Date: Tue, 29 Jun 2021 20:31:57 +0200 Subject: [PATCH] Respect visualMods for Sound - CheckBox, formats, modchange detect (#4295) * Respect visualMods for Sound - CheckBox, formats, modchange detect * Respect visualMods for Sound - german patch --- .../jsons/translations/German.properties | 3 +- .../jsons/translations/template.properties | 3 +- .../ui/pickerscreens/ModManagementScreen.kt | 28 +++++----- core/src/com/unciv/ui/utils/Sounds.kt | 52 ++++++++++++++----- 4 files changed, 56 insertions(+), 30 deletions(-) diff --git a/android/assets/jsons/translations/German.properties b/android/assets/jsons/translations/German.properties index 0551fb70a7..8f17c3a765 100644 --- a/android/assets/jsons/translations/German.properties +++ b/android/assets/jsons/translations/German.properties @@ -990,8 +990,7 @@ Current mods = Installierte Modifikationen Downloadable mods = Verfügbare Modifikationen Next page = Nächste Seite Open Github page = Auf Github öffnen -Enable as permanent visual mod = Als permanente visuelle Modifikation aktivieren -Disable as permanent visual mod = Als permanente visuelle Modifikation deaktivieren +Permanent audiovisual mod = Permanente audiovisuelle Mod Installed = Installiert Downloaded! = Heruntergeladen! Could not download mod = Konnte Modifikation nicht herunterladen diff --git a/android/assets/jsons/translations/template.properties b/android/assets/jsons/translations/template.properties index 3271375822..1fbf0a1815 100644 --- a/android/assets/jsons/translations/template.properties +++ b/android/assets/jsons/translations/template.properties @@ -991,8 +991,7 @@ Current mods = Downloadable mods = Next page = Open Github page = -Enable as permanent visual mod = -Disable as permanent visual mod = +Permanent audiovisual mod = Installed = Downloaded! = Could not download mod = diff --git a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt index 9eb31a5719..a202759cdb 100644 --- a/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/ModManagementScreen.kt @@ -419,20 +419,18 @@ class ModManagementScreen: PickerScreen(disableScroll = true) { val visualMods = game.settings.visualMods val isVisual = visualMods.contains(mod.name) modStateImages[mod.name]?.isVisual = isVisual - if (!isVisual) { - modActionTable.add("Enable as permanent visual mod".toTextButton().onClick { + + val visualCheckBox = CheckBox("Permanent audiovisual mod", skin) + visualCheckBox.isChecked = isVisual + modActionTable.add(visualCheckBox) + visualCheckBox.onChange { + if (visualCheckBox.isChecked) visualMods.add(mod.name) - game.settings.save() - ImageGetter.setNewRuleset(ImageGetter.ruleset) - refreshModActions(mod) - }) - } else { - modActionTable.add("Disable as permanent visual mod".toTextButton().onClick { + else visualMods.remove(mod.name) - game.settings.save() - ImageGetter.setNewRuleset(ImageGetter.ruleset) - refreshModActions(mod) - }) + game.settings.save() + ImageGetter.setNewRuleset(ImageGetter.ruleset) + refreshModActions(mod) } modActionTable.row() } @@ -502,4 +500,10 @@ class ModManagementScreen: PickerScreen(disableScroll = true) { modStateImages.remove(mod.name) refreshInstalledModTable() } + + override fun resize(width: Int, height: Int) { + if (stage.viewport.screenWidth != width || stage.viewport.screenHeight != height) { + game.setScreen(ModManagementScreen()) + } + } } diff --git a/core/src/com/unciv/ui/utils/Sounds.kt b/core/src/com/unciv/ui/utils/Sounds.kt index fd19edb897..af337b9b61 100644 --- a/core/src/com/unciv/ui/utils/Sounds.kt +++ b/core/src/com/unciv/ui/utils/Sounds.kt @@ -16,42 +16,66 @@ import java.io.File * app lifetime. */ object Sounds { + private enum class SupportedExtensions { mp3, ogg, wav } // Gdx won't do aac/m4a + private val soundMap = HashMap() private val separator = File.separator // just a shorthand for readability private var modListHash = Int.MIN_VALUE - /** Ensure cache is not outdated _and_ build list of folders to look for sounds */ + + /** Ensure cache is not outdated */ + private fun checkCache() { + if (!UncivGame.isCurrentInitialized()) return + val game = UncivGame.Current + + // Get a hash covering all mods - quickly, so don't map cast or copy the Set types + val hash1 = if (game.isGameInfoInitialized()) game.gameInfo.ruleSet.mods.hashCode() else 0 + val newHash = hash1.xor(game.settings.visualMods.hashCode()) + // If hash the same, leave the cache as is + if (modListHash != Int.MIN_VALUE && modListHash == newHash) return + + // Seems the mod list has changed - clear the cache + for (sound in soundMap.values) sound?.dispose() + soundMap.clear() + modListHash = newHash + } + + /** Build list of folders to look for sounds */ private fun getFolders(): Sequence { - if (!UncivGame.isCurrentInitialized() || !UncivGame.Current.isGameInfoInitialized()) // Allow sounds from main menu + if (!UncivGame.isCurrentInitialized()) + // Sounds before main menu shouldn't happen, but just in case return a default "" + // which translates to the built-in assets/sounds folder return sequenceOf("") + val game = UncivGame.Current + // Allow mod sounds - preferentially so they can override built-in sounds - val modList = UncivGame.Current.gameInfo.ruleSet.mods - val newHash = modList.hashCode() - if (modListHash == Int.MIN_VALUE || modListHash != newHash) { - // Seems the mod list has changed - start over - for (sound in soundMap.values) sound?.dispose() - soundMap.clear() - modListHash = newHash - } - // Should we also look in UncivGame.Current.settings.visualMods? + // audiovisual mods first, these are already available when game.gameInfo is not + val modList: MutableSet = game.settings.visualMods + if (game.isGameInfoInitialized()) + modList.addAll(game.gameInfo.ruleSet.mods) // Sounds from game mods + return modList.asSequence() .map { "mods$separator$it$separator" } + - sequenceOf("") + sequenceOf("") // represents builtin sounds folder } fun get(sound: UncivSound): Sound? { + checkCache() if (sound in soundMap) return soundMap[sound] val fileName = sound.value var file: FileHandle? = null - for (modFolder in getFolders()) { - val path = "${modFolder}sounds$separator$fileName.mp3" + for ( (modFolder, extension) in getFolders().flatMap { + folder -> SupportedExtensions.values().asSequence().map { folder to it } + } ) { + val path = "${modFolder}sounds$separator$fileName.${extension.name}" file = Gdx.files.internal(path) if (file.exists()) break } val newSound = if (file == null || !file.exists()) null else Gdx.audio.newSound(file) + // Store Sound for reuse or remember that the actual file is missing soundMap[sound] = newSound return newSound