From 096131ce15676d535036edfdf16f6192b8c0f4c9 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 22 Jun 2020 13:59:33 +0200 Subject: [PATCH] Drop SoundEffectInstance (mis)usage in favor of new SoundEffectInfo --- MLEM.Ui/Elements/Element.cs | 4 +- MLEM.Ui/Style/UiStyle.cs | 4 +- MLEM.Ui/UiSystem.cs | 4 +- MLEM/Extensions/SoundExtensions.cs | 21 ---------- MLEM/Misc/SoundEffectInfo.cs | 62 ++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 27 deletions(-) delete mode 100644 MLEM/Extensions/SoundExtensions.cs create mode 100644 MLEM/Misc/SoundEffectInfo.cs diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 02f8393..d29e0c4 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -362,11 +362,11 @@ namespace MLEM.Ui.Elements { /// /// A style property that contains the sound effect that is played when this element's is called /// - public StyleProp ActionSound; + public StyleProp ActionSound; /// /// A style property that contains the sound effect that is played when this element's is called /// - public StyleProp SecondActionSound; + public StyleProp SecondActionSound; /// /// Creates a new element with the given anchor and size and sets up some default event reactions. diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 99b36ea..58c1b26 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -133,10 +133,10 @@ namespace MLEM.Ui.Style { /// public float TextScale = 1; /// - /// The that should be played when an element's and events are called. + /// The that should be played when an element's and events are called. /// Note that this sound is only played if the callbacks have any subscribers. /// - public SoundEffectInstance ActionSound; + public SoundEffectInfo ActionSound; } } \ No newline at end of file diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 062bbea..37e88c4 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -210,11 +210,11 @@ namespace MLEM.Ui { }; this.OnElementPressed += e => { if (e.OnPressed != null) - e.ActionSound.Value?.Replay(); + e.ActionSound.Value?.Play(); }; this.OnElementSecondaryPressed += e => { if (e.OnSecondaryPressed != null) - e.SecondActionSound.Value?.Replay(); + e.SecondActionSound.Value?.Play(); }; this.TextFormatter = new TextFormatter(); diff --git a/MLEM/Extensions/SoundExtensions.cs b/MLEM/Extensions/SoundExtensions.cs deleted file mode 100644 index a080d47..0000000 --- a/MLEM/Extensions/SoundExtensions.cs +++ /dev/null @@ -1,21 +0,0 @@ -using System; -using Microsoft.Xna.Framework.Audio; - -namespace MLEM.Extensions { - /// - /// A set of extensions for dealing wiht - /// - public static class SoundExtensions { - - /// - /// Stops and plays a sound effect instance in one call - /// - /// The sound to stop and play - [Obsolete("When using the .NET Core version of MonoGame, the replay issue has been fixed. Just call Play() instead.")] - public static void Replay(this SoundEffectInstance sound) { - sound.Stop(); - sound.Play(); - } - - } -} \ No newline at end of file diff --git a/MLEM/Misc/SoundEffectInfo.cs b/MLEM/Misc/SoundEffectInfo.cs new file mode 100644 index 0000000..4ec3632 --- /dev/null +++ b/MLEM/Misc/SoundEffectInfo.cs @@ -0,0 +1,62 @@ +using Microsoft.Xna.Framework.Audio; + +namespace MLEM.Misc { + /// + /// A sound effect info is a wrapper around that additionally stores , and information. + /// Additionally, a can be created using . + /// + public class SoundEffectInfo { + + /// + /// The that is played by this info. + /// + public readonly SoundEffect Sound; + /// + /// Volume, ranging from 0.0 (silence) to 1.0 (full volume). Volume during playback is scaled by SoundEffect.MasterVolume. + /// + public float Volume; + /// + /// Pitch adjustment, ranging from -1.0 (down an octave) to 0.0 (no change) to 1.0 (up an octave). + /// + public float Pitch; + /// + /// Pan value ranging from -1.0 (left speaker) to 0.0 (centered), 1.0 (right speaker). + /// + public float Pan; + + /// + /// Creates a new sound effect info with the given values. + /// + /// The sound to play + /// The volume to play the sound with + /// The pitch to play the sound with + /// The pan to play the sound with + public SoundEffectInfo(SoundEffect sound, float volume = 1, float pitch = 0, float pan = 0) { + this.Sound = sound; + this.Volume = volume; + this.Pitch = pitch; + this.Pan = pan; + } + + /// + /// Plays this info's once. + /// + /// False if more sounds are currently playing than the platform allows + public bool Play() { + return this.Sound.Play(this.Volume, this.Pitch, this.Pan); + } + + /// + /// Creates a new with this sound effect info's data. + /// + /// A new sound effect instance, with this info's data applied + public SoundEffectInstance CreateInstance() { + var instance = this.Sound.CreateInstance(); + instance.Volume = this.Volume; + instance.Pitch = this.Pitch; + instance.Pan = this.Pan; + return instance; + } + + } +} \ No newline at end of file