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

288 lines
8.6 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.Extensions.Color4Extensions;
using System;
2019-09-07 06:20:11 +00:00
using osuTK;
using osu.Game.Graphics.Containers;
using System.Collections.Generic;
2019-09-07 20:42:30 +00:00
using osu.Framework.Extensions.IEnumerableExtensions;
2019-09-07 21:13:23 +00:00
using osuTK.Graphics;
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;
2019-09-07 20:42:30 +00:00
private readonly FillFlowContainer itemsFlow;
2019-09-07 04:31:07 +00:00
private readonly Button previousPageButton;
private readonly Button nextPageButton;
2019-09-07 04:31:07 +00:00
public PageSelector(int maxPages)
{
this.maxPages = maxPages;
AutoSizeAxes = Axes.Both;
InternalChild = new FillFlowContainer
2019-09-07 04:31:07 +00:00
{
AutoSizeAxes = Axes.Both,
2019-09-07 04:31:07 +00:00
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
previousPageButton = new Button(false, "prev")
{
Action = () => CurrentPage.Value -= 1,
},
2019-09-07 20:42:30 +00:00
itemsFlow = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
},
nextPageButton = new Button(true, "next")
{
Action = () => CurrentPage.Value += 1
}
}
2019-09-07 04:31:07 +00:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
2019-09-07 21:27:40 +00:00
CurrentPage.BindValueChanged(_ => redraw(), true);
2019-09-07 04:31:07 +00:00
}
2019-09-07 21:27:40 +00:00
private void redraw()
2019-09-07 04:31:07 +00:00
{
2019-09-07 21:27:40 +00:00
if (CurrentPage.Value > maxPages)
{
CurrentPage.Value = maxPages;
return;
}
if (CurrentPage.Value < 1)
{
CurrentPage.Value = 1;
return;
}
int newPage = CurrentPage.Value;
2019-09-07 19:33:26 +00:00
previousPageButton.Enabled.Value = newPage != 1;
nextPageButton.Enabled.Value = newPage != maxPages;
2019-09-07 04:31:07 +00:00
2019-09-07 20:42:30 +00:00
itemsFlow.Clear();
2019-09-07 06:20:11 +00:00
2019-09-07 19:33:26 +00:00
if (newPage > 3)
addDrawablePage(1);
2019-09-07 19:33:26 +00:00
if (newPage > 4)
addPlaceholder();
2019-09-07 19:33:26 +00:00
for (int i = Math.Max(newPage - 2, 1); i <= Math.Min(newPage + 2, maxPages); i++)
2019-09-07 04:31:07 +00:00
{
2019-09-07 19:33:26 +00:00
if (i == newPage)
2019-09-07 20:42:30 +00:00
addDrawableCurrentPage();
2019-09-07 05:20:09 +00:00
else
addDrawablePage(i);
2019-09-07 04:31:07 +00:00
}
2019-09-07 19:33:26 +00:00
if (newPage + 2 < maxPages - 1)
addPlaceholder();
2019-09-07 19:33:26 +00:00
if (newPage + 2 < maxPages)
addDrawablePage(maxPages);
}
2019-09-07 20:42:30 +00:00
private void addDrawablePage(int page) => itemsFlow.Add(new DrawablePage(page.ToString())
{
2019-09-07 20:42:30 +00:00
Action = () => CurrentPage.Value = page,
});
2019-09-07 04:31:07 +00:00
2019-09-07 20:42:30 +00:00
private void addPlaceholder() => itemsFlow.Add(new Placeholder());
2019-09-07 04:31:07 +00:00
2019-09-07 20:42:30 +00:00
private void addDrawableCurrentPage() => itemsFlow.Add(new SelectedPage(CurrentPage.Value.ToString()));
2019-09-07 04:31:07 +00:00
2019-09-07 20:42:30 +00:00
private abstract class PageItem : OsuHoverContainer
2019-09-07 04:31:07 +00:00
{
2019-09-07 21:13:23 +00:00
private const int margin = 10;
2019-09-07 20:42:30 +00:00
private const int height = 20;
2019-09-07 04:31:07 +00:00
2019-09-07 21:13:23 +00:00
protected override Container<Drawable> Content => contentContainer;
private readonly CircularContainer contentContainer;
2019-09-07 20:42:30 +00:00
protected PageItem(string text)
2019-09-07 04:31:07 +00:00
{
AutoSizeAxes = Axes.X;
Height = height;
2019-09-07 05:20:09 +00:00
2019-09-07 21:13:23 +00:00
base.Content.Add(contentContainer = new CircularContainer
2019-09-07 20:42:30 +00:00
{
AutoSizeAxes = Axes.X,
RelativeSizeAxes = Axes.Y,
Masking = true,
2019-09-07 21:13:23 +00:00
});
2019-09-07 05:20:09 +00:00
2019-09-07 20:42:30 +00:00
var background = CreateBackground();
2019-09-07 05:20:09 +00:00
if (background != null)
2019-09-07 21:13:23 +00:00
Add(background);
2019-09-07 05:20:09 +00:00
2019-09-07 20:42:30 +00:00
var drawableText = CreateText(text);
2019-09-07 21:37:11 +00:00
2019-09-07 20:42:30 +00:00
if (drawableText != null)
{
drawableText.Margin = new MarginPadding { Horizontal = margin };
2019-09-07 21:13:23 +00:00
Add(drawableText);
2019-09-07 20:42:30 +00:00
}
2019-09-07 21:13:23 +00:00
}
2019-09-07 05:20:09 +00:00
2019-09-07 21:13:23 +00:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
IdleColour = colours.Seafoam;
HoverColour = colours.Seafoam.Lighten(30f);
2019-09-07 05:20:09 +00:00
}
2019-09-07 20:42:30 +00:00
protected abstract Drawable CreateText(string text);
2019-09-07 05:20:09 +00:00
2019-09-07 21:13:23 +00:00
protected virtual Drawable CreateBackground() => null;
2019-09-07 05:20:09 +00:00
}
2019-09-07 20:42:30 +00:00
private class DrawablePage : PageItem
2019-09-07 05:20:09 +00:00
{
2019-09-07 20:42:30 +00:00
protected SpriteText SpriteText;
2019-09-07 05:20:09 +00:00
2019-09-07 20:42:30 +00:00
protected override IEnumerable<Drawable> EffectTargets => new[] { SpriteText };
2019-09-07 05:20:09 +00:00
2019-09-07 20:42:30 +00:00
public DrawablePage(string text)
: base(text)
2019-09-07 05:20:09 +00:00
{
}
2019-09-07 20:42:30 +00:00
protected override Drawable CreateText(string text) => SpriteText = new SpriteText
2019-09-07 05:20:09 +00:00
{
2019-09-07 20:42:30 +00:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Text = text,
2019-09-07 21:13:23 +00:00
Font = OsuFont.GetFont(size: 12),
2019-09-07 20:42:30 +00:00
};
2019-09-07 05:20:09 +00:00
}
private class SelectedPage : DrawablePage
2019-09-07 05:20:09 +00:00
{
private Box background;
2019-09-07 21:13:23 +00:00
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
2019-09-07 20:42:30 +00:00
public SelectedPage(string text)
2019-09-07 05:20:09 +00:00
: base(text)
{
}
2019-09-07 20:42:30 +00:00
protected override Drawable CreateBackground() => background = new Box
{
RelativeSizeAxes = Axes.Both,
};
2019-09-07 05:20:09 +00:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 20:42:30 +00:00
SpriteText.Colour = colours.GreySeafoamDark;
2019-09-07 04:31:07 +00:00
}
}
private class Placeholder : DrawablePage
{
public Placeholder()
: base("...")
{
}
}
2019-09-07 06:20:11 +00:00
2019-09-07 20:42:30 +00:00
private class Button : PageItem
2019-09-07 06:20:11 +00:00
{
2019-09-07 21:13:23 +00:00
private const int duration = 100;
2019-09-07 20:42:30 +00:00
private Box background;
private FillFlowContainer textContainer;
private SpriteIcon icon;
2019-09-07 21:13:23 +00:00
private readonly Box fadeBox;
2019-09-07 06:20:11 +00:00
2019-09-07 21:13:23 +00:00
protected override IEnumerable<Drawable> EffectTargets => new[] { textContainer };
2019-09-07 06:20:11 +00:00
public Button(bool rightAligned, string text)
2019-09-07 20:42:30 +00:00
: base(text)
2019-09-07 06:20:11 +00:00
{
2019-09-07 20:42:30 +00:00
var alignment = rightAligned ? Anchor.x0 : Anchor.x2;
2019-09-07 06:20:11 +00:00
2019-09-07 20:42:30 +00:00
textContainer.ForEach(drawable =>
2019-09-07 06:20:11 +00:00
{
2019-09-07 20:42:30 +00:00
drawable.Anchor = Anchor.y1 | alignment;
drawable.Origin = Anchor.y1 | alignment;
});
icon.Icon = alignment == Anchor.x2 ? FontAwesome.Solid.ChevronLeft : FontAwesome.Solid.ChevronRight;
2019-09-07 21:13:23 +00:00
Add(fadeBox = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(100)
});
2019-09-07 06:20:11 +00:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-09-07 21:13:23 +00:00
background.Colour = colours.GreySeafoamDark;
}
protected override void LoadComplete()
{
base.LoadComplete();
Enabled.BindValueChanged(onEnabledChanged, true);
}
private void onEnabledChanged(ValueChangedEvent<bool> enabled)
{
fadeBox.FadeTo(enabled.NewValue ? 0 : 1, duration);
2019-09-07 06:20:11 +00:00
}
2019-09-07 20:42:30 +00:00
protected override Drawable CreateBackground() => background = new Box
{
RelativeSizeAxes = Axes.Both,
};
protected override Drawable CreateText(string text) => textContainer = new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
2019-09-07 21:13:23 +00:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2019-09-07 20:42:30 +00:00
Direction = FillDirection.Horizontal,
Children = new Drawable[]
{
new SpriteText
{
Text = text.ToUpper(),
2019-09-07 21:13:23 +00:00
Font = OsuFont.GetFont(size: 12),
2019-09-07 20:42:30 +00:00
},
icon = new SpriteIcon
{
2019-09-07 21:13:23 +00:00
Size = new Vector2(8),
2019-09-07 20:42:30 +00:00
},
}
};
2019-09-07 06:20:11 +00:00
}
2019-09-07 04:31:07 +00:00
}
}