1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 02:06:08 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/PageSelector/PageSelector.cs

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