Skip to content

Add support for displaying "system title" on main menu #26172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Dec 28, 2023
30 changes: 30 additions & 0 deletions osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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.Linq;
using NUnit.Framework;
using osu.Framework.Testing;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Screens.Menu;

namespace osu.Game.Tests.Visual.Menus
{
public partial class TestSceneMainMenu : OsuGameTestScene
{
[Test]
public void TestSystemTitle()
{
AddStep("set system title", () => Game.ChildrenOfType<SystemTitle>().Single().Current.Value = new APISystemTitle
{
Image = @"https://assets.ppy.sh/main-menu/project-loved-2@2x.png",
Url = @"https://osu.ppy.sh/home/news/2023-12-21-project-loved-december-2023",
});
AddStep("set another title", () => Game.ChildrenOfType<SystemTitle>().Single().Current.Value = new APISystemTitle
{
Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png",
Url = @"https://osu.ppy.sh/community/contests/189",
});
AddStep("unset system title", () => Game.ChildrenOfType<SystemTitle>().Single().Current.Value = null);
}
}
}
15 changes: 15 additions & 0 deletions osu.Game/Online/API/Requests/GetSystemTitleRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 osu.Game.Online.API.Requests.Responses;

namespace osu.Game.Online.API.Requests
{
public class GetSystemTitleRequest : OsuJsonWebRequest<APISystemTitle>
{
public GetSystemTitleRequest()
: base(@"https://assets.ppy.sh/lazer-status.json")
Copy link
Collaborator Author

@bdach bdach Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Astute readers will inevitably point out that this (a) doesn't exist yet, (b) is not an API call.

I agreed upon doing this in this way with @peppy beforehand. Can be tested via e.g. python3 -m http.server or something (and changing URL, and probably also allowing insecure requests).

{
}
}
}
16 changes: 16 additions & 0 deletions osu.Game/Online/API/Requests/Responses/APISystemTitle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// 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 Newtonsoft.Json;

namespace osu.Game.Online.API.Requests.Responses
{
public record APISystemTitle
{
[JsonProperty(@"image")]
public string Image { get; set; } = string.Empty;

[JsonProperty(@"url")]
public string Url { get; set; } = string.Empty;
}
}
5 changes: 4 additions & 1 deletion osu.Game/OsuGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,10 @@ protected override void LoadComplete()
}, topMostOverlayContent.Add);

if (!args?.Any(a => a == @"--no-version-overlay") ?? true)
loadComponentSingleFile(versionManager = new VersionManager { Depth = int.MinValue }, ScreenContainer.Add);
{
dependencies.Cache(versionManager = new VersionManager { Depth = int.MinValue });
loadComponentSingleFile(versionManager, ScreenContainer.Add);
Comment on lines +999 to +1000
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This excursion is to prevent the version display on the bottom and the system title stepping on each other.

}

loadComponentSingleFile(osuLogo, _ =>
{
Expand Down
15 changes: 15 additions & 0 deletions osu.Game/Screens/Menu/MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public partial class MainMenu : OsuScreen, IHandlePresentBeatmap, IKeyBindingHan
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }

[Resolved(canBeNull: true)]
private VersionManager versionManager { get; set; }

protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();

protected override bool PlayExitSound => false;
Expand All @@ -91,6 +94,7 @@ public partial class MainMenu : OsuScreen, IHandlePresentBeatmap, IKeyBindingHan
private ParallaxContainer buttonsContainer;
private SongTicker songTicker;
private Container logoTarget;
private SystemTitle systemTitle;

private Sample reappearSampleSwoosh;

Expand Down Expand Up @@ -153,6 +157,7 @@ private void load(BeatmapListingOverlay beatmapListing, SettingsOverlay settings
Margin = new MarginPadding { Right = 15, Top = 5 }
},
new KiaiMenuFountains(),
systemTitle = new SystemTitle(),
holdToExitGameOverlay?.CreateProxy() ?? Empty()
});

