mirror of
https://github.com/ppy/osu.git
synced 2025-01-12 15:22:55 +08:00
Rename and expand usability of SkinProvidingContainer
This commit is contained in:
parent
7fd63b39fe
commit
de2c6aa23d
38
osu.Game/Skinning/BeatmapSkinProvidingContainer.cs
Normal file
38
osu.Game/Skinning/BeatmapSkinProvidingContainer.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
}
|
@ -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 @@ namespace osu.Game.Skinning
|
||||
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 @@ namespace osu.Game.Skinning
|
||||
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 @@ namespace osu.Game.Skinning
|
||||
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 @@ namespace osu.Game.Skinning
|
||||
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)
|
||||
return val;
|
||||
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 @@ namespace osu.Game.Skinning
|
||||
|
||||
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 @@ namespace osu.Game.Skinning
|
||||
base.Dispose(isDisposing);
|
||||
|
||||
if (fallbackSource != null)
|
||||
fallbackSource.SourceChanged -= onSourceChanged;
|
||||
fallbackSource.SourceChanged -= TriggerSourceChanged;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user