2023-11-07 06:59:00 +08: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.
|
|
|
|
|
2023-11-08 07:05:19 +08:00
|
|
|
using System;
|
2024-01-05 00:24:00 +08:00
|
|
|
using System.Collections.Generic;
|
2023-11-07 06:59:00 +08:00
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2023-11-14 21:01:56 +08:00
|
|
|
using osu.Framework.Graphics.Textures;
|
2023-11-07 06:59:00 +08:00
|
|
|
using osu.Framework.Localisation;
|
|
|
|
using osu.Framework.Text;
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play.HUD
|
|
|
|
{
|
|
|
|
public partial class ArgonCounterTextComponent : CompositeDrawable, IHasText
|
|
|
|
{
|
|
|
|
private readonly ArgonCounterSpriteText wireframesPart;
|
|
|
|
private readonly ArgonCounterSpriteText textPart;
|
2023-11-10 05:34:29 +08:00
|
|
|
private readonly OsuSpriteText labelText;
|
2023-11-07 06:59:00 +08:00
|
|
|
|
|
|
|
public IBindable<float> WireframeOpacity { get; } = new BindableFloat();
|
2023-11-10 05:34:29 +08:00
|
|
|
public Bindable<int> RequiredDisplayDigits { get; } = new BindableInt();
|
2023-11-15 12:45:23 +08:00
|
|
|
public Bindable<bool> ShowLabel { get; } = new BindableBool();
|
2023-11-07 06:59:00 +08:00
|
|
|
|
2023-11-10 14:26:37 +08:00
|
|
|
public Container NumberContainer { get; private set; }
|
|
|
|
|
2023-11-07 06:59:00 +08:00
|
|
|
public LocalisableString Text
|
|
|
|
{
|
|
|
|
get => textPart.Text;
|
2024-01-07 12:10:50 +08:00
|
|
|
set => textPart.Text = value;
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
|
2023-11-10 05:34:29 +08:00
|
|
|
public ArgonCounterTextComponent(Anchor anchor, LocalisableString? label = null)
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
|
|
|
Anchor = anchor;
|
|
|
|
Origin = anchor;
|
|
|
|
AutoSizeAxes = Axes.Both;
|
|
|
|
|
|
|
|
InternalChild = new FillFlowContainer
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Direction = FillDirection.Vertical,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
2023-11-10 05:34:29 +08:00
|
|
|
labelText = new OsuSpriteText
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
2023-11-15 12:45:23 +08:00
|
|
|
Alpha = 0,
|
2023-11-07 06:59:00 +08:00
|
|
|
Text = label.GetValueOrDefault(),
|
|
|
|
Font = OsuFont.Torus.With(size: 12, weight: FontWeight.Bold),
|
|
|
|
Margin = new MarginPadding { Left = 2.5f },
|
|
|
|
},
|
2023-11-10 14:26:37 +08:00
|
|
|
NumberContainer = new Container
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
Children = new[]
|
|
|
|
{
|
2023-11-10 05:34:29 +08:00
|
|
|
wireframesPart = new ArgonCounterSpriteText(wireframesLookup)
|
|
|
|
{
|
|
|
|
Anchor = anchor,
|
|
|
|
Origin = anchor,
|
|
|
|
},
|
|
|
|
textPart = new ArgonCounterSpriteText(textLookup)
|
|
|
|
{
|
|
|
|
Anchor = anchor,
|
|
|
|
Origin = anchor,
|
|
|
|
},
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-01-07 12:10:50 +08:00
|
|
|
|
|
|
|
RequiredDisplayDigits.BindValueChanged(digits => wireframesPart.Text = new string('#', digits.NewValue));
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
|
2023-11-10 05:34:29 +08:00
|
|
|
private string textLookup(char c)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '.':
|
|
|
|
return @"dot";
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
return @"percentage";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return c.ToString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string wireframesLookup(char c)
|
|
|
|
{
|
|
|
|
if (c == '.') return @"dot";
|
|
|
|
|
|
|
|
return @"wireframes";
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
labelText.Colour = colours.Blue0;
|
|
|
|
}
|
|
|
|
|
2023-11-07 06:59:00 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
WireframeOpacity.BindValueChanged(v => wireframesPart.Alpha = v.NewValue, true);
|
2023-11-15 12:45:23 +08:00
|
|
|
ShowLabel.BindValueChanged(s => labelText.Alpha = s.NewValue ? 1 : 0, true);
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private partial class ArgonCounterSpriteText : OsuSpriteText
|
|
|
|
{
|
2023-11-08 07:05:19 +08:00
|
|
|
private readonly Func<char, string> getLookup;
|
2023-11-07 06:59:00 +08:00
|
|
|
|
|
|
|
private GlyphStore glyphStore = null!;
|
|
|
|
|
|
|
|
protected override char FixedWidthReferenceCharacter => '5';
|
|
|
|
|
2023-11-08 07:05:19 +08:00
|
|
|
public ArgonCounterSpriteText(Func<char, string> getLookup)
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
2023-11-08 07:05:19 +08:00
|
|
|
this.getLookup = getLookup;
|
2023-11-07 06:59:00 +08:00
|
|
|
|
|
|
|
Shadow = false;
|
|
|
|
UseFullGlyphHeight = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2023-11-14 21:01:56 +08:00
|
|
|
private void load(TextureStore textures)
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
2024-01-05 00:24:00 +08:00
|
|
|
const string font_name = @"argon-counter";
|
|
|
|
|
2023-11-10 05:34:29 +08:00
|
|
|
Spacing = new Vector2(-2f, 0f);
|
2024-01-05 00:24:00 +08:00
|
|
|
Font = new FontUsage(font_name, 1);
|
|
|
|
glyphStore = new GlyphStore(font_name, textures, getLookup);
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override TextBuilder CreateTextBuilder(ITexturedGlyphLookupStore store) => base.CreateTextBuilder(glyphStore);
|
|
|
|
|
|
|
|
private class GlyphStore : ITexturedGlyphLookupStore
|
|
|
|
{
|
2024-01-05 00:24:00 +08:00
|
|
|
private readonly string fontName;
|
2023-11-14 21:01:56 +08:00
|
|
|
private readonly TextureStore textures;
|
2023-11-08 07:05:19 +08:00
|
|
|
private readonly Func<char, string> getLookup;
|
2023-11-07 06:59:00 +08:00
|
|
|
|
2024-01-05 00:24:00 +08:00
|
|
|
private readonly Dictionary<char, ITexturedCharacterGlyph?> cache = new Dictionary<char, ITexturedCharacterGlyph?>();
|
|
|
|
|
|
|
|
public GlyphStore(string fontName, TextureStore textures, Func<char, string> getLookup)
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
2024-01-05 00:24:00 +08:00
|
|
|
this.fontName = fontName;
|
2023-11-14 21:01:56 +08:00
|
|
|
this.textures = textures;
|
2023-11-08 07:05:19 +08:00
|
|
|
this.getLookup = getLookup;
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
|
2023-11-24 11:45:55 +08:00
|
|
|
public ITexturedCharacterGlyph? Get(string? fontName, char character)
|
2023-11-07 06:59:00 +08:00
|
|
|
{
|
2024-01-05 00:24:00 +08:00
|
|
|
// We only service one font.
|
2024-01-09 13:06:59 +08:00
|
|
|
if (fontName != this.fontName)
|
|
|
|
return null;
|
2024-01-05 00:24:00 +08:00
|
|
|
|
|
|
|
if (cache.TryGetValue(character, out var cached))
|
|
|
|
return cached;
|
|
|
|
|
2023-11-08 07:05:19 +08:00
|
|
|
string lookup = getLookup(character);
|
2023-11-14 21:01:56 +08:00
|
|
|
var texture = textures.Get($"Gameplay/Fonts/{fontName}-{lookup}");
|
2023-11-07 06:59:00 +08:00
|
|
|
|
2024-01-05 00:24:00 +08:00
|
|
|
TexturedCharacterGlyph? glyph = null;
|
|
|
|
|
|
|
|
if (texture != null)
|
|
|
|
glyph = new TexturedCharacterGlyph(new CharacterGlyph(character, 0, 0, texture.Width, texture.Height, null), texture, 0.125f);
|
2023-11-07 06:59:00 +08:00
|
|
|
|
2024-01-05 00:24:00 +08:00
|
|
|
cache[character] = glyph;
|
|
|
|
return glyph;
|
2023-11-07 06:59:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public Task<ITexturedCharacterGlyph?> GetAsync(string fontName, char character) => Task.Run(() => Get(fontName, character));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|