2020-09-04 18:52:07 +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.
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using osu.Framework.Allocation;
|
2020-09-08 07:43:07 +00:00
|
|
|
using osu.Framework.Bindables;
|
2020-09-04 18:52:07 +00:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
using osu.Framework.Input.Events;
|
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
|
|
|
using osu.Game.Overlays;
|
|
|
|
using osuTK;
|
|
|
|
using osuTK.Graphics;
|
|
|
|
|
|
|
|
namespace osu.Game.Collections
|
|
|
|
{
|
2020-09-08 07:50:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Visualises a <see cref="BeatmapCollection"/> inside a <see cref="DrawableCollectionList"/>.
|
|
|
|
/// </summary>
|
2020-09-04 18:55:43 +00:00
|
|
|
public class DrawableCollectionListItem : OsuRearrangeableListItem<BeatmapCollection>
|
2020-09-04 18:52:07 +00:00
|
|
|
{
|
|
|
|
private const float item_height = 35;
|
2020-09-08 04:02:58 +00:00
|
|
|
private const float button_width = item_height * 0.75f;
|
|
|
|
|
2020-09-08 08:05:43 +00:00
|
|
|
/// <summary>
|
2020-09-09 06:31:08 +00:00
|
|
|
/// Whether the <see cref="BeatmapCollection"/> currently exists inside the <see cref="CollectionManager"/>.
|
2020-09-08 08:05:43 +00:00
|
|
|
/// </summary>
|
|
|
|
public IBindable<bool> IsCreated => isCreated;
|
|
|
|
|
2020-09-08 07:43:07 +00:00
|
|
|
private readonly Bindable<bool> isCreated = new Bindable<bool>();
|
|
|
|
|
2020-09-08 07:50:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Creates a new <see cref="DrawableCollectionListItem"/>.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="item">The <see cref="BeatmapCollection"/>.</param>
|
2020-09-09 06:31:08 +00:00
|
|
|
/// <param name="isCreated">Whether <paramref name="item"/> currently exists inside the <see cref="CollectionManager"/>.</param>
|
2020-09-08 07:43:07 +00:00
|
|
|
public DrawableCollectionListItem(BeatmapCollection item, bool isCreated)
|
2020-09-04 18:52:07 +00:00
|
|
|
: base(item)
|
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
this.isCreated.Value = isCreated;
|
|
|
|
|
|
|
|
ShowDragHandle.BindTo(this.isCreated);
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 07:43:07 +00:00
|
|
|
protected override Drawable CreateContent() => new ItemContent(Model)
|
|
|
|
{
|
|
|
|
IsCreated = { BindTarget = isCreated }
|
|
|
|
};
|
2020-09-04 18:52:07 +00:00
|
|
|
|
2020-09-08 07:50:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The main content of the <see cref="DrawableCollectionListItem"/>.
|
|
|
|
/// </summary>
|
2020-09-04 18:52:07 +00:00
|
|
|
private class ItemContent : CircularContainer
|
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
public readonly Bindable<bool> IsCreated = new Bindable<bool>();
|
|
|
|
|
|
|
|
private readonly IBindable<string> collectionName;
|
2020-09-04 18:52:07 +00:00
|
|
|
private readonly BeatmapCollection collection;
|
|
|
|
|
2020-09-09 06:39:15 +00:00
|
|
|
[Resolved(CanBeNull = true)]
|
2020-09-09 06:31:08 +00:00
|
|
|
private CollectionManager collectionManager { get; set; }
|
2020-09-08 07:43:07 +00:00
|
|
|
|
2020-09-08 08:05:31 +00:00
|
|
|
private Container textBoxPaddingContainer;
|
2020-09-04 18:52:07 +00:00
|
|
|
private ItemTextBox textBox;
|
|
|
|
|
|
|
|
public ItemContent(BeatmapCollection collection)
|
|
|
|
{
|
|
|
|
this.collection = collection;
|
|
|
|
|
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
Height = item_height;
|
|
|
|
Masking = true;
|
2020-09-08 07:43:07 +00:00
|
|
|
|
|
|
|
collectionName = collection.Name.GetBoundCopy();
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2022-01-15 00:06:39 +00:00
|
|
|
private void load()
|
2020-09-04 18:52:07 +00:00
|
|
|
{
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
new DeleteButton(collection)
|
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
Origin = Anchor.CentreRight,
|
2020-09-08 07:43:07 +00:00
|
|
|
IsCreated = { BindTarget = IsCreated },
|
2020-09-04 18:52:07 +00:00
|
|
|
IsTextBoxHovered = v => textBox.ReceivePositionalInputAt(v)
|
|
|
|
},
|
2020-09-08 08:05:31 +00:00
|
|
|
textBoxPaddingContainer = new Container
|
2020-09-04 18:52:07 +00:00
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2020-09-08 04:02:58 +00:00
|
|
|
Padding = new MarginPadding { Right = button_width },
|
2020-09-04 18:52:07 +00:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
textBox = new ItemTextBox
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Size = Vector2.One,
|
|
|
|
CornerRadius = item_height / 2,
|
2020-09-08 07:43:07 +00:00
|
|
|
PlaceholderText = IsCreated.Value ? string.Empty : "Create a new collection"
|
2020-09-04 18:52:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-09-08 07:43:07 +00:00
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2020-09-08 08:05:31 +00:00
|
|
|
|
2021-12-23 04:21:10 +00:00
|
|
|
// Bind late, as the collection name may change externally while still loading.
|
|
|
|
textBox.Current = collection.Name;
|
|
|
|
|
2020-09-08 07:43:07 +00:00
|
|
|
collectionName.BindValueChanged(_ => createNewCollection(), true);
|
2020-09-08 08:05:31 +00:00
|
|
|
IsCreated.BindValueChanged(created => textBoxPaddingContainer.Padding = new MarginPadding { Right = created.NewValue ? button_width : 0 }, true);
|
2020-09-08 07:43:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void createNewCollection()
|
|
|
|
{
|
|
|
|
if (IsCreated.Value)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(collectionName.Value))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Add the new collection and disable our placeholder. If all text is removed, the placeholder should not show back again.
|
2020-09-09 06:39:15 +00:00
|
|
|
collectionManager?.Collections.Add(collection);
|
2020-09-08 07:43:07 +00:00
|
|
|
textBox.PlaceholderText = string.Empty;
|
|
|
|
|
|
|
|
// When this item changes from placeholder to non-placeholder (via changing containers), its textbox will lose focus, so it needs to be re-focused.
|
|
|
|
Schedule(() => GetContainingInputManager().ChangeFocus(textBox));
|
|
|
|
|
|
|
|
IsCreated.Value = true;
|
|
|
|
}
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private class ItemTextBox : OsuTextBox
|
|
|
|
{
|
|
|
|
protected override float LeftRightPadding => item_height / 2;
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
2021-12-10 05:15:00 +00:00
|
|
|
BackgroundUnfocused = colours.GreySeaFoamDarker.Darken(0.5f);
|
|
|
|
BackgroundFocused = colours.GreySeaFoam;
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 13:47:19 +00:00
|
|
|
public class DeleteButton : CompositeDrawable
|
2020-09-04 18:52:07 +00:00
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
public readonly IBindable<bool> IsCreated = new Bindable<bool>();
|
|
|
|
|
2020-09-04 18:52:07 +00:00
|
|
|
public Func<Vector2, bool> IsTextBoxHovered;
|
|
|
|
|
|
|
|
[Resolved(CanBeNull = true)]
|
2022-04-18 09:09:14 +00:00
|
|
|
private IDialogOverlay dialogOverlay { get; set; }
|
2020-09-04 18:52:07 +00:00
|
|
|
|
2020-09-09 06:39:15 +00:00
|
|
|
[Resolved(CanBeNull = true)]
|
2020-09-09 06:31:08 +00:00
|
|
|
private CollectionManager collectionManager { get; set; }
|
2020-09-08 05:38:25 +00:00
|
|
|
|
2020-09-04 18:52:07 +00:00
|
|
|
private readonly BeatmapCollection collection;
|
|
|
|
|
2020-09-08 07:43:07 +00:00
|
|
|
private Drawable fadeContainer;
|
2020-09-04 18:52:07 +00:00
|
|
|
private Drawable background;
|
|
|
|
|
|
|
|
public DeleteButton(BeatmapCollection collection)
|
|
|
|
{
|
|
|
|
this.collection = collection;
|
2020-09-08 04:02:58 +00:00
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
|
|
|
|
Width = button_width + item_height / 2; // add corner radius to cover with fill
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
InternalChild = fadeContainer = new Container
|
2020-09-04 18:52:07 +00:00
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Alpha = 0.1f,
|
|
|
|
Children = new[]
|
2020-09-04 18:52:07 +00:00
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
background = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Colour = colours.Red
|
|
|
|
},
|
|
|
|
new SpriteIcon
|
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreRight,
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
X = -button_width * 0.6f,
|
|
|
|
Size = new Vector2(10),
|
|
|
|
Icon = FontAwesome.Solid.Trash
|
|
|
|
}
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-08 07:43:07 +00:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
IsCreated.BindValueChanged(created => Alpha = created.NewValue ? 1 : 0, true);
|
|
|
|
}
|
|
|
|
|
2020-09-04 18:52:07 +00:00
|
|
|
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => base.ReceivePositionalInputAt(screenSpacePos) && !IsTextBoxHovered(screenSpacePos);
|
|
|
|
|
|
|
|
protected override bool OnHover(HoverEvent e)
|
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
fadeContainer.FadeTo(1f, 100, Easing.Out);
|
2020-09-04 18:52:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnHoverLost(HoverLostEvent e)
|
|
|
|
{
|
2020-09-08 07:43:07 +00:00
|
|
|
fadeContainer.FadeTo(0.1f, 100);
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override bool OnClick(ClickEvent e)
|
|
|
|
{
|
|
|
|
background.FlashColour(Color4.White, 150);
|
2020-09-08 05:38:25 +00:00
|
|
|
|
|
|
|
if (collection.Beatmaps.Count == 0)
|
|
|
|
deleteCollection();
|
|
|
|
else
|
|
|
|
dialogOverlay?.Push(new DeleteCollectionDialog(collection, deleteCollection));
|
|
|
|
|
2020-09-04 18:52:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-09-08 05:38:25 +00:00
|
|
|
|
2020-09-09 06:39:15 +00:00
|
|
|
private void deleteCollection() => collectionManager?.Collections.Remove(collection);
|
2020-09-04 18:52:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|