Add audio feedback for rearranging list items

This commit is contained in:
Jamie Taylor
2022-06-23 17:18:37 +09:00
parent ac91c9de7d
commit 4a316fad2f

View File

@ -3,9 +3,14 @@
#nullable disable #nullable disable
using System.Collections.Specialized;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Utils;
namespace osu.Game.Graphics.Containers namespace osu.Game.Graphics.Containers
{ {
@ -18,11 +23,44 @@ namespace osu.Game.Graphics.Containers
protected override ScrollContainer<Drawable> CreateScrollContainer() => new OsuScrollContainer(); protected override ScrollContainer<Drawable> CreateScrollContainer() => new OsuScrollContainer();
private Sample sampleSwap;
private double sampleLastPlaybackTime;
protected sealed override RearrangeableListItem<TModel> CreateDrawable(TModel item) => CreateOsuDrawable(item).With(d => protected sealed override RearrangeableListItem<TModel> CreateDrawable(TModel item) => CreateOsuDrawable(item).With(d =>
{ {
d.DragActive.BindTo(DragActive); d.DragActive.BindTo(DragActive);
}); });
protected abstract OsuRearrangeableListItem<TModel> CreateOsuDrawable(TModel item); protected abstract OsuRearrangeableListItem<TModel> CreateOsuDrawable(TModel item);
protected OsuRearrangeableListContainer()
{
Items.CollectionChanged += (_, args) =>
{
if (args.Action == NotifyCollectionChangedAction.Move)
playSwapSample();
};
}
private void playSwapSample()
{
if (Time.Current - sampleLastPlaybackTime <= 35)
return;
var channel = sampleSwap?.GetChannel();
if (channel == null)
return;
channel.Frequency.Value = 0.96 + RNG.NextDouble(0.08);
channel.Play();
sampleLastPlaybackTime = Time.Current;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleSwap = audio.Samples.Get(@"UI/item-swap");
sampleLastPlaybackTime = Time.Current;
}
} }
} }