1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-30 15:27:28 +08:00
osu-lazer/osu.Game/Skinning/LocalSkinOverrideContainer.cs

111 lines
3.8 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
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;
2018-04-25 15:15:23 +08:00
private Bindable<bool> beatmapSkins = new Bindable<bool>();
private Bindable<bool> beatmapHitsounds = new Bindable<bool>();
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
public TValue? GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue?> query) where TConfiguration : SkinConfiguration where TValue : struct
{
TValue? val = null;
2018-04-25 15:32:06 +08:00
if ((source as Skin)?.Configuration is TConfiguration conf)
2018-04-13 17:19:50 +08:00
val = query?.Invoke(conf);
return val ?? fallbackSource?.GetValue(query);
}
public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration where TValue : class
{
TValue val = null;
2018-04-25 15:32:06 +08:00
if ((source as Skin)?.Configuration is TConfiguration conf)
2018-04-13 17:19:50 +08:00
val = query?.Invoke(conf);
return val ?? fallbackSource?.GetValue(query);
}
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)
{
2018-04-25 15:15:23 +08:00
beatmapSkins = config.GetBindable<bool>(OsuSetting.BeatmapSkins);
beatmapSkins.BindValueChanged(_ => onSourceChanged());
2018-04-25 15:15:23 +08:00
beatmapHitsounds = config.GetBindable<bool>(OsuSetting.BeatmapHitsounds);
beatmapHitsounds.BindValueChanged(_ => onSourceChanged(), true);
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;
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= onSourceChanged;
}
}
}