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

80 lines
2.9 KiB
C#
Raw Normal View History

2018-03-20 15:26:36 +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;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Textures;
2018-03-20 15:26:36 +08:00
namespace osu.Game.Skinning
{
public class LocalSkinOverrideContainer : Container, ISkinSource
{
public event Action SourceChanged;
public Drawable GetDrawableComponent(string componentName) => source.GetDrawableComponent(componentName) ?? fallbackSource?.GetDrawableComponent(componentName);
public Texture GetTexture(string componentName) => source.GetTexture(componentName) ?? fallbackSource.GetTexture(componentName);
2018-03-20 15:26:36 +08:00
public SampleChannel GetSample(string sampleName) => source.GetSample(sampleName) ?? fallbackSource?.GetSample(sampleName);
2018-03-22 17:50:19 +08:00
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);
2018-03-22 17:50:19 +08:00
return val ?? fallbackSource?.GetValue(query);
}
2018-03-22 17:50:19 +08:00
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);
2018-03-22 17:50:19 +08:00
return val ?? fallbackSource?.GetValue(query);
}
2018-03-20 15:26:36 +08:00
private readonly ISkinSource source;
private ISkinSource fallbackSource;
public LocalSkinOverrideContainer(ISkinSource source)
{
this.source = source;
}
private void onSourceChanged() => SourceChanged?.Invoke();
2018-03-20 15:26:36 +08:00
protected override IReadOnlyDependencyContainer CreateLocalDependencies(IReadOnlyDependencyContainer parent)
{
var dependencies = new DependencyContainer(base.CreateLocalDependencies(parent));
fallbackSource = dependencies.Get<ISkinSource>();
dependencies.CacheAs<ISkinSource>(this);
return dependencies;
}
protected override void LoadComplete()
{
base.LoadComplete();
if (fallbackSource != null)
fallbackSource.SourceChanged += onSourceChanged;
}
2018-03-20 15:26:36 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= onSourceChanged;
2018-03-20 15:26:36 +08:00
}
}
}