osu/osu.Game/Skinning/LegacySpriteText.cs

73 lines
2.2 KiB
C#
Raw Normal View History

2019-08-30 04:04:11 +00:00
// 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.Threading.Tasks;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Text;
using osu.Game.Graphics.Sprites;
namespace osu.Game.Skinning
{
public class LegacySpriteText : OsuSpriteText
{
private readonly LegacyGlyphStore glyphStore;
protected override char FixedWidthReferenceCharacter => '5';
protected override char[] FixedWidthExcludeCharacters => new[] { ',', '.', '%', 'x' };
2020-10-15 08:48:50 +00:00
public LegacySpriteText(ISkin skin, string font = "score")
2019-08-30 04:04:11 +00:00
{
Shadow = false;
UseFullGlyphHeight = false;
Font = new FontUsage(font, 1, fixedWidth: true);
2019-08-30 04:04:11 +00:00
glyphStore = new LegacyGlyphStore(skin);
}
protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) => base.CreateTextBuilder(glyphStore);
2019-08-30 04:04:11 +00:00
private class LegacyGlyphStore : ITexturedGlyphLookupStore
{
private readonly ISkin skin;
public LegacyGlyphStore(ISkin skin)
{
this.skin = skin;
}
public ITexturedCharacterGlyph Get(string fontName, char character)
{
var lookup = getLookupName(character);
var texture = skin.GetTexture($"{fontName}-{lookup}");
2019-08-30 04:04:11 +00:00
if (texture == null)
return null;
return new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture.Width, null), texture, 1f / texture.ScaleAdjust);
}
private static string getLookupName(char character)
{
switch (character)
{
case ',':
return "comma";
case '.':
return "dot";
case '%':
return "percent";
default:
return character.ToString();
}
}
2019-08-30 04:04:11 +00:00
public Task<ITexturedCharacterGlyph> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));
}
}
}