diff --git a/osu.Game.Tests/Visual/UserInterface/TestScenePageSelector.cs b/osu.Game.Tests/Visual/UserInterface/TestScenePageSelector.cs index 1595a7f22d..3b32a2a0dc 100644 --- a/osu.Game.Tests/Visual/UserInterface/TestScenePageSelector.cs +++ b/osu.Game.Tests/Visual/UserInterface/TestScenePageSelector.cs @@ -1,9 +1,11 @@ // 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.Allocation; using osu.Framework.Graphics; +using osu.Framework.Testing; using osu.Game.Graphics.UserInterface.PageSelector; using osu.Game.Overlays; @@ -28,36 +30,38 @@ namespace osu.Game.Tests.Visual.UserInterface }); } + [Test] + public void TestOmittedPages() + { + setAvailablePages(100); + + AddAssert("Correct page buttons", () => pageSelector.ChildrenOfType().Select(p => p.PageNumber).SequenceEqual(new[] { 1, 2, 3, 100 })); + } + [Test] public void TestResetCurrentPage() { - AddStep("Set 10 pages", () => setMaxPages(10)); - AddStep("Select page 5", () => setCurrentPage(5)); - AddStep("Set 11 pages", () => setMaxPages(11)); - AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 1); + setAvailablePages(10); + selectPage(6); + setAvailablePages(11); + AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 0); } [Test] public void TestOutOfBoundsSelection() { - AddStep("Set 10 pages", () => setMaxPages(10)); - AddStep("Select page 11", () => setCurrentPage(11)); - AddAssert("Page 10 is current", () => pageSelector.CurrentPage.Value == pageSelector.MaxPages.Value); + setAvailablePages(10); + selectPage(11); + AddAssert("Page 10 is current", () => pageSelector.CurrentPage.Value == pageSelector.AvailablePages.Value - 1); - AddStep("Select page -1", () => setCurrentPage(-1)); - AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 1); + selectPage(-1); + AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 0); } - [Test] - public void TestNegativeMaxPages() - { - AddStep("Set -10 pages", () => setMaxPages(-10)); - AddAssert("Page 1 is current", () => pageSelector.CurrentPage.Value == 1); - AddAssert("Max is 1", () => pageSelector.MaxPages.Value == 1); - } + private void selectPage(int pageIndex) => + AddStep($"Select page {pageIndex}", () => pageSelector.CurrentPage.Value = pageIndex); - private void setMaxPages(int maxPages) => pageSelector.MaxPages.Value = maxPages; - - private void setCurrentPage(int currentPage) => pageSelector.CurrentPage.Value = currentPage; + private void setAvailablePages(int availablePages) => + AddStep($"Set available pages to {availablePages}", () => pageSelector.AvailablePages.Value = availablePages); } } diff --git a/osu.Game/Graphics/UserInterface/PageSelector/PageSelector.cs b/osu.Game/Graphics/UserInterface/PageSelector/PageSelector.cs index 4089fb5511..ae644bb852 100644 --- a/osu.Game/Graphics/UserInterface/PageSelector/PageSelector.cs +++ b/osu.Game/Graphics/UserInterface/PageSelector/PageSelector.cs @@ -1,19 +1,20 @@ // 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 osu.Framework.Graphics.Containers; using osu.Framework.Graphics; using osu.Framework.Bindables; -using osu.Framework.Extensions.IEnumerableExtensions; namespace osu.Game.Graphics.UserInterface.PageSelector { public class PageSelector : CompositeDrawable { - public readonly BindableInt CurrentPage = new BindableInt(1); - public readonly BindableInt MaxPages = new BindableInt(1); + public readonly BindableInt CurrentPage = new BindableInt { MinValue = 0, }; - private readonly FillFlowContainer itemsFlow; + public readonly BindableInt AvailablePages = new BindableInt(1) { MinValue = 1, }; + + private readonly FillFlowContainer itemsFlow; private readonly PageSelectorPrevNextButton previousPageButton; private readonly PageSelectorPrevNextButton nextPageButton; @@ -32,7 +33,7 @@ namespace osu.Game.Graphics.UserInterface.PageSelector { Action = () => CurrentPage.Value -= 1, }, - itemsFlow = new FillFlowContainer + itemsFlow = new FillFlowContainer { AutoSizeAxes = Axes.Both, Direction = FillDirection.Horizontal, @@ -49,60 +50,43 @@ namespace osu.Game.Graphics.UserInterface.PageSelector { base.LoadComplete(); - MaxPages.BindValueChanged(_ => redraw()); - CurrentPage.BindValueChanged(page => onCurrentPageChanged(page.NewValue)); - redraw(); + CurrentPage.BindValueChanged(onCurrentPageChanged); + AvailablePages.BindValueChanged(_ => redraw(), true); } - private void onCurrentPageChanged(int newPage) + private void onCurrentPageChanged(ValueChangedEvent currentPage) { - if (newPage < 1) + if (currentPage.NewValue >= AvailablePages.Value) { - CurrentPage.Value = 1; + CurrentPage.Value = AvailablePages.Value - 1; return; } - if (newPage > MaxPages.Value) - { - CurrentPage.Value = MaxPages.Value; - return; - } + foreach (var page in itemsFlow.OfType()) + page.Selected = page.PageNumber == currentPage.NewValue + 1; - itemsFlow.ForEach(page => page.Selected = page.Page == newPage); - updateButtonsState(); + previousPageButton.Enabled.Value = currentPage.NewValue != 0; + nextPageButton.Enabled.Value = currentPage.NewValue < AvailablePages.Value - 1; } private void redraw() { itemsFlow.Clear(); - if (MaxPages.Value < 1) + for (int i = 0; i < AvailablePages.Value; i++) { - MaxPages.Value = 1; - return; + int pageIndex = i; + + itemsFlow.Add(new PageSelectorPageButton(pageIndex + 1) + { + Action = () => CurrentPage.Value = pageIndex, + }); } - for (int i = 1; i <= MaxPages.Value; i++) - addDrawablePage(i); - - if (CurrentPage.Value == 1) - CurrentPage.TriggerChange(); + if (CurrentPage.Value != 0) + CurrentPage.Value = 0; else - CurrentPage.Value = 1; + CurrentPage.TriggerChange(); } - - private void updateButtonsState() - { - int newPage = CurrentPage.Value; - int maxPages = MaxPages.Value; - - previousPageButton.Enabled.Value = newPage != 1; - nextPageButton.Enabled.Value = newPage != maxPages; - } - - private void addDrawablePage(int page) => itemsFlow.Add(new PageSelectorPageButton(page) - { - Action = () => CurrentPage.Value = page, - }); } } diff --git a/osu.Game/Graphics/UserInterface/PageSelector/PageSelectorPageButton.cs b/osu.Game/Graphics/UserInterface/PageSelector/PageSelectorPageButton.cs index 6aac75565e..247a003492 100644 --- a/osu.Game/Graphics/UserInterface/PageSelector/PageSelectorPageButton.cs +++ b/osu.Game/Graphics/UserInterface/PageSelector/PageSelectorPageButton.cs @@ -17,13 +17,13 @@ namespace osu.Game.Graphics.UserInterface.PageSelector set => selected.Value = value; } - public int Page { get; } + public int PageNumber { get; } private OsuSpriteText text; - public PageSelectorPageButton(int page) + public PageSelectorPageButton(int pageNumber) { - Page = page; + PageNumber = pageNumber; Action = () => { @@ -35,7 +35,7 @@ namespace osu.Game.Graphics.UserInterface.PageSelector protected override Drawable CreateContent() => text = new OsuSpriteText { Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold), - Text = Page.ToString(), + Text = PageNumber.ToString(), }; [BackgroundDependencyLoader]