1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 07:27:25 +08:00
osu-lazer/osu.Game/Overlays/Wiki/WikiSidebar.cs

114 lines
3.7 KiB
C#
Raw Normal View History

2021-06-04 10:28:31 +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.
2021-06-04 12:09:20 +08:00
using System.Collections.Generic;
2021-06-04 16:21:44 +08:00
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
2021-06-04 12:09:20 +08:00
using osu.Framework.Allocation;
2021-06-04 11:16:49 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2021-06-04 11:17:38 +08:00
using osu.Framework.Graphics.Containers.Markdown;
2021-06-04 11:16:49 +08:00
using osu.Game.Graphics;
2021-06-04 12:09:20 +08:00
using osu.Game.Graphics.Containers;
2021-06-04 11:16:49 +08:00
using osu.Game.Graphics.Sprites;
2021-06-04 10:28:31 +08:00
namespace osu.Game.Overlays.Wiki
{
public class WikiSidebar : OverlaySidebar
{
2021-06-04 11:16:49 +08:00
private FillFlowContainer tableOfContents;
protected override Drawable CreateContent() => new FillFlowContainer
{
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
new OsuSpriteText
{
Text = "CONTENTS",
Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold),
},
tableOfContents = new FillFlowContainer
{
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
}
},
};
2021-06-04 11:17:38 +08:00
2021-06-05 00:32:42 +08:00
public void AddEntry(HeadingBlock headingBlock, MarkdownHeading heading)
2021-06-04 11:17:38 +08:00
{
2021-06-04 16:21:44 +08:00
switch (headingBlock.Level)
2021-06-04 11:17:38 +08:00
{
case 2:
2021-06-04 12:12:42 +08:00
case 3:
2021-06-04 16:21:44 +08:00
string title = getTitle(headingBlock.Inline);
2021-06-05 00:31:51 +08:00
tableOfContents.Add(new TableOfContentsEntry(title, heading, headingBlock.Level == 3));
2021-06-04 12:12:42 +08:00
break;
2021-06-04 11:17:38 +08:00
}
}
2021-06-04 12:09:20 +08:00
2021-06-04 16:21:44 +08:00
private string getTitle(ContainerInline containerInline)
{
foreach (var inline in containerInline)
{
switch (inline)
{
case LiteralInline literalInline:
return literalInline.Content.ToString();
2021-06-05 19:26:03 +08:00
case LinkInline linkInline:
if (!linkInline.IsImage)
return getTitle(linkInline);
break;
2021-06-04 16:21:44 +08:00
}
}
return string.Empty;
}
2021-06-05 00:31:51 +08:00
private class TableOfContentsEntry : OsuHoverContainer
2021-06-04 12:09:20 +08:00
{
[Resolved]
private OverlayColourProvider colourProvider { get; set; }
2021-06-04 12:43:32 +08:00
private readonly MarkdownHeading target;
2021-06-04 13:50:03 +08:00
private readonly OsuTextFlowContainer textFlow;
2021-06-05 00:31:51 +08:00
public TableOfContentsEntry(string text, MarkdownHeading target, bool subtitle = false)
2021-06-04 12:09:20 +08:00
{
2021-06-04 12:43:32 +08:00
this.target = target;
2021-06-04 12:09:20 +08:00
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2021-06-04 13:50:03 +08:00
Child = textFlow = new OsuTextFlowContainer(t =>
{
t.Font = OsuFont.GetFont(size: subtitle ? 12 : 15);
}).With(f =>
2021-06-04 12:09:20 +08:00
{
2021-06-04 13:50:03 +08:00
f.AddText(text);
f.RelativeSizeAxes = Axes.X;
f.AutoSizeAxes = Axes.Y;
});
Margin = new MarginPadding { Top = subtitle ? 5 : 10 };
Padding = new MarginPadding { Left = subtitle ? 10 : 0 };
2021-06-04 12:09:20 +08:00
}
2021-06-04 13:50:03 +08:00
protected override IEnumerable<Drawable> EffectTargets => new Drawable[] { textFlow };
2021-06-04 12:09:20 +08:00
[BackgroundDependencyLoader]
2021-06-05 00:48:27 +08:00
private void load(OverlayScrollContainer scrollContainer)
2021-06-04 12:09:20 +08:00
{
IdleColour = colourProvider.Light2;
HoverColour = colourProvider.Light1;
2021-06-05 00:48:27 +08:00
Action = () => scrollContainer.ScrollTo(target);
2021-06-04 12:09:20 +08:00
}
}
2021-06-04 10:28:31 +08:00
}
}