1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-19 04:42:55 +08:00
osu-lazer/osu.Game/Skinning/SkinProvidingContainer.cs

123 lines
4.1 KiB
C#
Raw Normal View History

// 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;
using osu.Framework.Allocation;
using osu.Framework.Audio.Sample;
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
{
/// <summary>
/// A container which adds a local <see cref="ISkinSource"/> to the hierarchy.
/// </summary>
public class SkinProvidingContainer : Container, ISkinSource
2018-04-13 17:19:50 +08:00
{
public event Action SourceChanged;
2019-04-25 16:36:17 +08:00
private readonly ISkin skin;
2019-06-29 09:45:11 +08:00
private ISkinSource fallbackSource;
protected virtual bool AllowDrawableLookup(ISkinComponent component) => true;
protected virtual bool AllowTextureLookup(string componentName) => true;
protected virtual bool AllowSampleLookup(ISampleInfo componentName) => true;
protected virtual bool AllowConfigurationLookup => true;
protected virtual bool AllowColourLookup => true;
public SkinProvidingContainer(ISkin skin)
{
2019-04-25 16:36:17 +08:00
this.skin = skin;
RelativeSizeAxes = Axes.Both;
}
public Drawable GetDrawableComponent(ISkinComponent component)
2018-04-20 23:17:57 +08:00
{
Drawable sourceDrawable;
if (AllowDrawableLookup(component) && (sourceDrawable = skin?.GetDrawableComponent(component)) != null)
2018-04-20 23:17:57 +08:00
return sourceDrawable;
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)
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;
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
public ISample GetSample(ISampleInfo sampleInfo)
{
ISample sourceChannel;
if (AllowSampleLookup(sampleInfo) && (sourceChannel = skin?.GetSample(sampleInfo)) != null)
return sourceChannel;
2019-08-23 19:32:43 +08:00
return fallbackSource?.GetSample(sampleInfo);
}
2018-04-13 17:19:50 +08:00
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
2018-04-13 17:19:50 +08:00
{
if (skin != null)
{
if (lookup is GlobalSkinColours || lookup is SkinCustomColourLookup)
return lookupWithFallback<TLookup, TValue>(lookup, AllowColourLookup);
return lookupWithFallback<TLookup, TValue>(lookup, AllowConfigurationLookup);
}
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
2018-04-13 17:19:50 +08:00
}
private IBindable<TValue> lookupWithFallback<TLookup, TValue>(TLookup lookup, bool canUseSkinLookup)
{
if (canUseSkinLookup)
{
var bindable = skin.GetConfig<TLookup, TValue>(lookup);
if (bindable != null)
return bindable;
}
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
}
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>();
if (fallbackSource != null)
fallbackSource.SourceChanged += TriggerSourceChanged;
2018-04-13 17:19:50 +08:00
dependencies.CacheAs<ISkinSource>(this);
return dependencies;
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
// Must be done before base.Dispose()
SourceChanged = null;
2018-04-13 17:19:50 +08:00
base.Dispose(isDisposing);
if (fallbackSource != null)
fallbackSource.SourceChanged -= TriggerSourceChanged;
2018-04-13 17:19:50 +08:00
}
}
}