Music pause on "leave game" question, not world screen menu (#5491)

* Music pause on leave game question, not worldscreen menu

* Music pause on leave game question, not worldscreen menu - log back off
This commit is contained in:
SomeTroglodyte 2021-10-16 19:06:25 +02:00 committed by GitHub
parent 3c71fe41dc
commit de0c1161f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 8 deletions

View File

@ -29,7 +29,7 @@ class MusicController {
private const val musicHistorySize = 8 // number of names to keep to avoid playing the same in short succession
private val fileExtensions = listOf("mp3", "ogg") // flac, opus, m4a... blocked by Gdx, `wav` we don't want
internal const val consoleLog = true
internal const val consoleLog = false
private fun getFile(path: String) =
if (musicLocation == FileType.External && Gdx.files.isExternalStorageAvailable)
@ -85,8 +85,11 @@ class MusicController {
//region Pure functions
/** @return the path of the playing track or null if none playing */
private fun currentlyPlaying(): String = if (state != ControllerState.Playing && state != ControllerState.PlaySingle) ""
else musicHistory.peekLast()
private fun currentlyPlaying(): String = when(state) {
ControllerState.Playing, ControllerState.PlaySingle, ControllerState.Pause ->
musicHistory.peekLast()
else -> ""
}
/** Registers a callback that will be called with the new track name every time it changes.
* The track name will be prettified ("Modname: Track" instead of "mods/Modname/music/Track.ogg").
@ -325,6 +328,8 @@ class MusicController {
* @param speedFactor accelerate (>1) or slow down (<1) the fade-out. Clamped to 1/1000..1000.
*/
fun pause(speedFactor: Float = 1f) {
if (consoleLog)
println("MusicTrackController.pause called")
if ((state != ControllerState.Playing && state != ControllerState.PlaySingle) || current == null) return
val fadingStep = defaultFadingStep * speedFactor.coerceIn(0.001f..1000f)
current!!.startFade(MusicTrackController.State.FadeOut, fadingStep)
@ -340,6 +345,8 @@ class MusicController {
* @param speedFactor accelerate (>1) or slow down (<1) the fade-in. Clamped to 1/1000..1000.
*/
fun resume(speedFactor: Float = 1f) {
if (consoleLog)
println("MusicTrackController.resume called")
if (state == ControllerState.Pause && current != null) {
val fadingStep = defaultFadingStep * speedFactor.coerceIn(0.001f..1000f)
current!!.startFade(MusicTrackController.State.FadeIn, fadingStep)

View File

@ -35,9 +35,11 @@ class ExitGamePopup(screen: CameraStageBaseScreen, force: Boolean = false)
: YesNoPopup (
question = "Do you want to exit the game?",
action = { Gdx.app.exit() },
screen = screen
screen = screen,
restoreDefault = { screen.game.musicController.resume() }
) {
init {
screen.game.musicController.pause()
open(force)
}
}

View File

@ -14,7 +14,6 @@ import com.unciv.ui.worldscreen.WorldScreen
class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) {
init {
defaults().fillX()
worldScreen.game.musicController.pause()
addButton("Main menu") { worldScreen.game.setScreen(MainMenuScreen()) }
addButton("Civilopedia") { worldScreen.game.setScreen(CivilopediaScreen(worldScreen.gameInfo.ruleSet)) }
@ -34,9 +33,7 @@ class WorldScreenMenuPopup(val worldScreen: WorldScreen) : Popup(worldScreen) {
close()
WorldScreenCommunityPopup(worldScreen).open(force = true)
}
addCloseButton {
worldScreen.game.musicController.resume()
}
addCloseButton()
pack()
}
}