2021-04-21 09:21:07 +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.
|
|
|
|
|
2022-06-17 07:37:17 +00:00
|
|
|
#nullable disable
|
|
|
|
|
2021-06-04 02:29:36 +00:00
|
|
|
using System;
|
2021-05-20 06:58:17 +00:00
|
|
|
using System.Linq;
|
2021-05-19 12:09:03 +00:00
|
|
|
using System.Threading;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
2022-03-04 04:30:08 +00:00
|
|
|
using osu.Game.Extensions;
|
2021-05-19 12:09:03 +00:00
|
|
|
using osu.Game.Online.API;
|
|
|
|
using osu.Game.Online.API.Requests;
|
|
|
|
using osu.Game.Online.API.Requests.Responses;
|
2021-04-21 09:21:07 +00:00
|
|
|
using osu.Game.Overlays.Wiki;
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
{
|
|
|
|
public partial class WikiOverlay : OnlineOverlay<WikiHeader>
|
|
|
|
{
|
2021-06-01 05:02:32 +00:00
|
|
|
private const string index_path = @"main_page";
|
2021-05-19 12:09:03 +00:00
|
|
|
|
2022-12-23 13:17:42 +00:00
|
|
|
public string CurrentPath => path.Value;
|
|
|
|
|
2021-05-19 12:09:03 +00:00
|
|
|
private readonly Bindable<string> path = new Bindable<string>(index_path);
|
|
|
|
|
|
|
|
private readonly Bindable<APIWikiPage> wikiData = new Bindable<APIWikiPage>();
|
|
|
|
|
|
|
|
[Resolved]
|
|
|
|
private IAPIProvider api { get; set; }
|
|
|
|
|
|
|
|
private GetWikiRequest request;
|
|
|
|
|
|
|
|
private CancellationTokenSource cancellationToken;
|
|
|
|
|
|
|
|
private bool displayUpdateRequired = true;
|
|
|
|
|
2021-06-04 02:29:36 +00:00
|
|
|
private WikiArticlePage articlePage;
|
|
|
|
|
2021-04-21 09:21:07 +00:00
|
|
|
public WikiOverlay()
|
|
|
|
: base(OverlayColourScheme.Orange, false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-06-01 05:01:08 +00:00
|
|
|
public void ShowPage(string pagePath = index_path)
|
|
|
|
{
|
|
|
|
path.Value = pagePath.Trim('/');
|
|
|
|
Show();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override WikiHeader CreateHeader() => new WikiHeader
|
|
|
|
{
|
|
|
|
ShowIndexPage = () => ShowPage(),
|
|
|
|
ShowParentPage = showParentPage,
|
|
|
|
};
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
path.BindValueChanged(onPathChanged);
|
|
|
|
wikiData.BindTo(Header.WikiPageData);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 02:29:36 +00: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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:09:03 +00:00
|
|
|
private void onPathChanged(ValueChangedEvent<string> e)
|
|
|
|
{
|
2022-07-20 06:43:32 +00:00
|
|
|
// the path could change as a result of redirecting to a newer location of the same page.
|
2022-07-20 05:58:24 +00:00
|
|
|
// we already have the correct wiki data, so we can safely return here.
|
|
|
|
if (e.NewValue == wikiData.Value?.Path)
|
|
|
|
return;
|
|
|
|
|
2022-12-23 13:17:42 +00:00
|
|
|
if (e.NewValue == "error")
|
|
|
|
return;
|
|
|
|
|
2021-05-19 12:09:03 +00:00
|
|
|
cancellationToken?.Cancel();
|
|
|
|
request?.Cancel();
|
|
|
|
|
2022-03-04 04:30:08 +00:00
|
|
|
string[] values = e.NewValue.Split('/', 2);
|
|
|
|
|
2022-03-04 06:57:13 +00:00
|
|
|
if (values.Length > 1 && LanguageExtensions.TryParseCultureCode(values[0], out var language))
|
|
|
|
request = new GetWikiRequest(values[1], language);
|
2022-03-04 04:30:08 +00:00
|
|
|
else
|
|
|
|
request = new GetWikiRequest(e.NewValue);
|
2021-05-19 12:09:03 +00:00
|
|
|
|
|
|
|
Loading.Show();
|
|
|
|
|
|
|
|
request.Success += response => Schedule(() => onSuccess(response));
|
2022-12-23 08:45:40 +00:00
|
|
|
request.Failure += ex =>
|
|
|
|
{
|
|
|
|
if (ex is not OperationCanceledException)
|
2022-12-23 15:42:46 +00:00
|
|
|
Schedule(onFail, request.Path);
|
2022-12-23 08:45:40 +00:00
|
|
|
};
|
2021-05-19 12:09:03 +00:00
|
|
|
|
|
|
|
api.PerformAsync(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void onSuccess(APIWikiPage response)
|
|
|
|
{
|
|
|
|
wikiData.Value = response;
|
2022-07-20 05:58:24 +00:00
|
|
|
path.Value = response.Path;
|
2021-05-25 07:20:04 +00:00
|
|
|
|
2021-06-01 05:02:32 +00:00
|
|
|
if (response.Layout == index_path)
|
2021-05-25 07:20:04 +00:00
|
|
|
{
|
|
|
|
LoadDisplay(new WikiMainPage
|
|
|
|
{
|
2021-05-25 07:37:14 +00:00
|
|
|
Markdown = response.Markdown,
|
|
|
|
Padding = new MarginPadding
|
|
|
|
{
|
|
|
|
Vertical = 20,
|
|
|
|
Horizontal = 50,
|
|
|
|
},
|
2021-05-25 07:20:04 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
2021-05-19 12:09:03 +00:00
|
|
|
{
|
2021-06-04 02:29:36 +00:00
|
|
|
LoadDisplay(articlePage = new WikiArticlePage($@"{api.WebsiteRootUrl}/wiki/{path.Value}/", response.Markdown));
|
2021-05-25 07:20:04 +00:00
|
|
|
}
|
2021-05-19 12:09:03 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 15:42:46 +00:00
|
|
|
private void onFail(string originalPath)
|
2021-06-03 12:40:54 +00:00
|
|
|
{
|
2022-12-23 13:17:42 +00:00
|
|
|
path.Value = "error";
|
2021-06-04 16:59:11 +00:00
|
|
|
LoadDisplay(articlePage = new WikiArticlePage($@"{api.WebsiteRootUrl}/wiki/",
|
2022-12-23 15:42:46 +00:00
|
|
|
$"Something went wrong when trying to fetch page \"{originalPath}\".\n\n[Return to the main page](Main_Page)."));
|
2021-06-03 12:40:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 06:58:17 +00:00
|
|
|
private void showParentPage()
|
|
|
|
{
|
2021-10-27 04:04:41 +00:00
|
|
|
string parentPath = string.Join("/", path.Value.Split('/').SkipLast(1));
|
2021-05-20 06:58:17 +00:00
|
|
|
ShowPage(parentPath);
|
|
|
|
}
|
|
|
|
|
2021-05-19 12:09:03 +00:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
cancellationToken?.Cancel();
|
|
|
|
request?.Cancel();
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
}
|
2021-04-21 09:21:07 +00:00
|
|
|
}
|
|
|
|
}
|