2018-02-23 19:34:08 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-03-16 09:36:26 +08:00
using System ;
2018-02-23 19:34:08 +08:00
using osu.Framework.Allocation ;
using osu.Framework.Graphics.Containers ;
namespace osu.Game.Skinning
{
/// <summary>
/// A drawable which has a callback when the skin changes.
/// </summary>
public abstract class SkinReloadableDrawable : CompositeDrawable
{
2018-03-16 09:36:26 +08:00
private readonly Func < ISkinSource , bool > allowFallback ;
2018-03-20 15:26:36 +08:00
private ISkinSource skin ;
2018-02-23 19:34:08 +08:00
/// <summary>
/// Whether fallback to default skin should be allowed if the custom skin is missing this resource.
/// </summary>
2018-03-16 09:36:26 +08:00
private bool allowDefaultFallback = > allowFallback = = null | | allowFallback . Invoke ( skin ) ;
2018-02-23 19:34:08 +08:00
/// <summary>
2018-02-24 22:07:02 +08:00
/// Create a new <see cref="SkinReloadableDrawable"/>
2018-02-23 19:34:08 +08:00
/// </summary>
2018-03-24 17:22:55 +08:00
/// <param name="allowFallback">A conditional to decide whether to allow fallback to the default implementation if a skinned element is not present.</param>
2018-03-16 09:36:26 +08:00
protected SkinReloadableDrawable ( Func < ISkinSource , bool > allowFallback = null )
2018-02-23 19:34:08 +08:00
{
2018-03-16 09:36:26 +08:00
this . allowFallback = allowFallback ;
2018-02-23 19:34:08 +08:00
}
[BackgroundDependencyLoader]
2018-03-20 15:26:36 +08:00
private void load ( ISkinSource source )
2018-02-23 19:34:08 +08:00
{
2018-03-20 15:26:36 +08:00
skin = source ;
skin . SourceChanged + = onChange ;
2018-02-23 19:34:08 +08:00
}
2018-03-20 15:26:36 +08:00
private void onChange ( ) = > SkinChanged ( skin , allowDefaultFallback ) ;
2018-02-23 19:34:08 +08:00
protected override void LoadAsyncComplete ( )
{
base . LoadAsyncComplete ( ) ;
2018-03-20 15:26:36 +08:00
onChange ( ) ;
2018-02-23 19:34:08 +08:00
}
/// <summary>
/// Called when a change is made to the skin.
/// </summary>
/// <param name="skin">The new skin.</param>
/// <param name="allowFallback">Whether fallback to default skin should be allowed if the custom skin is missing this resource.</param>
2018-03-20 15:26:36 +08:00
protected virtual void SkinChanged ( ISkinSource skin , bool allowFallback )
2018-02-23 19:34:08 +08:00
{
}
}
}