1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 01:27:29 +08:00

Add SkinnableSprite implementation

This commit is contained in:
Dean Herbert 2019-06-24 14:39:20 +09:00
parent 85fdf7d97f
commit 3f22c0a311
2 changed files with 44 additions and 7 deletions

View File

@ -23,6 +23,8 @@ namespace osu.Game.Skinning
/// </summary>
protected Drawable Drawable { get; private set; }
protected virtual T CreateDefault(string name) => createDefault(name);
private readonly Func<string, T> createDefault;
private readonly string componentName;
@ -37,34 +39,44 @@ namespace osu.Game.Skinning
/// <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>
public SkinnableDrawable(string name, Func<string, T> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: this(name, allowFallback, restrictSize)
{
createDefault = defaultImplementation;
}
protected SkinnableDrawable(string name, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: base(allowFallback)
{
componentName = name;
createDefault = defaultImplementation;
this.restrictSize = restrictSize;
RelativeSizeAxes = Axes.Both;
}
protected virtual bool ApplySizeToDefault => false;
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
Drawable = skin.GetDrawableComponent(componentName);
bool isDefault = false;
if (Drawable == null && allowFallback)
{
Drawable = CreateDefault(componentName);
isDefault = true;
}
if (Drawable != null)
{
if (restrictSize)
if (restrictSize && (!isDefault || ApplySizeToDefault))
{
Drawable.RelativeSizeAxes = Axes.Both;
Drawable.Size = Vector2.One;
Drawable.Scale = Vector2.One;
Drawable.FillMode = FillMode.Fit;
}
}
else if (allowFallback)
Drawable = createDefault(componentName);
if (Drawable != null)
{
Drawable.Origin = Anchor.Centre;
Drawable.Anchor = Anchor.Centre;

View File

@ -0,0 +1,25 @@
// 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.
using System;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
namespace osu.Game.Skinning
{
public class SkinnableSprite : SkinnableDrawable<Sprite>
{
protected override bool ApplySizeToDefault => true;
protected override Sprite CreateDefault(string name) => new Sprite { Texture = textures.Get(name) };
[Resolved]
private TextureStore textures { get; set; }
public SkinnableSprite(string name, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: base(name, allowFallback, restrictSize)
{
}
}
}