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
|
|
|
|
|
|
|
|
|
using System;
|
2021-05-31 17:00:06 +08:00
|
|
|
|
using JetBrains.Annotations;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2019-09-03 16:57:34 +08:00
|
|
|
|
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
|
|
|
|
|
{
|
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-04-13 17:19:50 +08:00
|
|
|
|
{
|
|
|
|
|
public event Action SourceChanged;
|
|
|
|
|
|
2021-05-31 17:00:06 +08:00
|
|
|
|
[CanBeNull]
|
2019-04-25 16:36:17 +08:00
|
|
|
|
private readonly ISkin skin;
|
2019-06-29 09:45:11 +08:00
|
|
|
|
|
2021-05-31 17:00:06 +08:00
|
|
|
|
[CanBeNull]
|
2019-02-05 16:50:32 +08:00
|
|
|
|
private ISkinSource fallbackSource;
|
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
private readonly NoFallbackProxy noFallbackLookupProxy;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2019-08-28 15:36:20 +08:00
|
|
|
|
protected virtual bool AllowSampleLookup(ISampleInfo componentName) => 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;
|
|
|
|
|
|
2019-08-26 11:21:11 +08:00
|
|
|
|
public SkinProvidingContainer(ISkin skin)
|
2019-02-05 16:50:32 +08:00
|
|
|
|
{
|
2019-04-25 16:36:17 +08:00
|
|
|
|
this.skin = skin;
|
2019-08-27 17:27:21 +08:00
|
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2021-06-09 14:25:42 +08:00
|
|
|
|
|
|
|
|
|
noFallbackLookupProxy = new NoFallbackProxy(this);
|
2021-06-09 16:56:07 +08:00
|
|
|
|
|
|
|
|
|
if (skin is ISkinSource source)
|
|
|
|
|
source.SourceChanged += TriggerSourceChanged;
|
2019-02-05 16:50:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 16:04:38 +08:00
|
|
|
|
public ISkin FindProvider(Func<ISkin, bool> lookupFunction)
|
|
|
|
|
{
|
2021-06-07 22:23:44 +08:00
|
|
|
|
if (skin is ISkinSource source)
|
|
|
|
|
{
|
|
|
|
|
if (source.FindProvider(lookupFunction) is ISkin found)
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
else if (skin != null)
|
|
|
|
|
{
|
2021-06-09 14:25:42 +08: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 22:23:44 +08:00
|
|
|
|
return skin;
|
|
|
|
|
}
|
2021-05-31 16:04:38 +08:00
|
|
|
|
|
2021-05-31 17:00:06 +08:00
|
|
|
|
return fallbackSource?.FindProvider(lookupFunction);
|
2021-05-31 16:04:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 13:39:02 +08:00
|
|
|
|
public Drawable GetDrawableComponent(ISkinComponent component)
|
2021-06-09 14:25:42 +08:00
|
|
|
|
=> GetDrawableComponent(component, true);
|
|
|
|
|
|
|
|
|
|
public Drawable GetDrawableComponent(ISkinComponent component, bool fallback)
|
2018-04-20 23:17:57 +08:00
|
|
|
|
{
|
|
|
|
|
Drawable sourceDrawable;
|
2019-08-30 13:39:02 +08:00
|
|
|
|
if (AllowDrawableLookup(component) && (sourceDrawable = skin?.GetDrawableComponent(component)) != null)
|
2018-04-20 23:17:57 +08:00
|
|
|
|
return sourceDrawable;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-08-30 13:39:02 +08:00
|
|
|
|
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)
|
2021-06-09 14:25:42 +08:00
|
|
|
|
=> GetTexture(componentName, wrapModeS, wrapModeT, true);
|
|
|
|
|
|
|
|
|
|
public Texture GetTexture(string componentName, WrapMode wrapModeS, WrapMode wrapModeT, bool fallback)
|
2018-04-20 23:17:57 +08:00
|
|
|
|
{
|
|
|
|
|
Texture sourceTexture;
|
2020-07-17 15:54:30 +08:00
|
|
|
|
if (AllowTextureLookup(componentName) && (sourceTexture = skin?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
|
2018-04-20 23:17:57 +08:00
|
|
|
|
return sourceTexture;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
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
|
|
|
|
|
2021-02-18 17:32:28 +08:00
|
|
|
|
public ISample GetSample(ISampleInfo sampleInfo)
|
2021-06-09 14:25:42 +08:00
|
|
|
|
=> GetSample(sampleInfo, true);
|
|
|
|
|
|
|
|
|
|
public ISample GetSample(ISampleInfo sampleInfo, bool fallback)
|
2018-04-20 23:30:41 +08:00
|
|
|
|
{
|
2021-02-18 17:32:28 +08:00
|
|
|
|
ISample sourceChannel;
|
2019-08-28 15:36:20 +08:00
|
|
|
|
if (AllowSampleLookup(sampleInfo) && (sourceChannel = skin?.GetSample(sampleInfo)) != null)
|
2018-04-20 23:30:41 +08:00
|
|
|
|
return sourceChannel;
|
2019-02-27 20:07:17 +08:00
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-08-23 19:32:43 +08:00
|
|
|
|
return fallbackSource?.GetSample(sampleInfo);
|
2018-04-20 23:30:41 +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-06-09 14:25:42 +08:00
|
|
|
|
=> GetConfig<TLookup, TValue>(lookup, true);
|
|
|
|
|
|
|
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup, bool fallback)
|
2018-04-13 17:19:50 +08:00
|
|
|
|
{
|
2021-01-14 02:07:07 +08:00
|
|
|
|
if (skin != null)
|
2019-09-03 16:57:34 +08:00
|
|
|
|
{
|
2021-01-15 12:30:24 +08:00
|
|
|
|
if (lookup is GlobalSkinColours || lookup is SkinCustomColourLookup)
|
2021-06-09 14:25:42 +08:00
|
|
|
|
return lookupWithFallback<TLookup, TValue>(lookup, AllowColourLookup, fallback);
|
2021-01-18 15:13:58 +08:00
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
return lookupWithFallback<TLookup, TValue>(lookup, AllowConfigurationLookup, fallback);
|
2019-09-03 16:57:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
if (!fallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2019-09-03 16:57:34 +08:00
|
|
|
|
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
private IBindable<TValue> lookupWithFallback<TLookup, TValue>(TLookup lookup, bool canUseSkinLookup, bool canUseFallback)
|
2021-01-14 05:05:46 +08:00
|
|
|
|
{
|
2021-01-18 15:13:58 +08:00
|
|
|
|
if (canUseSkinLookup)
|
|
|
|
|
{
|
2021-05-31 17:00:06 +08:00
|
|
|
|
var bindable = skin?.GetConfig<TLookup, TValue>(lookup);
|
2021-01-18 15:13:58 +08:00
|
|
|
|
if (bindable != null)
|
|
|
|
|
return bindable;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 14:25:42 +08:00
|
|
|
|
if (!canUseFallback)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-01-18 15:13:58 +08:00
|
|
|
|
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
|
2021-01-14 05:05:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 11:21:11 +08:00
|
|
|
|
protected virtual void TriggerSourceChanged() => 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>();
|
2019-02-27 13:30:04 +08:00
|
|
|
|
if (fallbackSource != null)
|
2019-08-26 11:21:11 +08:00
|
|
|
|
fallbackSource.SourceChanged += TriggerSourceChanged;
|
2019-02-27 13:30:04 +08:00
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
|
dependencies.CacheAs<ISkinSource>(this);
|
|
|
|
|
|
2019-02-27 13:30:04 +08:00
|
|
|
|
return dependencies;
|
2018-04-13 17:19:50 +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-04-13 17:19:50 +08:00
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
if (fallbackSource != null)
|
2019-08-26 11:21:11 +08:00
|
|
|
|
fallbackSource.SourceChanged -= TriggerSourceChanged;
|
2021-06-09 16:56:07 +08:00
|
|
|
|
|
|
|
|
|
if (skin is ISkinSource source)
|
|
|
|
|
source.SourceChanged -= TriggerSourceChanged;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
2021-06-09 14:25:42 +08: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 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|