osu/osu.Game/Skinning/LocalSkinOverrideContainer.cs

104 lines
3.6 KiB
C#
Raw Normal View History

2018-04-13 09:19:50 +00: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 15:17:57 +00:00
using osu.Framework.Configuration;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Textures;
2018-04-20 15:17:57 +00:00
using osu.Game.Configuration;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Skinning
{
public class LocalSkinOverrideContainer : Container, ISkinSource
{
public event Action SourceChanged;
2018-04-20 15:17:57 +00:00
public Drawable GetDrawableComponent(string componentName)
{
Drawable sourceDrawable;
if (!ignoreBeatmapSkin && (sourceDrawable = source.GetDrawableComponent(componentName)) != null)
return sourceDrawable;
return fallbackSource?.GetDrawableComponent(componentName);
}
2018-04-13 09:19:50 +00:00
2018-04-20 15:17:57 +00:00
public Texture GetTexture(string componentName)
{
Texture sourceTexture;
if (!ignoreBeatmapSkin && (sourceTexture = source.GetTexture(componentName)) != null)
return sourceTexture;
return fallbackSource.GetTexture(componentName);
}
2018-04-13 09:19:50 +00:00
public SampleChannel GetSample(string sampleName) => source.GetSample(sampleName) ?? fallbackSource?.GetSample(sampleName);
public TValue? GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue?> query) where TConfiguration : SkinConfiguration where TValue : struct
{
TValue? val = null;
var conf = (source as Skin)?.Configuration as TConfiguration;
if (conf != null)
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;
var conf = (source as Skin)?.Configuration as TConfiguration;
if (conf != null)
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();
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateLocalDependencies(parent));
fallbackSource = dependencies.Get<ISkinSource>();
dependencies.CacheAs<ISkinSource>(this);
return dependencies;
}
2018-04-20 15:17:57 +00:00
private Bindable<bool> ignoreBeatmapSkin = new Bindable<bool>();
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
ignoreBeatmapSkin = config.GetBindable<bool>(OsuSetting.IgnoreBeatmapSkin);
ignoreBeatmapSkin.ValueChanged += val => onSourceChanged();
ignoreBeatmapSkin.TriggerChange();
}
2018-04-13 09:19:50 +00: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;
}
}
}