From ef2a16dd8ff260efa5117a10352e6ec771dfa2c3 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 23 Mar 2024 16:41:40 +0800 Subject: [PATCH 01/15] Various renaming and class updates to allow multiple menu banners --- .../Visual/Menus/TestSceneMainMenu.cs | 46 +++++++++----- ...tleRequest.cs => GetMenuContentRequest.cs} | 4 +- .../API/Requests/Responses/APIMenuContent.cs | 42 +++++++++++++ .../API/Requests/Responses/APIMenuImage.cs | 57 +++++++++++++++++ .../API/Requests/Responses/APISystemTitle.cs | 30 --------- osu.Game/Screens/Menu/MainMenu.cs | 8 +-- .../{SystemTitle.cs => OnlineMenuBanner.cs} | 62 ++++++++++--------- 7 files changed, 169 insertions(+), 80 deletions(-) rename osu.Game/Online/API/Requests/{GetSystemTitleRequest.cs => GetMenuContentRequest.cs} (74%) create mode 100644 osu.Game/Online/API/Requests/Responses/APIMenuContent.cs create mode 100644 osu.Game/Online/API/Requests/Responses/APIMenuImage.cs delete mode 100644 osu.Game/Online/API/Requests/Responses/APISystemTitle.cs rename osu.Game/Screens/Menu/{SystemTitle.cs => OnlineMenuBanner.cs} (79%) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs b/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs index 7053a9d544..3c78edb8a5 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs @@ -13,30 +13,48 @@ namespace osu.Game.Tests.Visual.Menus { public partial class TestSceneMainMenu : OsuGameTestScene { - private SystemTitle systemTitle => Game.ChildrenOfType().Single(); + private OnlineMenuBanner onlineMenuBanner => Game.ChildrenOfType().Single(); [Test] - public void TestSystemTitle() + public void TestOnlineMenuBanner() { - AddStep("set system title", () => systemTitle.Current.Value = new APISystemTitle + AddStep("set online content", () => onlineMenuBanner.Current.Value = new APIMenuContent { - 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", + Images = new[] + { + new APIMenuImage + { + 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", + } + } }); - AddAssert("system title not visible", () => systemTitle.State.Value, () => Is.EqualTo(Visibility.Hidden)); + AddAssert("system title not visible", () => onlineMenuBanner.State.Value, () => Is.EqualTo(Visibility.Hidden)); AddStep("enter menu", () => InputManager.Key(Key.Enter)); - AddUntilStep("system title visible", () => systemTitle.State.Value, () => Is.EqualTo(Visibility.Visible)); - AddStep("set another title", () => systemTitle.Current.Value = new APISystemTitle + AddUntilStep("system title visible", () => onlineMenuBanner.State.Value, () => Is.EqualTo(Visibility.Visible)); + AddStep("set another title", () => onlineMenuBanner.Current.Value = new APIMenuContent { - Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png", - Url = @"https://osu.ppy.sh/community/contests/189", + Images = new[] + { + new APIMenuImage + { + Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png", + Url = @"https://osu.ppy.sh/community/contests/189", + } + } }); - AddStep("set title with nonexistent image", () => systemTitle.Current.Value = new APISystemTitle + AddStep("set title with nonexistent image", () => onlineMenuBanner.Current.Value = new APIMenuContent { - Image = @"https://test.invalid/@2x", // .invalid TLD reserved by https://datatracker.ietf.org/doc/html/rfc2606#section-2 - Url = @"https://osu.ppy.sh/community/contests/189", + Images = new[] + { + new APIMenuImage + { + Image = @"https://test.invalid/@2x", // .invalid TLD reserved by https://datatracker.ietf.org/doc/html/rfc2606#section-2 + Url = @"https://osu.ppy.sh/community/contests/189", + } + } }); - AddStep("unset system title", () => systemTitle.Current.Value = null); + AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); } } } diff --git a/osu.Game/Online/API/Requests/GetSystemTitleRequest.cs b/osu.Game/Online/API/Requests/GetMenuContentRequest.cs similarity index 74% rename from osu.Game/Online/API/Requests/GetSystemTitleRequest.cs rename to osu.Game/Online/API/Requests/GetMenuContentRequest.cs index 52ca0c11eb..ad2bac6696 100644 --- a/osu.Game/Online/API/Requests/GetSystemTitleRequest.cs +++ b/osu.Game/Online/API/Requests/GetMenuContentRequest.cs @@ -5,9 +5,9 @@ using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Online.API.Requests { - public class GetSystemTitleRequest : OsuJsonWebRequest + public class GetMenuContentRequest : OsuJsonWebRequest { - public GetSystemTitleRequest() + public GetMenuContentRequest() : base(@"https://assets.ppy.sh/lazer-status.json") { } diff --git a/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs b/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs new file mode 100644 index 0000000000..acee6c99ba --- /dev/null +++ b/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs @@ -0,0 +1,42 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using Newtonsoft.Json; + +namespace osu.Game.Online.API.Requests.Responses +{ + public class APIMenuContent : IEquatable + { + /// + /// Images which should be displayed in rotation. + /// + [JsonProperty(@"images")] + public APIMenuImage[] Images { get; init; } = Array.Empty(); + + public DateTimeOffset LastUpdated { get; init; } + + public bool Equals(APIMenuContent? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + + return LastUpdated.Equals(other.LastUpdated); + } + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + + if (obj.GetType() != GetType()) return false; + + return Equals((APIMenuContent)obj); + } + + public override int GetHashCode() + { + return LastUpdated.GetHashCode(); + } + } +} diff --git a/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs b/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs new file mode 100644 index 0000000000..4824e23d4b --- /dev/null +++ b/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs @@ -0,0 +1,57 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System; +using Newtonsoft.Json; + +namespace osu.Game.Online.API.Requests.Responses +{ + public class APIMenuImage : IEquatable + { + /// + /// A URL pointing to the image which should be displayed. Generally should be an @2x image filename. + /// + [JsonProperty(@"image")] + public string Image { get; init; } = string.Empty; + + /// + /// A URL that should be opened on clicking the image. + /// + [JsonProperty(@"url")] + public string Url { get; init; } = string.Empty; + + /// + /// The time at which this item should begin displaying. If null, will display immediately. + /// + [JsonProperty(@"begins")] + public DateTimeOffset? Begins { get; set; } + + /// + /// The time at which this item should stop displaying. If null, will display indefinitely. + /// + [JsonProperty(@"expires")] + public DateTimeOffset? Expires { get; set; } + + public bool Equals(APIMenuImage? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + + return Image == other.Image && Url == other.Url; + } + + public override bool Equals(object? obj) + { + if (ReferenceEquals(null, obj)) return false; + if (ReferenceEquals(this, obj)) return true; + if (obj.GetType() != GetType()) return false; + + return Equals((APIMenuImage)obj); + } + + public override int GetHashCode() + { + return HashCode.Combine(Image, Url); + } + } +} diff --git a/osu.Game/Online/API/Requests/Responses/APISystemTitle.cs b/osu.Game/Online/API/Requests/Responses/APISystemTitle.cs deleted file mode 100644 index bfa5c1043b..0000000000 --- a/osu.Game/Online/API/Requests/Responses/APISystemTitle.cs +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. -// See the LICENCE file in the repository root for full licence text. - -using System; -using Newtonsoft.Json; - -namespace osu.Game.Online.API.Requests.Responses -{ - public class APISystemTitle : IEquatable - { - [JsonProperty(@"image")] - public string Image { get; set; } = string.Empty; - - [JsonProperty(@"url")] - public string Url { get; set; } = string.Empty; - - public bool Equals(APISystemTitle? other) - { - if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; - - return Image == other.Image && Url == other.Url; - } - - public override bool Equals(object? obj) => obj is APISystemTitle other && Equals(other); - - // ReSharper disable NonReadonlyMemberInGetHashCode - public override int GetHashCode() => HashCode.Combine(Image, Url); - } -} diff --git a/osu.Game/Screens/Menu/MainMenu.cs b/osu.Game/Screens/Menu/MainMenu.cs index decb901c32..235c5d5c56 100644 --- a/osu.Game/Screens/Menu/MainMenu.cs +++ b/osu.Game/Screens/Menu/MainMenu.cs @@ -98,7 +98,7 @@ namespace osu.Game.Screens.Menu private ParallaxContainer buttonsContainer; private SongTicker songTicker; private Container logoTarget; - private SystemTitle systemTitle; + private OnlineMenuBanner onlineMenuBanner; private MenuTip menuTip; private FillFlowContainer bottomElementsFlow; private SupporterDisplay supporterDisplay; @@ -178,7 +178,7 @@ namespace osu.Game.Screens.Menu Anchor = Anchor.TopCentre, Origin = Anchor.TopCentre, }, - systemTitle = new SystemTitle + onlineMenuBanner = new OnlineMenuBanner { Anchor = Anchor.TopCentre, Origin = Anchor.TopCentre, @@ -201,12 +201,12 @@ namespace osu.Game.Screens.Menu case ButtonSystemState.Initial: case ButtonSystemState.Exit: ApplyToBackground(b => b.FadeColour(Color4.White, 500, Easing.OutSine)); - systemTitle.State.Value = Visibility.Hidden; + onlineMenuBanner.State.Value = Visibility.Hidden; break; default: ApplyToBackground(b => b.FadeColour(OsuColour.Gray(0.8f), 500, Easing.OutSine)); - systemTitle.State.Value = Visibility.Visible; + onlineMenuBanner.State.Value = Visibility.Visible; break; } }; diff --git a/osu.Game/Screens/Menu/SystemTitle.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs similarity index 79% rename from osu.Game/Screens/Menu/SystemTitle.cs rename to osu.Game/Screens/Menu/OnlineMenuBanner.cs index 813a470ed6..cf20196f85 100644 --- a/osu.Game/Screens/Menu/SystemTitle.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Linq; using System.Threading; using System.Threading.Tasks; using osu.Framework.Allocation; @@ -18,42 +19,28 @@ using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Screens.Menu { - public partial class SystemTitle : VisibilityContainer + public partial class OnlineMenuBanner : VisibilityContainer { - internal Bindable Current { get; } = new Bindable(); + internal Bindable Current { get; } = new Bindable(new APIMenuContent()); private const float transition_duration = 500; private Container content = null!; private CancellationTokenSource? cancellationTokenSource; - private SystemTitleImage? currentImage; - - private ScheduledDelegate? openUrlAction; + private MenuImage? currentImage; [BackgroundDependencyLoader] - private void load(OsuGame? game) + private void load() { AutoSizeAxes = Axes.Both; AutoSizeDuration = transition_duration; AutoSizeEasing = Easing.OutQuint; - InternalChild = content = new OsuClickableContainer + InternalChild = content = new Container { Anchor = Anchor.Centre, Origin = Anchor.Centre, AutoSizeAxes = Axes.Both, - Action = () => - { - currentImage?.Flash(); - - // Delay slightly to allow animation to play out. - openUrlAction?.Cancel(); - openUrlAction = Scheduler.AddDelayed(() => - { - if (!string.IsNullOrEmpty(Current.Value?.Url)) - game?.HandleLink(Current.Value.Url); - }, 250); - } }; } @@ -98,7 +85,7 @@ namespace osu.Game.Screens.Menu private void checkForUpdates() { - var request = new GetSystemTitleRequest(); + var request = new GetMenuContentRequest(); Task.Run(() => request.Perform()) .ContinueWith(r => { @@ -121,12 +108,12 @@ namespace osu.Game.Screens.Menu cancellationTokenSource = null; currentImage?.FadeOut(500, Easing.OutQuint).Expire(); - if (string.IsNullOrEmpty(Current.Value?.Image)) + if (Current.Value.Images.Length == 0) return; - LoadComponentAsync(new SystemTitleImage(Current.Value), loaded => + LoadComponentAsync(new MenuImage(Current.Value.Images.First()), loaded => { - if (!loaded.SystemTitle.Equals(Current.Value)) + if (!loaded.Image.Equals(Current.Value.Images.First())) loaded.Dispose(); content.Add(currentImage = loaded); @@ -134,22 +121,24 @@ namespace osu.Game.Screens.Menu } [LongRunningLoad] - private partial class SystemTitleImage : CompositeDrawable + private partial class MenuImage : OsuClickableContainer { - public readonly APISystemTitle SystemTitle; + public readonly APIMenuImage Image; private Sprite flash = null!; - public SystemTitleImage(APISystemTitle systemTitle) + private ScheduledDelegate? openUrlAction; + + public MenuImage(APIMenuImage image) { - SystemTitle = systemTitle; + Image = image; } [BackgroundDependencyLoader] - private void load(LargeTextureStore textureStore) + private void load(LargeTextureStore textureStore, OsuGame game) { - Texture? texture = textureStore.Get(SystemTitle.Image); - if (texture != null && SystemTitle.Image.Contains(@"@2x")) + Texture? texture = textureStore.Get(Image.Image); + if (texture != null && Image.Image.Contains(@"@2x")) texture.ScaleAdjust *= 2; AutoSizeAxes = Axes.Both; @@ -163,6 +152,19 @@ namespace osu.Game.Screens.Menu Blending = BlendingParameters.Additive, }, }; + + Action = () => + { + Flash(); + + // Delay slightly to allow animation to play out. + openUrlAction?.Cancel(); + openUrlAction = Scheduler.AddDelayed(() => + { + if (!string.IsNullOrEmpty(Image.Url)) + game?.HandleLink(Image.Url); + }, 250); + }; } protected override void LoadComplete() From 4c82e44291fdf23bf324d1d8f4a3092ede9437da Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 23 Mar 2024 20:07:17 +0800 Subject: [PATCH 02/15] Add isolated test coverage of online menu banner --- .../Visual/Menus/TestSceneMainMenu.cs | 23 ------ .../Visual/Menus/TestSceneOnlineMenuBanner.cs | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+), 23 deletions(-) create mode 100644 osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs diff --git a/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs b/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs index 3c78edb8a5..e2a841d79a 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneMainMenu.cs @@ -32,29 +32,6 @@ namespace osu.Game.Tests.Visual.Menus AddAssert("system title not visible", () => onlineMenuBanner.State.Value, () => Is.EqualTo(Visibility.Hidden)); AddStep("enter menu", () => InputManager.Key(Key.Enter)); AddUntilStep("system title visible", () => onlineMenuBanner.State.Value, () => Is.EqualTo(Visibility.Visible)); - AddStep("set another title", () => onlineMenuBanner.Current.Value = new APIMenuContent - { - Images = new[] - { - new APIMenuImage - { - Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png", - Url = @"https://osu.ppy.sh/community/contests/189", - } - } - }); - AddStep("set title with nonexistent image", () => onlineMenuBanner.Current.Value = new APIMenuContent - { - Images = new[] - { - new APIMenuImage - { - Image = @"https://test.invalid/@2x", // .invalid TLD reserved by https://datatracker.ietf.org/doc/html/rfc2606#section-2 - Url = @"https://osu.ppy.sh/community/contests/189", - } - } - }); - AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); } } } diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs new file mode 100644 index 0000000000..a80212e0a1 --- /dev/null +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -0,0 +1,71 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using NUnit.Framework; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +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 TestSceneOnlineMenuBanner : OsuTestScene + { + private OnlineMenuBanner onlineMenuBanner = null!; + + [SetUpSteps] + public void SetUpSteps() + { + AddStep("Create banner", () => + { + Child = onlineMenuBanner = new OnlineMenuBanner + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + State = { Value = Visibility.Visible } + }; + }); + } + + [Test] + public void TestBasic() + { + AddAssert("system title not visible", () => onlineMenuBanner.State.Value, () => Is.EqualTo(Visibility.Hidden)); + AddStep("set online content", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + 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", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png", + Url = @"https://osu.ppy.sh/community/contests/189", + } + } + }); + AddStep("set title with nonexistent image", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + Image = @"https://test.invalid/@2x", // .invalid TLD reserved by https://datatracker.ietf.org/doc/html/rfc2606#section-2 + Url = @"https://osu.ppy.sh/community/contests/189", + } + } + }); + AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); + } + } +} From ec4a9a5fdd4ffa1df41b2e26227d27d1277ae8d2 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sat, 23 Mar 2024 23:07:31 +0800 Subject: [PATCH 03/15] Make work again for simple case --- .../API/Requests/Responses/APIMenuContent.cs | 12 ++++++--- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 25 +++++++------------ 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs b/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs index acee6c99ba..7b53488030 100644 --- a/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs +++ b/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs @@ -2,6 +2,7 @@ // See the LICENCE file in the repository root for full licence text. using System; +using System.Linq; using Newtonsoft.Json; namespace osu.Game.Online.API.Requests.Responses @@ -14,14 +15,12 @@ namespace osu.Game.Online.API.Requests.Responses [JsonProperty(@"images")] public APIMenuImage[] Images { get; init; } = Array.Empty(); - public DateTimeOffset LastUpdated { get; init; } - public bool Equals(APIMenuContent? other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; - return LastUpdated.Equals(other.LastUpdated); + return Images.SequenceEqual(other.Images); } public override bool Equals(object? obj) @@ -36,7 +35,12 @@ namespace osu.Game.Online.API.Requests.Responses public override int GetHashCode() { - return LastUpdated.GetHashCode(); + var hash = new HashCode(); + + foreach (var image in Images) + hash.Add(image.GetHashCode()); + + return hash.ToHashCode(); } } } diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index cf20196f85..613a6eed4c 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -78,7 +78,7 @@ namespace osu.Game.Screens.Menu { base.LoadComplete(); - Current.BindValueChanged(_ => loadNewImage(), true); + Current.BindValueChanged(_ => loadNewImages(), true); checkForUpdates(); } @@ -102,7 +102,7 @@ namespace osu.Game.Screens.Menu }); } - private void loadNewImage() + private void loadNewImages() { cancellationTokenSource?.Cancel(); cancellationTokenSource = null; @@ -131,19 +131,19 @@ namespace osu.Game.Screens.Menu public MenuImage(APIMenuImage image) { + AutoSizeAxes = Axes.Both; + Image = image; } [BackgroundDependencyLoader] - private void load(LargeTextureStore textureStore, OsuGame game) + private void load(LargeTextureStore textureStore, OsuGame? game) { Texture? texture = textureStore.Get(Image.Image); if (texture != null && Image.Image.Contains(@"@2x")) texture.ScaleAdjust *= 2; - AutoSizeAxes = Axes.Both; - - InternalChildren = new Drawable[] + Children = new Drawable[] { new Sprite { Texture = texture }, flash = new Sprite @@ -155,7 +155,9 @@ namespace osu.Game.Screens.Menu Action = () => { - Flash(); + flash.FadeInFromZero(50) + .Then() + .FadeOut(500, Easing.OutQuint); // Delay slightly to allow animation to play out. openUrlAction?.Cancel(); @@ -174,15 +176,6 @@ namespace osu.Game.Screens.Menu this.FadeInFromZero(500, Easing.OutQuint); flash.FadeOutFromOne(4000, Easing.OutQuint); } - - public Drawable Flash() - { - flash.FadeInFromZero(50) - .Then() - .FadeOut(500, Easing.OutQuint); - - return this; - } } } } From a4c619ea97c190a9a4929d89268677af7b72a985 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sun, 24 Mar 2024 15:14:56 +0800 Subject: [PATCH 04/15] Add basic support for loading multiple images --- .../Visual/Menus/TestSceneOnlineMenuBanner.cs | 21 ++++- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 80 ++++++++++--------- 2 files changed, 64 insertions(+), 37 deletions(-) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs index a80212e0a1..4cc379a18b 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -31,7 +31,6 @@ namespace osu.Game.Tests.Visual.Menus [Test] public void TestBasic() { - AddAssert("system title not visible", () => onlineMenuBanner.State.Value, () => Is.EqualTo(Visibility.Hidden)); AddStep("set online content", () => onlineMenuBanner.Current.Value = new APIMenuContent { Images = new[] @@ -43,6 +42,7 @@ namespace osu.Game.Tests.Visual.Menus } }, }); + AddStep("set another title", () => onlineMenuBanner.Current.Value = new APIMenuContent { Images = new[] @@ -54,6 +54,24 @@ namespace osu.Game.Tests.Visual.Menus } } }); + + AddStep("set multiple images", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + 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", + }, + new APIMenuImage + { + Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png", + Url = @"https://osu.ppy.sh/community/contests/189", + } + }, + }); + AddStep("set title with nonexistent image", () => onlineMenuBanner.Current.Value = new APIMenuContent { Images = new[] @@ -65,6 +83,7 @@ namespace osu.Game.Tests.Visual.Menus } } }); + AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); } } diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 613a6eed4c..587a93fb67 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -27,7 +27,6 @@ namespace osu.Game.Screens.Menu private Container content = null!; private CancellationTokenSource? cancellationTokenSource; - private MenuImage? currentImage; [BackgroundDependencyLoader] private void load() @@ -48,32 +47,6 @@ namespace osu.Game.Screens.Menu protected override void PopOut() => content.FadeOut(transition_duration, Easing.OutQuint); - protected override bool OnHover(HoverEvent e) - { - content.ScaleTo(1.05f, 2000, Easing.OutQuint); - return base.OnHover(e); - } - - protected override void OnHoverLost(HoverLostEvent e) - { - content.ScaleTo(1f, 500, Easing.OutQuint); - base.OnHoverLost(e); - } - - protected override bool OnMouseDown(MouseDownEvent e) - { - content.ScaleTo(0.95f, 500, Easing.OutQuint); - return base.OnMouseDown(e); - } - - protected override void OnMouseUp(MouseUpEvent e) - { - content - .ScaleTo(0.95f) - .ScaleTo(1, 500, Easing.OutElastic); - base.OnMouseUp(e); - } - protected override void LoadComplete() { base.LoadComplete(); @@ -106,17 +79,26 @@ namespace osu.Game.Screens.Menu { cancellationTokenSource?.Cancel(); cancellationTokenSource = null; - currentImage?.FadeOut(500, Easing.OutQuint).Expire(); - if (Current.Value.Images.Length == 0) + var newContent = Current.Value; + + foreach (var i in content) + { + i.FadeOutFromOne(100, Easing.OutQuint) + .Expire(); + } + + if (newContent.Images.Length == 0) return; - LoadComponentAsync(new MenuImage(Current.Value.Images.First()), loaded => + LoadComponentsAsync(newContent.Images.Select(i => new MenuImage(i)), loaded => { - if (!loaded.Image.Equals(Current.Value.Images.First())) - loaded.Dispose(); + if (!newContent.Equals(Current.Value)) + return; - content.Add(currentImage = loaded); + content.AddRange(loaded); + + loaded.First().Show(); }, (cancellationTokenSource ??= new CancellationTokenSource()).Token); } @@ -132,6 +114,8 @@ namespace osu.Game.Screens.Menu public MenuImage(APIMenuImage image) { AutoSizeAxes = Axes.Both; + Anchor = Anchor.BottomCentre; + Origin = Anchor.BottomCentre; Image = image; } @@ -169,13 +153,37 @@ namespace osu.Game.Screens.Menu }; } - protected override void LoadComplete() + public override void Show() { - base.LoadComplete(); - this.FadeInFromZero(500, Easing.OutQuint); flash.FadeOutFromOne(4000, Easing.OutQuint); } + + protected override bool OnHover(HoverEvent e) + { + this.ScaleTo(1.05f, 2000, Easing.OutQuint); + return true; + } + + protected override void OnHoverLost(HoverLostEvent e) + { + this.ScaleTo(1f, 500, Easing.OutQuint); + base.OnHoverLost(e); + } + + protected override bool OnMouseDown(MouseDownEvent e) + { + this.ScaleTo(0.95f, 500, Easing.OutQuint); + return true; + } + + protected override void OnMouseUp(MouseUpEvent e) + { + this + .ScaleTo(0.95f) + .ScaleTo(1, 500, Easing.OutElastic); + base.OnMouseUp(e); + } } } } From d0b164b44f6d986f965c469cd838bfacbe0a3de6 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Sun, 24 Mar 2024 23:37:30 +0800 Subject: [PATCH 05/15] Add automatic rotation support --- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 38 +++++++++++++++++++---- 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 587a93fb67..a4648265ae 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -28,6 +28,10 @@ namespace osu.Game.Screens.Menu private Container content = null!; private CancellationTokenSource? cancellationTokenSource; + private int displayIndex = -1; + + private ScheduledDelegate? nextDisplay; + [BackgroundDependencyLoader] private void load() { @@ -77,16 +81,16 @@ namespace osu.Game.Screens.Menu private void loadNewImages() { + nextDisplay?.Cancel(); + cancellationTokenSource?.Cancel(); cancellationTokenSource = null; var newContent = Current.Value; - foreach (var i in content) - { - i.FadeOutFromOne(100, Easing.OutQuint) - .Expire(); - } + // A better fade out would be nice, but the menu content changes *very* rarely + // so let's keep things simple for now. + content.Clear(true); if (newContent.Images.Length == 0) return; @@ -96,12 +100,34 @@ namespace osu.Game.Screens.Menu if (!newContent.Equals(Current.Value)) return; + // start hidden + foreach (var image in loaded) + image.Hide(); + content.AddRange(loaded); - loaded.First().Show(); + displayIndex = -1; + showNext(); }, (cancellationTokenSource ??= new CancellationTokenSource()).Token); } + private void showNext() + { + nextDisplay?.Cancel(); + + bool previousShowing = displayIndex >= 0; + if (previousShowing) + content[displayIndex % content.Count].FadeOut(400, Easing.OutQuint); + + displayIndex++; + + using (BeginDelayedSequence(previousShowing ? 300 : 0)) + content[displayIndex % content.Count].Show(); + + if (content.Count > 1) + nextDisplay = Scheduler.AddDelayed(showNext, 12000); + } + [LongRunningLoad] private partial class MenuImage : OsuClickableContainer { From 3847aae57d88ea2156c7599c53a0e994fffd4760 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Mar 2024 12:14:40 +0800 Subject: [PATCH 06/15] Don't rotate when hovering --- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 26 ++++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index a4648265ae..74062d5b9f 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -21,6 +21,8 @@ namespace osu.Game.Screens.Menu { public partial class OnlineMenuBanner : VisibilityContainer { + public double DelayBetweenRotation = 7500; + internal Bindable Current { get; } = new Bindable(new APIMenuContent()); private const float transition_duration = 500; @@ -115,21 +117,29 @@ namespace osu.Game.Screens.Menu { nextDisplay?.Cancel(); - bool previousShowing = displayIndex >= 0; - if (previousShowing) - content[displayIndex % content.Count].FadeOut(400, Easing.OutQuint); + // If the user is hovering a banner, don't rotate yet. + bool anyHovered = content.Any(i => i.IsHovered); - displayIndex++; + if (!anyHovered) + { + bool previousShowing = displayIndex >= 0; + if (previousShowing) + content[displayIndex % content.Count].FadeOut(400, Easing.OutQuint); - using (BeginDelayedSequence(previousShowing ? 300 : 0)) - content[displayIndex % content.Count].Show(); + displayIndex++; + + using (BeginDelayedSequence(previousShowing ? 300 : 0)) + content[displayIndex % content.Count].Show(); + } if (content.Count > 1) - nextDisplay = Scheduler.AddDelayed(showNext, 12000); + { + nextDisplay = Scheduler.AddDelayed(showNext, DelayBetweenRotation); + } } [LongRunningLoad] - private partial class MenuImage : OsuClickableContainer + public partial class MenuImage : OsuClickableContainer { public readonly APIMenuImage Image; From e9f15534ed7ea64b7cfe77360f488b3a63e703db Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Mar 2024 12:14:47 +0800 Subject: [PATCH 07/15] Improve test coverage --- .../Visual/Menus/TestSceneOnlineMenuBanner.cs | 75 ++++++++++++++++--- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 7 +- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs index 4cc379a18b..6be5b80983 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . 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.Graphics; using osu.Framework.Graphics.Containers; @@ -21,6 +22,8 @@ namespace osu.Game.Tests.Visual.Menus { Child = onlineMenuBanner = new OnlineMenuBanner { + FetchOnlineContent = false, + DelayBetweenRotation = 500, Anchor = Anchor.Centre, Origin = Anchor.Centre, State = { Value = Visibility.Visible } @@ -43,6 +46,18 @@ namespace osu.Game.Tests.Visual.Menus }, }); + AddUntilStep("wait for one image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 1) + return false; + + var image = images.Single(); + + return image.IsPresent && image.Image.Url == "https://osu.ppy.sh/home/news/2023-12-21-project-loved-december-2023"; + }); + AddStep("set another title", () => onlineMenuBanner.Current.Value = new APIMenuContent { Images = new[] @@ -55,6 +70,40 @@ namespace osu.Game.Tests.Visual.Menus } }); + AddUntilStep("wait for new image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 1) + return false; + + var image = images.Single(); + + return image.IsPresent && image.Image.Url == "https://osu.ppy.sh/community/contests/189"; + }); + + AddStep("set title with nonexistent image", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + Image = @"https://test.invalid/@2x", // .invalid TLD reserved by https://datatracker.ietf.org/doc/html/rfc2606#section-2 + Url = @"https://osu.ppy.sh/community/contests/189", + } + } + }); + + AddUntilStep("wait for no image shown", () => !onlineMenuBanner.ChildrenOfType().Any()); + + AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); + + AddUntilStep("wait for no image shown", () => !onlineMenuBanner.ChildrenOfType().Any()); + } + + [Test] + public void TestMultipleImages() + { AddStep("set multiple images", () => onlineMenuBanner.Current.Value = new APIMenuContent { Images = new[] @@ -72,19 +121,25 @@ namespace osu.Game.Tests.Visual.Menus }, }); - AddStep("set title with nonexistent image", () => onlineMenuBanner.Current.Value = new APIMenuContent + AddUntilStep("wait for first image shown", () => { - Images = new[] - { - new APIMenuImage - { - Image = @"https://test.invalid/@2x", // .invalid TLD reserved by https://datatracker.ietf.org/doc/html/rfc2606#section-2 - Url = @"https://osu.ppy.sh/community/contests/189", - } - } + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 2) + return false; + + return images.First().IsPresent && !images.Last().IsPresent; }); - AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); + AddUntilStep("wait for second image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 2) + return false; + + return !images.First().IsPresent && images.Last().IsPresent; + }); } } } diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 74062d5b9f..2ab6417370 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -21,7 +21,9 @@ namespace osu.Game.Screens.Menu { public partial class OnlineMenuBanner : VisibilityContainer { - public double DelayBetweenRotation = 7500; + public double DelayBetweenRotation { get; set; } = 7500; + + public bool FetchOnlineContent { get; set; } = true; internal Bindable Current { get; } = new Bindable(new APIMenuContent()); @@ -64,6 +66,9 @@ namespace osu.Game.Screens.Menu private void checkForUpdates() { + if (!FetchOnlineContent) + return; + var request = new GetMenuContentRequest(); Task.Run(() => request.Perform()) .ContinueWith(r => From f0614928b182bf2d575c5e298e29d2254fd28ecb Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Mar 2024 13:19:12 +0800 Subject: [PATCH 08/15] Read from new location --- osu.Game/Online/API/Requests/GetMenuContentRequest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Online/API/Requests/GetMenuContentRequest.cs b/osu.Game/Online/API/Requests/GetMenuContentRequest.cs index ad2bac6696..26747489d6 100644 --- a/osu.Game/Online/API/Requests/GetMenuContentRequest.cs +++ b/osu.Game/Online/API/Requests/GetMenuContentRequest.cs @@ -8,7 +8,7 @@ namespace osu.Game.Online.API.Requests public class GetMenuContentRequest : OsuJsonWebRequest { public GetMenuContentRequest() - : base(@"https://assets.ppy.sh/lazer-status.json") + : base(@"https://assets.ppy.sh/menu-content.json") { } } From 057f86dd145dd5d0ba573737ca61aceb27dcc70a Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Mar 2024 14:28:23 +0800 Subject: [PATCH 09/15] Add handling of expiration --- .../Visual/Menus/TestSceneOnlineMenuBanner.cs | 60 +++++++++++++++++++ .../API/Requests/Responses/APIMenuImage.cs | 4 ++ osu.Game/Screens/Menu/OnlineMenuBanner.cs | 60 ++++++++++++------- 3 files changed, 102 insertions(+), 22 deletions(-) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs index 6be5b80983..2dd08ce306 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -1,6 +1,7 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using System.Linq; using NUnit.Framework; using osu.Framework.Graphics; @@ -141,5 +142,64 @@ namespace osu.Game.Tests.Visual.Menus return !images.First().IsPresent && images.Last().IsPresent; }); } + + [Test] + public void TestExpiry() + { + AddStep("set multiple images, second expiring soon", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + 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", + }, + new APIMenuImage + { + Image = @"https://assets.ppy.sh/main-menu/wf2023-vote@2x.png", + Url = @"https://osu.ppy.sh/community/contests/189", + Expires = DateTimeOffset.Now.AddSeconds(2), + } + }, + }); + + AddUntilStep("wait for first image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 2) + return false; + + return images.First().IsPresent && !images.Last().IsPresent; + }); + + AddUntilStep("wait for second image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 2) + return false; + + return !images.First().IsPresent && images.Last().IsPresent; + }); + + AddUntilStep("wait for expiry", () => + { + return onlineMenuBanner + .ChildrenOfType() + .Any(i => !i.Image.IsCurrent); + }); + + AddUntilStep("wait for first image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 2) + return false; + + return images.First().IsPresent && !images.Last().IsPresent; + }); + } } } diff --git a/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs b/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs index 4824e23d4b..42129ca96e 100644 --- a/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs +++ b/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs @@ -20,6 +20,10 @@ namespace osu.Game.Online.API.Requests.Responses [JsonProperty(@"url")] public string Url { get; init; } = string.Empty; + public bool IsCurrent => + (Begins == null || Begins < DateTimeOffset.UtcNow) && + (Expires == null || Expires > DateTimeOffset.UtcNow); + /// /// The time at which this item should begin displaying. If null, will display immediately. /// diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 2ab6417370..55ceb84d7b 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -29,7 +29,7 @@ namespace osu.Game.Screens.Menu private const float transition_duration = 500; - private Container content = null!; + private Container content = null!; private CancellationTokenSource? cancellationTokenSource; private int displayIndex = -1; @@ -43,7 +43,7 @@ namespace osu.Game.Screens.Menu AutoSizeDuration = transition_duration; AutoSizeEasing = Easing.OutQuint; - InternalChild = content = new Container + InternalChild = content = new Container { Anchor = Anchor.Centre, Origin = Anchor.Centre, @@ -59,8 +59,7 @@ namespace osu.Game.Screens.Menu { base.LoadComplete(); - Current.BindValueChanged(_ => loadNewImages(), true); - + Current.BindValueChanged(loadNewImages, true); checkForUpdates(); } @@ -86,25 +85,24 @@ namespace osu.Game.Screens.Menu }); } - private void loadNewImages() + /// + /// Takes and materialises and displays drawables for all valid images to be displayed. + /// + /// + private void loadNewImages(ValueChangedEvent images) { nextDisplay?.Cancel(); cancellationTokenSource?.Cancel(); cancellationTokenSource = null; - var newContent = Current.Value; - // A better fade out would be nice, but the menu content changes *very* rarely // so let's keep things simple for now. content.Clear(true); - if (newContent.Images.Length == 0) - return; - - LoadComponentsAsync(newContent.Images.Select(i => new MenuImage(i)), loaded => + LoadComponentsAsync(images.NewValue.Images.Select(i => new MenuImage(i)), loaded => { - if (!newContent.Equals(Current.Value)) + if (!images.NewValue.Equals(Current.Value)) return; // start hidden @@ -127,20 +125,38 @@ namespace osu.Game.Screens.Menu if (!anyHovered) { - bool previousShowing = displayIndex >= 0; - if (previousShowing) - content[displayIndex % content.Count].FadeOut(400, Easing.OutQuint); + int previousIndex = displayIndex; - displayIndex++; + if (displayIndex == -1) + displayIndex = 0; - using (BeginDelayedSequence(previousShowing ? 300 : 0)) - content[displayIndex % content.Count].Show(); + // To handle expiration simply, arrange all images in best-next order. + // Fade in the first valid one, then handle fading out the last if required. + var currentRotation = content + .Skip(displayIndex + 1) + .Concat(content.Take(displayIndex + 1)); + + foreach (var image in currentRotation) + { + if (!image.Image.IsCurrent) continue; + + using (BeginDelayedSequence(previousIndex >= 0 ? 300 : 0)) + { + displayIndex = content.IndexOf(image); + + if (displayIndex != previousIndex) + image.Show(); + + break; + } + } + + if (previousIndex >= 0 && previousIndex != displayIndex) + content[previousIndex].FadeOut(400, Easing.OutQuint); } - if (content.Count > 1) - { - nextDisplay = Scheduler.AddDelayed(showNext, DelayBetweenRotation); - } + // Re-scheduling this method will both handle rotation and re-checking for expiration dates. + nextDisplay = Scheduler.AddDelayed(showNext, DelayBetweenRotation); } [LongRunningLoad] From bb9fa52fda51f46e4e0fe39e2be8196ab0dc0fba Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 25 Mar 2024 14:53:05 +0800 Subject: [PATCH 10/15] Fix `displayIndex` not being correctly set to `-1` after last expiry date --- .../Visual/Menus/TestSceneOnlineMenuBanner.cs | 36 ++++++++++++++++++- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 11 +++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs index 2dd08ce306..380085ce04 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -144,7 +144,41 @@ namespace osu.Game.Tests.Visual.Menus } [Test] - public void TestExpiry() + public void TestFutureSingle() + { + AddStep("set image with time constraints", () => onlineMenuBanner.Current.Value = new APIMenuContent + { + Images = new[] + { + new APIMenuImage + { + 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", + Begins = DateTimeOffset.Now.AddSeconds(2), + Expires = DateTimeOffset.Now.AddSeconds(5), + }, + }, + }); + + AddUntilStep("wait for no image shown", () => !onlineMenuBanner.ChildrenOfType().Any(i => i.IsPresent)); + + AddUntilStep("wait for one image shown", () => + { + var images = onlineMenuBanner.ChildrenOfType(); + + if (images.Count() != 1) + return false; + + var image = images.Single(); + + return image.IsPresent && image.Image.Url == "https://osu.ppy.sh/home/news/2023-12-21-project-loved-december-2023"; + }); + + AddUntilStep("wait for no image shown", () => !onlineMenuBanner.ChildrenOfType().Any(i => i.IsPresent)); + } + + [Test] + public void TestExpiryMultiple() { AddStep("set multiple images, second expiring soon", () => onlineMenuBanner.Current.Value = new APIMenuContent { diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 55ceb84d7b..37bec7aa63 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -127,14 +127,15 @@ namespace osu.Game.Screens.Menu { int previousIndex = displayIndex; - if (displayIndex == -1) - displayIndex = 0; - // To handle expiration simply, arrange all images in best-next order. // Fade in the first valid one, then handle fading out the last if required. var currentRotation = content - .Skip(displayIndex + 1) - .Concat(content.Take(displayIndex + 1)); + .Skip(Math.Max(0, previousIndex) + 1) + .Concat(content.Take(Math.Max(0, previousIndex) + 1)); + + // After the loop, displayIndex will be the new valid index or -1 if + // none valid. + displayIndex = -1; foreach (var image in currentRotation) { From 9474156df4bb33bda65c0bd2d68d565825f1b3d1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 26 Mar 2024 20:21:12 +0800 Subject: [PATCH 11/15] Improve equality implementations --- .../API/Requests/Responses/APIMenuContent.cs | 10 +--------- .../API/Requests/Responses/APIMenuImage.cs | 17 +++++------------ 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs b/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs index 7b53488030..6aad0f6c87 100644 --- a/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs +++ b/osu.Game/Online/API/Requests/Responses/APIMenuContent.cs @@ -23,15 +23,7 @@ namespace osu.Game.Online.API.Requests.Responses return Images.SequenceEqual(other.Images); } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - - if (obj.GetType() != GetType()) return false; - - return Equals((APIMenuContent)obj); - } + public override bool Equals(object? other) => other is APIMenuContent content && Equals(content); public override int GetHashCode() { diff --git a/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs b/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs index 42129ca96e..8aff08099a 100644 --- a/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs +++ b/osu.Game/Online/API/Requests/Responses/APIMenuImage.cs @@ -28,34 +28,27 @@ namespace osu.Game.Online.API.Requests.Responses /// The time at which this item should begin displaying. If null, will display immediately. /// [JsonProperty(@"begins")] - public DateTimeOffset? Begins { get; set; } + public DateTimeOffset? Begins { get; init; } /// /// The time at which this item should stop displaying. If null, will display indefinitely. /// [JsonProperty(@"expires")] - public DateTimeOffset? Expires { get; set; } + public DateTimeOffset? Expires { get; init; } public bool Equals(APIMenuImage? other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; - return Image == other.Image && Url == other.Url; + return Image == other.Image && Url == other.Url && Begins == other.Begins && Expires == other.Expires; } - public override bool Equals(object? obj) - { - if (ReferenceEquals(null, obj)) return false; - if (ReferenceEquals(this, obj)) return true; - if (obj.GetType() != GetType()) return false; - - return Equals((APIMenuImage)obj); - } + public override bool Equals(object? other) => other is APIMenuImage content && Equals(content); public override int GetHashCode() { - return HashCode.Combine(Image, Url); + return HashCode.Combine(Image, Url, Begins, Expires); } } } From fd649edabae36603cccfc8a847a81de05b32f257 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 26 Mar 2024 20:21:48 +0800 Subject: [PATCH 12/15] Also don't rotate images during a drag operation --- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 37bec7aa63..260c021719 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -120,8 +120,8 @@ namespace osu.Game.Screens.Menu { nextDisplay?.Cancel(); - // If the user is hovering a banner, don't rotate yet. - bool anyHovered = content.Any(i => i.IsHovered); + // If the user is interacting with a banner, don't rotate yet. + bool anyHovered = content.Any(i => i.IsHovered || i.IsDragged); if (!anyHovered) { @@ -242,6 +242,8 @@ namespace osu.Game.Screens.Menu .ScaleTo(1, 500, Easing.OutElastic); base.OnMouseUp(e); } + + protected override bool OnDragStart(DragStartEvent e) => true; } } } From e77d4c8cfaf66e1e717b0094632a3704b77e73f0 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 26 Mar 2024 20:28:03 +0800 Subject: [PATCH 13/15] Remove unnecessary `Math.Max` --- osu.Game/Screens/Menu/OnlineMenuBanner.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/osu.Game/Screens/Menu/OnlineMenuBanner.cs b/osu.Game/Screens/Menu/OnlineMenuBanner.cs index 260c021719..6f98b73939 100644 --- a/osu.Game/Screens/Menu/OnlineMenuBanner.cs +++ b/osu.Game/Screens/Menu/OnlineMenuBanner.cs @@ -130,8 +130,8 @@ namespace osu.Game.Screens.Menu // To handle expiration simply, arrange all images in best-next order. // Fade in the first valid one, then handle fading out the last if required. var currentRotation = content - .Skip(Math.Max(0, previousIndex) + 1) - .Concat(content.Take(Math.Max(0, previousIndex) + 1)); + .Skip(previousIndex + 1) + .Concat(content.Take(previousIndex + 1)); // After the loop, displayIndex will be the new valid index or -1 if // none valid. From dee88573a756f9652628d13f2033cd4b78244870 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Tue, 26 Mar 2024 13:44:12 +0100 Subject: [PATCH 14/15] Fix test failure in visual browser I'm not sure why it's failing headless and I'm not particularly interested in finding that out right now. --- osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs index 380085ce04..60e42838d8 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -9,6 +9,7 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Testing; using osu.Game.Online.API.Requests.Responses; using osu.Game.Screens.Menu; +using osuTK; namespace osu.Game.Tests.Visual.Menus { @@ -95,7 +96,7 @@ namespace osu.Game.Tests.Visual.Menus } }); - AddUntilStep("wait for no image shown", () => !onlineMenuBanner.ChildrenOfType().Any()); + AddUntilStep("wait for no image shown", () => onlineMenuBanner.ChildrenOfType().Single().Size, () => Is.EqualTo(Vector2.Zero)); AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent()); From b4ccbc68e447f0dba68470d70510195bfad009f5 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Tue, 26 Mar 2024 21:20:22 +0800 Subject: [PATCH 15/15] Fix failing test --- osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs index 60e42838d8..0b90fd13c3 100644 --- a/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs +++ b/osu.Game.Tests/Visual/Menus/TestSceneOnlineMenuBanner.cs @@ -96,7 +96,7 @@ namespace osu.Game.Tests.Visual.Menus } }); - AddUntilStep("wait for no image shown", () => onlineMenuBanner.ChildrenOfType().Single().Size, () => Is.EqualTo(Vector2.Zero)); + AddUntilStep("wait for no image shown", () => onlineMenuBanner.ChildrenOfType().SingleOrDefault()?.Size, () => Is.EqualTo(Vector2.Zero)); AddStep("unset system title", () => onlineMenuBanner.Current.Value = new APIMenuContent());