osu/osu.Game/Overlays/Wiki/WikiMainPage.cs

105 lines
3.3 KiB
C#
Raw Normal View History

2021-05-21 03:04:45 +00: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-05-23 10:56:27 +00:00
using System.Collections.Generic;
2021-05-27 10:12:15 +00:00
using System.Diagnostics;
2021-05-23 10:56:27 +00:00
using System.Linq;
2021-05-21 03:04:45 +00:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2021-05-21 03:04:45 +00:00
using HtmlAgilityPack;
using osu.Framework.Allocation;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2021-05-21 03:04:45 +00:00
namespace osu.Game.Overlays.Wiki
{
public class WikiMainPage : FillFlowContainer
{
2021-05-21 03:04:45 +00:00
public string Markdown;
2021-05-21 03:04:45 +00:00
public WikiMainPage()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
}
2021-05-21 03:04:45 +00:00
[BackgroundDependencyLoader]
private void load()
{
var html = new HtmlDocument();
html.LoadHtml(Markdown);
2021-05-23 12:07:22 +00:00
var panels = createPanels(html).ToArray();
2021-05-21 03:04:45 +00:00
Children = new Drawable[]
{
2021-05-23 11:52:20 +00:00
createBlurb(html),
new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2021-05-23 12:07:22 +00:00
RowDimensions = Enumerable.Repeat(new Dimension(GridSizeMode.AutoSize), panels.Length).ToArray(),
Content = panels,
2021-05-23 11:52:20 +00:00
},
2021-05-21 03:04:45 +00:00
};
}
private Container createBlurb(HtmlDocument html)
{
2021-05-27 17:50:59 +00:00
var blurbNode = html.DocumentNode.SelectSingleNode("//div[contains(@class, 'wiki-main-page__blurb')]");
2021-05-21 03:04:45 +00:00
return new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Vertical = 30,
},
Child = new OsuTextFlowContainer(t => t.Font = OsuFont.GetFont(Typeface.Inter, size: 12, weight: FontWeight.Light))
2021-05-21 03:04:45 +00:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2021-05-21 03:04:45 +00:00
Text = blurbNode.InnerText,
TextAnchor = Anchor.TopCentre,
2021-05-21 03:04:45 +00:00
}
};
}
2021-05-23 10:49:26 +00:00
private IEnumerable<Drawable[]> createPanels(HtmlDocument html)
2021-05-23 10:49:26 +00:00
{
var panelsNode = html.DocumentNode.SelectNodes("//div[contains(@class, 'wiki-main-page-panel')]").ToArray();
2021-05-23 10:49:26 +00:00
2021-05-27 10:12:15 +00:00
Debug.Assert(panelsNode.Length > 1);
int i = 0;
2021-05-27 06:34:55 +00:00
while (i < panelsNode.Length)
2021-05-23 10:49:26 +00:00
{
bool isFullWidth = panelsNode[i].HasClass("wiki-main-page-panel--full");
2021-05-23 10:49:26 +00:00
if (isFullWidth)
2021-05-23 10:49:26 +00:00
{
yield return new Drawable[]
{
2021-05-27 06:34:55 +00:00
new WikiPanelContainer(panelsNode[i++].InnerText, true)
{
2021-05-27 06:24:06 +00:00
// This is required to fill up the space of "null" drawable below.
Width = 2,
},
null,
};
}
2021-05-27 06:34:55 +00:00
else
{
yield return new Drawable[]
{
2021-05-27 06:34:55 +00:00
new WikiPanelContainer(panelsNode[i++].InnerText),
2021-05-27 17:55:14 +00:00
i < panelsNode.Length ? new WikiPanelContainer(panelsNode[i++].InnerText) : null,
};
}
2021-05-23 10:49:26 +00:00
}
}
2021-05-21 03:04:45 +00:00
}
}