2019-09-07 12:31:07 +08:00
|
|
|
|
// 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.
|
|
|
|
|
|
2022-01-04 18:05:14 +08:00
|
|
|
|
using System;
|
2019-09-07 12:31:07 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
|
2020-01-05 00:03:39 +08:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface.PageSelector
|
2019-09-07 12:31:07 +08:00
|
|
|
|
{
|
|
|
|
|
public class PageSelector : CompositeDrawable
|
|
|
|
|
{
|
2022-01-04 17:46:44 +08:00
|
|
|
|
public readonly BindableInt CurrentPage = new BindableInt { MinValue = 0, };
|
2019-09-07 12:31:07 +08:00
|
|
|
|
|
2022-01-04 17:46:44 +08:00
|
|
|
|
public readonly BindableInt AvailablePages = new BindableInt(1) { MinValue = 1, };
|
|
|
|
|
|
|
|
|
|
private readonly FillFlowContainer itemsFlow;
|
2019-09-07 12:31:07 +08:00
|
|
|
|
|
2022-01-04 16:52:40 +08:00
|
|
|
|
private readonly PageSelectorPrevNextButton previousPageButton;
|
|
|
|
|
private readonly PageSelectorPrevNextButton nextPageButton;
|
2019-09-08 03:31:08 +08:00
|
|
|
|
|
2019-09-10 06:36:25 +08:00
|
|
|
|
public PageSelector()
|
2019-09-07 12:31:07 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2022-01-04 15:46:42 +08:00
|
|
|
|
|
2019-09-08 03:31:08 +08:00
|
|
|
|
InternalChild = new FillFlowContainer
|
2019-09-07 12:31:07 +08:00
|
|
|
|
{
|
2019-09-08 03:31:08 +08:00
|
|
|
|
AutoSizeAxes = Axes.Both,
|
2019-09-07 12:31:07 +08:00
|
|
|
|
Direction = FillDirection.Horizontal,
|
2019-09-08 03:31:08 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2022-01-04 16:52:40 +08:00
|
|
|
|
previousPageButton = new PageSelectorPrevNextButton(false, "prev")
|
2019-09-08 03:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value -= 1,
|
|
|
|
|
},
|
2022-01-04 17:46:44 +08:00
|
|
|
|
itemsFlow = new FillFlowContainer
|
2019-09-08 03:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
},
|
2022-01-04 16:52:40 +08:00
|
|
|
|
nextPageButton = new PageSelectorPrevNextButton(true, "next")
|
2019-09-08 03:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-07 12:31:07 +08:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2022-01-05 13:53:32 +08:00
|
|
|
|
CurrentPage.BindValueChanged(_ => Scheduler.AddOnce(redraw));
|
|
|
|
|
AvailablePages.BindValueChanged(_ =>
|
|
|
|
|
{
|
|
|
|
|
CurrentPage.Value = 0;
|
|
|
|
|
|
|
|
|
|
// AddOnce as the reset of CurrentPage may also trigger a redraw.
|
|
|
|
|
Scheduler.AddOnce(redraw);
|
|
|
|
|
}, true);
|
2019-09-07 12:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 18:05:14 +08:00
|
|
|
|
private void redraw()
|
2019-09-07 12:31:07 +08:00
|
|
|
|
{
|
2022-01-04 18:05:14 +08:00
|
|
|
|
if (CurrentPage.Value >= AvailablePages.Value)
|
2019-09-08 05:27:40 +08:00
|
|
|
|
{
|
2022-01-04 17:46:44 +08:00
|
|
|
|
CurrentPage.Value = AvailablePages.Value - 1;
|
2019-09-08 05:27:40 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 18:05:14 +08:00
|
|
|
|
previousPageButton.Enabled.Value = CurrentPage.Value != 0;
|
|
|
|
|
nextPageButton.Enabled.Value = CurrentPage.Value < AvailablePages.Value - 1;
|
2019-09-10 06:36:25 +08:00
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
itemsFlow.Clear();
|
|
|
|
|
|
2022-01-04 18:05:14 +08:00
|
|
|
|
int totalPages = AvailablePages.Value;
|
|
|
|
|
bool lastWasEllipsis = false;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < totalPages; i++)
|
2020-01-05 02:14:56 +08:00
|
|
|
|
{
|
2022-01-04 17:46:44 +08:00
|
|
|
|
int pageIndex = i;
|
2020-01-05 02:14:56 +08:00
|
|
|
|
|
2022-01-04 18:05:14 +08:00
|
|
|
|
bool shouldShowPage = pageIndex == 0 || pageIndex == totalPages - 1 || Math.Abs(pageIndex - CurrentPage.Value) <= 2;
|
|
|
|
|
|
|
|
|
|
if (shouldShowPage)
|
|
|
|
|
{
|
|
|
|
|
lastWasEllipsis = false;
|
|
|
|
|
itemsFlow.Add(new PageSelectorPageButton(pageIndex + 1)
|
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value = pageIndex,
|
|
|
|
|
Selected = CurrentPage.Value == pageIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else if (!lastWasEllipsis)
|
2022-01-04 17:46:44 +08:00
|
|
|
|
{
|
2022-01-04 18:05:14 +08:00
|
|
|
|
lastWasEllipsis = true;
|
|
|
|
|
itemsFlow.Add(new PageEllipsis());
|
|
|
|
|
}
|
2022-01-04 17:46:44 +08:00
|
|
|
|
}
|
2019-09-07 13:46:16 +08:00
|
|
|
|
}
|
2019-09-07 12:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|