mirror of
https://github.com/ppy/osu.git
synced 2024-12-14 07:42:57 +08:00
Split out hover sample debounce logic so it can be more easily used in other places
This commit is contained in:
parent
0040f14132
commit
970039b7e3
@ -0,0 +1,46 @@
|
||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
||||
// See the LICENCE file in the repository root for full licence text.
|
||||
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Input.Events;
|
||||
using osu.Game.Configuration;
|
||||
|
||||
namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
/// <summary>
|
||||
/// Handles debouncing hover sounds at a global level to ensure the effects are not overwhelming.
|
||||
/// </summary>
|
||||
public abstract class HoverSampleDebounceComponent : CompositeDrawable
|
||||
{
|
||||
/// <summary>
|
||||
/// Length of debounce for hover sound playback, in milliseconds.
|
||||
/// </summary>
|
||||
public double HoverDebounceTime { get; } = 20;
|
||||
|
||||
private Bindable<double?> lastPlaybackTime;
|
||||
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(AudioManager audio, SessionStatics statics)
|
||||
{
|
||||
lastPlaybackTime = statics.GetBindable<double?>(Static.LastHoverSoundPlaybackTime);
|
||||
}
|
||||
|
||||
protected override bool OnHover(HoverEvent e)
|
||||
{
|
||||
bool enoughTimePassedSinceLastPlayback = !lastPlaybackTime.Value.HasValue || Time.Current - lastPlaybackTime.Value >= HoverDebounceTime;
|
||||
|
||||
if (enoughTimePassedSinceLastPlayback)
|
||||
{
|
||||
PlayHoverSample();
|
||||
lastPlaybackTime.Value = Time.Current;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public abstract void PlayHoverSample();
|
||||
}
|
||||
}
|
@ -5,11 +5,8 @@ using System.ComponentModel;
|
||||
using osu.Framework.Allocation;
|
||||
using osu.Framework.Audio;
|
||||
using osu.Framework.Audio.Sample;
|
||||
using osu.Framework.Bindables;
|
||||
using osu.Framework.Extensions;
|
||||
using osu.Framework.Graphics;
|
||||
using osu.Framework.Graphics.Containers;
|
||||
using osu.Framework.Input.Events;
|
||||
using osu.Game.Configuration;
|
||||
using osu.Framework.Utils;
|
||||
|
||||
@ -19,19 +16,12 @@ namespace osu.Game.Graphics.UserInterface
|
||||
/// Adds hover sounds to a drawable.
|
||||
/// Does not draw anything.
|
||||
/// </summary>
|
||||
public class HoverSounds : CompositeDrawable
|
||||
public class HoverSounds : HoverSampleDebounceComponent
|
||||
{
|
||||
private SampleChannel sampleHover;
|
||||
|
||||
/// <summary>
|
||||
/// Length of debounce for hover sound playback, in milliseconds.
|
||||
/// </summary>
|
||||
public double HoverDebounceTime { get; } = 20;
|
||||
|
||||
protected readonly HoverSampleSet SampleSet;
|
||||
|
||||
private Bindable<double?> lastPlaybackTime;
|
||||
|
||||
public HoverSounds(HoverSampleSet sampleSet = HoverSampleSet.Normal)
|
||||
{
|
||||
SampleSet = sampleSet;
|
||||
@ -41,27 +31,13 @@ namespace osu.Game.Graphics.UserInterface
|
||||
[BackgroundDependencyLoader]
|
||||
private void load(AudioManager audio, SessionStatics statics)
|
||||
{
|
||||
lastPlaybackTime = statics.GetBindable<double?>(Static.LastHoverSoundPlaybackTime);
|
||||
|
||||
sampleHover = audio.Samples.Get($@"UI/generic-hover{SampleSet.GetDescription()}");
|
||||
}
|
||||
|
||||
protected override bool OnHover(HoverEvent e)
|
||||
public override void PlayHoverSample()
|
||||
{
|
||||
if (sampleHover == null)
|
||||
return false;
|
||||
|
||||
bool enoughTimePassedSinceLastPlayback = !lastPlaybackTime.Value.HasValue || Time.Current - lastPlaybackTime.Value >= HoverDebounceTime;
|
||||
|
||||
if (enoughTimePassedSinceLastPlayback)
|
||||
{
|
||||
sampleHover.Frequency.Value = 0.96 + RNG.NextDouble(0.08);
|
||||
sampleHover.Play();
|
||||
|
||||
lastPlaybackTime.Value = Time.Current;
|
||||
}
|
||||
|
||||
return false;
|
||||
sampleHover.Frequency.Value = 0.96 + RNG.NextDouble(0.08);
|
||||
sampleHover.Play();
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user