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.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
using osu.Framework.Extensions.Color4Extensions;
using System;
namespace osu.Game.Graphics.UserInterface
{
@ -42,59 +45,144 @@ namespace osu.Game.Graphics.UserInterface
{
pillsFlow.Clear();
for (int i = 0; i < maxPages; i++)
for (int i = 1; i <= maxPages; i++)
{
addPagePill(i);
if (i == currentPage.Value)
addCurrentPagePill();
else
addPagePill(i);
}
}
private void addPagePill(int page)
{
var pill = new Pill(page);
if (page != currentPage.Value)
pill.Action = () => currentPage.Value = page;
pillsFlow.Add(pill);
pillsFlow.Add(new Page(page.ToString(), () => currentPage.Value = page));
}
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 margin = 8;
private readonly Box background;
protected readonly string Text;
public Pill(int page)
protected DrawablePage(string text)
{
Text = text;
AutoSizeAxes = Axes.X;
Height = height;
Child = new CircularContainer
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Masking = true,
Children = new Drawable[]
{
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = page.ToString(),
Margin = new MarginPadding { Horizontal = 8 }
}
}
};
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)
{
}
[BackgroundDependencyLoader]
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;
}
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,
}
};
}
}
}