1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-21 05:53:20 +08:00

Implement a single PageItem component

This commit is contained in:
Andrei Zavatski 2019-09-07 23:42:30 +03:00
parent ec8298ac53
commit df29465ba4

View File

@ -7,12 +7,12 @@ 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 osu.Framework.Extensions.Color4Extensions;
using System; using System;
using osuTK; using osuTK;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using System.Collections.Generic; using System.Collections.Generic;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Graphics.UserInterface namespace osu.Game.Graphics.UserInterface
{ {
@ -21,7 +21,7 @@ namespace osu.Game.Graphics.UserInterface
public readonly BindableInt CurrentPage = new BindableInt(1); public readonly BindableInt CurrentPage = new BindableInt(1);
private readonly int maxPages; private readonly int maxPages;
private readonly FillFlowContainer pillsFlow; private readonly FillFlowContainer itemsFlow;
private readonly Button previousPageButton; private readonly Button previousPageButton;
private readonly Button nextPageButton; private readonly Button nextPageButton;
@ -41,7 +41,7 @@ namespace osu.Game.Graphics.UserInterface
{ {
Action = () => CurrentPage.Value -= 1, Action = () => CurrentPage.Value -= 1,
}, },
pillsFlow = new FillFlowContainer itemsFlow = new FillFlowContainer
{ {
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
@ -66,7 +66,7 @@ namespace osu.Game.Graphics.UserInterface
previousPageButton.Enabled.Value = newPage != 1; previousPageButton.Enabled.Value = newPage != 1;
nextPageButton.Enabled.Value = newPage != maxPages; nextPageButton.Enabled.Value = newPage != maxPages;
pillsFlow.Clear(); itemsFlow.Clear();
if (newPage > 3) if (newPage > 3)
addDrawablePage(1); addDrawablePage(1);
@ -77,7 +77,7 @@ namespace osu.Game.Graphics.UserInterface
for (int i = Math.Max(newPage - 2, 1); i <= Math.Min(newPage + 2, maxPages); i++) for (int i = Math.Max(newPage - 2, 1); i <= Math.Min(newPage + 2, maxPages); i++)
{ {
if (i == newPage) if (i == newPage)
addCurrentPagePill(); addDrawableCurrentPage();
else else
addDrawablePage(i); addDrawablePage(i);
} }
@ -89,137 +89,101 @@ namespace osu.Game.Graphics.UserInterface
addDrawablePage(maxPages); addDrawablePage(maxPages);
} }
private void addDrawablePage(int page) private void addDrawablePage(int page) => itemsFlow.Add(new DrawablePage(page.ToString())
{ {
pillsFlow.Add(new Page(page.ToString(), () => CurrentPage.Value = page)); Action = () => CurrentPage.Value = page,
} });
private void addPlaceholder() private void addPlaceholder() => itemsFlow.Add(new Placeholder());
{
pillsFlow.Add(new Placeholder());
}
private void addCurrentPagePill() private void addDrawableCurrentPage() => itemsFlow.Add(new SelectedPage(CurrentPage.Value.ToString()));
{
pillsFlow.Add(new SelectedPage(CurrentPage.Value.ToString()));
}
private abstract class DrawablePage : CompositeDrawable private abstract class PageItem : OsuHoverContainer
{ {
private const int height = 20;
private const int margin = 8; private const int margin = 8;
private const int height = 20;
protected readonly string Text; protected PageItem(string text)
protected readonly Drawable Content;
protected DrawablePage(string text)
{ {
Text = text;
AutoSizeAxes = Axes.X; AutoSizeAxes = Axes.X;
Height = height; Height = height;
var contentContainer = new CircularContainer
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Masking = true,
};
var background = CreateBackground(); var background = CreateBackground();
if (background != null) if (background != null)
AddInternal(background); contentContainer.Add(background);
Content = CreateContent(); var drawableText = CreateText(text);
Content.Margin = new MarginPadding { Horizontal = margin }; if (drawableText != null)
{
drawableText.Margin = new MarginPadding { Horizontal = margin };
contentContainer.Add(drawableText);
}
AddInternal(Content); Add(contentContainer);
} }
protected abstract Drawable CreateContent(); protected abstract Drawable CreateText(string text);
protected virtual Drawable CreateBackground() => null; protected abstract Drawable CreateBackground();
} }
private abstract class ActivatedDrawablePage : DrawablePage private class DrawablePage : PageItem
{ {
protected readonly Action Action; protected SpriteText SpriteText;
public ActivatedDrawablePage(string text, Action action = null) protected override IEnumerable<Drawable> EffectTargets => new[] { SpriteText };
public DrawablePage(string text)
: base(text) : base(text)
{ {
Action = action;
} }
protected override bool OnClick(ClickEvent e) protected override Drawable CreateBackground() => null;
protected override Drawable CreateText(string text) => SpriteText = new SpriteText
{ {
Action?.Invoke(); Anchor = Anchor.Centre,
return base.OnClick(e); Origin = Anchor.Centre,
} Text = text,
} };
private class Page : ActivatedDrawablePage
{
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; IdleColour = colours.Seafoam;
Content.Colour = colours.Seafoam; HoverColour = colours.Seafoam.Lighten(30f);
} }
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 SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
} }
private class SelectedPage : DrawablePage private class SelectedPage : DrawablePage
{ {
private Box background; private Box background;
protected override IEnumerable<Drawable> EffectTargets => null;
public SelectedPage(string text) public SelectedPage(string text)
: base(text) : base(text)
{ {
} }
protected override Drawable CreateBackground() => background = new Box
{
RelativeSizeAxes = Axes.Both,
};
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OsuColour colours) private void load(OsuColour colours)
{ {
Content.Colour = colours.GreySeafoam;
background.Colour = colours.Seafoam; background.Colour = colours.Seafoam;
SpriteText.Colour = colours.GreySeafoamDark;
} }
protected override Drawable CreateContent() => 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,
}
};
} }
private class Placeholder : DrawablePage private class Placeholder : DrawablePage
@ -228,79 +192,28 @@ namespace osu.Game.Graphics.UserInterface
: base("...") : base("...")
{ {
} }
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Content.Colour = colours.Seafoam;
}
protected override Drawable CreateContent() => new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
} }
private class Button : OsuHoverContainer private class Button : PageItem
{ {
private const int height = 20; private Box background;
private const int margin = 8; private FillFlowContainer textContainer;
private SpriteIcon icon;
private readonly Anchor alignment;
private readonly Box background;
protected override IEnumerable<Drawable> EffectTargets => new[] { background }; protected override IEnumerable<Drawable> EffectTargets => new[] { background };
public Button(bool rightAligned, string text) public Button(bool rightAligned, string text)
: base(text)
{ {
alignment = rightAligned ? Anchor.x0 : Anchor.x2; var alignment = rightAligned ? Anchor.x0 : Anchor.x2;
AutoSizeAxes = Axes.X; textContainer.ForEach(drawable =>
Height = height;
Child = new CircularContainer
{ {
AutoSizeAxes = Axes.X, drawable.Anchor = Anchor.y1 | alignment;
RelativeSizeAxes = Axes.Y, drawable.Origin = Anchor.y1 | alignment;
Masking = true, });
Children = new Drawable[]
{ icon.Icon = alignment == Anchor.x2 ? FontAwesome.Solid.ChevronLeft : FontAwesome.Solid.ChevronRight;
background = new Box
{
RelativeSizeAxes = Axes.Both,
},
new Container
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Child = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Margin = new MarginPadding { Horizontal = margin },
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
new SpriteText
{
Anchor = Anchor.y1 | alignment,
Origin = Anchor.y1 | alignment,
Text = text.ToUpper(),
},
new SpriteIcon
{
Anchor = Anchor.y1 | alignment,
Origin = Anchor.y1 | alignment,
Icon = alignment == Anchor.x2 ? FontAwesome.Solid.ChevronLeft : FontAwesome.Solid.ChevronRight,
Size = new Vector2(10),
},
}
}
}
}
};
} }
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
@ -309,6 +222,28 @@ namespace osu.Game.Graphics.UserInterface
IdleColour = colours.GreySeafoamDark; IdleColour = colours.GreySeafoamDark;
HoverColour = colours.GrayA; HoverColour = colours.GrayA;
} }
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),
},
}
};
} }
} }
} }