Expand Down Expand Up @@ -263,6 +268,16 @@ bool displayLogin(Func<bool> originalAction)
}
}

protected override void Update()
{
base.Update();

systemTitle.Margin = new MarginPadding
{
Bottom = (versionManager?.DrawHeight + 5) ?? 0
};
}

protected override void LogoSuspending(OsuLogo logo)
{
var seq = logo.FadeOut(300, Easing.InSine)
Expand Down
124 changes: 124 additions & 0 deletions osu.Game/Screens/Menu/SystemTitle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// 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;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input.Events;
using osu.Framework.Platform;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;

namespace osu.Game.Screens.Menu
{
public partial class SystemTitle : CompositeDrawable
{
internal Bindable<APISystemTitle?> Current { get; } = new Bindable<APISystemTitle?>();

private Container content = null!;
private CancellationTokenSource? cancellationTokenSource;
private SystemTitleImage? currentImage;

[BackgroundDependencyLoader]
private void load(GameHost? gameHost)
{
Anchor = Anchor.BottomCentre;
Origin = Anchor.BottomCentre;
AutoSizeAxes = Axes.Both;

InternalChild = content = new ClickableContainer
{
AutoSizeAxes = Axes.Both,
Action = () =>
{
if (!string.IsNullOrEmpty(Current.Value?.Url))
gameHost?.OpenUrlExternally(Current.Value.Url);
}
};
}

protected override bool OnHover(HoverEvent e)
{
content.ScaleTo(1.1f, 500, Easing.OutBounce);
return base.OnHover(e);
}

protected override void OnHoverLost(HoverLostEvent e)
{
content.ScaleTo(1f, 500, Easing.OutBounce);
base.OnHoverLost(e);
}

protected override void LoadComplete()
{
base.LoadComplete();

Current.BindValueChanged(_ => loadNewImage(), true);

checkForUpdates();
Scheduler.AddDelayed(checkForUpdates, TimeSpan.FromMinutes(15).TotalMilliseconds, true);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this polling is necessary. Probably can be removed if just a once-off call at startup is deemed acceptable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refreshing is best. It will already only attempt a refresh when returning to the main menu, which should be pretty fine. I'd almost say to just check each time we return, but I guess a bit of delay is worth having.

}

private void checkForUpdates()
{
var request = new GetSystemTitleRequest();
Task.Run(() => request.Perform())
.ContinueWith(r =>
{
if (r.IsCompletedSuccessfully)
Schedule(() => Current.Value = request.ResponseObject);

// if the request failed, "observe" the exception.
// it isn't very important why this failed, as it's only for display.
// the inner error will be logged by framework mechanisms anyway.
if (r.IsFaulted)
_ = r.Exception;
});
}

private void loadNewImage()
{
cancellationTokenSource?.Cancel();
cancellationTokenSource = null;
currentImage?.FadeOut(500, Easing.OutQuint).Expire();

if (string.IsNullOrEmpty(Current.Value?.Image))
return;

LoadComponentAsync(new SystemTitleImage(Current.Value), loaded =>
{
if (loaded.SystemTitle != Current.Value)
loaded.Dispose();

loaded.FadeInFromZero(500, Easing.OutQuint);
content.Add(currentImage = loaded);
}, (cancellationTokenSource ??= new CancellationTokenSource()).Token);
}

[LongRunningLoad]
private partial class SystemTitleImage : Sprite
{
public readonly APISystemTitle SystemTitle;

public SystemTitleImage(APISystemTitle systemTitle)
{
SystemTitle = systemTitle;
}

[BackgroundDependencyLoader]
private void load(LargeTextureStore textureStore)
{
var texture = textureStore.Get(SystemTitle.Image);
if (SystemTitle.Image.Contains(@"@2x"))
texture.ScaleAdjust *= 2;
Texture = texture;
}
}
}
}