2019-01-24 16:43:03 +08:00
|
|
|
|
// 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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
|
|
|
using osu.Game.Audio;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
|
{
|
|
|
|
|
public class SkinnableSound : SkinReloadableDrawable
|
|
|
|
|
{
|
|
|
|
|
private readonly SampleInfo[] samples;
|
|
|
|
|
private SampleChannel[] channels;
|
|
|
|
|
|
|
|
|
|
private AudioManager audio;
|
|
|
|
|
|
|
|
|
|
public SkinnableSound(params SampleInfo[] samples)
|
|
|
|
|
{
|
|
|
|
|
this.samples = samples;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(AudioManager audio)
|
|
|
|
|
{
|
|
|
|
|
this.audio = audio;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Play() => channels?.ForEach(c => c.Play());
|
|
|
|
|
|
2018-11-15 19:26:12 +08:00
|
|
|
|
public override bool IsPresent => false; // We don't need to receive updates.
|
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
|
|
|
|
|
{
|
|
|
|
|
channels = samples.Select(s =>
|
|
|
|
|
{
|
|
|
|
|
var ch = loadChannel(s, skin.GetSample);
|
|
|
|
|
if (ch == null && allowFallback)
|
|
|
|
|
ch = loadChannel(s, audio.Sample.Get);
|
|
|
|
|
return ch;
|
|
|
|
|
}).Where(c => c != null).ToArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SampleChannel loadChannel(SampleInfo info, Func<string, SampleChannel> getSampleFunction)
|
|
|
|
|
{
|
2018-07-02 13:18:41 +08:00
|
|
|
|
foreach (var lookup in info.LookupNames)
|
|
|
|
|
{
|
|
|
|
|
var ch = getSampleFunction($"Gameplay/{lookup}");
|
|
|
|
|
if (ch == null)
|
|
|
|
|
continue;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
|
|
ch.Volume.Value = info.Volume / 100.0;
|
2018-07-02 13:18:41 +08:00
|
|
|
|
return ch;
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2018-07-02 13:18:41 +08:00
|
|
|
|
return null;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|