1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 09:17:48 +08:00
osu-lazer/osu.Game/Collections/DrawableCollectionListItem.cs

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

203 lines
7.2 KiB
C#
Raw Normal View History

2020-09-05 02:52:07 +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;
using osu.Framework.Allocation;
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.Graphics.UserInterface;
2020-09-05 02:52:07 +08:00
using osu.Framework.Input.Events;
2022-07-27 14:59:36 +08:00
using osu.Game.Database;
2020-09-05 02:52:07 +08:00
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 15:50:51 +08:00
/// <summary>
/// Visualises a <see cref="BeatmapCollection"/> inside a <see cref="DrawableCollectionList"/>.
2020-09-08 15:50:51 +08:00
/// </summary>
public class DrawableCollectionListItem : OsuRearrangeableListItem<Live<BeatmapCollection>>
2020-09-05 02:52:07 +08:00
{
private const float item_height = 35;
2020-09-08 12:02:58 +08:00
private const float button_width = item_height * 0.75f;
2020-09-08 15:50:51 +08:00
/// <summary>
/// Creates a new <see cref="DrawableCollectionListItem"/>.
/// </summary>
/// <param name="item">The <see cref="BeatmapCollection"/>.</param>
2022-07-27 14:59:36 +08:00
/// <param name="isCreated">Whether <paramref name="item"/> currently exists inside realm.</param>
public DrawableCollectionListItem(Live<BeatmapCollection> item, bool isCreated)
2020-09-05 02:52:07 +08:00
: base(item)
{
ShowDragHandle.Value = item.IsManaged;
2020-09-05 02:52:07 +08:00
}
protected override Drawable CreateContent() => new ItemContent(Model);
2020-09-05 02:52:07 +08:00
2020-09-08 15:50:51 +08:00
/// <summary>
/// The main content of the <see cref="DrawableCollectionListItem"/>.
/// </summary>
2020-09-05 02:52:07 +08:00
private class ItemContent : CircularContainer
{
private readonly Live<BeatmapCollection> collection;
private ItemTextBox textBox = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
2020-09-05 02:52:07 +08:00
public ItemContent(Live<BeatmapCollection> collection)
2020-09-05 02:52:07 +08:00
{
this.collection = collection;
RelativeSizeAxes = Axes.X;
Height = item_height;
Masking = true;
}
[BackgroundDependencyLoader]
2022-01-15 08:06:39 +08:00
private void load()
2020-09-05 02:52:07 +08:00
{
Children = new[]
2020-09-05 02:52:07 +08:00
{
collection.IsManaged
? new DeleteButton(collection)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
IsTextBoxHovered = v => textBox.ReceivePositionalInputAt(v)
}
: Empty(),
new Container
2020-09-05 02:52:07 +08:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = collection.IsManaged ? button_width : 0 },
2020-09-05 02:52:07 +08:00
Children = new Drawable[]
{
textBox = new ItemTextBox
{
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
CornerRadius = item_height / 2,
PlaceholderText = collection.IsManaged ? string.Empty : "Create a new collection"
2020-09-05 02:52:07 +08:00
},
}
},
};
}
protected override void LoadComplete()
{
base.LoadComplete();
// Bind late, as the collection name may change externally while still loading.
textBox.Current.Value = collection.PerformRead(c => c.IsValid ? c.Name : string.Empty);
textBox.OnCommit += onCommit;
}
private void onCommit(TextBox sender, bool newText)
{
if (collection.IsManaged)
collection.PerformWrite(c => c.Name = textBox.Current.Value);
else if (!string.IsNullOrEmpty(textBox.Current.Value))
realm.Write(r => r.Add(new BeatmapCollection(textBox.Current.Value)));
textBox.Text = string.Empty;
}
2020-09-05 02:52:07 +08:00
}
private class ItemTextBox : OsuTextBox
{
protected override float LeftRightPadding => item_height / 2;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
BackgroundUnfocused = colours.GreySeaFoamDarker.Darken(0.5f);
BackgroundFocused = colours.GreySeaFoam;
2020-09-05 02:52:07 +08:00
}
}
2020-09-07 21:47:19 +08:00
public class DeleteButton : CompositeDrawable
2020-09-05 02:52:07 +08:00
{
public Func<Vector2, bool> IsTextBoxHovered = null!;
2020-09-05 02:52:07 +08:00
[Resolved]
private IDialogOverlay? dialogOverlay { get; set; }
2020-09-05 02:52:07 +08:00
private readonly Live<BeatmapCollection> collection;
2020-09-05 02:52:07 +08:00
private Drawable fadeContainer = null!;
private Drawable background = null!;
2020-09-05 02:52:07 +08:00
public DeleteButton(Live<BeatmapCollection> collection)
2020-09-05 02:52:07 +08:00
{
this.collection = collection;
2020-09-08 12:02:58 +08:00
RelativeSizeAxes = Axes.Y;
Width = button_width + item_height / 2; // add corner radius to cover with fill
2020-09-05 02:52:07 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
InternalChild = fadeContainer = new Container
2020-09-05 02:52:07 +08:00
{
RelativeSizeAxes = Axes.Both,
Alpha = 0.1f,
Children = new[]
2020-09-05 02:52:07 +08: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-05 02:52:07 +08:00
}
};
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => base.ReceivePositionalInputAt(screenSpacePos) && !IsTextBoxHovered(screenSpacePos);
protected override bool OnHover(HoverEvent e)
{
fadeContainer.FadeTo(1f, 100, Easing.Out);
2020-09-05 02:52:07 +08:00
return false;
}
protected override void OnHoverLost(HoverLostEvent e)
{
fadeContainer.FadeTo(0.1f, 100);
2020-09-05 02:52:07 +08:00
}
protected override bool OnClick(ClickEvent e)
{
background.FlashColour(Color4.White, 150);
if (collection.PerformRead(c => c.BeatmapMD5Hashes.Count) == 0)
deleteCollection();
else
dialogOverlay?.Push(new DeleteCollectionDialog(collection, deleteCollection));
2020-09-05 02:52:07 +08:00
return true;
}
private void deleteCollection() => collection.PerformWrite(c => c.Realm.Remove(c));
2020-09-05 02:52:07 +08:00
}
}
}