1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 04:47:24 +08:00
osu-lazer/osu.Game/Skinning/LocalSkinOverrideContainer.cs

106 lines
3.7 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;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
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
{
/// <summary>
/// A container which overrides existing skin options with beatmap-local values.
/// </summary>
2018-04-13 17:19:50 +08:00
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
private readonly ISkinSource source;
private ISkinSource fallbackSource;
public LocalSkinOverrideContainer(ISkinSource source)
{
this.source = source;
}
2018-04-20 23:17:57 +08:00
public Drawable GetDrawableComponent(string componentName)
{
Drawable sourceDrawable;
2019-02-21 17:56:34 +08:00
if (beatmapSkins.Value && (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;
2019-02-21 17:56:34 +08:00
if (beatmapSkins.Value && (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;
2019-02-21 17:56:34 +08:00
if (beatmapHitsounds.Value && (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-02-21 17:56:34 +08:00
if (beatmapSkins.Value && (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 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;
}
}
}