From 3042b0659ea5e4987f0b6acb93f596034e3e3ca6 Mon Sep 17 00:00:00 2001 From: minjaesong Date: Thu, 25 Jan 2024 18:16:24 +0900 Subject: [PATCH] musicplayer: buttons are now well-behaving --- .../terrarum/musicplayer/gui/MusicPlayer.kt | 40 +++++++++++++++---- .../modulebasegame/TerrarumMusicGovernor.kt | 7 +++- .../gameactors/FixtureJukebox.kt | 4 +- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/MusicPlayer/src/net/torvald/terrarum/musicplayer/gui/MusicPlayer.kt b/MusicPlayer/src/net/torvald/terrarum/musicplayer/gui/MusicPlayer.kt index 5253bf55c..d5ee5c558 100644 --- a/MusicPlayer/src/net/torvald/terrarum/musicplayer/gui/MusicPlayer.kt +++ b/MusicPlayer/src/net/torvald/terrarum/musicplayer/gui/MusicPlayer.kt @@ -346,8 +346,15 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { // prev song if (mode < MODE_SHOW_LIST) { getPrevSongFromPlaylist()?.let { ingame.musicGovernor.unshiftPlaylist(it) } - App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) { - ingame.musicGovernor.startMusic() // required for "intermittent" mode + if (!shouldPlayerBeDisabled) { + App.audioMixer.requestFadeOut( + App.audioMixer.musicTrack, + AudioMixer.DEFAULT_FADEOUT_LEN / 3f + ) { + ingame.musicGovernor.startMusic() // required for "intermittent" mode + iHitTheStopButton = false + stopRequested = false + } } } // prev page in the playlist @@ -371,9 +378,12 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { App.audioMixer.musicTrack.nextTrack = null ingame.musicGovernor.stopMusic() thisMusic?.let { ingame.musicGovernor.queueMusicToPlayNext(it) } + iHitTheStopButton = true } else if (!shouldPlayerBeDisabled) { ingame.musicGovernor.startMusic() + iHitTheStopButton = false + stopRequested = false } } } @@ -381,8 +391,15 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { 3 -> { // next // next song if (mode < MODE_SHOW_LIST) { - App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) { - ingame.musicGovernor.startMusic() // required for "intermittent" mode, does seemingly nothing on "continuous" mode + if (!shouldPlayerBeDisabled) { + App.audioMixer.requestFadeOut( + App.audioMixer.musicTrack, + AudioMixer.DEFAULT_FADEOUT_LEN / 3f + ) { + ingame.musicGovernor.startMusic() // required for "intermittent" mode, does seemingly nothing on "continuous" mode + iHitTheStopButton = false + stopRequested = false + } } } // next page in the playlist @@ -432,6 +449,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { App.audioMixer.requestFadeOut(App.audioMixer.musicTrack, AudioMixer.DEFAULT_FADEOUT_LEN / 3f) { if (!shouldPlayerBeDisabled) { ingame.musicGovernor.startMusic() // required for "intermittent" mode + iHitTheStopButton = false + stopRequested = false } } } @@ -451,6 +470,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { loadNewAlbum(albumsList[index]) if (!shouldPlayerBeDisabled) { ingame.musicGovernor.startMusic() // required for "intermittent" mode + iHitTheStopButton = false + stopRequested = false } resetPlaylistScroll(App.audioMixer.musicTrack.nextTrack) } @@ -466,12 +487,15 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { // printdbg(this, "mode = $mode; req = $transitionRequest") - if (shouldPlayerBeDisabled) { - ingame.musicGovernor.stopMusic() + if (shouldPlayerBeDisabled || iHitTheStopButton) { + if (!stopRequested) { + stopRequested = true + ingame.musicGovernor.stopMusic() + } } else if (!jukeboxStopMonitorAlert && !App.audioMixer.musicTrack.isPlaying) { jukeboxStopMonitorAlert = true - ingame.musicGovernor.stopMusic(false, Math.random().toFloat() * 30f + 30f) + ingame.musicGovernor.stopMusic(false, ingame.musicGovernor.getRandomMusicInterval()) } else if (App.audioMixer.musicTrack.isPlaying) { jukeboxStopMonitorAlert = false @@ -479,6 +503,8 @@ class MusicPlayer(private val ingame: TerrarumIngame) : UICanvas() { } private var jukeboxStopMonitorAlert = true + private var iHitTheStopButton = false + private var stopRequested = false private fun resetAlbumlistScroll() { val currentlyPlaying = albumsList.indexOfFirst { it.canonicalPath.replace('\\', '/') == ingame.musicGovernor.playlistSource } diff --git a/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt b/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt index 75c983bc6..3bd440612 100644 --- a/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt +++ b/src/net/torvald/terrarum/modulebasegame/TerrarumMusicGovernor.kt @@ -324,11 +324,13 @@ class TerrarumMusicGovernor : MusicGovernor() { protected var ambState = 0 protected var ambFired = false + fun getRandomMusicInterval() = 25f + Math.random().toFloat() * 10f + private fun stopMusic(song: MusicContainer?, callStopMusicHook: Boolean = true, customPauseLen: Float? = null) { musicState = STATE_INTERMISSION intermissionAkku = 0f intermissionLength = customPauseLen ?: - if (diskJockeyingMode == "intermittent") 30f + 30f * Math.random().toFloat() else Float.POSITIVE_INFINITY // 30s-60s + if (diskJockeyingMode == "intermittent") getRandomMusicInterval() else Float.POSITIVE_INFINITY // 30s-60s musicFired = false if (callStopMusicHook && musicStopHooks.isNotEmpty()) musicStopHooks.forEach { if (song != null) { @@ -340,7 +342,8 @@ class TerrarumMusicGovernor : MusicGovernor() { fun stopMusic(callStopMusicHook: Boolean = true, pauseLen: Float = Float.POSITIVE_INFINITY) { stopMusic(App.audioMixer.musicTrack.currentTrack, callStopMusicHook, pauseLen) -// printdbg(this, "StopMusic Intermission2: $intermissionLength seconds") +// printdbg(this, "StopMusic Intermission2: $pauseLen seconds, called by:") +// printStackTrace(this) } fun startMusic() { diff --git a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt index 58413590d..8fb01fcc1 100644 --- a/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt +++ b/src/net/torvald/terrarum/modulebasegame/gameactors/FixtureJukebox.kt @@ -111,7 +111,7 @@ class FixtureJukebox : Electric, PlaysMusic { printdbg(this, "Stop music $title - $artist") - (INGAME.musicGovernor as TerrarumMusicGovernor).stopMusic(pauseLen = Math.random().toFloat() * 30f + 30f) + (INGAME.musicGovernor as TerrarumMusicGovernor).stopMusic(pauseLen = (INGAME.musicGovernor as TerrarumMusicGovernor).getRandomMusicInterval()) } discCurrentlyPlaying = index @@ -136,7 +136,7 @@ class FixtureJukebox : Electric, PlaysMusic { */ fun stopGracefully() { stopDiscPlayback() - (INGAME.musicGovernor as TerrarumMusicGovernor).stopMusic(pauseLen = Math.random().toFloat() * 30f + 30f) + (INGAME.musicGovernor as TerrarumMusicGovernor).stopMusic(pauseLen = (INGAME.musicGovernor as TerrarumMusicGovernor).getRandomMusicInterval()) } override fun drawBody(frameDelta: Float, batch: SpriteBatch) {