1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 15:57:24 +08:00

Add styling for footnote links

This commit is contained in:
Bartłomiej Dach 2022-12-16 20:59:14 +01:00
parent 6e55f2f779
commit 112613c2f0
No known key found for this signature in database
4 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,70 @@
// 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.Collections.Generic;
using Markdig.Extensions.Footnotes;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers.Markdown;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Overlays;
namespace osu.Game.Graphics.Containers.Markdown.Footnotes
{
public partial class OsuMarkdownFootnoteLink : OsuHoverContainer, IHasCustomTooltip
{
private readonly FootnoteLink footnoteLink;
private SpriteText spriteText = null!;
[Resolved]
private IMarkdownTextComponent parentTextComponent { get; set; } = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
[Resolved]
private OsuMarkdownContainer markdownContainer { get; set; } = null!;
protected override IEnumerable<Drawable> EffectTargets => spriteText.Yield();
public OsuMarkdownFootnoteLink(FootnoteLink footnoteLink)
{
this.footnoteLink = footnoteLink;
AutoSizeAxes = Axes.Both;
Action = () => { }; // TODO
}
[BackgroundDependencyLoader]
private void load()
{
IdleColour = colourProvider.Light2;
HoverColour = colourProvider.Light1;
spriteText = parentTextComponent.CreateSpriteText();
Add(spriteText.With(t =>
{
float baseSize = t.Font.Size;
t.Font = t.Font.With(size: baseSize * 0.58f);
t.Margin = new MarginPadding { Bottom = 0.33f * baseSize };
t.Text = LocalisableString.Format("[{0}]", footnoteLink.Index);
}));
}
public object TooltipContent
{
get
{
var span = footnoteLink.Footnote.LastChild.Span;
return markdownContainer.Text.Substring(span.Start, span.Length);
}
}
public ITooltip GetCustomTooltip() => new OsuMarkdownFootnoteTooltip(colourProvider);
}
}

View File

@ -0,0 +1,76 @@
// 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 Markdig.Extensions.Footnotes;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Containers.Markdown;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Game.Overlays;
using osuTK;
namespace osu.Game.Graphics.Containers.Markdown.Footnotes
{
public partial class OsuMarkdownFootnoteTooltip : CompositeDrawable, ITooltip
{
private readonly FootnoteMarkdownContainer markdownContainer;
[Cached]
private OverlayColourProvider colourProvider;
public OsuMarkdownFootnoteTooltip(OverlayColourProvider colourProvider)
{
this.colourProvider = colourProvider;
Masking = true;
Width = 200;
AutoSizeAxes = Axes.Y;
CornerRadius = 4;
InternalChildren = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background6
},
markdownContainer = new FootnoteMarkdownContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
DocumentMargin = new MarginPadding(),
DocumentPadding = new MarginPadding { Horizontal = 10, Vertical = 5 }
}
};
}
public void Move(Vector2 pos) => Position = pos;
public void SetContent(object content) => markdownContainer.SetContent((string)content);
private partial class FootnoteMarkdownContainer : OsuMarkdownContainer
{
private string? lastFootnote;
public void SetContent(string footnote)
{
if (footnote == lastFootnote)
return;
lastFootnote = Text = footnote;
}
public override MarkdownTextFlowContainer CreateTextFlow() => new FootnoteMarkdownTextFlowContainer();
}
private partial class FootnoteMarkdownTextFlowContainer : OsuMarkdownTextFlowContainer
{
protected override void AddFootnoteBacklink(FootnoteLink footnoteBacklink)
{
// we don't want footnote backlinks to show up in tooltips.
}
}
}
}

View File

@ -11,6 +11,7 @@ using Markdig.Extensions.Footnotes;
using Markdig.Extensions.Tables; using Markdig.Extensions.Tables;
using Markdig.Extensions.Yaml; using Markdig.Extensions.Yaml;
using Markdig.Syntax; using Markdig.Syntax;
using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Containers.Markdown; using osu.Framework.Graphics.Containers.Markdown;
@ -19,6 +20,7 @@ using osu.Game.Graphics.Sprites;
namespace osu.Game.Graphics.Containers.Markdown namespace osu.Game.Graphics.Containers.Markdown
{ {
[Cached]
public partial class OsuMarkdownContainer : MarkdownContainer public partial class OsuMarkdownContainer : MarkdownContainer
{ {
/// <summary> /// <summary>

View File

@ -6,6 +6,7 @@
using System; using System;
using System.Linq; using System.Linq;
using Markdig.Extensions.CustomContainers; using Markdig.Extensions.CustomContainers;
using Markdig.Extensions.Footnotes;
using Markdig.Syntax.Inlines; using Markdig.Syntax.Inlines;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
@ -13,6 +14,7 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Containers.Markdown; using osu.Framework.Graphics.Containers.Markdown;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics.Containers.Markdown.Footnotes;
using osu.Game.Overlays; using osu.Game.Overlays;
using osu.Game.Users; using osu.Game.Users;
using osu.Game.Users.Drawables; using osu.Game.Users.Drawables;
@ -36,6 +38,8 @@ namespace osu.Game.Graphics.Containers.Markdown
Text = codeInline.Content Text = codeInline.Content
}); });
protected override void AddFootnoteLink(FootnoteLink footnoteLink) => AddDrawable(new OsuMarkdownFootnoteLink(footnoteLink));
protected override SpriteText CreateEmphasisedSpriteText(bool bold, bool italic) protected override SpriteText CreateEmphasisedSpriteText(bool bold, bool italic)
=> CreateSpriteText().With(t => t.Font = t.Font.With(weight: bold ? FontWeight.Bold : FontWeight.Regular, italics: italic)); => CreateSpriteText().With(t => t.Font = t.Font.With(weight: bold ? FontWeight.Bold : FontWeight.Regular, italics: italic));