From b4f79f0753354e8e700170e2e5c4c3bc2ef96c8c Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 28 Dec 2021 14:45:20 +0100 Subject: [PATCH] Added SoundEffectInstanceHandler.Stop --- CHANGELOG.md | 1 + MLEM/Sound/SoundEffectInstanceHandler.cs | 23 ++++++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f7852a..0ce21f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ Jump to version: Additions - Added StringBuilder overloads to GenericFont - Added ColorExtensions.Multiply +- Added SoundEffectInstanceHandler.Stop Improvements - Generify GenericFont's string drawing diff --git a/MLEM/Sound/SoundEffectInstanceHandler.cs b/MLEM/Sound/SoundEffectInstanceHandler.cs index 9b7d68c..0e03837 100644 --- a/MLEM/Sound/SoundEffectInstanceHandler.cs +++ b/MLEM/Sound/SoundEffectInstanceHandler.cs @@ -19,8 +19,7 @@ namespace MLEM.Sound { /// Creates a new sound effect instance handler with the given settings /// /// The game instance - public SoundEffectInstanceHandler(Game game) : base(game) { - } + public SoundEffectInstanceHandler(Game game) : base(game) {} /// public override void Update(GameTime gameTime) { @@ -36,8 +35,7 @@ namespace MLEM.Sound { for (var i = this.playingSounds.Count - 1; i >= 0; i--) { var entry = this.playingSounds[i]; if (entry.Instance.IsDisposed || entry.Instance.State == SoundState.Stopped) { - entry.Instance.Stop(true); - entry.OnStopped?.Invoke(entry.Instance); + entry.StopAndNotify(); this.playingSounds.RemoveAt(i); } else { entry.TryApply3D(this.listeners); @@ -69,6 +67,16 @@ namespace MLEM.Sound { entry.Instance.Resume(); } + /// + /// Stops all of the sound effect instances in this handler + /// + public void Stop() { + this.playingSounds.RemoveAll(e => { + e.StopAndNotify(); + return true; + }); + } + /// /// Adds a new to this handler. /// This also starts playing the instance. @@ -132,7 +140,7 @@ namespace MLEM.Sound { /// public readonly SoundEffectInstance Instance; /// - /// An action that is invoked when this entry's is stopped. + /// An action that is invoked when this entry's is stopped or after it finishes naturally. /// This action is invoked in . /// public readonly Action OnStopped; @@ -153,6 +161,11 @@ namespace MLEM.Sound { this.Instance.Apply3D(listeners, this.Emitter); } + internal void StopAndNotify() { + this.Instance.Stop(true); + this.OnStopped?.Invoke(this.Instance); + } + } }