1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00
osu-lazer/osu.Game/Skinning/SkinProvidingContainer.cs

264 lines
9.1 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 System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using JetBrains.Annotations;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-07-17 15:54:30 +08:00
using osu.Framework.Graphics.OpenGL.Textures;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Textures;
2019-08-23 19:32:43 +08:00
using osu.Game.Audio;
2018-04-13 17:19:50 +08: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 17:19:50 +08:00
{
public event Action SourceChanged;
/// <summary>
/// Skins which should be exposed by this container, in order of lookup precedence.
/// </summary>
protected readonly BindableList<ISkin> SkinSources = new BindableList<ISkin>();
2019-06-29 09:45:11 +08:00
/// <summary>
/// A dictionary mapping each <see cref="ISkin"/> from the <see cref="SkinSources"/>
/// to one that performs the "allow lookup" checks before proceeding with a lookup.
/// </summary>
private readonly Dictionary<ISkin, DisableableSkinSource> disableableSkinSources = new Dictionary<ISkin, DisableableSkinSource>();
[CanBeNull]
private ISkinSource fallbackSource;
protected virtual bool AllowDrawableLookup(ISkinComponent component) => true;
protected virtual bool AllowTextureLookup(string componentName) => true;
protected virtual bool AllowSampleLookup(ISampleInfo componentName) => true;
protected virtual bool AllowConfigurationLookup => true;
protected virtual bool AllowColourLookup => true;
/// <summary>
/// Constructs a new <see cref="SkinProvidingContainer"/> initialised with a single skin source.
/// </summary>
public SkinProvidingContainer([CanBeNull] ISkin skin)
: this()
{
if (skin != null)
SkinSources.Add(skin);
}
/// <summary>
/// Constructs a new <see cref="SkinProvidingContainer"/> with no sources.
/// Implementations can add or change sources through the <see cref="SkinSources"/> list.
/// </summary>
protected SkinProvidingContainer()
{
RelativeSizeAxes = Axes.Both;
SkinSources.BindCollectionChanged(((_, args) =>
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (var skin in args.NewItems.Cast<ISkin>())
{
disableableSkinSources.Add(skin, new DisableableSkinSource(skin, this));
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged;
}
break;
case NotifyCollectionChangedAction.Reset:
case NotifyCollectionChangedAction.Remove:
foreach (var skin in args.OldItems.Cast<ISkin>())
{
disableableSkinSources.Remove(skin);
if (skin is ISkinSource source)
source.SourceChanged -= OnSourceChanged;
}
break;
case NotifyCollectionChangedAction.Replace:
foreach (var skin in args.OldItems.Cast<ISkin>())
{
disableableSkinSources.Remove(skin);
if (skin is ISkinSource source)
source.SourceChanged -= OnSourceChanged;
}
foreach (var skin in args.NewItems.Cast<ISkin>())
{
disableableSkinSources.Add(skin, new DisableableSkinSource(skin, this));
if (skin is ISkinSource source)
source.SourceChanged += OnSourceChanged;
}
break;
}
}), true);
}
2021-05-31 16:04:38 +08:00
public ISkin FindProvider(Func<ISkin, bool> lookupFunction)
{
foreach (var skin in SkinSources)
{
if (lookupFunction(disableableSkinSources[skin]))
return skin;
}
2021-05-31 16:04:38 +08:00
return fallbackSource?.FindProvider(lookupFunction);
2021-05-31 16:04:38 +08:00
}
public Drawable GetDrawableComponent(ISkinComponent component)
2018-04-20 23:17:57 +08:00
{
foreach (var skin in SkinSources)
{
Drawable sourceDrawable;
if ((sourceDrawable = disableableSkinSources[skin]?.GetDrawableComponent(component)) != null)
return sourceDrawable;
}
return fallbackSource?.GetDrawableComponent(component);
2018-04-20 23:17:57 +08:00
}
2018-04-13 17:19:50 +08:00
2020-07-17 15:54:30 +08:00
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
2018-04-20 23:17:57 +08:00
{
foreach (var skin in SkinSources)
{
Texture sourceTexture;
if ((sourceTexture = disableableSkinSources[skin]?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
return sourceTexture;
}
2020-07-17 15:54:30 +08:00
return fallbackSource?.GetTexture(componentName, wrapModeS, wrapModeT);
2018-04-20 23:17:57 +08:00
}
2018-04-13 17:19:50 +08:00
public ISample GetSample(ISampleInfo sampleInfo)
{
foreach (var skin in SkinSources)
{
ISample sourceSample;
if ((sourceSample = disableableSkinSources[skin]?.GetSample(sampleInfo)) != null)
return sourceSample;
}
2019-08-23 19:32:43 +08:00
return fallbackSource?.GetSample(sampleInfo);
}
2018-04-13 17:19:50 +08:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
{
foreach (var skin in SkinSources)
{
IBindable<TValue> bindable;
if ((bindable = disableableSkinSources[skin]?.GetConfig<TLookup, TValue>(lookup)) != null)
return bindable;
}
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
}
protected virtual void OnSourceChanged() => SourceChanged?.Invoke();
2018-04-13 17:19:50 +08:00
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);
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;
foreach (var source in SkinSources.OfType<ISkinSource>())
source.SourceChanged -= OnSourceChanged;
2018-04-13 17:19:50 +08: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 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 17:19:50 +08:00
}
}
}