A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4using Markdig.Syntax.Inlines;
5using osu.Framework.Allocation;
6using osu.Framework.Graphics.Cursor;
7using osu.Framework.Graphics.Sprites;
8using osu.Framework.Localisation;
9using osu.Framework.Platform;
10using osuTK.Graphics;
11
12namespace osu.Framework.Graphics.Containers.Markdown
13{
14 /// <summary>
15 /// Visualises a link.
16 /// </summary>
17 /// <code>
18 /// [link text](url)
19 /// </code>
20 public class MarkdownLinkText : CompositeDrawable, IHasTooltip, IMarkdownTextComponent
21 {
22 public LocalisableString TooltipText => Url;
23
24 [Resolved]
25 private IMarkdownTextComponent parentTextComponent { get; set; }
26
27 [Resolved]
28 private GameHost host { get; set; }
29
30 private readonly string text;
31
32 protected readonly string Url;
33
34 public MarkdownLinkText(string text, string url)
35 {
36 this.text = text;
37 Url = url;
38
39 AutoSizeAxes = Axes.Both;
40 }
41
42 public MarkdownLinkText(string text, LinkInline linkInline)
43 : this(text, linkInline.Url ?? string.Empty)
44 {
45 }
46
47 public MarkdownLinkText(AutolinkInline autolinkInline)
48 : this(autolinkInline.Url, autolinkInline.Url)
49 {
50 }
51
52 [BackgroundDependencyLoader]
53 private void load()
54 {
55 SpriteText spriteText;
56 InternalChildren = new Drawable[]
57 {
58 new ClickableContainer
59 {
60 AutoSizeAxes = Axes.Both,
61 Child = spriteText = CreateSpriteText(),
62 Action = OnLinkPressed,
63 }
64 };
65
66 spriteText.Text = text;
67 }
68
69 protected virtual void OnLinkPressed() => host.OpenUrlExternally(Url);
70
71 public virtual SpriteText CreateSpriteText()
72 {
73 var spriteText = parentTextComponent.CreateSpriteText();
74 spriteText.Colour = Color4.DodgerBlue;
75 return spriteText;
76 }
77 }
78}