osu/osu.Game/Graphics/UserInterfaceV2/ColourPalette.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

186 lines
5.8 KiB
C#
Raw Normal View History

2021-04-18 17:29:04 +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.
2022-06-17 07:37:17 +00:00
#nullable disable
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
2021-04-18 17:29:04 +00:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Localisation;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
2021-04-18 17:29:04 +00:00
using osuTK;
namespace osu.Game.Graphics.UserInterfaceV2
{
/// <summary>
/// A component which displays a collection of colours in individual <see cref="ColourDisplay"/>s.
/// </summary>
2021-04-18 17:29:04 +00:00
public class ColourPalette : CompositeDrawable
{
public BindableList<Colour4> Colours { get; } = new BindableList<Colour4>();
2021-04-18 17:29:04 +00:00
private LocalisableString colourNamePrefix = "Colour";
2021-04-18 17:56:03 +00:00
public LocalisableString ColourNamePrefix
2021-04-18 17:56:03 +00:00
{
get => colourNamePrefix;
set
{
if (colourNamePrefix == value)
return;
colourNamePrefix = value;
if (IsLoaded)
reindexItems();
}
}
private FillFlowContainer palette;
private IEnumerable<ColourDisplay> colourDisplays => palette.OfType<ColourDisplay>();
2021-04-18 17:29:04 +00:00
[BackgroundDependencyLoader]
private void load()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AutoSizeDuration = fade_duration;
AutoSizeEasing = Easing.OutQuint;
2021-04-18 17:29:04 +00:00
InternalChild = palette = new FillFlowContainer
2021-04-18 17:29:04 +00:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(10),
Direction = FillDirection.Full
2021-04-18 17:29:04 +00:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
Colours.BindCollectionChanged((_, args) =>
{
if (args.Action != NotifyCollectionChangedAction.Replace)
updatePalette();
}, true);
FinishTransforms(true);
2021-04-18 17:29:04 +00:00
}
private const int fade_duration = 200;
private void updatePalette()
2021-04-18 17:29:04 +00:00
{
palette.Clear();
for (int i = 0; i < Colours.Count; ++i)
2021-04-18 17:29:04 +00:00
{
// copy to avoid accesses to modified closure.
int colourIndex = i;
ColourDisplay display;
palette.Add(display = new ColourDisplay
2021-04-18 17:29:04 +00:00
{
Current = { Value = Colours[colourIndex] }
2021-04-18 17:29:04 +00:00
});
display.Current.BindValueChanged(colour => Colours[colourIndex] = colour.NewValue);
display.DeleteRequested += colourDeletionRequested;
2021-04-18 17:29:04 +00:00
}
palette.Add(new AddColourButton
{
Action = () => Colours.Add(Colour4.White)
});
2021-04-18 17:29:04 +00:00
reindexItems();
}
private void colourDeletionRequested(ColourDisplay display) => Colours.RemoveAt(palette.IndexOf(display));
2021-04-18 17:29:04 +00:00
private void reindexItems()
{
int index = 1;
foreach (var colourDisplay in colourDisplays)
2021-04-18 17:29:04 +00:00
{
colourDisplay.ColourName = $"{colourNamePrefix} {index}";
2021-04-18 17:29:04 +00:00
index += 1;
}
}
internal class AddColourButton : CompositeDrawable
{
public Action Action
{
set => circularButton.Action = value;
}
private readonly OsuClickableContainer circularButton;
public AddColourButton()
{
AutoSizeAxes = Axes.Y;
Width = 100;
InternalChild = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
circularButton = new OsuClickableContainer
{
RelativeSizeAxes = Axes.X,
Height = 100,
CornerRadius = 50,
Masking = true,
BorderThickness = 5,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Colour4.Transparent,
AlwaysPresent = true
},
new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(20),
Icon = FontAwesome.Solid.Plus
}
}
},
new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = "New"
}
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
circularButton.BorderColour = colours.BlueDarker;
}
}
2021-04-18 17:29:04 +00:00
}
}