1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 02:15:17 +08:00
osu-lazer/osu.Game/Skinning/SkinnableDrawable.cs

78 lines
2.8 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +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;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +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)
{
}
}
public class SkinnableDrawable<T> : SkinReloadableDrawable
where T : Drawable
{
2018-09-27 16:33:27 +08:00
/// <summary>
/// The displayed component. May or may not be a type-<typeparamref name="T"/> member.
/// </summary>
2018-09-27 15:27:11 +08:00
protected Drawable Drawable { get; private set; }
2018-04-13 17:19:50 +08:00
private readonly Func<string, T> createDefault;
private readonly string componentName;
private readonly bool restrictSize;
/// <summary>
///
/// </summary>
/// <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>
/// <param name="allowFallback">A conditional to decide whether to allow fallback to the default implementation if a skinned element is not present.</param>
/// <param name="restrictSize">Whether a user-skin drawable should be limited to the size of our parent.</param>
2018-09-27 15:27:11 +08:00
public SkinnableDrawable(string name, Func<string, T> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: base(allowFallback)
2018-04-13 17:19:50 +08:00
{
componentName = name;
createDefault = defaultImplementation;
this.restrictSize = restrictSize;
RelativeSizeAxes = Axes.Both;
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
2018-09-27 15:27:11 +08:00
Drawable = skin.GetDrawableComponent(componentName);
if (Drawable != null)
2018-04-13 17:19:50 +08:00
{
if (restrictSize)
{
2018-09-27 15:27:11 +08:00
Drawable.RelativeSizeAxes = Axes.Both;
Drawable.Size = Vector2.One;
Drawable.Scale = Vector2.One;
Drawable.FillMode = FillMode.Fit;
2018-04-13 17:19:50 +08:00
}
}
else if (allowFallback)
2018-09-27 15:27:11 +08:00
Drawable = createDefault(componentName);
2018-04-13 17:19:50 +08:00
2018-09-27 15:27:11 +08:00
if (Drawable != null)
2018-04-13 17:19:50 +08:00
{
2018-09-27 15:27:11 +08:00
Drawable.Origin = Anchor.Centre;
Drawable.Anchor = Anchor.Centre;
2018-04-13 17:19:50 +08:00
2018-09-27 15:27:11 +08:00
InternalChild = Drawable;
2018-04-13 17:19:50 +08:00
}
else
ClearInternal();
}
}
}