1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 13:17:26 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/PageSelector.cs

250 lines
7.6 KiB
C#
Raw Normal View History

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;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2019-09-07 13:20:09 +08:00
using osu.Framework.Extensions.Color4Extensions;
using System;
2019-09-07 14:20:11 +08:00
using osuTK;
using osu.Game.Graphics.Containers;
using System.Collections.Generic;
2019-09-08 04:42:30 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
2019-09-07 12:31:07 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class PageSelector : CompositeDrawable
{
2019-09-07 14:20:11 +08:00
public readonly BindableInt CurrentPage = new BindableInt(1);
2019-09-07 12:31:07 +08:00
private readonly int maxPages;
2019-09-08 04:42:30 +08:00
private readonly FillFlowContainer itemsFlow;
2019-09-07 12:31:07 +08:00
private readonly Button previousPageButton;
private readonly Button nextPageButton;
2019-09-07 12:31:07 +08:00
public PageSelector(int maxPages)
{
this.maxPages = maxPages;
AutoSizeAxes = Axes.Both;
InternalChild = new FillFlowContainer
2019-09-07 12:31:07 +08:00
{
AutoSizeAxes = Axes.Both,
2019-09-07 12:31:07 +08:00
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
previousPageButton = new Button(false, "prev")
{
Action = () => CurrentPage.Value -= 1,
},
2019-09-08 04:42:30 +08:00
itemsFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
},
nextPageButton = new Button(true, "next")
{
Action = () => CurrentPage.Value += 1
}
}
2019-09-07 12:31:07 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-09-08 03:33:26 +08:00
CurrentPage.BindValueChanged(page => redraw(page.NewValue), true);
2019-09-07 12:31:07 +08:00
}
2019-09-08 03:33:26 +08:00
private void redraw(int newPage)
2019-09-07 12:31:07 +08:00
{
2019-09-08 03:33:26 +08:00
previousPageButton.Enabled.Value = newPage != 1;
nextPageButton.Enabled.Value = newPage != maxPages;
2019-09-07 12:31:07 +08:00
2019-09-08 04:42:30 +08:00
itemsFlow.Clear();
2019-09-07 14:20:11 +08:00
2019-09-08 03:33:26 +08:00
if (newPage > 3)
addDrawablePage(1);
2019-09-08 03:33:26 +08:00
if (newPage > 4)
addPlaceholder();
2019-09-08 03:33:26 +08:00
for (int i = Math.Max(newPage - 2, 1); i <= Math.Min(newPage + 2, maxPages); i++)
2019-09-07 12:31:07 +08:00
{
2019-09-08 03:33:26 +08:00
if (i == newPage)
2019-09-08 04:42:30 +08:00
addDrawableCurrentPage();
2019-09-07 13:20:09 +08:00
else
addDrawablePage(i);
2019-09-07 12:31:07 +08:00
}
2019-09-08 03:33:26 +08:00
if (newPage + 2 < maxPages - 1)
addPlaceholder();
2019-09-08 03:33:26 +08:00
if (newPage + 2 < maxPages)
addDrawablePage(maxPages);
}
2019-09-08 04:42:30 +08:00
private void addDrawablePage(int page) => itemsFlow.Add(new DrawablePage(page.ToString())
{
2019-09-08 04:42:30 +08:00
Action = () => CurrentPage.Value = page,
});
2019-09-07 12:31:07 +08:00
2019-09-08 04:42:30 +08:00
private void addPlaceholder() => itemsFlow.Add(new Placeholder());
2019-09-07 12:31:07 +08:00
2019-09-08 04:42:30 +08:00
private void addDrawableCurrentPage() => itemsFlow.Add(new SelectedPage(CurrentPage.Value.ToString()));
2019-09-07 12:31:07 +08:00
2019-09-08 04:42:30 +08:00
private abstract class PageItem : OsuHoverContainer
2019-09-07 12:31:07 +08:00
{
2019-09-07 13:20:09 +08:00
private const int margin = 8;
2019-09-08 04:42:30 +08:00
private const int height = 20;
2019-09-07 12:31:07 +08:00
2019-09-08 04:42:30 +08:00
protected PageItem(string text)
2019-09-07 12:31:07 +08:00
{
AutoSizeAxes = Axes.X;
Height = height;
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
var contentContainer = new CircularContainer
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Masking = true,
};
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
var background = CreateBackground();
2019-09-07 13:20:09 +08:00
if (background != null)
2019-09-08 04:42:30 +08:00
contentContainer.Add(background);
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
var drawableText = CreateText(text);
if (drawableText != null)
{
drawableText.Margin = new MarginPadding { Horizontal = margin };
contentContainer.Add(drawableText);
}
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
Add(contentContainer);
2019-09-07 13:20:09 +08:00
}
2019-09-08 04:42:30 +08:00
protected abstract Drawable CreateText(string text);
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
protected abstract Drawable CreateBackground();
2019-09-07 13:20:09 +08:00
}
2019-09-08 04:42:30 +08:00
private class DrawablePage : PageItem
2019-09-07 13:20:09 +08:00
{
2019-09-08 04:42:30 +08:00
protected SpriteText SpriteText;
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
protected override IEnumerable<Drawable> EffectTargets => new[] { SpriteText };
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
public DrawablePage(string text)
: base(text)
2019-09-07 13:20:09 +08:00
{
}
2019-09-08 04:42:30 +08:00
protected override Drawable CreateBackground() => null;
2019-09-07 13:20:09 +08:00
2019-09-08 04:42:30 +08:00
protected override Drawable CreateText(string text) => SpriteText = new SpriteText
2019-09-07 13:20:09 +08:00
{
2019-09-08 04:42:30 +08:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = text,
};
2019-09-07 12:31:07 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-08 04:42:30 +08:00
IdleColour = colours.Seafoam;
HoverColour = colours.Seafoam.Lighten(30f);
2019-09-07 13:20:09 +08:00
}
}
private class SelectedPage : DrawablePage
2019-09-07 13:20:09 +08:00
{
private Box background;
2019-09-08 04:42:30 +08:00
protected override IEnumerable<Drawable> EffectTargets => null;
public SelectedPage(string text)
2019-09-07 13:20:09 +08:00
: base(text)
{
}
2019-09-08 04:42:30 +08:00
protected override Drawable CreateBackground() => background = new Box
{
RelativeSizeAxes = Axes.Both,
};
2019-09-07 13:20:09 +08:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 12:31:07 +08:00
background.Colour = colours.Seafoam;
2019-09-08 04:42:30 +08:00
SpriteText.Colour = colours.GreySeafoamDark;
2019-09-07 12:31:07 +08:00
}
}
private class Placeholder : DrawablePage
{
public Placeholder()
: base("...")
{
}
}
2019-09-07 14:20:11 +08:00
2019-09-08 04:42:30 +08:00
private class Button : PageItem
2019-09-07 14:20:11 +08:00
{
2019-09-08 04:42:30 +08:00
private Box background;
private FillFlowContainer textContainer;
private SpriteIcon icon;
2019-09-07 14:20:11 +08:00
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
2019-09-07 14:20:11 +08:00
public Button(bool rightAligned, string text)
2019-09-08 04:42:30 +08:00
: base(text)
2019-09-07 14:20:11 +08:00
{
2019-09-08 04:42:30 +08:00
var alignment = rightAligned ? Anchor.x0 : Anchor.x2;
2019-09-07 14:20:11 +08:00
2019-09-08 04:42:30 +08:00
textContainer.ForEach(drawable =>
2019-09-07 14:20:11 +08:00
{
2019-09-08 04:42:30 +08:00
drawable.Anchor = Anchor.y1 | alignment;
drawable.Origin = Anchor.y1 | alignment;
});
icon.Icon = alignment == Anchor.x2 ? FontAwesome.Solid.ChevronLeft : FontAwesome.Solid.ChevronRight;
2019-09-07 14:20:11 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
IdleColour = colours.GreySeafoamDark;
HoverColour = colours.GrayA;
2019-09-07 14:20:11 +08:00
}
2019-09-08 04:42:30 +08:00
protected override Drawable CreateBackground() => background = new Box
{
RelativeSizeAxes = Axes.Both,
};
protected override Drawable CreateText(string text) => textContainer = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
new SpriteText
{
Text = text.ToUpper(),
},
icon = new SpriteIcon
{
Size = new Vector2(10),
},
}
};
2019-09-07 14:20:11 +08:00
}
2019-09-07 12:31:07 +08:00
}
}