1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:17:26 +08:00

Implement skinnable sprite text

This commit is contained in:
smoogipoo 2018-09-27 16:27:11 +09:00
parent b84994e643
commit 0d8276c5f8
4 changed files with 104 additions and 21 deletions

View File

@ -4,7 +4,6 @@
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Sprites;
using OpenTK.Graphics;
using osu.Framework.Graphics.Shapes;
@ -14,7 +13,7 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
{
public class NumberPiece : Container
{
private readonly SpriteText number;
private readonly SkinnableSpriteText number;
public string Text
{
@ -41,15 +40,14 @@ namespace osu.Game.Rulesets.Osu.Objects.Drawables.Pieces
},
Child = new Box()
}, s => s.GetTexture("Play/osu/hitcircle") == null),
number = new OsuSpriteText
number = new SkinnableSpriteText("Play/osu/number-text", _ => new OsuSpriteText
{
Text = @"1",
Font = @"Venera",
UseFullGlyphHeight = false,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
TextSize = 40,
Alpha = 1
}, restrictSize: false)
{
Text = @"1"
}
};
}

View File

@ -12,7 +12,7 @@ using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.IO.Stores;
using osu.Game.Database;
using OpenTK;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Skinning
{
@ -56,11 +56,15 @@ namespace osu.Game.Skinning
case "Play/Great":
componentName = "hit300";
break;
case "Play/osu/number-text":
// Todo: Not necessarily default font
return hasFont("default") ? new LegacySpriteText(Textures, "default") : null;
}
var texture = GetTexture(componentName);
if (texture == null) return null;
if (texture == null)
return null;
return new Sprite { Texture = texture };
}
@ -84,6 +88,8 @@ namespace osu.Game.Skinning
public override SampleChannel GetSample(string sampleName) => Samples.Get(sampleName);
private bool hasFont(string fontName) => GetTexture($"{fontName}-0") != null;
protected class LegacySkinResourceStore<T> : IResourceStore<byte[]>
where T : INamedFileInfo
{
@ -146,5 +152,39 @@ namespace osu.Game.Skinning
#endregion
}
private class LegacySpriteText : OsuSpriteText
{
private readonly TextureStore textures;
private readonly string font;
public LegacySpriteText(TextureStore textures, string font)
{
this.textures = textures;
this.font = font;
Shadow = false;
UseFullGlyphHeight = false;
}
protected override Texture GetTextureForCharacter(char c)
{
string textureName = $"{font}-{c}";
float ratio = 36;
var texture = textures.Get($"{textureName}@2x");
if (texture == null)
{
ratio = 18;
texture = textures.Get(textureName);
}
if (texture != null)
texture.ScaleAdjust = ratio;
return texture;
}
}
}
}

View File

@ -18,6 +18,8 @@ namespace osu.Game.Skinning
public class SkinnableDrawable<T> : SkinReloadableDrawable
where T : Drawable
{
protected Drawable Drawable { get; private set; }
private readonly Func<string, T> createDefault;
private readonly string componentName;
@ -31,7 +33,8 @@ namespace osu.Game.Skinning
/// <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>
public SkinnableDrawable(string name, Func<string, T> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true) : base(allowFallback)
public SkinnableDrawable(string name, Func<string, T> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: base(allowFallback)
{
componentName = name;
createDefault = defaultImplementation;
@ -42,26 +45,28 @@ namespace osu.Game.Skinning
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
var drawable = skin.GetDrawableComponent(componentName);
if (drawable != null)
Drawable = null;
Drawable = skin.GetDrawableComponent(componentName);
if (Drawable != null)
{
if (restrictSize)
{
drawable.RelativeSizeAxes = Axes.Both;
drawable.Size = Vector2.One;
drawable.Scale = Vector2.One;
drawable.FillMode = FillMode.Fit;
Drawable.RelativeSizeAxes = Axes.Both;
Drawable.Size = Vector2.One;
Drawable.Scale = Vector2.One;
Drawable.FillMode = FillMode.Fit;
}
}
else if (allowFallback)
drawable = createDefault(componentName);
Drawable = createDefault(componentName);
if (drawable != null)
if (Drawable != null)
{
drawable.Origin = Anchor.Centre;
drawable.Anchor = Anchor.Centre;
Drawable.Origin = Anchor.Centre;
Drawable.Anchor = Anchor.Centre;
InternalChild = drawable;
InternalChild = Drawable;
}
else
ClearInternal();

View File

@ -0,0 +1,40 @@
// 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.Sprites;
namespace osu.Game.Skinning
{
public class SkinnableSpriteText : SkinnableDrawable<SpriteText>, IHasText
{
public SkinnableSpriteText(string name, Func<string, SpriteText> defaultImplementation, Func<ISkinSource, bool> allowFallback = null, bool restrictSize = true)
: base(name, defaultImplementation, allowFallback, restrictSize)
{
}
protected override void SkinChanged(ISkinSource skin, bool allowFallback)
{
base.SkinChanged(skin, allowFallback);
if (Drawable is IHasText textDrawable)
textDrawable.Text = Text;
}
private string text;
public string Text
{
get => text;
set
{
if (text == value)
return;
text = value;
if (Drawable is IHasText textDrawable)
textDrawable.Text = value;
}
}
}
}