1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 15:47:26 +08:00
osu-lazer/osu.Game/Skinning/LocalSkinOverrideContainer.cs

103 lines
3.5 KiB
C#
Raw Normal View History

// 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 osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
2018-04-20 23:17:57 +08:00
using osu.Framework.Configuration;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Textures;
2018-04-20 23:17:57 +08:00
using osu.Game.Configuration;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Skinning
{
public class LocalSkinOverrideContainer : Container, ISkinSource
{
public event Action SourceChanged;
private readonly Bindable<bool> beatmapSkins = new Bindable<bool>();
private readonly Bindable<bool> beatmapHitsounds = new Bindable<bool>();
2018-04-25 15:15:23 +08:00
2018-04-20 23:17:57 +08:00
public Drawable GetDrawableComponent(string componentName)
{
Drawable sourceDrawable;
2018-04-25 15:15:23 +08:00
if (beatmapSkins && (sourceDrawable = source.GetDrawableComponent(componentName)) != null)
2018-04-20 23:17:57 +08:00
return sourceDrawable;
return fallbackSource?.GetDrawableComponent(componentName);
}
2018-04-13 17:19:50 +08:00
2018-04-20 23:17:57 +08:00
public Texture GetTexture(string componentName)
{
Texture sourceTexture;
2018-04-25 15:15:23 +08:00
if (beatmapSkins && (sourceTexture = source.GetTexture(componentName)) != null)
2018-04-20 23:17:57 +08:00
return sourceTexture;
return fallbackSource.GetTexture(componentName);
}
2018-04-13 17:19:50 +08:00
public SampleChannel GetSample(string sampleName)
{
SampleChannel sourceChannel;
2018-04-25 15:15:23 +08:00
if (beatmapHitsounds && (sourceChannel = source.GetSample(sampleName)) != null)
return sourceChannel;
return fallbackSource?.GetSample(sampleName);
}
2018-04-13 17:19:50 +08:00
2019-01-07 19:12:39 +08:00
public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration
2018-04-13 17:19:50 +08:00
{
2019-01-07 19:12:39 +08:00
TValue val;
2018-04-25 15:32:06 +08:00
if ((source as Skin)?.Configuration is TConfiguration conf)
2019-01-07 19:12:39 +08:00
if (beatmapSkins && (val = query.Invoke(conf)) != null)
2018-12-08 05:16:09 +08:00
return val;
2019-01-07 19:12:39 +08:00
return fallbackSource == null ? default : fallbackSource.GetValue(query);
2018-04-13 17:19:50 +08:00
}
private readonly ISkinSource source;
private ISkinSource fallbackSource;
public LocalSkinOverrideContainer(ISkinSource source)
{
this.source = source;
}
private void onSourceChanged() => SourceChanged?.Invoke();
2018-07-11 16:07:14 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2018-04-13 17:19:50 +08:00
{
2018-07-11 16:07:14 +08:00
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2018-04-13 17:19:50 +08:00
fallbackSource = dependencies.Get<ISkinSource>();
dependencies.CacheAs<ISkinSource>(this);
return dependencies;
}
2018-04-20 23:17:57 +08:00
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.BeatmapSkins, beatmapSkins);
config.BindWith(OsuSetting.BeatmapHitsounds, beatmapHitsounds);
2018-04-20 23:17:57 +08:00
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
if (fallbackSource != null)
fallbackSource.SourceChanged += onSourceChanged;
beatmapSkins.BindValueChanged(_ => onSourceChanged());
beatmapHitsounds.BindValueChanged(_ => onSourceChanged(), true);
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= onSourceChanged;
}
}
}