mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 14:38:34 +01:00
Drop SoundEffectInstance (mis)usage in favor of new SoundEffectInfo
This commit is contained in:
parent
7dc08afa5a
commit
096131ce15
5 changed files with 68 additions and 27 deletions
|
@ -362,11 +362,11 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A style property that contains the sound effect that is played when this element's <see cref="OnPressed"/> is called
|
/// A style property that contains the sound effect that is played when this element's <see cref="OnPressed"/> is called
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StyleProp<SoundEffectInstance> ActionSound;
|
public StyleProp<SoundEffectInfo> ActionSound;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// A style property that contains the sound effect that is played when this element's <see cref="OnSecondaryPressed"/> is called
|
/// A style property that contains the sound effect that is played when this element's <see cref="OnSecondaryPressed"/> is called
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public StyleProp<SoundEffectInstance> SecondActionSound;
|
public StyleProp<SoundEffectInfo> SecondActionSound;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a new element with the given anchor and size and sets up some default event reactions.
|
/// Creates a new element with the given anchor and size and sets up some default event reactions.
|
||||||
|
|
|
@ -133,10 +133,10 @@ namespace MLEM.Ui.Style {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public float TextScale = 1;
|
public float TextScale = 1;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The <see cref="SoundEffectInstance"/> that should be played when an element's <see cref="Element.OnPressed"/> and <see cref="Element.OnSecondaryPressed"/> events are called.
|
/// The <see cref="SoundEffectInfo"/> that should be played when an element's <see cref="Element.OnPressed"/> and <see cref="Element.OnSecondaryPressed"/> events are called.
|
||||||
/// Note that this sound is only played if the callbacks have any subscribers.
|
/// Note that this sound is only played if the callbacks have any subscribers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public SoundEffectInstance ActionSound;
|
public SoundEffectInfo ActionSound;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -210,11 +210,11 @@ namespace MLEM.Ui {
|
||||||
};
|
};
|
||||||
this.OnElementPressed += e => {
|
this.OnElementPressed += e => {
|
||||||
if (e.OnPressed != null)
|
if (e.OnPressed != null)
|
||||||
e.ActionSound.Value?.Replay();
|
e.ActionSound.Value?.Play();
|
||||||
};
|
};
|
||||||
this.OnElementSecondaryPressed += e => {
|
this.OnElementSecondaryPressed += e => {
|
||||||
if (e.OnSecondaryPressed != null)
|
if (e.OnSecondaryPressed != null)
|
||||||
e.SecondActionSound.Value?.Replay();
|
e.SecondActionSound.Value?.Play();
|
||||||
};
|
};
|
||||||
|
|
||||||
this.TextFormatter = new TextFormatter();
|
this.TextFormatter = new TextFormatter();
|
||||||
|
|
|
@ -1,21 +0,0 @@
|
||||||
using System;
|
|
||||||
using Microsoft.Xna.Framework.Audio;
|
|
||||||
|
|
||||||
namespace MLEM.Extensions {
|
|
||||||
/// <summary>
|
|
||||||
/// A set of extensions for dealing wiht <see cref="SoundEffectInstance"/>
|
|
||||||
/// </summary>
|
|
||||||
public static class SoundExtensions {
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Stops and plays a sound effect instance in one call
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="sound">The sound to stop and play</param>
|
|
||||||
[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();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
62
MLEM/Misc/SoundEffectInfo.cs
Normal file
62
MLEM/Misc/SoundEffectInfo.cs
Normal file
|
@ -0,0 +1,62 @@
|
||||||
|
using Microsoft.Xna.Framework.Audio;
|
||||||
|
|
||||||
|
namespace MLEM.Misc {
|
||||||
|
/// <summary>
|
||||||
|
/// A sound effect info is a wrapper around <see cref="SoundEffect"/> that additionally stores <see cref="Volume"/>, <see cref="Pitch"/> and <see cref="Pan"/> information.
|
||||||
|
/// Additionally, a <see cref="SoundEffectInstance"/> can be created using <see cref="CreateInstance"/>.
|
||||||
|
/// </summary>
|
||||||
|
public class SoundEffectInfo {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The <see cref="SoundEffect"/> that is played by this info.
|
||||||
|
/// </summary>
|
||||||
|
public readonly SoundEffect Sound;
|
||||||
|
/// <summary>
|
||||||
|
/// Volume, ranging from 0.0 (silence) to 1.0 (full volume). Volume during playback is scaled by SoundEffect.MasterVolume.
|
||||||
|
/// </summary>
|
||||||
|
public float Volume;
|
||||||
|
/// <summary>
|
||||||
|
/// Pitch adjustment, ranging from -1.0 (down an octave) to 0.0 (no change) to 1.0 (up an octave).
|
||||||
|
/// </summary>
|
||||||
|
public float Pitch;
|
||||||
|
/// <summary>
|
||||||
|
/// Pan value ranging from -1.0 (left speaker) to 0.0 (centered), 1.0 (right speaker).
|
||||||
|
/// </summary>
|
||||||
|
public float Pan;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new sound effect info with the given values.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sound">The sound to play</param>
|
||||||
|
/// <param name="volume">The volume to play the sound with</param>
|
||||||
|
/// <param name="pitch">The pitch to play the sound with</param>
|
||||||
|
/// <param name="pan">The pan to play the sound with</param>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Plays this info's <see cref="Sound"/> once.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>False if more sounds are currently playing than the platform allows</returns>
|
||||||
|
public bool Play() {
|
||||||
|
return this.Sound.Play(this.Volume, this.Pitch, this.Pan);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="SoundEffectInstance"/> with this sound effect info's data.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A new sound effect instance, with this info's data applied</returns>
|
||||||
|
public SoundEffectInstance CreateInstance() {
|
||||||
|
var instance = this.Sound.CreateInstance();
|
||||||
|
instance.Volume = this.Volume;
|
||||||
|
instance.Pitch = this.Pitch;
|
||||||
|
instance.Pan = this.Pan;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue