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
|
|
|
|
|
2018-03-20 07:26:36 +00:00
|
|
|
|
using System;
|
2021-06-11 09:29:28 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-10-12 02:28:11 +00:00
|
|
|
|
using System.Linq;
|
2018-03-20 07:26:36 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2019-09-03 08:57:34 +00:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-03-20 07:26:36 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2018-03-20 07:28:39 +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
|
|
|
|
|
2018-03-20 07:26:36 +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 partial class SkinProvidingContainer : Container, ISkinSource
|
2018-03-20 07:26:36 +00:00
|
|
|
|
{
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public event Action? SourceChanged;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
protected ISkinSource? ParentSource { get; private set; }
|
2019-06-29 01:45:11 +00:00
|
|
|
|
|
2021-06-21 23:51:00 +00: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 08:50:32 +00:00
|
|
|
|
|
2022-11-09 07:04:56 +00:00
|
|
|
|
protected virtual bool AllowDrawableLookup(ISkinComponentLookup lookup) => true;
|
2019-08-26 03:21:11 +00:00
|
|
|
|
|
|
|
|
|
protected virtual bool AllowTextureLookup(string componentName) => true;
|
|
|
|
|
|
2021-12-26 13:29:07 +00:00
|
|
|
|
protected virtual bool AllowSampleLookup(ISampleInfo sampleInfo) => 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-10-12 04:04:18 +00:00
|
|
|
|
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>
|
2021-10-12 02:28:11 +00:00
|
|
|
|
private (ISkin skin, DisableableSkinSource wrapped)[] skinSources = Array.Empty<(ISkin skin, DisableableSkinSource wrapped)>();
|
2021-07-06 08:07:25 +00:00
|
|
|
|
|
2021-06-10 08:56:13 +00:00
|
|
|
|
/// <summary>
|
2021-06-11 08:25:07 +00:00
|
|
|
|
/// Constructs a new <see cref="SkinProvidingContainer"/> initialised with a single skin source.
|
2021-06-10 08:56:13 +00:00
|
|
|
|
/// </summary>
|
2022-11-09 04:44:59 +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-11 09:58:38 +00:00
|
|
|
|
if (skin != null)
|
2021-10-12 02:55:04 +00:00
|
|
|
|
SetSources(new[] { 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.
|
|
|
|
|
/// </summary>
|
2021-06-09 17:36:34 +00:00
|
|
|
|
protected SkinProvidingContainer()
|
|
|
|
|
{
|
2019-08-27 09:27:21 +00:00
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-02-05 08:50:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
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-06-11 09:29:28 +00:00
|
|
|
|
|
2021-07-07 04:51:51 +00:00
|
|
|
|
ParentSource = dependencies.Get<ISkinSource>();
|
|
|
|
|
if (ParentSource != null)
|
|
|
|
|
ParentSource.SourceChanged += TriggerSourceChanged;
|
2021-06-11 08:34:22 +00:00
|
|
|
|
|
2021-07-07 04:51:51 +00:00
|
|
|
|
dependencies.CacheAs<ISkinSource>(this);
|
2021-06-11 09:29:28 +00:00
|
|
|
|
|
2021-07-07 04:51:51 +00:00
|
|
|
|
TriggerSourceChanged();
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
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
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public ISkin? FindProvider(Func<ISkin, bool> lookupFunction)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
{
|
2021-07-06 08:04:59 +00:00
|
|
|
|
foreach (var (skin, lookupWrapper) in skinSources)
|
2021-06-07 14:23:44 +00:00
|
|
|
|
{
|
2021-07-06 08:04:59 +00:00
|
|
|
|
if (lookupFunction(lookupWrapper))
|
2021-06-07 14:23:44 +00:00
|
|
|
|
return skin;
|
|
|
|
|
}
|
2021-05-31 08:04:38 +00:00
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
if (!AllowFallingBackToParent)
|
|
|
|
|
return null;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
return ParentSource?.FindProvider(lookupFunction);
|
2018-04-20 15:17:57 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-06-22 07:19:55 +00:00
|
|
|
|
public IEnumerable<ISkin> AllSources
|
2018-04-20 15:30:41 +00:00
|
|
|
|
{
|
2021-06-22 07:19:55 +00:00
|
|
|
|
get
|
|
|
|
|
{
|
2021-07-06 13:51:56 +00:00
|
|
|
|
foreach (var i in skinSources)
|
|
|
|
|
yield return i.skin;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
if (AllowFallingBackToParent && ParentSource != null)
|
2021-06-22 07:19:55 +00:00
|
|
|
|
{
|
2021-07-06 07:41:48 +00:00
|
|
|
|
foreach (var skin in ParentSource.AllSources)
|
2021-06-22 07:19:55 +00:00
|
|
|
|
yield return skin;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-20 15:30:41 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-11-09 07:04:56 +00:00
|
|
|
|
public Drawable? GetDrawableComponent(ISkinComponentLookup lookup)
|
2018-03-22 09:10:28 +00:00
|
|
|
|
{
|
2021-07-06 08:04:59 +00:00
|
|
|
|
foreach (var (_, lookupWrapper) in skinSources)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
{
|
2022-11-09 04:44:59 +00:00
|
|
|
|
Drawable? sourceDrawable;
|
2022-11-09 05:11:41 +00:00
|
|
|
|
if ((sourceDrawable = lookupWrapper.GetDrawableComponent(lookup)) != null)
|
2021-06-11 09:29:28 +00:00
|
|
|
|
return sourceDrawable;
|
2021-06-09 17:36:34 +00:00
|
|
|
|
}
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
if (!AllowFallingBackToParent)
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-11-09 05:11:41 +00:00
|
|
|
|
return ParentSource?.GetDrawableComponent(lookup);
|
2018-04-20 15:17:57 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public Texture? GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
{
|
2021-07-06 08:04:59 +00:00
|
|
|
|
foreach (var (_, lookupWrapper) in skinSources)
|
2019-09-03 08:57:34 +00:00
|
|
|
|
{
|
2022-11-09 04:44:59 +00:00
|
|
|
|
Texture? sourceTexture;
|
2021-07-06 08:04:59 +00:00
|
|
|
|
if ((sourceTexture = lookupWrapper.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
|
2021-06-11 09:29:28 +00:00
|
|
|
|
return sourceTexture;
|
2021-06-09 17:36:34 +00:00
|
|
|
|
}
|
2021-01-18 07:13:58 +00:00
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
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
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public ISample? GetSample(ISampleInfo sampleInfo)
|
2018-04-20 15:30:41 +00:00
|
|
|
|
{
|
2021-07-06 08:04:59 +00:00
|
|
|
|
foreach (var (_, lookupWrapper) in skinSources)
|
2021-06-09 17:36:34 +00:00
|
|
|
|
{
|
2022-11-09 04:44:59 +00:00
|
|
|
|
ISample? sourceSample;
|
2021-07-06 08:04:59 +00:00
|
|
|
|
if ((sourceSample = lookupWrapper.GetSample(sampleInfo)) != null)
|
2021-06-11 09:29:28 +00:00
|
|
|
|
return sourceSample;
|
2019-09-03 08:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
if (!AllowFallingBackToParent)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return ParentSource?.GetSample(sampleInfo);
|
2018-03-22 09:10:28 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public IBindable<TValue>? GetConfig<TLookup, TValue>(TLookup lookup)
|
|
|
|
|
where TLookup : notnull
|
|
|
|
|
where TValue : notnull
|
2021-01-13 21:05:46 +00:00
|
|
|
|
{
|
2021-07-06 08:04:59 +00:00
|
|
|
|
foreach (var (_, lookupWrapper) in skinSources)
|
2021-01-18 07:13:58 +00:00
|
|
|
|
{
|
2022-11-09 04:44:59 +00:00
|
|
|
|
IBindable<TValue>? bindable;
|
2021-07-06 08:04:59 +00:00
|
|
|
|
if ((bindable = lookupWrapper.GetConfig<TLookup, TValue>(lookup)) != null)
|
2021-01-18 07:13:58 +00:00
|
|
|
|
return bindable;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
if (!AllowFallingBackToParent)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return ParentSource?.GetConfig<TLookup, TValue>(lookup);
|
2021-01-13 21:05:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-07-06 07:57:19 +00:00
|
|
|
|
/// <summary>
|
2021-10-12 02:55:04 +00:00
|
|
|
|
/// Replace the sources used for lookups in this container.
|
2021-07-06 07:57:19 +00:00
|
|
|
|
/// </summary>
|
2021-10-12 04:04:48 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This does not implicitly fire a <see cref="SourceChanged"/> event. Consider calling <see cref="TriggerSourceChanged"/> if required.
|
|
|
|
|
/// </remarks>
|
2021-10-12 02:55:04 +00:00
|
|
|
|
/// <param name="sources">The new sources.</param>
|
|
|
|
|
protected void SetSources(IEnumerable<ISkin> sources)
|
2021-07-07 04:51:51 +00:00
|
|
|
|
{
|
2021-10-12 04:04:18 +00:00
|
|
|
|
lock (sourceSetLock)
|
2021-10-12 02:28:11 +00:00
|
|
|
|
{
|
2021-10-12 04:04:18 +00:00
|
|
|
|
foreach (var skin in skinSources)
|
|
|
|
|
{
|
|
|
|
|
if (skin.skin is ISkinSource source)
|
|
|
|
|
source.SourceChanged -= TriggerSourceChanged;
|
|
|
|
|
}
|
2021-10-12 02:28:11 +00:00
|
|
|
|
|
2021-10-12 04:04:18 +00:00
|
|
|
|
skinSources = sources.Select(skin => (skin, new DisableableSkinSource(skin, this))).ToArray();
|
2021-10-12 02:55:04 +00:00
|
|
|
|
|
2021-10-12 04:04:18 +00:00
|
|
|
|
foreach (var skin in skinSources)
|
|
|
|
|
{
|
|
|
|
|
if (skin.skin is ISkinSource source)
|
|
|
|
|
source.SourceChanged += TriggerSourceChanged;
|
|
|
|
|
}
|
2021-10-12 02:55:04 +00:00
|
|
|
|
}
|
2018-03-23 20:40:26 +00:00
|
|
|
|
}
|
2019-02-27 05:30:04 +00:00
|
|
|
|
|
2021-07-07 04:51:51 +00:00
|
|
|
|
/// <summary>
|
2021-10-12 04:04:48 +00:00
|
|
|
|
/// 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>
|
2021-10-12 04:04:48 +00:00
|
|
|
|
protected virtual void RefreshSources() { }
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-07-06 08:37:34 +00:00
|
|
|
|
protected void TriggerSourceChanged()
|
2021-07-06 07:57:19 +00:00
|
|
|
|
{
|
|
|
|
|
// Expose to implementations, giving them a chance to react before notifying external consumers.
|
2021-10-12 04:04:48 +00:00
|
|
|
|
RefreshSources();
|
2021-07-06 07:57:19 +00:00
|
|
|
|
|
|
|
|
|
SourceChanged?.Invoke();
|
2018-03-23 20:40:26 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2018-03-20 07:26:36 +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-03-20 07:26:36 +00:00
|
|
|
|
base.Dispose(isDisposing);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2021-07-06 07:41:48 +00:00
|
|
|
|
if (ParentSource != null)
|
2021-07-06 08:37:34 +00:00
|
|
|
|
ParentSource.SourceChanged -= TriggerSourceChanged;
|
2021-06-09 08:56:07 +00:00
|
|
|
|
|
2021-07-06 13:51:56 +00:00
|
|
|
|
foreach (var i in skinSources)
|
|
|
|
|
{
|
|
|
|
|
if (i.skin is ISkinSource source)
|
|
|
|
|
source.SourceChanged -= TriggerSourceChanged;
|
|
|
|
|
}
|
2018-03-20 07:26:36 +00:00
|
|
|
|
}
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
2021-06-11 09:29:28 +00:00
|
|
|
|
private class DisableableSkinSource : ISkin
|
2021-06-09 06:25:42 +00:00
|
|
|
|
{
|
2021-06-11 09:29:28 +00:00
|
|
|
|
private readonly ISkin skin;
|
2021-06-09 06:25:42 +00:00
|
|
|
|
private readonly SkinProvidingContainer provider;
|
|
|
|
|
|
2021-06-11 09:29:28 +00:00
|
|
|
|
public DisableableSkinSource(ISkin skin, SkinProvidingContainer provider)
|
2021-06-09 06:25:42 +00:00
|
|
|
|
{
|
2021-06-11 09:29:28 +00:00
|
|
|
|
this.skin = skin;
|
2021-06-09 06:25:42 +00:00
|
|
|
|
this.provider = provider;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-09 07:04:56 +00:00
|
|
|
|
public Drawable? GetDrawableComponent(ISkinComponentLookup lookup)
|
2021-06-11 09:29:28 +00:00
|
|
|
|
{
|
2022-11-09 05:11:41 +00:00
|
|
|
|
if (provider.AllowDrawableLookup(lookup))
|
|
|
|
|
return skin.GetDrawableComponent(lookup);
|
2021-06-11 09:29:28 +00:00
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public Texture? GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT)
|
2021-06-11 09:29:28 +00:00
|
|
|
|
{
|
|
|
|
|
if (provider.AllowTextureLookup(componentName))
|
|
|
|
|
return skin.GetTexture(componentName, wrapModeS, wrapModeT);
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public ISample? GetSample(ISampleInfo sampleInfo)
|
2021-06-11 09:29:28 +00:00
|
|
|
|
{
|
|
|
|
|
if (provider.AllowSampleLookup(sampleInfo))
|
|
|
|
|
return skin.GetSample(sampleInfo);
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
2021-06-11 09:29:28 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
2022-11-09 04:44:59 +00:00
|
|
|
|
public IBindable<TValue>? GetConfig<TLookup, TValue>(TLookup lookup)
|
|
|
|
|
where TLookup : notnull
|
|
|
|
|
where TValue : notnull
|
2021-06-09 06:25:42 +00:00
|
|
|
|
{
|
2021-06-11 09:29:28 +00:00
|
|
|
|
switch (lookup)
|
|
|
|
|
{
|
2022-06-24 12:25:23 +00:00
|
|
|
|
case GlobalSkinColours:
|
|
|
|
|
case SkinComboColourLookup:
|
|
|
|
|
case SkinCustomColourLookup:
|
2021-06-11 09:29:28 +00:00
|
|
|
|
if (provider.AllowColourLookup)
|
|
|
|
|
return skin.GetConfig<TLookup, TValue>(lookup);
|
2021-06-09 06:25:42 +00:00
|
|
|
|
|
2021-06-11 09:29:28 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
if (provider.AllowConfigurationLookup)
|
|
|
|
|
return skin.GetConfig<TLookup, TValue>(lookup);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2018-03-20 07:26:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|