1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 03:47:26 +08:00
osu-lazer/osu.Game/Skinning/SkinnableDrawable.cs

71 lines
2.7 KiB
C#
Raw Normal View History

2018-02-22 16:33:47 +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
using System;
using osu.Framework.Graphics;
using OpenTK;
2018-02-22 16:33:47 +08:00
namespace osu.Game.Skinning
{
public class SkinnableDrawable : SkinnableDrawable<Drawable>
{
public SkinnableDrawable(string name, Func<string, Drawable> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: base(name, defaultImplementation, allowFallback, restrictSize)
2018-02-22 16:33:47 +08:00
{
}
}
public class SkinnableDrawable<T> : SkinReloadableDrawable
2018-02-22 16:33:47 +08:00
where T : Drawable
{
private readonly Func<string, T> createDefault;
2018-02-22 16:33:47 +08:00
private readonly string componentName;
2018-02-22 16:33:47 +08:00
2018-03-12 10:02:02 +08:00
private readonly bool restrictSize;
/// <summary>
2018-03-12 10:02:02 +08:00
///
/// </summary>
2018-03-12 10:02:02 +08:00
/// <param name="name">The namespace-complete resource name for this skinnable element.</param>
/// <param name="defaultImplementation">A function to create the default skin implementation of this element.</param>
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-12 10:02:02 +08:00
/// <param name="restrictSize">Whether a user-skin drawable should be limited to the size of our parent.</param>
public SkinnableDrawable(string name, Func<string, T> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true) : base(allowFallback)
2018-02-22 16:33:47 +08:00
{
componentName = name;
createDefault = defaultImplementation;
2018-03-12 10:02:02 +08:00
this.restrictSize = restrictSize;
2018-02-22 16:33:47 +08:00
RelativeSizeAxes = Axes.Both;
2018-02-22 16:33:47 +08:00
}
2018-03-20 15:26:36 +08:00
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
2018-02-22 16:33:47 +08:00
{
var drawable = skin.GetDrawableComponent(componentName);
if (drawable != null)
{
2018-03-12 10:02:02 +08:00
if (restrictSize)
{
drawable.RelativeSizeAxes = Axes.Both;
drawable.Size = Vector2.One;
drawable.Scale = Vector2.One;
drawable.FillMode = FillMode.Fit;
}
}
else if (allowFallback)
drawable = createDefault(componentName);
2018-02-22 16:33:47 +08:00
if (drawable != null)
{
drawable.Origin = Anchor.Centre;
drawable.Anchor = Anchor.Centre;
2018-02-22 16:33:47 +08:00
InternalChild = drawable;
}
2018-02-22 16:33:47 +08:00
else
ClearInternal();
}
}
}