1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-25 03:22:57 +08:00
osu-lazer/osu.Game/Graphics/UserInterfaceV2/ColourPalette.cs

120 lines
3.3 KiB
C#
Raw Normal View History

2021-04-19 01:29:04 +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 System.Linq;
2021-04-19 01:29:04 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Sprites;
2021-04-19 01:29:04 +08:00
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterfaceV2
{
/// <summary>
/// A component which displays a collection of colours in individual <see cref="ColourDisplay"/>s.
/// </summary>
2021-04-19 01:29:04 +08:00
public class ColourPalette : CompositeDrawable
{
public BindableList<Color4> Colours { get; } = new BindableList<Color4>();
2021-04-19 01:56:03 +08:00
private string colourNamePrefix = "Colour";
public string ColourNamePrefix
{
get => colourNamePrefix;
set
{
if (colourNamePrefix == value)
return;
colourNamePrefix = value;
if (IsLoaded)
reindexItems();
}
}
2021-04-19 01:29:04 +08:00
private FillFlowContainer<ColourDisplay> palette;
private Container placeholder;
2021-04-19 01:29:04 +08:00
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
InternalChildren = new Drawable[]
2021-04-19 01:29:04 +08:00
{
palette = new FillFlowContainer<ColourDisplay>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10),
Direction = FillDirection.Full
},
placeholder = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Child = new OsuSpriteText
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Text = "(none)",
Font = OsuFont.Default.With(weight: FontWeight.Bold)
}
}
2021-04-19 01:29:04 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Colours.BindCollectionChanged((_, __) => updatePalette(), true);
FinishTransforms(true);
2021-04-19 01:29:04 +08:00
}
private const int fade_duration = 200;
2021-04-19 01:29:04 +08:00
private void updatePalette()
{
palette.Clear();
if (Colours.Any())
{
palette.FadeIn(fade_duration, Easing.OutQuint);
placeholder.FadeOut(fade_duration, Easing.OutQuint);
}
else
{
palette.FadeOut(fade_duration, Easing.OutQuint);
placeholder.FadeIn(fade_duration, Easing.OutQuint);
}
2021-04-19 01:29:04 +08:00
foreach (var item in Colours)
{
palette.Add(new ColourDisplay
{
Current = { Value = item }
});
}
reindexItems();
}
private void reindexItems()
{
int index = 1;
foreach (var colour in palette)
{
2021-04-19 01:56:03 +08:00
colour.ColourName = $"{colourNamePrefix} {index}";
2021-04-19 01:29:04 +08:00
index += 1;
}
}
}
}