2019-01-24 17:43:03 +09: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 18:19:50 +09:00
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Audio.Sample;
|
2019-09-03 17:57:34 +09:00
|
|
|
|
using osu.Framework.Bindables;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2019-08-23 14:32:43 +03:00
|
|
|
|
using osu.Game.Audio;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Skinning
|
|
|
|
|
{
|
2019-02-05 17:50:32 +09:00
|
|
|
|
/// <summary>
|
2019-08-26 12:21:11 +09:00
|
|
|
|
/// A container which adds a local <see cref="ISkinSource"/> to the hierarchy.
|
2019-02-05 17:50:32 +09:00
|
|
|
|
/// </summary>
|
2019-08-26 12:21:11 +09:00
|
|
|
|
public class SkinProvidingContainer : Container, ISkinSource
|
2018-04-13 18:19:50 +09:00
|
|
|
|
{
|
|
|
|
|
public event Action SourceChanged;
|
|
|
|
|
|
2019-04-25 17:36:17 +09:00
|
|
|
|
private readonly ISkin skin;
|
2019-06-28 20:45:11 -05:00
|
|
|
|
|
2019-02-05 17:50:32 +09:00
|
|
|
|
private ISkinSource fallbackSource;
|
|
|
|
|
|
2019-08-30 14:39:02 +09:00
|
|
|
|
protected virtual bool AllowDrawableLookup(ISkinComponent component) => true;
|
2019-08-26 12:21:11 +09:00
|
|
|
|
|
|
|
|
|
protected virtual bool AllowTextureLookup(string componentName) => true;
|
|
|
|
|
|
2019-08-28 16:36:20 +09:00
|
|
|
|
protected virtual bool AllowSampleLookup(ISampleInfo componentName) => true;
|
2019-08-26 12:21:11 +09:00
|
|
|
|
|
|
|
|
|
protected virtual bool AllowConfigurationLookup => true;
|
|
|
|
|
|
|
|
|
|
public SkinProvidingContainer(ISkin skin)
|
2019-02-05 17:50:32 +09:00
|
|
|
|
{
|
2019-04-25 17:36:17 +09:00
|
|
|
|
this.skin = skin;
|
2019-08-27 18:27:21 +09:00
|
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
2019-02-05 17:50:32 +09:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 14:39:02 +09:00
|
|
|
|
public Drawable GetDrawableComponent(ISkinComponent component)
|
2018-04-20 18:17:57 +03:00
|
|
|
|
{
|
|
|
|
|
Drawable sourceDrawable;
|
2019-08-30 14:39:02 +09:00
|
|
|
|
if (AllowDrawableLookup(component) && (sourceDrawable = skin?.GetDrawableComponent(component)) != null)
|
2018-04-20 18:17:57 +03:00
|
|
|
|
return sourceDrawable;
|
2019-02-27 21:07:17 +09:00
|
|
|
|
|
2019-08-30 14:39:02 +09:00
|
|
|
|
return fallbackSource?.GetDrawableComponent(component);
|
2018-04-20 18:17:57 +03:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-04-20 18:17:57 +03:00
|
|
|
|
public Texture GetTexture(string componentName)
|
|
|
|
|
{
|
|
|
|
|
Texture sourceTexture;
|
2019-08-26 12:21:11 +09:00
|
|
|
|
if (AllowTextureLookup(componentName) && (sourceTexture = skin?.GetTexture(componentName)) != null)
|
2018-04-20 18:17:57 +03:00
|
|
|
|
return sourceTexture;
|
2019-02-27 21:07:17 +09:00
|
|
|
|
|
2019-10-08 12:52:34 +09:00
|
|
|
|
return fallbackSource?.GetTexture(componentName);
|
2018-04-20 18:17:57 +03:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-08-23 14:32:43 +03:00
|
|
|
|
public SampleChannel GetSample(ISampleInfo sampleInfo)
|
2018-04-20 18:30:41 +03:00
|
|
|
|
{
|
|
|
|
|
SampleChannel sourceChannel;
|
2019-08-28 16:36:20 +09:00
|
|
|
|
if (AllowSampleLookup(sampleInfo) && (sourceChannel = skin?.GetSample(sampleInfo)) != null)
|
2018-04-20 18:30:41 +03:00
|
|
|
|
return sourceChannel;
|
2019-02-27 21:07:17 +09:00
|
|
|
|
|
2019-08-23 14:32:43 +03:00
|
|
|
|
return fallbackSource?.GetSample(sampleInfo);
|
2018-04-20 18:30:41 +03:00
|
|
|
|
}
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2019-09-03 17:57:34 +09:00
|
|
|
|
public IBindable<TValue> GetConfig<TLookup, TValue>(TLookup lookup)
|
2018-04-13 18:19:50 +09:00
|
|
|
|
{
|
2019-09-03 17:57:34 +09:00
|
|
|
|
if (AllowConfigurationLookup && skin != null)
|
|
|
|
|
{
|
|
|
|
|
var bindable = skin.GetConfig<TLookup, TValue>(lookup);
|
|
|
|
|
if (bindable != null)
|
|
|
|
|
return bindable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fallbackSource?.GetConfig<TLookup, TValue>(lookup);
|
2018-04-13 18:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-26 12:21:11 +09:00
|
|
|
|
protected virtual void TriggerSourceChanged() => SourceChanged?.Invoke();
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
2018-07-11 17:07:14 +09:00
|
|
|
|
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
|
2018-04-13 18:19:50 +09:00
|
|
|
|
{
|
2018-07-11 17:07:14 +09:00
|
|
|
|
var dependencies = new DependencyContainer(base.CreateChildDependencies(parent));
|
2018-04-13 18:19:50 +09:00
|
|
|
|
|
|
|
|
|
fallbackSource = dependencies.Get<ISkinSource>();
|
2019-02-27 14:30:04 +09:00
|
|
|
|
if (fallbackSource != null)
|
2019-08-26 12:21:11 +09:00
|
|
|
|
fallbackSource.SourceChanged += TriggerSourceChanged;
|
2019-02-27 14:30:04 +09:00
|
|
|
|
|
2018-04-13 18:19:50 +09:00
|
|
|
|
dependencies.CacheAs<ISkinSource>(this);
|
|
|
|
|
|
2019-02-27 14:30:04 +09:00
|
|
|
|
return dependencies;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
2019-02-27 21:04:14 +09:00
|
|
|
|
// Must be done before base.Dispose()
|
|
|
|
|
SourceChanged = null;
|
|
|
|
|
|
2018-04-13 18:19:50 +09:00
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
|
|
if (fallbackSource != null)
|
2019-08-26 12:21:11 +09:00
|
|
|
|
fallbackSource.SourceChanged -= TriggerSourceChanged;
|
2018-04-13 18:19:50 +09:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|