Rename and expand usability of SkinProvidingContainer

This commit is contained in:
Dean Herbert 2019-08-26 12:21:11 +09:00
parent 7fd63b39fe
commit de2c6aa23d
2 changed files with 57 additions and 25 deletions

View File

@ -0,0 +1,38 @@
// 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.
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Configuration;
namespace osu.Game.Skinning
{
/// <summary>
/// A container which overrides existing skin options with beatmap-local values.
/// </summary>
public class BeatmapSkinProvidingContainer : SkinProvidingContainer
{
private readonly Bindable<bool> beatmapSkins = new Bindable<bool>();
private readonly Bindable<bool> beatmapHitsounds = new Bindable<bool>();
protected override bool AllowConfigurationLookup => beatmapSkins.Value;
protected override bool AllowDrawableLookup(string componentName) => beatmapSkins.Value;
protected override bool AllowTextureLookup(string componentName) => beatmapSkins.Value;
protected override bool AllowSampleLookup(string componentName) => beatmapHitsounds.Value;
public BeatmapSkinProvidingContainer(ISkin skin)
: base(skin)
{
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
{
config.BindWith(OsuSetting.BeatmapSkins, beatmapSkins);
config.BindWith(OsuSetting.BeatmapHitsounds, beatmapHitsounds);
beatmapSkins.BindValueChanged(_ => TriggerSourceChanged());
beatmapHitsounds.BindValueChanged(_ => TriggerSourceChanged());
}
}
}

View File

@ -4,29 +4,32 @@
using System;
using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Textures;
using osu.Game.Configuration;
namespace osu.Game.Skinning
{
/// <summary>
/// A container which overrides existing skin options with beatmap-local values.
/// A container which adds a local <see cref="ISkinSource"/> to the hierarchy.
/// </summary>
public class LocalSkinOverrideContainer : Container, ISkinSource
public class SkinProvidingContainer : Container, ISkinSource
{
public event Action SourceChanged;
private readonly Bindable<bool> beatmapSkins = new Bindable<bool>();
private readonly Bindable<bool> beatmapHitsounds = new Bindable<bool>();
private readonly ISkin skin;
private ISkinSource fallbackSource;
public LocalSkinOverrideContainer(ISkin skin)
protected virtual bool AllowDrawableLookup(string componentName) => true;
protected virtual bool AllowTextureLookup(string componentName) => true;
protected virtual bool AllowSampleLookup(string componentName) => true;
protected virtual bool AllowConfigurationLookup => true;
public SkinProvidingContainer(ISkin skin)
{
this.skin = skin;
}
@ -34,7 +37,7 @@ public LocalSkinOverrideContainer(ISkin skin)
public Drawable GetDrawableComponent(string componentName)
{
Drawable sourceDrawable;
if (beatmapSkins.Value && (sourceDrawable = skin?.GetDrawableComponent(componentName)) != null)
if (AllowDrawableLookup(componentName) && (sourceDrawable = skin?.GetDrawableComponent(componentName)) != null)
return sourceDrawable;
return fallbackSource?.GetDrawableComponent(componentName);
@ -43,7 +46,7 @@ public Drawable GetDrawableComponent(string componentName)
public Texture GetTexture(string componentName)
{
Texture sourceTexture;
if (beatmapSkins.Value && (sourceTexture = skin?.GetTexture(componentName)) != null)
if (AllowTextureLookup(componentName) && (sourceTexture = skin?.GetTexture(componentName)) != null)
return sourceTexture;
return fallbackSource.GetTexture(componentName);
@ -52,7 +55,7 @@ public Texture GetTexture(string componentName)
public SampleChannel GetSample(string sampleName)
{
SampleChannel sourceChannel;
if (beatmapHitsounds.Value && (sourceChannel = skin?.GetSample(sampleName)) != null)
if (AllowSampleLookup(sampleName) && (sourceChannel = skin?.GetSample(sampleName)) != null)
return sourceChannel;
return fallbackSource?.GetSample(sampleName);
@ -61,14 +64,13 @@ public SampleChannel GetSample(string sampleName)
public TValue GetValue<TConfiguration, TValue>(Func<TConfiguration, TValue> query) where TConfiguration : SkinConfiguration
{
TValue val;
if ((skin as Skin)?.Configuration is TConfiguration conf)
if (beatmapSkins.Value && (val = query.Invoke(conf)) != null)
if (AllowConfigurationLookup && skin != null && (val = skin.GetValue(query)) != null)
return val;
return fallbackSource == null ? default : fallbackSource.GetValue(query);
}
private void onSourceChanged() => SourceChanged?.Invoke();
protected virtual void TriggerSourceChanged() => SourceChanged?.Invoke();
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
@ -76,18 +78,10 @@ protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnl
fallbackSource = dependencies.Get<ISkinSource>();
if (fallbackSource != null)
fallbackSource.SourceChanged += onSourceChanged;
fallbackSource.SourceChanged += TriggerSourceChanged;
dependencies.CacheAs<ISkinSource>(this);
var config = dependencies.Get<OsuConfigManager>();
config.BindWith(OsuSetting.BeatmapSkins, beatmapSkins);
config.BindWith(OsuSetting.BeatmapHitsounds, beatmapHitsounds);
beatmapSkins.BindValueChanged(_ => onSourceChanged());
beatmapHitsounds.BindValueChanged(_ => onSourceChanged());
return dependencies;
}
@ -99,7 +93,7 @@ protected override void Dispose(bool isDisposing)
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= onSourceChanged;
fallbackSource.SourceChanged -= TriggerSourceChanged;
}
}
}