1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-21 22:47:24 +08:00
osu-lazer/osu.Game/Collections/DrawableCollectionList.cs

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

128 lines
4.6 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.
2022-06-17 15:37:17 +08:00
#nullable disable
using System.Linq;
using osu.Framework.Bindables;
2020-09-05 02:52:07 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2022-07-27 14:59:36 +08:00
using osu.Game.Beatmaps;
2020-09-05 02:52:07 +08:00
using osu.Game.Graphics.Containers;
using osuTK;
namespace osu.Game.Collections
{
2020-09-08 15:50:51 +08:00
/// <summary>
2022-07-27 14:59:36 +08:00
/// Visualises a list of <see cref="RealmBeatmapCollection"/>s.
2020-09-08 15:50:51 +08:00
/// </summary>
2022-07-27 14:59:36 +08:00
public class DrawableCollectionList : OsuRearrangeableListContainer<RealmBeatmapCollection>
2020-09-05 02:52:07 +08:00
{
private Scroll scroll;
protected override ScrollContainer<Drawable> CreateScrollContainer() => scroll = new Scroll();
2020-09-05 02:52:07 +08:00
2022-07-27 14:59:36 +08:00
protected override FillFlowContainer<RearrangeableListItem<RealmBeatmapCollection>> CreateListFillFlowContainer() => new Flow
2020-09-05 02:52:07 +08:00
{
DragActive = { BindTarget = DragActive }
2020-09-05 02:52:07 +08:00
};
2022-07-27 14:59:36 +08:00
// TODO: source from realm
protected override OsuRearrangeableListItem<RealmBeatmapCollection> CreateOsuDrawable(RealmBeatmapCollection item)
{
2022-07-27 14:59:36 +08:00
if (item.ID == scroll.PlaceholderItem.Model.ID)
return scroll.ReplacePlaceholder();
return new DrawableCollectionListItem(item, true);
}
2020-09-08 15:50:51 +08:00
/// <summary>
/// The scroll container for this <see cref="DrawableCollectionList"/>.
/// Contains the main flow of <see cref="DrawableCollectionListItem"/> and attaches a placeholder item to the end of the list.
/// </summary>
/// <remarks>
/// Use <see cref="ReplacePlaceholder"/> to transfer the placeholder into the main list.
/// </remarks>
private class Scroll : OsuScrollContainer
{
2020-09-08 15:50:51 +08:00
/// <summary>
/// The currently-displayed placeholder item.
/// </summary>
public DrawableCollectionListItem PlaceholderItem { get; private set; }
protected override Container<Drawable> Content => content;
private readonly Container content;
private readonly Container<DrawableCollectionListItem> placeholderContainer;
public Scroll()
{
ScrollbarVisible = false;
Padding = new MarginPadding(10);
base.Content.Add(new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
LayoutDuration = 200,
LayoutEasing = Easing.OutQuint,
Children = new Drawable[]
{
content = new Container { RelativeSizeAxes = Axes.X },
placeholderContainer = new Container<DrawableCollectionListItem>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
}
}
});
ReplacePlaceholder();
}
protected override void Update()
{
base.Update();
// AutoSizeAxes cannot be used as the height should represent the post-layout-transform height at all times, so that the placeholder doesn't bounce around.
content.Height = ((Flow)Child).Children.Sum(c => c.DrawHeight + 5);
}
/// <summary>
/// Replaces the current <see cref="PlaceholderItem"/> with a new one, and returns the previous.
/// </summary>
2020-09-08 15:50:51 +08:00
/// <returns>The current <see cref="PlaceholderItem"/>.</returns>
public DrawableCollectionListItem ReplacePlaceholder()
{
var previous = PlaceholderItem;
placeholderContainer.Clear(false);
2022-07-27 14:59:36 +08:00
placeholderContainer.Add(PlaceholderItem = new DrawableCollectionListItem(new RealmBeatmapCollection(), false));
return previous;
}
}
2020-09-08 15:50:51 +08:00
/// <summary>
/// The flow of <see cref="DrawableCollectionListItem"/>. Disables layout easing unless a drag is in progress.
/// </summary>
2022-07-27 14:59:36 +08:00
private class Flow : FillFlowContainer<RearrangeableListItem<RealmBeatmapCollection>>
{
public readonly IBindable<bool> DragActive = new Bindable<bool>();
public Flow()
{
Spacing = new Vector2(0, 5);
LayoutEasing = Easing.OutQuint;
}
protected override void LoadComplete()
{
base.LoadComplete();
DragActive.BindValueChanged(active => LayoutDuration = active.NewValue ? 200 : 0);
}
}
2020-09-05 02:52:07 +08:00
}
}