osu/osu.Game/Graphics/UserInterface/PageSelector.cs

390 lines
11 KiB
C#
Raw Normal View History

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.
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 05:20:09 +00:00
using osu.Framework.Input.Events;
using osu.Framework.Extensions.Color4Extensions;
using System;
2019-09-07 06:20:11 +00:00
using osuTK;
2019-09-07 04:31:07 +00:00
namespace osu.Game.Graphics.UserInterface
{
public class PageSelector : CompositeDrawable
{
2019-09-07 06:20:11 +00:00
public readonly BindableInt CurrentPage = new BindableInt(1);
2019-09-07 04:31:07 +00:00
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(_ => redraw(), true);
2019-09-07 04:31:07 +00:00
}
private void redraw()
2019-09-07 04:31:07 +00:00
{
pillsFlow.Clear();
2019-09-07 06:20:11 +00:00
if (CurrentPage.Value == 1)
addPreviousPageButton();
else
addPreviousPageButton(() => CurrentPage.Value -= 1);
if (CurrentPage.Value > 3)
addDrawablePage(1);
if (CurrentPage.Value > 4)
addPlaceholder();
for (int i = Math.Max(CurrentPage.Value - 2, 1); i <= Math.Min(CurrentPage.Value + 2, maxPages); i++)
2019-09-07 04:31:07 +00:00
{
if (i == CurrentPage.Value)
2019-09-07 05:20:09 +00:00
addCurrentPagePill();
else
addDrawablePage(i);
2019-09-07 04:31:07 +00:00
}
if (CurrentPage.Value + 2 < maxPages - 1)
addPlaceholder();
if (CurrentPage.Value + 2 < maxPages)
addDrawablePage(maxPages);
2019-09-07 06:20:11 +00:00
if (CurrentPage.Value == maxPages)
addNextPageButton();
else
addNextPageButton(() => CurrentPage.Value += 1);
}
private void addDrawablePage(int page)
{
pillsFlow.Add(new Page(page.ToString(), () => CurrentPage.Value = page));
2019-09-07 04:31:07 +00:00
}
private void addPlaceholder()
2019-09-07 04:31:07 +00:00
{
pillsFlow.Add(new Placeholder());
2019-09-07 05:20:09 +00:00
}
2019-09-07 04:31:07 +00:00
2019-09-07 05:20:09 +00:00
private void addCurrentPagePill()
{
pillsFlow.Add(new SelectedPage(CurrentPage.Value.ToString()));
2019-09-07 04:31:07 +00:00
}
2019-09-07 06:20:11 +00:00
private void addPreviousPageButton(Action action = null)
{
pillsFlow.Add(new PreviousPageButton(action));
}
private void addNextPageButton(Action action = null)
{
pillsFlow.Add(new NextPageButton(action));
}
2019-09-07 05:20:09 +00:00
private abstract class DrawablePage : CompositeDrawable
2019-09-07 04:31:07 +00:00
{
private const int height = 20;
2019-09-07 05:20:09 +00:00
private const int margin = 8;
2019-09-07 04:31:07 +00:00
2019-09-07 05:20:09 +00:00
protected readonly string Text;
2019-09-07 06:20:11 +00:00
protected readonly Drawable Content;
2019-09-07 04:31:07 +00:00
2019-09-07 05:20:09 +00:00
protected DrawablePage(string text)
2019-09-07 04:31:07 +00:00
{
2019-09-07 05:20:09 +00:00
Text = text;
2019-09-07 04:31:07 +00:00
AutoSizeAxes = Axes.X;
Height = height;
2019-09-07 05:20:09 +00:00
var background = CreateBackground();
if (background != null)
AddInternal(background);
2019-09-07 06:20:11 +00:00
Content = CreateContent();
Content.Margin = new MarginPadding { Horizontal = margin };
2019-09-07 05:20:09 +00:00
2019-09-07 06:20:11 +00:00
AddInternal(Content);
2019-09-07 05:20:09 +00:00
}
protected abstract Drawable CreateContent();
protected virtual Drawable CreateBackground() => null;
}
private abstract class ActivatedDrawablePage : DrawablePage
{
2019-09-07 06:20:11 +00:00
protected readonly Action Action;
2019-09-07 05:20:09 +00:00
2019-09-07 06:20:11 +00:00
public ActivatedDrawablePage(string text, Action action = null)
2019-09-07 05:20:09 +00:00
: base(text)
{
2019-09-07 06:20:11 +00:00
Action = action;
2019-09-07 05:20:09 +00:00
}
protected override bool OnClick(ClickEvent e)
{
2019-09-07 06:20:11 +00:00
Action?.Invoke();
2019-09-07 05:20:09 +00:00
return base.OnClick(e);
}
}
private class Page : ActivatedDrawablePage
{
private OsuColour colours;
public Page(string text, Action action)
: base(text, action)
{
2019-09-07 04:31:07 +00:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 05:20:09 +00:00
this.colours = colours;
2019-09-07 06:20:11 +00:00
Content.Colour = colours.Seafoam;
2019-09-07 05:20:09 +00:00
}
protected override bool OnHover(HoverEvent e)
{
2019-09-07 06:20:11 +00:00
Content.Colour = colours.Seafoam.Lighten(30f);
2019-09-07 05:20:09 +00:00
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
2019-09-07 06:20:11 +00:00
Content.Colour = colours.Seafoam;
2019-09-07 05:20:09 +00:00
base.OnHoverLost(e);
}
2019-09-07 06:20:11 +00:00
protected override Drawable CreateContent() => new SpriteText
2019-09-07 05:20:09 +00:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
}
private class SelectedPage : DrawablePage
2019-09-07 05:20:09 +00:00
{
private Box background;
public SelectedPage(string text)
2019-09-07 05:20:09 +00:00
: base(text)
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 06:20:11 +00:00
Content.Colour = colours.GreySeafoam;
2019-09-07 04:31:07 +00:00
background.Colour = colours.Seafoam;
}
2019-09-07 05:20:09 +00:00
2019-09-07 06:20:11 +00:00
protected override Drawable CreateContent() => new SpriteText
2019-09-07 05:20:09 +00:00
{
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 04:31:07 +00:00
}
private class Placeholder : DrawablePage
{
public Placeholder()
: base("...")
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 06:20:11 +00:00
Content.Colour = colours.Seafoam;
}
2019-09-07 06:20:11 +00:00
protected override Drawable CreateContent() => new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
}
2019-09-07 06:20:11 +00:00
private class PreviousPageButton : ActivatedDrawablePage
{
private OsuColour colours;
private Box background;
public PreviousPageButton(Action action)
: base("prev", action)
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
this.colours = colours;
Content.Colour = colours.Seafoam;
background.Colour = colours.GreySeafoam;
if (Action == null)
{
Content.FadeColour(colours.GrayA);
background.FadeColour(colours.GrayA);
}
}
protected override bool OnHover(HoverEvent e)
{
Content.Colour = colours.Seafoam.Lighten(30f);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
Content.Colour = colours.Seafoam;
base.OnHoverLost(e);
}
protected override Drawable CreateContent() => new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(3),
Children = new Drawable[]
{
new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = FontAwesome.Solid.CaretLeft,
Size = new Vector2(10),
},
new SpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Text = Text.ToUpper(),
}
}
};
protected override Drawable CreateBackground() => new CircularContainer
{
RelativeSizeAxes = Axes.Both,
Masking = true,
Child = background = new Box
{
RelativeSizeAxes = Axes.Both,
}
};
}
private class NextPageButton : ActivatedDrawablePage
{
private OsuColour colours;
private Box background;
public NextPageButton(Action action)
: base("next", action)
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
this.colours = colours;
Content.Colour = colours.Seafoam;
background.Colour = colours.GreySeafoam;
if (Action == null)
{
Content.FadeColour(colours.GrayA);
background.FadeColour(colours.GrayA);
}
}
protected override bool OnHover(HoverEvent e)
{
Content.Colour = colours.Seafoam.Lighten(30f);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
Content.Colour = colours.Seafoam;
base.OnHoverLost(e);
}
protected override Drawable CreateContent() => new FillFlowContainer
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(3),
Children = new Drawable[]
{
new SpriteText
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Text = Text.ToUpper(),
},
new SpriteIcon
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Icon = FontAwesome.Solid.CaretRight,
Size = new Vector2(10),
},
}
};
protected override Drawable CreateBackground() => new CircularContainer
{
RelativeSizeAxes = Axes.Both,
Masking = true,
Child = background = new Box
{
RelativeSizeAxes = Axes.Both,
}
};
}
2019-09-07 04:31:07 +00:00
}
}