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.
|
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-06-20 18:20:46 +00:00
|
|
|
using System.Collections.Specialized;
|
2021-04-18 18:15:30 +00:00
|
|
|
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;
|
2021-07-11 17:48:21 +00:00
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Game.Graphics.Containers;
|
2021-04-18 18:15:30 +00:00
|
|
|
using osu.Game.Graphics.Sprites;
|
2021-04-18 17:29:04 +00:00
|
|
|
using osuTK;
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.UserInterfaceV2
|
|
|
|
{
|
2021-04-19 03:37:56 +00:00
|
|
|
/// <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
|
|
|
|
{
|
2021-06-20 17:37:09 +00:00
|
|
|
public BindableList<Colour4> Colours { get; } = new BindableList<Colour4>();
|
2021-04-18 17:29:04 +00:00
|
|
|
|
2021-04-18 17:56:03 +00:00
|
|
|
private string colourNamePrefix = "Colour";
|
|
|
|
|
|
|
|
public string ColourNamePrefix
|
|
|
|
{
|
|
|
|
get => colourNamePrefix;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (colourNamePrefix == value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
colourNamePrefix = value;
|
|
|
|
|
|
|
|
if (IsLoaded)
|
|
|
|
reindexItems();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
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;
|
2021-07-11 17:48:21 +00:00
|
|
|
AutoSizeDuration = fade_duration;
|
|
|
|
AutoSizeEasing = Easing.OutQuint;
|
2021-04-18 17:29:04 +00:00
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
InternalChild = palette = new FillFlowContainer
|
2021-04-18 17:29:04 +00:00
|
|
|
{
|
2021-07-11 17:48:21 +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();
|
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
Colours.BindCollectionChanged((_, args) =>
|
|
|
|
{
|
|
|
|
if (args.Action != NotifyCollectionChangedAction.Replace)
|
|
|
|
updatePalette();
|
|
|
|
}, true);
|
2021-04-18 18:15:30 +00:00
|
|
|
FinishTransforms(true);
|
2021-04-18 17:29:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 18:15:30 +00:00
|
|
|
private const int fade_duration = 200;
|
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
private void updatePalette()
|
2021-04-18 17:29:04 +00:00
|
|
|
{
|
|
|
|
palette.Clear();
|
|
|
|
|
2021-06-20 18:20:46 +00:00
|
|
|
for (int i = 0; i < Colours.Count; ++i)
|
2021-04-18 17:29:04 +00:00
|
|
|
{
|
2021-06-20 18:20:46 +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
|
|
|
{
|
2021-06-20 18:20:46 +00:00
|
|
|
Current = { Value = Colours[colourIndex] }
|
2021-04-18 17:29:04 +00:00
|
|
|
});
|
2021-06-20 18:20:46 +00:00
|
|
|
|
|
|
|
display.Current.BindValueChanged(colour => Colours[colourIndex] = colour.NewValue);
|
2021-07-11 18:02:39 +00:00
|
|
|
display.DeleteRequested += colourDeletionRequested;
|
2021-04-18 17:29:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
palette.Add(new AddColourButton
|
|
|
|
{
|
|
|
|
Action = () => Colours.Add(Colour4.White)
|
|
|
|
});
|
|
|
|
|
2021-04-18 17:29:04 +00:00
|
|
|
reindexItems();
|
|
|
|
}
|
|
|
|
|
2021-07-11 18:02:39 +00:00
|
|
|
private void colourDeletionRequested(ColourDisplay display) => Colours.RemoveAt(palette.IndexOf(display));
|
|
|
|
|
2021-04-18 17:29:04 +00:00
|
|
|
private void reindexItems()
|
|
|
|
{
|
|
|
|
int index = 1;
|
|
|
|
|
2021-07-11 17:48:21 +00:00
|
|
|
foreach (var colourDisplay in colourDisplays)
|
2021-04-18 17:29:04 +00:00
|
|
|
{
|
2021-07-11 17:48:21 +00:00
|
|
|
colourDisplay.ColourName = $"{colourNamePrefix} {index}";
|
2021-04-18 17:29:04 +00:00
|
|
|
index += 1;
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 17:48:21 +00:00
|
|
|
|
2021-07-11 18:14:42 +00:00
|
|
|
internal class AddColourButton : CompositeDrawable
|
2021-07-11 17:48:21 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|