Add support for removing colours from palette

This commit is contained in:
Bartłomiej Dach
2021-07-11 20:02:39 +02:00
parent 9a7537cd56
commit 3f005886d6
3 changed files with 81 additions and 44 deletions

View File

@ -5,6 +5,7 @@ using NUnit.Framework;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Utils; using osu.Framework.Utils;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.UserInterfaceV2; using osu.Game.Graphics.UserInterfaceV2;
using osuTK.Graphics; using osuTK.Graphics;
@ -34,6 +35,9 @@ namespace osu.Game.Tests.Visual.UserInterface
{ {
AddStep("create component", () => AddStep("create component", () =>
{ {
Child = new OsuContextMenuContainer
{
RelativeSizeAxes = Axes.Both,
Child = new Container Child = new Container
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
@ -46,6 +50,7 @@ namespace osu.Game.Tests.Visual.UserInterface
Origin = Anchor.Centre, Origin = Anchor.Centre,
ColourNamePrefix = "My colour #" ColourNamePrefix = "My colour #"
} }
}
}; };
component.Label = "a sample component"; component.Label = "a sample component";

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions; using osu.Framework.Extensions;
@ -12,6 +13,7 @@ using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osuTK; using osuTK;
namespace osu.Game.Graphics.UserInterfaceV2 namespace osu.Game.Graphics.UserInterfaceV2
@ -19,12 +21,17 @@ namespace osu.Game.Graphics.UserInterfaceV2
/// <summary> /// <summary>
/// A component which displays a colour along with related description text. /// A component which displays a colour along with related description text.
/// </summary> /// </summary>
public class ColourDisplay : CompositeDrawable, IHasCurrentValue<Colour4>, IHasPopover public class ColourDisplay : CompositeDrawable, IHasCurrentValue<Colour4>
{ {
/// <summary>
/// Invoked when the user has requested the colour corresponding to this <see cref="ColourDisplay"/>
/// to be removed from its palette.
/// </summary>
public event Action<ColourDisplay> DeleteRequested;
private readonly BindableWithCurrent<Colour4> current = new BindableWithCurrent<Colour4>(); private readonly BindableWithCurrent<Colour4> current = new BindableWithCurrent<Colour4>();
private Box fill; private OsuClickableContainer colourCircle;
private OsuSpriteText colourHexCode;
private OsuSpriteText colourName; private OsuSpriteText colourName;
public Bindable<Colour4> Current public Bindable<Colour4> Current
@ -63,12 +70,37 @@ namespace osu.Game.Graphics.UserInterfaceV2
Spacing = new Vector2(0, 10), Spacing = new Vector2(0, 10),
Children = new Drawable[] Children = new Drawable[]
{ {
new OsuClickableContainer colourCircle = new ColourCircle
{ {
RelativeSizeAxes = Axes.X, Current = { BindTarget = Current },
Height = 100, DeleteRequested = () => DeleteRequested?.Invoke(this)
CornerRadius = 50, },
Masking = true, colourName = new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
}
}
};
}
private class ColourCircle : OsuClickableContainer, IHasPopover, IHasContextMenu
{
public Bindable<Colour4> Current { get; } = new Bindable<Colour4>();
public Action DeleteRequested { get; set; }
private readonly Box fill;
private readonly OsuSpriteText colourHexCode;
public ColourCircle()
{
RelativeSizeAxes = Axes.X;
Height = 100;
CornerRadius = 50;
Masking = true;
Action = this.ShowPopover;
Children = new Drawable[] Children = new Drawable[]
{ {
fill = new Box fill = new Box
@ -81,15 +113,6 @@ namespace osu.Game.Graphics.UserInterfaceV2
Origin = Anchor.Centre, Origin = Anchor.Centre,
Font = OsuFont.Default.With(size: 12) Font = OsuFont.Default.With(size: 12)
} }
},
Action = this.ShowPopover
},
colourName = new OsuSpriteText
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre
}
}
}; };
} }
@ -97,14 +120,14 @@ namespace osu.Game.Graphics.UserInterfaceV2
{ {
base.LoadComplete(); base.LoadComplete();
current.BindValueChanged(_ => updateColour(), true); Current.BindValueChanged(_ => updateColour(), true);
} }
private void updateColour() private void updateColour()
{ {
fill.Colour = current.Value; fill.Colour = Current.Value;
colourHexCode.Text = current.Value.ToHex(); colourHexCode.Text = Current.Value.ToHex();
colourHexCode.Colour = OsuColour.ForegroundTextColourFor(current.Value); colourHexCode.Colour = OsuColour.ForegroundTextColourFor(Current.Value);
} }
public Popover GetPopover() => new OsuPopover(false) public Popover GetPopover() => new OsuPopover(false)
@ -114,5 +137,11 @@ namespace osu.Game.Graphics.UserInterfaceV2
Current = { BindTarget = Current } Current = { BindTarget = Current }
} }
}; };
public MenuItem[] ContextMenuItems => new MenuItem[]
{
new OsuMenuItem("Delete", MenuItemType.Destructive, () => DeleteRequested?.Invoke())
};
}
} }
} }

View File

@ -92,6 +92,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
}); });
display.Current.BindValueChanged(colour => Colours[colourIndex] = colour.NewValue); display.Current.BindValueChanged(colour => Colours[colourIndex] = colour.NewValue);
display.DeleteRequested += colourDeletionRequested;
} }
palette.Add(new AddColourButton palette.Add(new AddColourButton
@ -102,6 +103,8 @@ namespace osu.Game.Graphics.UserInterfaceV2
reindexItems(); reindexItems();
} }
private void colourDeletionRequested(ColourDisplay display) => Colours.RemoveAt(palette.IndexOf(display));
private void reindexItems() private void reindexItems()
{ {
int index = 1; int index = 1;