mirror of
https://github.com/ppy/osu.git
synced 2025-02-22 07:33:20 +08:00
Add SkinnableSprite implementation
This commit is contained in:
parent
2a4bd612d7
commit
06b087db70
@ -23,6 +23,8 @@ namespace osu.Game.Skinning
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected Drawable Drawable { get; private set; }
|
protected Drawable Drawable { get; private set; }
|
||||||
|
|
||||||
|
protected virtual T CreateDefault() => createDefault(componentName);
|
||||||
|
|
||||||
private readonly Func<string, T> createDefault;
|
private readonly Func<string, T> createDefault;
|
||||||
|
|
||||||
private readonly string componentName;
|
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="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>
|
/// <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)
|
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)
|
: base(allowFallback)
|
||||||
{
|
{
|
||||||
componentName = name;
|
componentName = name;
|
||||||
createDefault = defaultImplementation;
|
|
||||||
this.restrictSize = restrictSize;
|
this.restrictSize = restrictSize;
|
||||||
|
|
||||||
RelativeSizeAxes = Axes.Both;
|
RelativeSizeAxes = Axes.Both;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected virtual bool ApplySizeToDefault => false;
|
||||||
|
|
||||||
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
|
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
|
||||||
{
|
{
|
||||||
Drawable = skin.GetDrawableComponent(componentName);
|
Drawable = skin.GetDrawableComponent(componentName);
|
||||||
|
|
||||||
|
bool isDefault = false;
|
||||||
|
|
||||||
|
if (Drawable == null && allowFallback)
|
||||||
|
{
|
||||||
|
Drawable = CreateDefault();
|
||||||
|
isDefault = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (Drawable != null)
|
if (Drawable != null)
|
||||||
{
|
{
|
||||||
if (restrictSize)
|
if (restrictSize && (!isDefault || ApplySizeToDefault))
|
||||||
{
|
{
|
||||||
Drawable.RelativeSizeAxes = Axes.Both;
|
Drawable.RelativeSizeAxes = Axes.Both;
|
||||||
Drawable.Size = Vector2.One;
|
Drawable.Size = Vector2.One;
|
||||||
Drawable.Scale = Vector2.One;
|
Drawable.Scale = Vector2.One;
|
||||||
Drawable.FillMode = FillMode.Fit;
|
Drawable.FillMode = FillMode.Fit;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else if (allowFallback)
|
|
||||||
Drawable = createDefault(componentName);
|
|
||||||
|
|
||||||
if (Drawable != null)
|
|
||||||
{
|
|
||||||
Drawable.Origin = Anchor.Centre;
|
Drawable.Origin = Anchor.Centre;
|
||||||
Drawable.Anchor = 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