1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-13 07:22:54 +08:00

Allow changing font of text elements

This commit is contained in:
ansel 2022-12-03 22:44:54 +03:00
parent 0db6c2ada1
commit b41f30c868
3 changed files with 52 additions and 11 deletions

View File

@ -11,12 +11,11 @@ using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Extensions.LocalisationExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Extensions;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using osu.Game.Localisation;
using osu.Game.Resources.Localisation.Web;
@ -24,10 +23,8 @@ using osu.Game.Resources.Localisation.Web;
namespace osu.Game.Skinning.Components
{
[UsedImplicitly]
public partial class BeatmapAttributeText : Container, ISkinnableDrawable
public partial class BeatmapAttributeText : DefaultTextSkinComponent
{
public bool UsesFixedAnchor { get; set; }
[SettingSource("Attribute", "The attribute to be displayed.")]
public Bindable<BeatmapAttribute> Attribute { get; } = new Bindable<BeatmapAttribute>(BeatmapAttribute.StarRating);
@ -67,7 +64,6 @@ namespace osu.Game.Skinning.Components
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Font = OsuFont.Default.With(size: 40)
}
};
}
@ -122,6 +118,8 @@ namespace osu.Game.Skinning.Components
text.Text = LocalisableString.Format(numberedTemplate, args);
}
protected override void SetFont(FontUsage font) => text.Font = font.With(size: 40);
}
public enum BeatmapAttribute

View File

@ -0,0 +1,42 @@
// 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 osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Configuration;
using osu.Game.Graphics;
namespace osu.Game.Skinning.Components
{
/// <summary>
/// Skin element that contains text and have ability to control its font.
/// </summary>
public abstract partial class DefaultTextSkinComponent : Container, ISkinnableDrawable
{
public bool UsesFixedAnchor { get; set; }
[SettingSource("Font", "Font to use.")]
public Bindable<DefaultFont> Font { get; } = new Bindable<DefaultFont>(DefaultFont.Torus);
protected abstract void SetFont(FontUsage font);
protected override void LoadComplete()
{
base.LoadComplete();
Font.BindValueChanged(e =>
{
FontUsage f = e.NewValue switch
{
DefaultFont.Venera => OsuFont.Numeric,
DefaultFont.Torus => OsuFont.Torus,
DefaultFont.TorusAlt => OsuFont.TorusAlternate,
DefaultFont.Inter => OsuFont.Inter,
_ => OsuFont.Default
};
SetFont(f);
}, true);
}
}
}

View File

@ -4,7 +4,7 @@
using JetBrains.Annotations;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
@ -12,17 +12,16 @@ using osu.Game.Graphics.Sprites;
namespace osu.Game.Skinning.Components
{
[UsedImplicitly]
public partial class TextElement : Container, ISkinnableDrawable
public partial class TextElement : DefaultTextSkinComponent
{
public bool UsesFixedAnchor { get; set; }
[SettingSource("Text", "The text to be displayed.")]
public Bindable<string> Text { get; } = new Bindable<string>("Circles!");
private readonly OsuSpriteText text;
public TextElement()
{
AutoSizeAxes = Axes.Both;
OsuSpriteText text;
InternalChildren = new Drawable[]
{
text = new OsuSpriteText
@ -34,5 +33,7 @@ namespace osu.Game.Skinning.Components
};
text.Current.BindTo(Text);
}
protected override void SetFont(FontUsage font) => text.Font = font.With(size: 40);
}
}