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-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-05-31 09:00:06 +00:00
|
|
|
|
[CanBeNull]
|
2019-04-25 08:36:17 +00:00
|
|
|
|
private readonly ISkin skin;
|
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;
|
|
|
|
|
|
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;
|
|
|
|
|
|
2019-08-26 03:21:11 +00:00
|
|
|
|
public SkinProvidingContainer(ISkin skin)
|
2019-02-05 08:50:32 +00:00
|
|
|
|
{
|
2019-04-25 08:36:17 +00:00
|
|
|
|
this.skin = skin;
|
2019-08-27 09:27:21 +00:00
|
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
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-07 14:23:44 +00:00
|
|
|
|
if (skin is ISkinSource source)
|
|
|
|
|
{
|
|
|
|
|
if (source.FindProvider(lookupFunction) is ISkin found)
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
else if (skin != null)
|
|
|
|
|
{
|
|
|
|
|
if (lookupFunction(skin))
|
|
|
|
|
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)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
{
|
|
|
|
|
Drawable sourceDrawable;
|
2019-08-30 05:39:02 +00:00
|
|
|
|
if (AllowDrawableLookup(component) && (sourceDrawable = skin?.GetDrawableComponent(component)) != null)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
return sourceDrawable;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
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)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
{
|
|
|
|
|
Texture sourceTexture;
|
2020-07-17 07:54:30 +00:00
|
|
|
|
if (AllowTextureLookup(componentName) && (sourceTexture = skin?.GetTexture(componentName, wrapModeS, wrapModeT)) != null)
|
2018-04-20 15:17:57 +00:00
|
|
|
|
return sourceTexture;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
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)
|
2018-04-20 15:30:41 +00:00
|
|
|
|
{
|
2021-02-18 09:32:28 +00:00
|
|
|
|
ISample sourceChannel;
|
2019-08-28 07:36:20 +00:00
|
|
|
|
if (AllowSampleLookup(sampleInfo) && (sourceChannel = skin?.GetSample(sampleInfo)) != null)
|
2018-04-20 15:30:41 +00:00
|
|
|
|
return sourceChannel;
|
2019-02-27 12:07:17 +00:00
|
|
|
|
|
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)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2021-01-13 18:07:07 +00:00
|
|
|
|
if (skin != null)
|
2019-09-03 08:57:34 +00:00
|
|
|
|
{
|
2021-01-15 04:30:24 +00:00
|
|
|
|
if (lookup is GlobalSkinColours || lookup is SkinCustomColourLookup)
|
2021-01-14 21:53:55 +00:00
|
|
|
|
return lookupWithFallback<TLookup, TValue>(lookup, AllowColourLookup);
|
2021-01-18 07:13:58 +00:00
|
|
|
|
|
|
|
|
|
return lookupWithFallback<TLookup, TValue>(lookup, AllowConfigurationLookup);
|
2019-09-03 08:57:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-13 21:49:14 +00:00
|
|
|
|
private IBindable<TValue> lookupWithFallback<TLookup, TValue>(TLookup lookup, bool canUseSkinLookup)
|
2021-01-13 21:05:46 +00:00
|
|
|
|
{
|
2021-01-18 07:13:58 +00:00
|
|
|
|
if (canUseSkinLookup)
|
|
|
|
|
{
|
2021-05-31 09:00:06 +00:00
|
|
|
|
var bindable = skin?.GetConfig<TLookup, TValue>(lookup);
|
2021-01-18 07:13:58 +00:00
|
|
|
|
if (bindable != null)
|
|
|
|
|
return bindable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
|
2021-01-13 21:05:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 03:21:11 +00:00
|
|
|
|
protected virtual void TriggerSourceChanged() => 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)
|
2019-08-26 03:21:11 +00:00
|
|
|
|
fallbackSource.SourceChanged += TriggerSourceChanged;
|
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)
|
2019-08-26 03:21:11 +00:00
|
|
|
|
fallbackSource.SourceChanged -= TriggerSourceChanged;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|