2021-04-27 15:17:51 +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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2022-10-09 02:26:20 +08:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using Markdig.Extensions.CustomContainers;
|
2021-04-27 15:17:51 +08:00
|
|
|
using Markdig.Syntax.Inlines;
|
2021-05-02 23:35:30 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-05-07 15:40:01 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-06-01 15:03:05 +08:00
|
|
|
using osu.Framework.Graphics.Containers;
|
2021-04-27 15:17:51 +08:00
|
|
|
using osu.Framework.Graphics.Containers.Markdown;
|
2021-06-01 15:03:05 +08:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
2021-05-03 10:18:45 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-05-02 23:35:30 +08:00
|
|
|
using osu.Game.Overlays;
|
2022-10-09 02:26:20 +08:00
|
|
|
using osu.Game.Users;
|
|
|
|
using osu.Game.Users.Drawables;
|
|
|
|
using osuTK;
|
2021-04-27 15:17:51 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers.Markdown
|
|
|
|
{
|
|
|
|
public partial class OsuMarkdownTextFlowContainer : MarkdownTextFlowContainer
|
|
|
|
{
|
|
|
|
protected override void AddLinkText(string text, LinkInline linkInline)
|
|
|
|
=> AddDrawable(new OsuMarkdownLinkText(text, linkInline));
|
2021-05-02 23:35:30 +08:00
|
|
|
|
2021-07-10 18:58:00 +08:00
|
|
|
protected override void AddAutoLink(AutolinkInline autolinkInline)
|
|
|
|
=> AddDrawable(new OsuMarkdownLinkText(autolinkInline));
|
|
|
|
|
2021-06-10 10:38:07 +08:00
|
|
|
protected override void AddImage(LinkInline linkInline) => AddDrawable(new OsuMarkdownImage(linkInline));
|
|
|
|
|
2021-06-01 15:03:57 +08:00
|
|
|
// TODO : Change font to monospace
|
|
|
|
protected override void AddCodeInLine(CodeInline codeInline) => AddDrawable(new OsuMarkdownInlineCode
|
|
|
|
{
|
|
|
|
Text = codeInline.Content
|
|
|
|
});
|
2021-05-04 10:09:51 +08:00
|
|
|
|
|
|
|
protected override SpriteText CreateEmphasisedSpriteText(bool bold, bool italic)
|
2021-05-07 15:40:01 +08:00
|
|
|
=> CreateSpriteText().With(t => t.Font = t.Font.With(weight: bold ? FontWeight.Bold : FontWeight.Regular, italics: italic));
|
2021-06-01 15:03:05 +08:00
|
|
|
|
2022-10-09 02:26:20 +08:00
|
|
|
protected override void AddCustomComponent(CustomContainerInline inline)
|
|
|
|
{
|
|
|
|
if (!(inline.FirstChild is LiteralInline literal))
|
|
|
|
{
|
|
|
|
base.AddCustomComponent(inline);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string[] attributes = literal.Content.ToString().Trim(' ', '{', '}').Split();
|
|
|
|
string flagAttribute = attributes.SingleOrDefault(a => a.StartsWith(@"flag", StringComparison.Ordinal));
|
|
|
|
|
|
|
|
if (flagAttribute == null)
|
|
|
|
{
|
|
|
|
base.AddCustomComponent(inline);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
string flag = flagAttribute.Split('=').Last().Trim('"');
|
2022-10-11 00:38:37 +08:00
|
|
|
|
|
|
|
if (!Enum.TryParse<CountryCode>(flag, out var countryCode))
|
|
|
|
countryCode = CountryCode.Unknown;
|
|
|
|
|
|
|
|
AddDrawable(new DrawableFlag(countryCode) { Size = new Vector2(20, 15) });
|
2022-10-09 02:26:20 +08:00
|
|
|
}
|
|
|
|
|
2021-06-01 15:03:05 +08:00
|
|
|
private partial class OsuMarkdownInlineCode : Container
|
|
|
|
{
|
|
|
|
[Resolved]
|
|
|
|
private IMarkdownTextComponent parentTextComponent { get; set; }
|
|
|
|
|
|
|
|
public string Text;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OverlayColourProvider colourProvider)
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2021-06-01 15:14:12 +08:00
|
|
|
CornerRadius = 4;
|
|
|
|
Masking = true;
|
2021-06-01 15:03:05 +08:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Colour = colourProvider.Background6,
|
|
|
|
},
|
|
|
|
parentTextComponent.CreateSpriteText().With(t =>
|
|
|
|
{
|
|
|
|
t.Colour = colourProvider.Light1;
|
|
|
|
t.Text = Text;
|
2021-06-01 15:14:12 +08:00
|
|
|
t.Padding = new MarginPadding
|
|
|
|
{
|
|
|
|
Vertical = 1,
|
|
|
|
Horizontal = 4,
|
|
|
|
};
|
2021-06-01 15:03:05 +08:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2021-04-27 15:17:51 +08:00
|
|
|
}
|
|
|
|
}
|