mirror of
https://github.com/ppy/osu.git
synced 2024-11-11 23:07:26 +08:00
Add SkinnableSprite implementation
This commit is contained in:
parent
2a4bd612d7
commit
06b087db70
@ -23,6 +23,8 @@ namespace osu.Game.Skinning
|
||||
/// </summary>
|
||||
protected Drawable Drawable { get; private set; }
|
||||
|
||||
protected virtual T CreateDefault() => createDefault(componentName);
|
||||
|
||||
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();
|
||||
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;
|
||||
|
||||
|
28
osu.Game/Skinning/SkinnableSprite.cs
Normal file
28
osu.Game/Skinning/SkinnableSprite.cs
Normal file
@ -0,0 +1,28 @@
|
||||
// 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;
|
||||
|
||||
private readonly string defaultName;
|
||||
|
||||
protected override Sprite CreateDefault() => new Sprite { Texture = textures.Get(defaultName) };
|
||||
|
||||
[Resolved]
|
||||
private TextureStore textures { get; set; }
|
||||
|
||||
public SkinnableSprite(string name, string defaultName, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
|
||||
: base(name, allowFallback, restrictSize)
|
||||
{
|
||||
this.defaultName = defaultName;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user