1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 12:37:29 +08:00

Implement CurrentPage class

This commit is contained in:
Andrei Zavatski 2019-09-07 08:20:09 +03:00
parent 508b26ac69
commit f77cd6582d

View File

@ -8,6 +8,9 @@ using osu.Framework.Bindables;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics.Shapes; using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Extensions.Color4Extensions;
using System;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
@ -42,59 +45,144 @@ namespace osu.Game.Graphics.UserInterface
{ {
pillsFlow.Clear(); pillsFlow.Clear();
for (int i = 0; i < maxPages; i++) for (int i = 1; i <= maxPages; i++)
{ {
if (i == currentPage.Value)
addCurrentPagePill();
else
addPagePill(i); addPagePill(i);
} }
} }
private void addPagePill(int page) private void addPagePill(int page)
{ {
var pill = new Pill(page); pillsFlow.Add(new Page(page.ToString(), () => currentPage.Value = page));
if (page != currentPage.Value)
pill.Action = () => currentPage.Value = page;
pillsFlow.Add(pill);
} }
private class Pill : OsuClickableContainer private void addCurrentPagePill()
{
pillsFlow.Add(new CurrentPage(currentPage.Value.ToString()));
}
private abstract class DrawablePage : CompositeDrawable
{ {
private const int height = 20; private const int height = 20;
private const int margin = 8;
private readonly Box background; protected readonly string Text;
public Pill(int page) protected DrawablePage(string text)
{ {
Text = text;
AutoSizeAxes = Axes.X; AutoSizeAxes = Axes.X;
Height = height; Height = height;
Child = new CircularContainer
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
{ {
AutoSizeAxes = Axes.X, private readonly Action action;
RelativeSizeAxes = Axes.Y,
Masking = true, public ActivatedDrawablePage(string text, Action action)
Children = new Drawable[] : base(text)
{ {
background = new Box this.action = action;
}
protected override bool OnClick(ClickEvent e)
{ {
RelativeSizeAxes = Axes.Both, action?.Invoke();
}, return base.OnClick(e);
new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = page.ToString(),
Margin = new MarginPadding { Horizontal = 8 }
} }
} }
};
private class Page : ActivatedDrawablePage
{
private SpriteText text;
private OsuColour colours;
public Page(string text, Action action)
: base(text, action)
{
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours)
{ {
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;
background.Colour = colours.Seafoam; background.Colour = colours.Seafoam;
} }
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,
}
};
} }
} }
} }