2019-01-24 08:43:03 +00:00
|
|
|
|
// 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;
|
2021-06-10 09:17:51 +00:00
|
|
|
|
using System.Collections.Specialized;
|
|
|
|
|
using System.Linq;
|
2021-05-31 09:00:06 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2019-09-03 08:57:34 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2019-02-05 08:50:32 +00:00
|
|
|
|
/// <summary>
|
2019-08-26 03:21:11 +00:00
|
|
|
|
/// A container which adds a local <see cref="ISkinSource"/> to the hierarchy.
|
2019-02-05 08:50:32 +00:00
|
|
|
|
/// </summary>
|
2019-08-26 03:21:11 +00:00
|
|
|
|
public class SkinProvidingContainer : Container, ISkinSource
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
public event Action SourceChanged;
|
|
|
|
|
|
2021-06-09 17:36:34 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The list of skins provided by this <see cref="SkinProvidingContainer"/>.
|
|
|
|
|
/// </summary>
|
2021-06-10 09:17:51 +00:00
|
|
|
|
protected readonly BindableList<ISkin> SkinSources = new BindableList<ISkin>();
|
2019-06-29 01:45:11 +00:00
|
|
|
|
|
2021-05-31 09:00:06 +00:00
|
|
|
|
[CanBeNull]
|
2019-02-05 08:50:32 +00:00
|
|
|
|
private ISkinSource fallbackSource;
|
|
|
|
|
|
2021-06-09 06:25:42 +00:00
|
|
|
|
private readonly NoFallbackProxy noFallbackLookupProxy;
|
|
|
|
|
|
2019-08-30 05:39:02 +00:00
|
|
|
|
protected virtual bool AllowDrawableLookup(ISkinComponent component) => true;
|
2019-08-26 03:21:11 +00:00
|
|
|
|
|
|
|
|
|
protected virtual bool AllowTextureLookup(string componentName) => true;
|
|
|
|
|
|
2019-08-28 07:36:20 +00:00
|
|
|
|
protected virtual bool AllowSampleLookup(ISampleInfo componentName) => true;
|
2019-08-26 03:21:11 +00:00
|
|
|
|
|
|
|
|
|
protected virtual bool AllowConfigurationLookup => true;
|
|
|
|
|
|
2021-01-13 18:07:07 +00:00
|
|
|
|
protected virtual bool AllowColourLookup => true;
|
|
|
|
|
|
2021-06-10 08:56:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new <see cref="SkinProvidingContainer"/> with a single skin added to the protected <see cref="SkinSources"/> list.
|
|
|
|
|
/// </summary>
|
2019-08-26 03:21:11 +00:00
|
|
|
|
public SkinProvidingContainer(ISkin skin)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
: this()
|
2019-02-05 08:50:32 +00:00
|
|
|
|
{
|
2021-06-10 08:55:22 +00:00
|
|
|
|
SkinSources.Add(skin);
|
2021-06-09 17:36:34 +00:00
|
|
|
|
}
|
2019-08-27 09:27:21 +00:00
|
|
|
|
|
2021-06-10 08:56:13 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructs a new <see cref="SkinProvidingContainer"/> with no sources.
|
|
|
|
|
/// Up to the implementation for adding to the <see cref="SkinSources"/> list.
|
|
|
|
|
/// </summary>
|
2021-06-09 17:36:34 +00:00
|
|
|
|
protected SkinProvidingContainer()
|
|
|
|
|
{
|
2019-08-27 09:27:21 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
|
|
|
|
noFallbackLookupProxy = new NoFallbackProxy(this);
|
2021-06-09 08:56:07 +00:00
|
|
|
|
|
2021-06-10 09:17:51 +00:00
|
|
|
|
SkinSources.BindCollectionChanged(((_, args) =>
|
|
|
|
|
{
|
|
|
|
|
switch (args.Action)
|
|
|
|
|
{
|
|
|
|
|
case NotifyCollectionChangedAction.Add:
|
|
|
|
|
foreach (var source in args.NewItems.Cast<ISkin>().OfType<ISkinSource>())
|
|
|
|
|
source.SourceChanged += OnSourceChanged;
|
|
|
|
|
|
|
|
|
|
break;
|
2021-06-10 10:15:18 +00:00
|
|
|
|
|
|
|
|
|
case NotifyCollectionChangedAction.Remove:
|
|
|
|
|
foreach (var source in args.OldItems.Cast<ISkin>().OfType<ISkinSource>())
|
|
|
|
|
source.SourceChanged -= OnSourceChanged;
|
|
|
|
|
|
|
|
|
|
break;
|
2021-06-10 09:17:51 +00:00
|
|
|
|
}
|
|
|
|
|
}), true);
|
2019-02-05 08:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 08:04:38 +00:00
|
|
|
|
public ISkin FindProvider(Func<ISkin, bool> lookupFunction)
|
|
|
|
|
{
|
2021-06-10 08:55:22 +00:00
|
|
|
|
foreach (var skin in SkinSources)
|
2021-06-07 14:23:44 +00:00
|
|
|
|
{
|
2021-06-09 06:25:42 +00:00
|
|
|
|
// a proxy must be used here to correctly pass through the "Allow" checks without implicitly falling back to the fallbackSource.
|
|
|
|
|
if (lookupFunction(noFallbackLookupProxy))
|
2021-06-07 14:23:44 +00:00
|
|
|
|
return skin;
|
|
|
|
|
}
|
2021-05-31 08:04:38 +00:00
|
|
|
|
|
2021-05-31 09:00:06 +00:00
|
|
|
|
return fallbackSource?.FindProvider(lookupFunction);
|
2021-05-31 08:04:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 05:39:02 +00:00
|
|
|
|
public Drawable GetDrawableComponent(ISkinComponent component)
|
2021-06-09 06:25:42 +00:00
|
|
|
|
=> GetDrawableComponent(component, true);
|
|
|
|
|
|
|
|
|
|
public Drawable GetDrawableComponent(ISkinComponent component, bool fallback)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
{
|
2021-06-09 17:36:34 +00:00
|
|
|
|
if (AllowDrawableLookup(component))
|
|
|
|
|
{
|
2021-06-10 08:55:22 +00:00
|
|
|
|
foreach (var skin in SkinSources)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
{
|
|
|
|
|
Drawable sourceDrawable;
|
|
|
|
|
if ((sourceDrawable = skin?.GetDrawableComponent(component)) != null)
|
|
|
|
|
return sourceDrawable;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2021-06-09 06:25:42 +00:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-08-30 05:39:02 +00:00
|
|
|
|
return fallbackSource?.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)
|
2021-06-09 06:25:42 +00:00
|
|
|
|
=> GetTexture(componentName, wrapModeS, wrapModeT, true);
|
|
|
|
|
|
|
|
|
|
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool fallback)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
{
|
2021-06-09 17:36:34 +00:00
|
|
|
|
if (AllowTextureLookup(componentName))
|
|
|
|
|
{
|
2021-06-10 08:55:22 +00:00
|
|
|
|
foreach (var skin in SkinSources)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
{
|
|
|
|
|
Texture sourceTexture;
|
|
|
|
|
if ((sourceTexture = skin?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
|
|
|
|
|
return sourceTexture;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2021-06-09 06:25:42 +00:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2020-07-17 07:54:30 +00:00
|
|
|
|
return fallbackSource?.GetTexture(componentName, wrapModeS, wrapModeT);
|
2018-04-20 15:17:57 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-02-18 09:32:28 +00:00
|
|
|
|
public ISample GetSample(ISampleInfo sampleInfo)
|
2021-06-09 06:25:42 +00:00
|
|
|
|
=> GetSample(sampleInfo, true);
|
|
|
|
|
|
|
|
|
|
public ISample GetSample(ISampleInfo sampleInfo, bool fallback)
|
2018-04-20 15:30:41 +00:00
|
|
|
|
{
|
2021-06-09 17:36:34 +00:00
|
|
|
|
if (AllowSampleLookup(sampleInfo))
|
|
|
|
|
{
|
2021-06-10 08:55:22 +00:00
|
|
|
|
foreach (var skin in SkinSources)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
{
|
|
|
|
|
ISample sourceSample;
|
|
|
|
|
if ((sourceSample = skin?.GetSample(sampleInfo)) != null)
|
|
|
|
|
return sourceSample;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2021-06-09 06:25:42 +00:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-08-23 11:32:43 +00:00
|
|
|
|
return fallbackSource?.GetSample(sampleInfo);
|
2018-04-20 15:30:41 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2019-09-03 08:57:34 +00:00
|
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
|
2021-06-09 06:25:42 +00:00
|
|
|
|
=> GetConfig<TLookup, TValue>(lookup, true);
|
|
|
|
|
|
|
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup, bool fallback)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-06-09 17:36:34 +00:00
|
|
|
|
if (lookup is GlobalSkinColours || lookup is SkinCustomColourLookup)
|
2021-06-10 09:17:51 +00:00
|
|
|
|
return lookupWithFallback<TLookup, TValue>(lookup, AllowColourLookup, fallback);
|
2019-09-03 08:57:34 +00:00
|
|
|
|
|
2021-06-10 09:17:51 +00:00
|
|
|
|
return lookupWithFallback<TLookup, TValue>(lookup, AllowConfigurationLookup, fallback);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 06:25:42 +00:00
|
|
|
|
private IBindable<TValue> lookupWithFallback<TLookup, TValue>(TLookup lookup, bool canUseSkinLookup, bool canUseFallback)
|
2021-01-13 21:05:46 +00:00
|
|
|
|
{
|
2021-01-18 07:13:58 +00:00
|
|
|
|
if (canUseSkinLookup)
|
|
|
|
|
{
|
2021-06-10 08:55:22 +00:00
|
|
|
|
foreach (var skin in SkinSources)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
{
|
|
|
|
|
IBindable<TValue> bindable;
|
|
|
|
|
if ((bindable = skin?.GetConfig<TLookup, TValue>(lookup)) != null)
|
|
|
|
|
return bindable;
|
|
|
|
|
}
|
2021-01-18 07:13:58 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 06:25:42 +00:00
|
|
|
|
if (!canUseFallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-01-18 07:13:58 +00:00
|
|
|
|
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
|
2021-01-13 21:05:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 17:36:34 +00:00
|
|
|
|
protected virtual void OnSourceChanged() => SourceChanged?.Invoke();
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-07-11 08:07:14 +00:00
|
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2018-07-11 08:07:14 +00:00
|
|
|
|
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
fallbackSource = dependencies.Get<ISkinSource>();
|
2019-02-27 05:30:04 +00:00
|
|
|
|
if (fallbackSource != null)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
fallbackSource.SourceChanged += OnSourceChanged;
|
2019-02-27 05:30:04 +00:00
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
dependencies.CacheAs<ISkinSource>(this);
|
|
|
|
|
|
2019-02-27 05:30:04 +00:00
|
|
|
|
return dependencies;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
2019-02-27 12:04:14 +00:00
|
|
|
|
// Must be done before base.Dispose()
|
|
|
|
|
SourceChanged = null;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
if (fallbackSource != null)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
fallbackSource.SourceChanged -= OnSourceChanged;
|
2021-06-09 08:56:07 +00:00
|
|
|
|
|
2021-06-10 14:23:15 +00:00
|
|
|
|
foreach (var source in SkinSources.OfType<ISkinSource>())
|
|
|
|
|
source.SourceChanged -= OnSourceChanged;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
|
|
|
|
private class NoFallbackProxy : ISkinSource
|
|
|
|
|
{
|
|
|
|
|
private readonly SkinProvidingContainer provider;
|
|
|
|
|
|
|
|
|
|
public NoFallbackProxy(SkinProvidingContainer provider)
|
|
|
|
|
{
|
|
|
|
|
this.provider = provider;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Drawable GetDrawableComponent(ISkinComponent component)
|
|
|
|
|
=> provider.GetDrawableComponent(component, false);
|
|
|
|
|
|
|
|
|
|
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
|
|
|
|
|
=> provider.GetTexture(componentName, wrapModeS, wrapModeT, false);
|
|
|
|
|
|
|
|
|
|
public ISample GetSample(ISampleInfo sampleInfo)
|
|
|
|
|
=> provider.GetSample(sampleInfo, false);
|
|
|
|
|
|
|
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
|
|
|
|
|
=> provider.GetConfig<TLookup, TValue>(lookup, false);
|
|
|
|
|
|
|
|
|
|
public event Action SourceChanged
|
|
|
|
|
{
|
|
|
|
|
add => provider.SourceChanged += value;
|
|
|
|
|
remove => provider.SourceChanged -= value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISkin FindProvider(Func<ISkin, bool> lookupFunction) =>
|
|
|
|
|
provider.FindProvider(lookupFunction);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|