diff --git a/osu.Game.Tests/Visual/Online/TestSceneShowMoreButton.cs b/osu.Game.Tests/Visual/Online/TestSceneShowMoreButton.cs new file mode 100644 index 0000000000..bccb263600 --- /dev/null +++ b/osu.Game.Tests/Visual/Online/TestSceneShowMoreButton.cs @@ -0,0 +1,55 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Game.Overlays.Profile.Sections; +using System; +using System.Collections.Generic; +using osu.Framework.Graphics; + +namespace osu.Game.Tests.Visual.Online +{ + public class TestSceneShowMoreButton : OsuTestScene + { + public override IReadOnlyList RequiredTypes => new[] + { + typeof(ShowMoreButton), + }; + + public TestSceneShowMoreButton() + { + ShowMoreButton button = null; + + int fireCount = 0; + + Add(button = new ShowMoreButton + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Action = () => + { + fireCount++; + // ReSharper disable once AccessToModifiedClosure + // ReSharper disable once PossibleNullReferenceException + Scheduler.AddDelayed(() => button.IsLoading = false, 2000); + } + }); + + AddStep("click button", () => button.Click()); + + AddAssert("action fired once", () => fireCount == 1); + AddAssert("is in loading state", () => button.IsLoading); + + AddStep("click button", () => button.Click()); + + AddAssert("action not fired", () => fireCount == 1); + AddAssert("is in loading state", () => button.IsLoading); + + AddUntilStep("wait for loaded", () => !button.IsLoading); + + AddStep("click button", () => button.Click()); + + AddAssert("action fired twice", () => fireCount == 2); + AddAssert("is in loading state", () => button.IsLoading); + } + } +} diff --git a/osu.Game/Graphics/Containers/OsuHoverContainer.cs b/osu.Game/Graphics/Containers/OsuHoverContainer.cs index c4f85926ee..eb2d926424 100644 --- a/osu.Game/Graphics/Containers/OsuHoverContainer.cs +++ b/osu.Game/Graphics/Containers/OsuHoverContainer.cs @@ -24,7 +24,8 @@ namespace osu.Game.Graphics.Containers { Enabled.ValueChanged += e => { - if (!e.NewValue) unhover(); + if (!e.NewValue) + unhover(); }; } @@ -49,7 +50,8 @@ namespace osu.Game.Graphics.Containers private void unhover() { - if (!isHovered) return; + if (!isHovered) + return; isHovered = false; EffectTargets.ForEach(d => d.FadeColour(IdleColour, FADE_DURATION, Easing.OutQuint)); diff --git a/osu.Game/Overlays/Profile/Sections/Beatmaps/PaginatedBeatmapContainer.cs b/osu.Game/Overlays/Profile/Sections/Beatmaps/PaginatedBeatmapContainer.cs index 0fc1398f5d..b6b0e605d7 100644 --- a/osu.Game/Overlays/Profile/Sections/Beatmaps/PaginatedBeatmapContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/Beatmaps/PaginatedBeatmapContainer.cs @@ -29,13 +29,11 @@ namespace osu.Game.Overlays.Profile.Sections.Beatmaps protected override void ShowMore() { - base.ShowMore(); - request = new GetUserBeatmapsRequest(User.Value.Id, type, VisiblePages++ * ItemsPerPage); request.Success += sets => Schedule(() => { - ShowMoreButton.FadeTo(sets.Count == ItemsPerPage ? 1 : 0); - ShowMoreLoading.Hide(); + MoreButton.FadeTo(sets.Count == ItemsPerPage ? 1 : 0); + MoreButton.IsLoading = false; if (!sets.Any() && VisiblePages == 1) { diff --git a/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs b/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs index f2eb32c53b..6085b0bc05 100644 --- a/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/Historical/PaginatedMostPlayedBeatmapContainer.cs @@ -24,13 +24,11 @@ namespace osu.Game.Overlays.Profile.Sections.Historical protected override void ShowMore() { - base.ShowMore(); - request = new GetUserMostPlayedBeatmapsRequest(User.Value.Id, VisiblePages++ * ItemsPerPage); request.Success += beatmaps => Schedule(() => { - ShowMoreButton.FadeTo(beatmaps.Count == ItemsPerPage ? 1 : 0); - ShowMoreLoading.Hide(); + MoreButton.FadeTo(beatmaps.Count == ItemsPerPage ? 1 : 0); + MoreButton.IsLoading = false; if (!beatmaps.Any() && VisiblePages == 1) { diff --git a/osu.Game/Overlays/Profile/Sections/PaginatedContainer.cs b/osu.Game/Overlays/Profile/Sections/PaginatedContainer.cs index 46c65b9db7..8639acfc94 100644 --- a/osu.Game/Overlays/Profile/Sections/PaginatedContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/PaginatedContainer.cs @@ -7,20 +7,17 @@ using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Graphics; -using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; -using osu.Game.Graphics.UserInterface; using osu.Game.Online.API; using osu.Game.Rulesets; using osu.Game.Users; namespace osu.Game.Overlays.Profile.Sections { - public class PaginatedContainer : FillFlowContainer + public abstract class PaginatedContainer : FillFlowContainer { protected readonly FillFlowContainer ItemsContainer; - protected readonly OsuHoverContainer ShowMoreButton; - protected readonly LoadingAnimation ShowMoreLoading; + protected readonly ShowMoreButton MoreButton; protected readonly OsuSpriteText MissingText; protected int VisiblePages; @@ -32,7 +29,7 @@ namespace osu.Game.Overlays.Profile.Sections protected APIRequest RetrievalRequest; protected RulesetStore Rulesets; - public PaginatedContainer(Bindable user, string header, string missing) + protected PaginatedContainer(Bindable user, string header, string missing) { User.BindTo(user); @@ -45,38 +42,27 @@ namespace osu.Game.Overlays.Profile.Sections new OsuSpriteText { Text = header, - Font = OsuFont.GetFont(size: 15, weight: FontWeight.Regular, italics: true), + Font = OsuFont.GetFont(size: 20, weight: FontWeight.Bold), Margin = new MarginPadding { Top = 10, Bottom = 10 }, }, ItemsContainer = new FillFlowContainer { AutoSizeAxes = Axes.Y, RelativeSizeAxes = Axes.X, - Margin = new MarginPadding { Bottom = 10 } + Direction = FillDirection.Vertical, + Spacing = new Vector2(0, 2), }, - ShowMoreButton = new OsuHoverContainer + MoreButton = new ShowMoreButton { + Anchor = Anchor.TopCentre, + Origin = Anchor.TopCentre, Alpha = 0, + Margin = new MarginPadding { Top = 10 }, Action = ShowMore, - AutoSizeAxes = Axes.Both, - Anchor = Anchor.TopCentre, - Origin = Anchor.TopCentre, - Child = new OsuSpriteText - { - Font = OsuFont.GetFont(size: 14), - Text = "show more", - Padding = new MarginPadding { Vertical = 10, Horizontal = 15 }, - } - }, - ShowMoreLoading = new LoadingAnimation - { - Anchor = Anchor.TopCentre, - Origin = Anchor.TopCentre, - Size = new Vector2(14), }, MissingText = new OsuSpriteText { - Font = OsuFont.GetFont(size: 14), + Font = OsuFont.GetFont(size: 15), Text = missing, Alpha = 0, }, @@ -97,16 +83,11 @@ namespace osu.Game.Overlays.Profile.Sections { VisiblePages = 0; ItemsContainer.Clear(); - ShowMoreButton.Hide(); if (e.NewValue != null) ShowMore(); } - protected virtual void ShowMore() - { - ShowMoreLoading.Show(); - ShowMoreButton.Hide(); - } + protected abstract void ShowMore(); } } diff --git a/osu.Game/Overlays/Profile/Sections/Ranks/PaginatedScoreContainer.cs b/osu.Game/Overlays/Profile/Sections/Ranks/PaginatedScoreContainer.cs index 470bed2854..a149cfa12e 100644 --- a/osu.Game/Overlays/Profile/Sections/Ranks/PaginatedScoreContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/Ranks/PaginatedScoreContainer.cs @@ -1,7 +1,6 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Online.API.Requests; using osu.Game.Users; @@ -9,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using osu.Framework.Bindables; +using osu.Framework.Graphics; namespace osu.Game.Overlays.Profile.Sections.Ranks { @@ -31,8 +31,6 @@ namespace osu.Game.Overlays.Profile.Sections.Ranks protected override void ShowMore() { - base.ShowMore(); - request = new GetUserScoresRequest(User.Value.Id, type, VisiblePages++ * ItemsPerPage); request.Success += scores => Schedule(() => { @@ -41,8 +39,8 @@ namespace osu.Game.Overlays.Profile.Sections.Ranks if (!scores.Any() && VisiblePages == 1) { - ShowMoreButton.Hide(); - ShowMoreLoading.Hide(); + MoreButton.Hide(); + MoreButton.IsLoading = false; MissingText.Show(); return; } @@ -63,8 +61,8 @@ namespace osu.Game.Overlays.Profile.Sections.Ranks LoadComponentsAsync(drawableScores, s => { MissingText.Hide(); - ShowMoreButton.FadeTo(scores.Count == ItemsPerPage ? 1 : 0); - ShowMoreLoading.Hide(); + MoreButton.FadeTo(scores.Count == ItemsPerPage ? 1 : 0); + MoreButton.IsLoading = false; ItemsContainer.AddRange(s); }); diff --git a/osu.Game/Overlays/Profile/Sections/Recent/PaginatedRecentActivityContainer.cs b/osu.Game/Overlays/Profile/Sections/Recent/PaginatedRecentActivityContainer.cs index 4b4acb8fbc..b72aec7a44 100644 --- a/osu.Game/Overlays/Profile/Sections/Recent/PaginatedRecentActivityContainer.cs +++ b/osu.Game/Overlays/Profile/Sections/Recent/PaginatedRecentActivityContainer.cs @@ -22,13 +22,11 @@ namespace osu.Game.Overlays.Profile.Sections.Recent protected override void ShowMore() { - base.ShowMore(); - request = new GetUserRecentActivitiesRequest(User.Value.Id, VisiblePages++ * ItemsPerPage); request.Success += activities => Schedule(() => { - ShowMoreButton.FadeTo(activities.Count == ItemsPerPage ? 1 : 0); - ShowMoreLoading.Hide(); + MoreButton.FadeTo(activities.Count == ItemsPerPage ? 1 : 0); + MoreButton.IsLoading = false; if (!activities.Any() && VisiblePages == 1) { diff --git a/osu.Game/Overlays/Profile/Sections/ShowMoreButton.cs b/osu.Game/Overlays/Profile/Sections/ShowMoreButton.cs new file mode 100644 index 0000000000..5ed546c62b --- /dev/null +++ b/osu.Game/Overlays/Profile/Sections/ShowMoreButton.cs @@ -0,0 +1,146 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using osu.Framework.Allocation; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Shapes; +using osu.Framework.Graphics.Sprites; +using osu.Framework.Input.Events; +using osu.Game.Graphics; +using osu.Game.Graphics.Containers; +using osu.Game.Graphics.Sprites; +using osu.Game.Graphics.UserInterface; +using osuTK; +using System.Collections.Generic; + +namespace osu.Game.Overlays.Profile.Sections +{ + public class ShowMoreButton : OsuHoverContainer + { + private const float fade_duration = 200; + + private readonly Box background; + private readonly LoadingAnimation loading; + private readonly FillFlowContainer content; + + protected override IEnumerable EffectTargets => new[] { background }; + + private bool isLoading; + + public bool IsLoading + { + get => isLoading; + set + { + if (isLoading == value) + return; + + isLoading = value; + + Enabled.Value = !isLoading; + + if (value) + { + loading.FadeIn(fade_duration, Easing.OutQuint); + content.FadeOut(fade_duration, Easing.OutQuint); + } + else + { + loading.FadeOut(fade_duration, Easing.OutQuint); + content.FadeIn(fade_duration, Easing.OutQuint); + } + } + } + + public ShowMoreButton() + { + AutoSizeAxes = Axes.Both; + Children = new Drawable[] + { + new CircularContainer + { + Masking = true, + Size = new Vector2(140, 30), + Children = new Drawable[] + { + background = new Box + { + RelativeSizeAxes = Axes.Both, + }, + content = new FillFlowContainer + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + AutoSizeAxes = Axes.Both, + Direction = FillDirection.Horizontal, + Spacing = new Vector2(7), + Children = new Drawable[] + { + new ChevronIcon(), + new OsuSpriteText + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Font = OsuFont.GetFont(size: 12, weight: FontWeight.Bold), + Text = "show more".ToUpper(), + }, + new ChevronIcon(), + } + }, + loading = new LoadingAnimation + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Size = new Vector2(12) + }, + } + } + }; + } + + [BackgroundDependencyLoader] + private void load(OsuColour colors) + { + IdleColour = colors.GreySeafoamDark; + HoverColour = colors.GreySeafoam; + } + + protected override bool OnClick(ClickEvent e) + { + if (!Enabled.Value) + return false; + + try + { + return base.OnClick(e); + } + finally + { + // run afterwards as this will disable this button. + IsLoading = true; + } + } + + private class ChevronIcon : SpriteIcon + { + private const int bottom_margin = 2; + private const int icon_size = 8; + + public ChevronIcon() + { + Anchor = Anchor.Centre; + Origin = Anchor.Centre; + Margin = new MarginPadding { Bottom = bottom_margin }; + Size = new Vector2(icon_size); + Icon = FontAwesome.Solid.ChevronDown; + } + + [BackgroundDependencyLoader] + private void load(OsuColour colors) + { + Colour = colors.Yellow; + } + } + } +}