1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 21:27:24 +08:00

Prepare tests and general structure to support omission of pages

This commit is contained in:
Dean Herbert 2022-01-04 18:46:44 +09:00
parent e75c9519f3
commit 86f72b71b1
3 changed files with 52 additions and 64 deletions

View File

@ -1,9 +1,11 @@
// 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.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<PageSelectorPageButton>().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);
}
}

View File

@ -1,19 +1,20 @@
// 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 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<PageSelectorPageButton> 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<PageSelectorPageButton>
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<int> 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<PageSelectorPageButton>())
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,
});
}
}

View File

@ -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]