1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-12 13:17:26 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/PageSelector.cs

315 lines
9.7 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.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 14:20:11 +08:00
using osuTK;
using osu.Game.Graphics.Containers;
using System.Collections.Generic;
2019-09-07 12:31:07 +08:00
namespace osu.Game.Graphics.UserInterface
{
public class PageSelector : CompositeDrawable
{
2019-09-07 14:20:11 +08:00
public readonly BindableInt CurrentPage = new BindableInt(1);
2019-09-07 12:31:07 +08:00
private readonly int maxPages;
private readonly FillFlowContainer pillsFlow;
private readonly Button previousPageButton;
private readonly Button nextPageButton;
2019-09-07 12:31:07 +08:00
public PageSelector(int maxPages)
{
this.maxPages = maxPages;
AutoSizeAxes = Axes.Both;
InternalChild = new FillFlowContainer
2019-09-07 12:31:07 +08:00
{
AutoSizeAxes = Axes.Both,
2019-09-07 12:31:07 +08:00
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
previousPageButton = new Button(false, "prev")
{
Action = () => CurrentPage.Value -= 1,
},
pillsFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
},
nextPageButton = new Button(true, "next")
{
Action = () => CurrentPage.Value += 1
}
}
2019-09-07 12:31:07 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
CurrentPage.BindValueChanged(_ => redraw(), true);
2019-09-07 12:31:07 +08:00
}
private void redraw()
2019-09-07 12:31:07 +08:00
{
previousPageButton.Enabled.Value = CurrentPage.Value != 1;
nextPageButton.Enabled.Value = CurrentPage.Value != maxPages;
2019-09-07 12:31:07 +08:00
pillsFlow.Clear();
2019-09-07 14:20:11 +08:00
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 12:31:07 +08:00
{
if (i == CurrentPage.Value)
2019-09-07 13:20:09 +08:00
addCurrentPagePill();
else
addDrawablePage(i);
2019-09-07 12:31:07 +08:00
}
if (CurrentPage.Value + 2 < maxPages - 1)
addPlaceholder();
if (CurrentPage.Value + 2 < maxPages)
addDrawablePage(maxPages);
}
private void addDrawablePage(int page)
{
pillsFlow.Add(new Page(page.ToString(), () => CurrentPage.Value = page));
2019-09-07 12:31:07 +08:00
}
private void addPlaceholder()
2019-09-07 12:31:07 +08:00
{
pillsFlow.Add(new Placeholder());
2019-09-07 13:20:09 +08:00
}
2019-09-07 12:31:07 +08:00
2019-09-07 13:20:09 +08:00
private void addCurrentPagePill()
{
pillsFlow.Add(new SelectedPage(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 14:20:11 +08:00
protected readonly Drawable Content;
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);
2019-09-07 14:20:11 +08:00
Content = CreateContent();
Content.Margin = new MarginPadding { Horizontal = margin };
2019-09-07 13:20:09 +08:00
2019-09-07 14:20:11 +08:00
AddInternal(Content);
2019-09-07 13:20:09 +08:00
}
protected abstract Drawable CreateContent();
protected virtual Drawable CreateBackground() => null;
}
private abstract class ActivatedDrawablePage : DrawablePage
{
2019-09-07 14:20:11 +08:00
protected readonly Action Action;
2019-09-07 13:20:09 +08:00
2019-09-07 14:20:11 +08:00
public ActivatedDrawablePage(string text, Action action = null)
2019-09-07 13:20:09 +08:00
: base(text)
{
2019-09-07 14:20:11 +08:00
Action = action;
2019-09-07 13:20:09 +08:00
}
protected override bool OnClick(ClickEvent e)
{
2019-09-07 14:20:11 +08:00
Action?.Invoke();
2019-09-07 13:20:09 +08:00
return base.OnClick(e);
}
}
private class Page : ActivatedDrawablePage
{
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;
2019-09-07 14:20:11 +08:00
Content.Colour = colours.Seafoam;
2019-09-07 13:20:09 +08:00
}
protected override bool OnHover(HoverEvent e)
{
2019-09-07 14:20:11 +08:00
Content.Colour = colours.Seafoam.Lighten(30f);
2019-09-07 13:20:09 +08:00
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
2019-09-07 14:20:11 +08:00
Content.Colour = colours.Seafoam;
2019-09-07 13:20:09 +08:00
base.OnHoverLost(e);
}
2019-09-07 14:20:11 +08:00
protected override Drawable CreateContent() => new SpriteText
2019-09-07 13:20:09 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
}
private class SelectedPage : DrawablePage
2019-09-07 13:20:09 +08:00
{
private Box background;
public SelectedPage(string text)
2019-09-07 13:20:09 +08:00
: base(text)
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 14:20:11 +08:00
Content.Colour = colours.GreySeafoam;
2019-09-07 12:31:07 +08:00
background.Colour = colours.Seafoam;
}
2019-09-07 13:20:09 +08:00
2019-09-07 14:20:11 +08:00
protected override Drawable CreateContent() => new SpriteText
2019-09-07 13:20:09 +08: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 12:31:07 +08:00
}
private class Placeholder : DrawablePage
{
public Placeholder()
: base("...")
{
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 14:20:11 +08:00
Content.Colour = colours.Seafoam;
}
2019-09-07 14:20:11 +08:00
protected override Drawable CreateContent() => new SpriteText
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = Text
};
}
2019-09-07 14:20:11 +08:00
private class Button : OsuHoverContainer
2019-09-07 14:20:11 +08:00
{
private const int height = 20;
private const int margin = 8;
2019-09-07 14:20:11 +08:00
private readonly Anchor alignment;
private readonly Box background;
2019-09-07 14:20:11 +08:00
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
2019-09-07 14:20:11 +08:00
public Button(bool rightAligned, string text)
2019-09-07 14:20:11 +08:00
{
alignment = rightAligned ? Anchor.x0 : Anchor.x2;
2019-09-07 14:20:11 +08:00
AutoSizeAxes = Axes.X;
Height = height;
2019-09-07 14:20:11 +08:00
Child = new CircularContainer
2019-09-07 14:20:11 +08:00
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Masking = true,
Children = new Drawable[]
2019-09-07 14:20:11 +08:00
{
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),
},
}
}
}
2019-09-07 14:20:11 +08:00
}
};
2019-09-07 14:20:11 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
IdleColour = colours.GreySeafoamDark;
HoverColour = colours.GrayA;
2019-09-07 14:20:11 +08:00
}
}
2019-09-07 12:31:07 +08:00
}
}