// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System.Linq; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Game.Graphics.Containers; using osuTK; namespace osu.Game.Collections { /// /// Visualises a list of s. /// public class DrawableCollectionList : OsuRearrangeableListContainer { private Scroll scroll; protected override ScrollContainer CreateScrollContainer() => scroll = new Scroll(); protected override FillFlowContainer> CreateListFillFlowContainer() => new Flow { DragActive = { BindTarget = DragActive } }; protected override OsuRearrangeableListItem CreateOsuDrawable(BeatmapCollection item) { if (item == scroll.PlaceholderItem.Model) return scroll.ReplacePlaceholder(); return new DrawableCollectionListItem(item, true); } /// /// The scroll container for this . /// Contains the main flow of and attaches a placeholder item to the end of the list. /// /// /// Use to transfer the placeholder into the main list. /// private class Scroll : OsuScrollContainer { /// /// The currently-displayed placeholder item. /// public DrawableCollectionListItem PlaceholderItem { get; private set; } protected override Container Content => content; private readonly Container content; private readonly Container 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 { 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); } /// /// Replaces the current with a new one, and returns the previous. /// /// The current . public DrawableCollectionListItem ReplacePlaceholder() { var previous = PlaceholderItem; placeholderContainer.Clear(false); placeholderContainer.Add(PlaceholderItem = new DrawableCollectionListItem(new BeatmapCollection(), false)); return previous; } } /// /// The flow of . Disables layout easing unless a drag is in progress. /// private class Flow : FillFlowContainer> { public readonly IBindable DragActive = new Bindable(); public Flow() { Spacing = new Vector2(0, 5); LayoutEasing = Easing.OutQuint; } protected override void LoadComplete() { base.LoadComplete(); DragActive.BindValueChanged(active => LayoutDuration = active.NewValue ? 200 : 0); } } } }