1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-24 18:07:36 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/PageSelector.cs

189 lines
5.1 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.Game.Graphics.Containers;
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.Input.Events;
using osu.Framework.Extensions.Color4Extensions;
using System;
2019-09-07 12:31:07 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class PageSelector : CompositeDrawable
{
private BindableInt currentPage = new BindableInt(1);
private readonly int maxPages;
private readonly FillFlowContainer pillsFlow;
public PageSelector(int maxPages)
{
this.maxPages = maxPages;
AutoSizeAxes = Axes.Both;
InternalChild = pillsFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Direction = FillDirection.Horizontal,
};
}
protected override void LoadComplete()
{
base.LoadComplete();
currentPage.BindValueChanged(page => redraw(page.NewValue), true);
}
private void redraw(int newPage)
{
pillsFlow.Clear();
2019-09-07 13:20:09 +08:00
for (int i = 1; i <= maxPages; i++)
2019-09-07 12:31:07 +08:00
{
2019-09-07 13:20:09 +08:00
if (i == currentPage.Value)
addCurrentPagePill();
else
addPagePill(i);
2019-09-07 12:31:07 +08:00
}
}
private void addPagePill(int page)
{
2019-09-07 13:20:09 +08:00
pillsFlow.Add(new Page(page.ToString(), () => currentPage.Value = page));
}
2019-09-07 12:31:07 +08:00
2019-09-07 13:20:09 +08:00
private void addCurrentPagePill()
{
pillsFlow.Add(new CurrentPage(currentPage.Value.ToString()));
2019-09-07 12:31:07 +08:00
}
2019-09-07 13:20:09 +08:00
private abstract class DrawablePage : CompositeDrawable
2019-09-07 12:31:07 +08:00
{
private const int height = 20;
2019-09-07 13:20:09 +08:00
private const int margin = 8;
2019-09-07 12:31:07 +08:00
2019-09-07 13:20:09 +08:00
protected readonly string Text;
2019-09-07 12:31:07 +08:00
2019-09-07 13:20:09 +08:00
protected DrawablePage(string text)
2019-09-07 12:31:07 +08:00
{
2019-09-07 13:20:09 +08:00
Text = text;
2019-09-07 12:31:07 +08:00
AutoSizeAxes = Axes.X;
Height = height;
2019-09-07 13:20:09 +08:00
var background = CreateBackground();
if (background != null)
AddInternal(background);
var content = CreateContent();
content.Margin = new MarginPadding { Horizontal = margin };
AddInternal(content);
}
protected abstract Drawable CreateContent();
protected virtual Drawable CreateBackground() => null;
}
private abstract class ActivatedDrawablePage : DrawablePage
{
private readonly Action action;
public ActivatedDrawablePage(string text, Action action)
: base(text)
{
this.action = action;
}
protected override bool OnClick(ClickEvent e)
{
action?.Invoke();
return base.OnClick(e);
}
}
private class Page : ActivatedDrawablePage
{
private SpriteText text;
private OsuColour colours;
public Page(string text, Action action)
: base(text, action)
{
2019-09-07 12:31:07 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 13:20:09 +08:00
this.colours = colours;
text.Colour = colours.Seafoam;
}
protected override bool OnHover(HoverEvent e)
{
text.Colour = colours.Seafoam.Lighten(30f);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
text.Colour = colours.Seafoam;
base.OnHoverLost(e);
}
protected override Drawable CreateContent() => text = new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
}
private class CurrentPage : DrawablePage
{
private SpriteText text;
private Box background;
public CurrentPage(string text)
: base(text)
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
text.Colour = colours.GreySeafoam;
2019-09-07 12:31:07 +08:00
background.Colour = colours.Seafoam;
}
2019-09-07 13:20:09 +08:00
protected override Drawable CreateContent() => text = new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
protected override Drawable CreateBackground() => new CircularContainer
{
RelativeSizeAxes = Axes.Both,
Masking = true,
Child = background = new Box
{
RelativeSizeAxes = Axes.Both,
}
};
2019-09-07 12:31:07 +08:00
}
}
}