1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 11:37:51 +08:00
osu-lazer/osu.Game/Overlays/WikiOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

197 lines
6.1 KiB
C#
Raw Normal View History

2021-04-21 17:21:07 +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 10:29:36 +08:00
using System;
2021-05-20 14:58:17 +08:00
using System.Linq;
2021-05-19 20:09:03 +08:00
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Game.Extensions;
2024-08-13 11:50:33 +08:00
using osu.Game.Localisation;
2021-05-19 20:09:03 +08:00
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
2021-04-21 17:21:07 +08:00
using osu.Game.Overlays.Wiki;
namespace osu.Game.Overlays
{
public partial class WikiOverlay : OnlineOverlay<WikiHeader>
{
public const string INDEX_PATH = @"Main_page";
2021-05-19 20:09:03 +08:00
2022-12-23 21:17:42 +08:00
public string CurrentPath => path.Value;
private readonly Bindable<string> path = new Bindable<string>(INDEX_PATH);
2024-08-13 15:08:06 +08:00
private readonly Bindable<APIWikiPage?> wikiData = new Bindable<APIWikiPage?>();
private readonly IBindable<Language> language = new Bindable<Language>();
2021-05-19 20:09:03 +08:00
[Resolved]
2024-08-13 15:08:06 +08:00
private IAPIProvider api { get; set; } = null!;
2021-05-19 20:09:03 +08:00
2024-08-13 15:08:06 +08:00
[Resolved]
private OsuGameBase game { get; set; } = null!;
2021-05-19 20:09:03 +08:00
2024-08-13 15:08:06 +08:00
private GetWikiRequest? request;
private CancellationTokenSource? cancellationToken;
private WikiArticlePage? articlePage;
2021-05-19 20:09:03 +08:00
private bool displayUpdateRequired = true;
2021-04-21 17:21:07 +08:00
public WikiOverlay()
: base(OverlayColourScheme.Orange, false)
{
}
2024-08-13 15:08:06 +08:00
protected override void LoadComplete()
2024-08-13 11:50:33 +08:00
{
2024-08-13 15:08:06 +08:00
base.LoadComplete();
path.BindValueChanged(onPathChanged);
wikiData.BindTo(Header.WikiPageData);
language.BindTo(game.CurrentLanguage);
language.BindValueChanged(onLangChanged);
2024-08-13 11:50:33 +08:00
}
public void ShowPage(string pagePath = INDEX_PATH)
2021-06-01 13:01:08 +08:00
{
path.Value = pagePath.Trim('/');
Show();
}
protected override WikiHeader CreateHeader() => new WikiHeader
{
ShowIndexPage = () => ShowPage(),
ShowParentPage = showParentPage,
};
protected override void PopIn()
{
base.PopIn();
if (displayUpdateRequired)
{
path.TriggerChange();
displayUpdateRequired = false;
}
}
protected override void PopOutComplete()
{
base.PopOutComplete();
displayUpdateRequired = true;
}
protected void LoadDisplay(Drawable display)
{
ScrollFlow.ScrollToStart();
LoadComponentAsync(display, loaded =>
{
Child = loaded;
Loading.Hide();
}, (cancellationToken = new CancellationTokenSource()).Token);
}
2021-06-04 10:29:36 +08:00
protected override void UpdateAfterChildren()
{
base.UpdateAfterChildren();
if (articlePage != null)
{
articlePage.SidebarContainer.Height = DrawHeight;
articlePage.SidebarContainer.Y = Math.Clamp(ScrollFlow.Current - Header.DrawHeight, 0, Math.Max(ScrollFlow.ScrollContent.DrawHeight - DrawHeight - Header.DrawHeight, 0));
}
}
2024-08-13 13:34:11 +08:00
private void loadPage(string path, Language lang)
2021-05-19 20:09:03 +08:00
{
cancellationToken?.Cancel();
request?.Cancel();
2024-08-13 11:50:33 +08:00
// Language code + path, or just path1 + path2 in case
2024-08-13 13:34:11 +08:00
string[] values = path.Split('/', 2);
2024-08-13 13:34:11 +08:00
if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var parsedLang))
request = new GetWikiRequest(values[1], parsedLang);
else
2024-08-13 13:34:11 +08:00
request = new GetWikiRequest(path, lang);
2021-05-19 20:09:03 +08:00
Loading.Show();
request.Success += response => Schedule(() => onSuccess(response));
request.Failure += ex =>
{
if (ex is not OperationCanceledException)
Schedule(onFail, request.Path);
};
2021-05-19 20:09:03 +08:00
api.PerformAsync(request);
}
2024-08-13 13:34:11 +08:00
private void onPathChanged(ValueChangedEvent<string> e)
{
// the path could change as a result of redirecting to a newer location of the same page.
// we already have the correct wiki data, so we can safely return here.
if (e.NewValue == wikiData.Value?.Path)
return;
if (e.NewValue == "error")
return;
loadPage(e.NewValue, language.Value);
}
private void onLangChanged(ValueChangedEvent<Language> e)
{
// Path unmodified, just reload the page with new language value.
loadPage(path.Value, e.NewValue);
}
2021-05-19 20:09:03 +08:00
private void onSuccess(APIWikiPage response)
{
wikiData.Value = response;
path.Value = response.Path;
2021-05-25 15:20:04 +08:00
if (response.Layout.Equals(INDEX_PATH, StringComparison.OrdinalIgnoreCase))
2021-05-25 15:20:04 +08:00
{
LoadDisplay(new WikiMainPage
{
2021-05-25 15:37:14 +08:00
Markdown = response.Markdown,
Padding = new MarginPadding
{
Vertical = 20,
Horizontal = HORIZONTAL_PADDING,
2021-05-25 15:37:14 +08:00
},
2021-05-25 15:20:04 +08:00
});
}
else
2021-05-19 20:09:03 +08:00
{
2021-06-04 10:29:36 +08:00
LoadDisplay(articlePage = new WikiArticlePage($@"{api.WebsiteRootUrl}/wiki/{path.Value}/", response.Markdown));
2021-05-25 15:20:04 +08:00
}
2021-05-19 20:09:03 +08:00
}
private void onFail(string originalPath)
2021-06-03 20:40:54 +08:00
{
wikiData.Value = null;
2022-12-23 21:17:42 +08:00
path.Value = "error";
LoadDisplay(articlePage = new WikiArticlePage($@"{api.WebsiteRootUrl}/wiki/",
$"Something went wrong when trying to fetch page \"{originalPath}\".\n\n[Return to the main page]({INDEX_PATH})."));
2021-06-03 20:40:54 +08:00
}
2021-05-20 14:58:17 +08:00
private void showParentPage()
{
string parentPath = string.Join("/", path.Value.Split('/').SkipLast(1));
2021-05-20 14:58:17 +08:00
ShowPage(parentPath);
}
2021-05-19 20:09:03 +08:00
protected override void Dispose(bool isDisposing)
{
cancellationToken?.Cancel();
request?.Cancel();
base.Dispose(isDisposing);
}
2021-04-21 17:21:07 +08:00
}
}