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.
|
|
|
|
|
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Bindables;
|
2019-09-08 04:42:30 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2019-09-07 12:31:07 +08:00
|
|
|
|
|
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
|
|
|
|
|
{
|
2019-09-07 14:20:11 +08:00
|
|
|
|
public readonly BindableInt CurrentPage = new BindableInt(1);
|
2019-09-10 06:36:25 +08:00
|
|
|
|
public readonly BindableInt MaxPages = new BindableInt(1);
|
2019-09-07 12:31:07 +08:00
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
private readonly FillFlowContainer<DrawablePage> itemsFlow;
|
2019-09-07 12:31:07 +08:00
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
private readonly PageSelectorButton previousPageButton;
|
|
|
|
|
private readonly PageSelectorButton 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[]
|
|
|
|
|
{
|
2020-01-05 02:14:56 +08:00
|
|
|
|
previousPageButton = new PageSelectorButton(false, "prev")
|
2019-09-08 03:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value -= 1,
|
|
|
|
|
},
|
2020-01-05 02:14:56 +08:00
|
|
|
|
itemsFlow = new FillFlowContainer<DrawablePage>
|
2019-09-08 03:31:08 +08:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
},
|
2020-01-05 02:14:56 +08:00
|
|
|
|
nextPageButton = new PageSelectorButton(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();
|
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
MaxPages.BindValueChanged(_ => redraw());
|
|
|
|
|
CurrentPage.BindValueChanged(page => onCurrentPageChanged(page.NewValue));
|
|
|
|
|
redraw();
|
2019-09-07 12:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
private void onCurrentPageChanged(int newPage)
|
2019-09-07 12:31:07 +08:00
|
|
|
|
{
|
2020-01-05 02:14:56 +08:00
|
|
|
|
if (newPage < 1)
|
2019-09-08 05:27:40 +08:00
|
|
|
|
{
|
2020-01-05 02:14:56 +08:00
|
|
|
|
CurrentPage.Value = 1;
|
2019-09-08 05:27:40 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
if (newPage > MaxPages.Value)
|
2019-09-10 06:36:25 +08:00
|
|
|
|
{
|
2020-01-05 02:14:56 +08:00
|
|
|
|
CurrentPage.Value = MaxPages.Value;
|
2019-09-10 06:36:25 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 03:05:34 +08:00
|
|
|
|
itemsFlow.ForEach(page => page.Selected = page.Page == newPage);
|
2020-01-05 02:14:56 +08:00
|
|
|
|
updateButtonsState();
|
2019-09-10 06:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
private void redraw()
|
2019-09-10 06:36:25 +08:00
|
|
|
|
{
|
2020-01-05 02:14:56 +08:00
|
|
|
|
itemsFlow.Clear();
|
|
|
|
|
|
|
|
|
|
if (MaxPages.Value < 1)
|
|
|
|
|
{
|
|
|
|
|
MaxPages.Value = 1;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i <= MaxPages.Value; i++)
|
|
|
|
|
addDrawablePage(i);
|
|
|
|
|
|
2020-01-05 03:25:08 +08:00
|
|
|
|
if (CurrentPage.Value == 1)
|
|
|
|
|
CurrentPage.TriggerChange();
|
|
|
|
|
else
|
2019-09-08 05:27:40 +08:00
|
|
|
|
CurrentPage.Value = 1;
|
2019-09-10 06:36:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
private void updateButtonsState()
|
2019-09-10 06:36:25 +08:00
|
|
|
|
{
|
2019-09-08 05:27:40 +08:00
|
|
|
|
int newPage = CurrentPage.Value;
|
2019-09-10 06:36:25 +08:00
|
|
|
|
int maxPages = MaxPages.Value;
|
2019-09-08 05:27:40 +08:00
|
|
|
|
|
2019-09-08 03:33:26 +08:00
|
|
|
|
previousPageButton.Enabled.Value = newPage != 1;
|
|
|
|
|
nextPageButton.Enabled.Value = newPage != maxPages;
|
2019-09-07 13:46:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 02:14:56 +08:00
|
|
|
|
private void addDrawablePage(int page) => itemsFlow.Add(new DrawablePage(page)
|
2019-09-07 13:46:16 +08:00
|
|
|
|
{
|
2019-09-08 04:42:30 +08:00
|
|
|
|
Action = () => CurrentPage.Value = page,
|
|
|
|
|
});
|
2019-09-07 12:31:07 +08:00
|
|
|
|
}
|
|
|
|
|
}
|