osu/osu.Game/Skinning/LegacySpriteText.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3.3 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;
2019-08-30 04:04:11 +00:00
using System.Threading.Tasks;
using osu.Framework.Allocation;
2019-08-30 04:04:11 +00:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Text;
using osu.Game.Graphics.Sprites;
2021-03-06 23:28:08 +00:00
using osuTK;
2019-08-30 04:04:11 +00:00
namespace osu.Game.Skinning
{
public sealed partial class LegacySpriteText : OsuSpriteText
2019-08-30 04:04:11 +00:00
{
public Vector2? MaxSizePerGlyph { get; init; }
public bool FixedWidth { get; init; }
private readonly LegacyFont font;
private LegacyGlyphStore glyphStore = null!;
2019-08-30 04:04:11 +00:00
protected override char FixedWidthReferenceCharacter => '5';
protected override char[] FixedWidthExcludeCharacters => new[] { ',', '.', '%', 'x' };
// ReSharper disable once UnusedMember.Global
// being unused is the point here
public new FontUsage Font
{
get => base.Font;
set => throw new InvalidOperationException(@"Attempting to use this setter will not work correctly. "
+ $@"Use specific init-only properties exposed by {nameof(LegacySpriteText)} instead.");
}
public LegacySpriteText(LegacyFont font)
2019-08-30 04:04:11 +00:00
{
this.font = font;
2019-08-30 04:04:11 +00:00
Shadow = false;
UseFullGlyphHeight = false;
}
2019-08-30 04:04:11 +00:00
[BackgroundDependencyLoader]
private void load(ISkinSource skin)
{
base.Font = new FontUsage(skin.GetFontPrefix(font), 1, fixedWidth: FixedWidth);
2021-03-06 23:28:08 +00:00
Spacing = new Vector2(-skin.GetFontOverlap(font), 0);
glyphStore = new LegacyGlyphStore(skin, MaxSizePerGlyph);
2019-08-30 04:04:11 +00:00
}
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;
private readonly Vector2? maxSize;
2019-08-30 04:04:11 +00:00
public LegacyGlyphStore(ISkin skin, Vector2? maxSize)
2019-08-30 04:04:11 +00:00
{
this.skin = skin;
this.maxSize = maxSize;
2019-08-30 04:04:11 +00:00
}
public ITexturedCharacterGlyph? Get(string fontName, char character)
2019-08-30 04:04:11 +00:00
{
string lookup = getLookupName(character);
var texture = skin.GetTexture($"{fontName}-{lookup}");
2019-08-30 04:04:11 +00:00
if (texture == null)
return null;
if (maxSize != null)
texture = texture.WithMaximumSize(maxSize.Value);
return new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture.Width, texture.Height, null), texture, 1f / texture.ScaleAdjust);
2019-08-30 04:04:11 +00:00
}
private static string getLookupName(char character)
{
switch (character)
{
case ',':
return "comma";
case '.':
return "dot";
case '%':
return "percent";
default:
return character.ToString();
}
}
public Task<ITexturedCharacterGlyph?> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));
2019-08-30 04:04:11 +00:00
}
}
}