osu/osu.Game/Skinning/SkinProvidingContainer.cs

290 lines
9.5 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 09:19:50 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-07-17 07:54:30 +00:00
using osu.Framework.Graphics.OpenGL.Textures;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics.Textures;
2019-08-23 11:32:43 +00:00
using osu.Game.Audio;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Skinning
{
/// <summary>
/// A container which adds a local <see cref="ISkinSource"/> to the hierarchy.
/// </summary>
public class SkinProvidingContainer : Container, ISkinSource
2018-04-13 09:19:50 +00:00
{
public event Action SourceChanged;
[CanBeNull]
protected ISkinSource ParentSource { get; private set; }
2019-06-29 01:45:11 +00:00
/// <summary>
/// Whether falling back to parent <see cref="ISkinSource"/>s is allowed in this container.
/// </summary>
protected virtual bool AllowFallingBackToParent => true;
protected virtual bool AllowDrawableLookup(ISkinComponent component) => true;
protected virtual bool AllowTextureLookup(string componentName) => true;
protected virtual bool AllowSampleLookup(ISampleInfo sampleInfo) => true;
protected virtual bool AllowConfigurationLookup => true;
protected virtual bool AllowColourLookup => true;
private readonly object sourceSetLock = new object();
2021-07-06 08:07:25 +00:00
/// <summary>
/// A dictionary mapping each <see cref="ISkin"/> source to a wrapper which handles lookup allowances.
/// </summary>
private (ISkin skin, DisableableSkinSource wrapped)[] skinSources = Array.Empty<(ISkin skin, DisableableSkinSource wrapped)>();
2021-07-06 08:07:25 +00:00
/// <summary>
/// Constructs a new <see cref="SkinProvidingContainer"/> initialised with a single skin source.
/// </summary>
public SkinProvidingContainer([CanBeNull] ISkin skin)
: this()
{
if (skin != null)
SetSources(new[] { skin });
}
/// <summary>
/// Constructs a new <see cref="SkinProvidingContainer"/> with no sources.
/// </summary>
protected SkinProvidingContainer()
{
RelativeSizeAxes = Axes.Both;
}
2021-07-07 04:51:51 +00:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
2018-04-20 15:17:57 +00:00
{
2021-07-07 04:51:51 +00:00
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
2021-07-07 04:51:51 +00:00
ParentSource = dependencies.Get<ISkinSource>();
if (ParentSource != null)
ParentSource.SourceChanged += TriggerSourceChanged;
2021-07-07 04:51:51 +00:00
dependencies.CacheAs<ISkinSource>(this);
2021-07-07 04:51:51 +00:00
TriggerSourceChanged();
2021-07-07 04:51:51 +00:00
return dependencies;
2018-04-20 15:17:57 +00:00
}
2018-04-13 09:19:50 +00:00
2021-05-31 08:04:38 +00:00
public ISkin FindProvider(Func<ISkin, bool> lookupFunction)
2018-04-20 15:17:57 +00:00
{
foreach (var (skin, lookupWrapper) in skinSources)
{
if (lookupFunction(lookupWrapper))
return skin;
}
2021-05-31 08:04:38 +00:00
if (!AllowFallingBackToParent)
return null;
return ParentSource?.FindProvider(lookupFunction);
2018-04-20 15:17:57 +00:00
}
2018-04-13 09:19:50 +00:00
public IEnumerable<ISkin> AllSources
{
get
{
2021-07-06 13:51:56 +00:00
foreach (var i in skinSources)
yield return i.skin;
if (AllowFallingBackToParent && ParentSource != null)
{
foreach (var skin in ParentSource.AllSources)
yield return skin;
}
}
}
2018-04-13 09:19:50 +00:00
public Drawable GetDrawableComponent(ISkinComponent component)
2018-04-13 09:19:50 +00:00
{
foreach (var (_, lookupWrapper) in skinSources)
{
Drawable sourceDrawable;
if ((sourceDrawable = lookupWrapper.GetDrawableComponent(component)) != null)
return sourceDrawable;
}
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetDrawableComponent(component);
2018-04-20 15:17:57 +00:00
}
2018-04-13 09:19:50 +00:00
2020-07-17 07:54:30 +00:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
2018-04-20 15:17:57 +00:00
{
foreach (var (_, lookupWrapper) in skinSources)
{
Texture sourceTexture;
if ((sourceTexture = lookupWrapper.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
return sourceTexture;
}
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetTexture(componentName, wrapModeS, wrapModeT);
2018-04-20 15:17:57 +00:00
}
2018-04-13 09:19:50 +00:00
public ISample GetSample(ISampleInfo sampleInfo)
{
foreach (var (_, lookupWrapper) in skinSources)
{
ISample sourceSample;
if ((sourceSample = lookupWrapper.GetSample(sampleInfo)) != null)
return sourceSample;
}
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetSample(sampleInfo);
2018-04-13 09:19:50 +00:00
}
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
{
foreach (var (_, lookupWrapper) in skinSources)
{
IBindable<TValue> bindable;
if ((bindable = lookupWrapper.GetConfig<TLookup, TValue>(lookup)) != null)
return bindable;
}
if (!AllowFallingBackToParent)
return null;
return ParentSource?.GetConfig<TLookup, TValue>(lookup);
}
/// <summary>
/// Replace the sources used for lookups in this container.
/// </summary>
/// <remarks>
/// This does not implicitly fire a <see cref="SourceChanged"/> event. Consider calling <see cref="TriggerSourceChanged"/> if required.
/// </remarks>
/// <param name="sources">The new sources.</param>
protected void SetSources(IEnumerable<ISkin> sources)
2021-07-07 04:51:51 +00:00
{
lock (sourceSetLock)
{
foreach (var skin in skinSources)
{
if (skin.skin is ISkinSource source)
source.SourceChanged -= TriggerSourceChanged;
}
skinSources = sources.Select(skin => (skin, new DisableableSkinSource(skin, this))).ToArray();
foreach (var skin in skinSources)
{
if (skin.skin is ISkinSource source)
source.SourceChanged += TriggerSourceChanged;
}
}
2018-04-13 09:19:50 +00:00
}
2021-07-07 04:51:51 +00:00
/// <summary>
/// Invoked after any consumed source change, before the external <see cref="SourceChanged"/> event is fired.
2021-07-07 04:51:51 +00:00
/// This is also invoked once initially during <see cref="CreateChildDependencies"/> to ensure sources are ready for children consumption.
/// </summary>
protected virtual void RefreshSources() { }
2018-04-13 09:19:50 +00:00
2021-07-06 08:37:34 +00:00
protected void TriggerSourceChanged()
{
// Expose to implementations, giving them a chance to react before notifying external consumers.
RefreshSources();
SourceChanged?.Invoke();
2018-04-13 09:19:50 +00:00
}
protected override void Dispose(bool isDisposing)
{
// Must be done before base.Dispose()
SourceChanged = null;
2018-04-13 09:19:50 +00:00
base.Dispose(isDisposing);
if (ParentSource != null)
2021-07-06 08:37:34 +00:00
ParentSource.SourceChanged -= TriggerSourceChanged;
2021-07-06 13:51:56 +00:00
foreach (var i in skinSources)
{
if (i.skin is ISkinSource source)
source.SourceChanged -= TriggerSourceChanged;
}
2018-04-13 09:19:50 +00:00
}
private class DisableableSkinSource : ISkin
{
private readonly ISkin skin;
private readonly SkinProvidingContainer provider;
public DisableableSkinSource(ISkin skin, SkinProvidingContainer provider)
{
this.skin = skin;
this.provider = provider;
}
public Drawable GetDrawableComponent(ISkinComponent component)
{
if (provider.AllowDrawableLookup(component))
return skin.GetDrawableComponent(component);
return null;
}
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
{
if (provider.AllowTextureLookup(componentName))
return skin.GetTexture(componentName, wrapModeS, wrapModeT);
return null;
}
public ISample GetSample(ISampleInfo sampleInfo)
{
if (provider.AllowSampleLookup(sampleInfo))
return skin.GetSample(sampleInfo);
return null;
}
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
{
switch (lookup)
{
case GlobalSkinColours _:
case SkinComboColourLookup _:
case SkinCustomColourLookup _:
if (provider.AllowColourLookup)
return skin.GetConfig<TLookup, TValue>(lookup);
break;
default:
if (provider.AllowConfigurationLookup)
return skin.GetConfig<TLookup, TValue>(lookup);
break;
}
return null;
}
2018-04-13 09:19:50 +00:00
}
}
}