1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-20 02:22:55 +08:00
osu-lazer/osu.Game/Skinning/LocalSkinOverrideContainer.cs

105 lines
3.6 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
2019-04-25 16:36:17 +08:00
private readonly ISkin skin;
private ISkinSource fallbackSource;
2019-04-25 16:36:17 +08:00
public LocalSkinOverrideContainer(ISkin skin)
{
2019-04-25 16:36:17 +08:00
this.skin = skin;
}
2018-04-20 23:17:57 +08:00
public Drawable GetDrawableComponent(string componentName)
{
Drawable sourceDrawable;
2019-04-25 16:36:17 +08:00
if (beatmapSkins.Value && (sourceDrawable = skin.GetDrawableComponent(componentName)) != null)
2018-04-20 23:17:57 +08:00
return sourceDrawable;
2018-04-20 23:17:57 +08:00
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-04-25 16:36:17 +08:00
if (beatmapSkins.Value && (sourceTexture = skin.GetTexture(componentName)) != null)
2018-04-20 23:17:57 +08:00
return sourceTexture;
2018-04-20 23:17:57 +08:00
return fallbackSource.GetTexture(componentName);
}
2018-04-13 17:19:50 +08:00
public SampleChannel GetSample(string sampleName)
{
SampleChannel sourceChannel;
2019-04-25 16:36:17 +08:00
if (beatmapHitsounds.Value && (sourceChannel = skin.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;
2019-04-25 16:36:17 +08:00
if ((skin 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>();
if (fallbackSource != null)
fallbackSource.SourceChanged += onSourceChanged;
2018-04-13 17:19:50 +08:00
dependencies.CacheAs<ISkinSource>(this);
var config = dependencies.Get<OsuConfigManager>();
2018-04-13 17:19:50 +08:00
config.BindWith(OsuSetting.BeatmapSkins, beatmapSkins);
config.BindWith(OsuSetting.BeatmapHitsounds, beatmapHitsounds);
beatmapSkins.BindValueChanged(_ => onSourceChanged());
beatmapHitsounds.BindValueChanged(_ => onSourceChanged());
return dependencies;
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
// Must be done before base.Dispose()
SourceChanged = null;
2018-04-13 17:19:50 +08:00
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= onSourceChanged;
}
}
}