diff --git a/osu.Game/Skinning/SkinnableDrawable.cs b/osu.Game/Skinning/SkinnableDrawable.cs index 3ca58dc625..64ef50111c 100644 --- a/osu.Game/Skinning/SkinnableDrawable.cs +++ b/osu.Game/Skinning/SkinnableDrawable.cs @@ -23,6 +23,8 @@ public class SkinnableDrawable : SkinReloadableDrawable /// protected Drawable Drawable { get; private set; } + protected virtual T CreateDefault() => createDefault(componentName); + private readonly Func createDefault; private readonly string componentName; @@ -37,34 +39,44 @@ public class SkinnableDrawable : SkinReloadableDrawable /// A conditional to decide whether to allow fallback to the default implementation if a skinned element is not present. /// Whether a user-skin drawable should be limited to the size of our parent. public SkinnableDrawable(string name, Func defaultImplementation, Func allowFallback = null, bool restrictSize = true) + : this(name, allowFallback, restrictSize) + { + createDefault = defaultImplementation; + } + + protected SkinnableDrawable(string name, Func 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; diff --git a/osu.Game/Skinning/SkinnableSprite.cs b/osu.Game/Skinning/SkinnableSprite.cs new file mode 100644 index 0000000000..4cf054b704 --- /dev/null +++ b/osu.Game/Skinning/SkinnableSprite.cs @@ -0,0 +1,28 @@ +// Copyright (c) ppy Pty Ltd . 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 + { + 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 allowFallback = null, bool restrictSize = true) + : base(name, allowFallback, restrictSize) + { + this.defaultName = defaultName; + } + } +}