mirror of
https://github.com/osukey/osukey.git
synced 2025-08-05 23:53:51 +09:00
Make MaxPages value a bindable
This commit is contained in:
@ -15,35 +15,40 @@ namespace osu.Game.Tests.Visual.UserInterface
|
||||
typeof(PageSelector)
|
||||
};
|
||||
|
||||
private readonly PageSelector pageSelector;
|
||||
|
||||
public TestScenePageSelector()
|
||||
{
|
||||
Child = new PageSelector(200)
|
||||
Child = pageSelector = new PageSelector
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
};
|
||||
|
||||
AddStep("1 max pages", () => redraw(1));
|
||||
AddStep("10 max pages", () => redraw(10));
|
||||
AddStep("200 max pages, current 199", () => redraw(200, 199));
|
||||
AddStep("200 max pages, current 201", () => redraw(200, 201));
|
||||
AddStep("200 max pages, current -10", () => redraw(200, -10));
|
||||
AddStep("10 max pages", () => setMaxPages(10));
|
||||
AddStep("200 max pages, current 199", () =>
|
||||
{
|
||||
setMaxPages(200);
|
||||
setCurrentPage(199);
|
||||
});
|
||||
AddStep("200 max pages, current 201", () =>
|
||||
{
|
||||
setMaxPages(200);
|
||||
setCurrentPage(201);
|
||||
});
|
||||
AddStep("200 max pages, current -10", () =>
|
||||
{
|
||||
setMaxPages(200);
|
||||
setCurrentPage(-10);
|
||||
});
|
||||
AddStep("-10 max pages", () =>
|
||||
{
|
||||
setMaxPages(-10);
|
||||
});
|
||||
}
|
||||
|
||||
private void redraw(int maxPages, int currentPage = 0)
|
||||
{
|
||||
Clear();
|
||||
private void setMaxPages(int maxPages) => pageSelector.MaxPages.Value = maxPages;
|
||||
|
||||
var selector = new PageSelector(maxPages)
|
||||
{
|
||||
Anchor = Anchor.Centre,
|
||||
Origin = Anchor.Centre,
|
||||
};
|
||||
|
||||
if (currentPage != 0)
|
||||
selector.CurrentPage.Value = currentPage;
|
||||
|
||||
Add(selector);
|
||||
}
|
||||
private void setCurrentPage(int currentPage) => pageSelector.CurrentPage.Value = currentPage;
|
||||
}
|
||||
}
|
||||
|
@ -20,17 +20,15 @@ namespace osu.Game.Graphics.UserInterface
|
||||
public class PageSelector : CompositeDrawable
|
||||
{
|
||||
public readonly BindableInt CurrentPage = new BindableInt(1);
|
||||
public readonly BindableInt MaxPages = new BindableInt(1);
|
||||
|
||||
private readonly int maxPages;
|
||||
private readonly FillFlowContainer itemsFlow;
|
||||
|
||||
private readonly Button previousPageButton;
|
||||
private readonly Button nextPageButton;
|
||||
|
||||
public PageSelector(int maxPages)
|
||||
public PageSelector()
|
||||
{
|
||||
this.maxPages = maxPages;
|
||||
|
||||
AutoSizeAxes = Axes.Both;
|
||||
InternalChild = new FillFlowContainer
|
||||
{
|
||||
@ -59,24 +57,48 @@ namespace osu.Game.Graphics.UserInterface
|
||||
{
|
||||
base.LoadComplete();
|
||||
|
||||
CurrentPage.BindValueChanged(_ => redraw(), true);
|
||||
MaxPages.BindValueChanged(pagesAmount => onMaxPagesChanged(pagesAmount.NewValue), true);
|
||||
CurrentPage.BindValueChanged(page => onCurrentPageChanged(page.NewValue), true);
|
||||
}
|
||||
|
||||
private void redraw()
|
||||
private void onMaxPagesChanged(int pagesAmount)
|
||||
{
|
||||
if (CurrentPage.Value > maxPages)
|
||||
if (pagesAmount < 1)
|
||||
{
|
||||
CurrentPage.Value = maxPages;
|
||||
MaxPages.Value = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
if (CurrentPage.Value < 1)
|
||||
if (CurrentPage.Value > pagesAmount)
|
||||
{
|
||||
CurrentPage.Value = pagesAmount;
|
||||
return;
|
||||
}
|
||||
|
||||
redraw();
|
||||
}
|
||||
|
||||
private void onCurrentPageChanged(int newPage)
|
||||
{
|
||||
if (newPage > MaxPages.Value)
|
||||
{
|
||||
CurrentPage.Value = MaxPages.Value;
|
||||
return;
|
||||
}
|
||||
|
||||
if (newPage < 1)
|
||||
{
|
||||
CurrentPage.Value = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
redraw();
|
||||
}
|
||||
|
||||
private void redraw()
|
||||
{
|
||||
int newPage = CurrentPage.Value;
|
||||
int maxPages = MaxPages.Value;
|
||||
|
||||
previousPageButton.Enabled.Value = newPage != 1;
|
||||
nextPageButton.Enabled.Value = newPage != maxPages;
|
||||
|
Reference in New Issue
Block a user