2019-09-07 04:31:07 +00: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 09:46:44 +00:00
|
|
|
|
using System.Linq;
|
2019-09-07 04:31:07 +00:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Bindables;
|
|
|
|
|
|
2020-01-04 16:03:39 +00:00
|
|
|
|
namespace osu.Game.Graphics.UserInterface.PageSelector
|
2019-09-07 04:31:07 +00:00
|
|
|
|
{
|
|
|
|
|
public class PageSelector : CompositeDrawable
|
|
|
|
|
{
|
2022-01-04 09:46:44 +00:00
|
|
|
|
public readonly BindableInt CurrentPage = new BindableInt { MinValue = 0, };
|
2019-09-07 04:31:07 +00:00
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
public readonly BindableInt AvailablePages = new BindableInt(1) { MinValue = 1, };
|
|
|
|
|
|
|
|
|
|
private readonly FillFlowContainer itemsFlow;
|
2019-09-07 04:31:07 +00:00
|
|
|
|
|
2022-01-04 08:52:40 +00:00
|
|
|
|
private readonly PageSelectorPrevNextButton previousPageButton;
|
|
|
|
|
private readonly PageSelectorPrevNextButton nextPageButton;
|
2019-09-07 19:31:08 +00:00
|
|
|
|
|
2019-09-09 22:36:25 +00:00
|
|
|
|
public PageSelector()
|
2019-09-07 04:31:07 +00:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both;
|
2022-01-04 07:46:42 +00:00
|
|
|
|
|
2019-09-07 19:31:08 +00:00
|
|
|
|
InternalChild = new FillFlowContainer
|
2019-09-07 04:31:07 +00:00
|
|
|
|
{
|
2019-09-07 19:31:08 +00:00
|
|
|
|
AutoSizeAxes = Axes.Both,
|
2019-09-07 04:31:07 +00:00
|
|
|
|
Direction = FillDirection.Horizontal,
|
2019-09-07 19:31:08 +00:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
2022-01-04 08:52:40 +00:00
|
|
|
|
previousPageButton = new PageSelectorPrevNextButton(false, "prev")
|
2019-09-07 19:31:08 +00:00
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value -= 1,
|
|
|
|
|
},
|
2022-01-04 09:46:44 +00:00
|
|
|
|
itemsFlow = new FillFlowContainer
|
2019-09-07 19:31:08 +00:00
|
|
|
|
{
|
|
|
|
|
AutoSizeAxes = Axes.Both,
|
|
|
|
|
Direction = FillDirection.Horizontal,
|
|
|
|
|
},
|
2022-01-04 08:52:40 +00:00
|
|
|
|
nextPageButton = new PageSelectorPrevNextButton(true, "next")
|
2019-09-07 19:31:08 +00:00
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value += 1
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-07 04:31:07 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
CurrentPage.BindValueChanged(onCurrentPageChanged);
|
|
|
|
|
AvailablePages.BindValueChanged(_ => redraw(), true);
|
2019-09-07 04:31:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
private void onCurrentPageChanged(ValueChangedEvent<int> currentPage)
|
2019-09-07 04:31:07 +00:00
|
|
|
|
{
|
2022-01-04 09:46:44 +00:00
|
|
|
|
if (currentPage.NewValue >= AvailablePages.Value)
|
2019-09-07 21:27:40 +00:00
|
|
|
|
{
|
2022-01-04 09:46:44 +00:00
|
|
|
|
CurrentPage.Value = AvailablePages.Value - 1;
|
2019-09-07 21:27:40 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
foreach (var page in itemsFlow.OfType<PageSelectorPageButton>())
|
|
|
|
|
page.Selected = page.PageNumber == currentPage.NewValue + 1;
|
2019-09-09 22:36:25 +00:00
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
previousPageButton.Enabled.Value = currentPage.NewValue != 0;
|
|
|
|
|
nextPageButton.Enabled.Value = currentPage.NewValue < AvailablePages.Value - 1;
|
2019-09-09 22:36:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-01-04 18:14:56 +00:00
|
|
|
|
private void redraw()
|
2019-09-09 22:36:25 +00:00
|
|
|
|
{
|
2020-01-04 18:14:56 +00:00
|
|
|
|
itemsFlow.Clear();
|
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
for (int i = 0; i < AvailablePages.Value; i++)
|
2020-01-04 18:14:56 +00:00
|
|
|
|
{
|
2022-01-04 09:46:44 +00:00
|
|
|
|
int pageIndex = i;
|
2020-01-04 18:14:56 +00:00
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
itemsFlow.Add(new PageSelectorPageButton(pageIndex + 1)
|
|
|
|
|
{
|
|
|
|
|
Action = () => CurrentPage.Value = pageIndex,
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-04 18:14:56 +00:00
|
|
|
|
|
2022-01-04 09:46:44 +00:00
|
|
|
|
if (CurrentPage.Value != 0)
|
|
|
|
|
CurrentPage.Value = 0;
|
2020-01-04 19:25:08 +00:00
|
|
|
|
else
|
2022-01-04 09:46:44 +00:00
|
|
|
|
CurrentPage.TriggerChange();
|
2019-09-07 05:46:16 +00:00
|
|
|
|
}
|
2019-09-07 04:31:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|