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