1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 14:42:55 +08:00
osu-lazer/osu.Game/Overlays/Music/PlaylistList.cs

269 lines
8.8 KiB
C#
Raw Normal View History

// 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.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.IEnumerableExtensions;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Music
{
public class PlaylistList : CompositeDrawable
{
public Action<BeatmapSetInfo> Selected;
2018-04-13 17:19:50 +08:00
private readonly ItemsScrollContainer items;
public PlaylistList()
{
InternalChild = items = new ItemsScrollContainer
{
RelativeSizeAxes = Axes.Both,
Selected = set => Selected?.Invoke(set),
2018-04-13 17:19:50 +08:00
};
}
public new MarginPadding Padding
{
get => base.Padding;
set => base.Padding = value;
2018-04-13 17:19:50 +08:00
}
public BeatmapSetInfo FirstVisibleSet => items.FirstVisibleSet;
public void Filter(string searchTerm) => items.SearchTerm = searchTerm;
private class ItemsScrollContainer : OsuScrollContainer
{
public Action<BeatmapSetInfo> Selected;
2018-04-13 17:19:50 +08:00
private readonly SearchContainer search;
private readonly FillFlowContainer<PlaylistItem> items;
private readonly IBindable<WorkingBeatmap> beatmapBacking = new Bindable<WorkingBeatmap>();
private IBindableList<BeatmapSetInfo> beatmaps;
[Resolved]
private MusicController musicController { get; set; }
2018-04-13 17:19:50 +08:00
public ItemsScrollContainer()
{
Children = new Drawable[]
{
search = new SearchContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
items = new ItemSearchContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
}
}
};
}
[BackgroundDependencyLoader]
2019-10-08 00:44:22 +08:00
private void load(IBindable<WorkingBeatmap> beatmap)
2018-04-13 17:19:50 +08:00
{
beatmaps = musicController.BeatmapSets.GetBoundCopy();
beatmaps.ItemsAdded += i => i.ForEach(addBeatmapSet);
beatmaps.ItemsRemoved += i => i.ForEach(removeBeatmapSet);
beatmaps.ForEach(addBeatmapSet);
2018-04-13 17:19:50 +08:00
beatmapBacking.BindTo(beatmap);
beatmapBacking.ValueChanged += _ => Scheduler.AddOnce(updateSelectedSet);
2018-04-13 17:19:50 +08:00
}
private void addBeatmapSet(BeatmapSetInfo obj)
{
if (obj == draggedItem?.BeatmapSetInfo) return;
Schedule(() => items.Insert(items.Count - 1, new PlaylistItem(obj) { OnSelect = set => Selected?.Invoke(set) }));
}
2018-04-13 17:19:50 +08:00
private void removeBeatmapSet(BeatmapSetInfo obj)
2018-04-13 17:19:50 +08:00
{
if (obj == draggedItem?.BeatmapSetInfo) return;
Schedule(() =>
{
var itemToRemove = items.FirstOrDefault(i => i.BeatmapSetInfo.ID == obj.ID);
if (itemToRemove != null)
items.Remove(itemToRemove);
});
}
2018-04-13 17:19:50 +08:00
private void updateSelectedSet()
2018-04-13 17:19:50 +08:00
{
foreach (PlaylistItem s in items.Children)
2019-10-06 04:53:05 +08:00
{
2018-05-25 10:00:56 +08:00
s.Selected = s.BeatmapSetInfo.ID == beatmapBacking.Value.BeatmapSetInfo?.ID;
2019-10-08 00:44:22 +08:00
if (s.Selected)
2019-10-06 04:53:05 +08:00
ScrollIntoView(s);
}
}
public string SearchTerm
{
get => search.SearchTerm;
set => search.SearchTerm = value;
2018-04-13 17:19:50 +08:00
}
public BeatmapSetInfo FirstVisibleSet => items.FirstOrDefault(i => i.MatchingFilter)?.BeatmapSetInfo;
private Vector2 nativeDragPosition;
private PlaylistItem draggedItem;
private int? dragDestination;
2018-10-02 11:02:47 +08:00
protected override bool OnDragStart(DragStartEvent e)
2018-04-13 17:19:50 +08:00
{
nativeDragPosition = e.ScreenSpaceMousePosition;
2018-04-13 17:19:50 +08:00
draggedItem = items.FirstOrDefault(d => d.IsDraggable);
2018-10-02 11:02:47 +08:00
return draggedItem != null || base.OnDragStart(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override bool OnDrag(DragEvent e)
2018-04-13 17:19:50 +08:00
{
nativeDragPosition = e.ScreenSpaceMousePosition;
2018-04-13 17:19:50 +08:00
if (draggedItem == null)
2018-10-02 11:02:47 +08:00
return base.OnDrag(e);
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
return true;
}
2018-10-02 11:02:47 +08:00
protected override bool OnDragEnd(DragEndEvent e)
2018-04-13 17:19:50 +08:00
{
nativeDragPosition = e.ScreenSpaceMousePosition;
2018-04-13 17:19:50 +08:00
if (draggedItem == null)
return base.OnDragEnd(e);
if (dragDestination != null)
musicController.ChangeBeatmapSetPosition(draggedItem.BeatmapSetInfo, dragDestination.Value);
draggedItem = null;
dragDestination = null;
return true;
2018-04-13 17:19:50 +08:00
}
protected override void Update()
{
base.Update();
if (draggedItem == null)
return;
updateScrollPosition();
updateDragPosition();
}
private void updateScrollPosition()
{
const float start_offset = 10;
const double max_power = 50;
const double exp_base = 1.05;
var localPos = ToLocalSpace(nativeDragPosition);
if (localPos.Y < start_offset)
{
if (Current <= 0)
return;
var power = Math.Min(max_power, Math.Abs(start_offset - localPos.Y));
ScrollBy(-(float)Math.Pow(exp_base, power));
}
else if (localPos.Y > DrawHeight - start_offset)
{
if (IsScrolledToEnd())
return;
var power = Math.Min(max_power, Math.Abs(DrawHeight - start_offset - localPos.Y));
ScrollBy((float)Math.Pow(exp_base, power));
}
}
private void updateDragPosition()
{
var itemsPos = items.ToLocalSpace(nativeDragPosition);
int srcIndex = (int)items.GetLayoutPosition(draggedItem);
// Find the last item with position < mouse position. Note we can't directly use
// the item positions as they are being transformed
float heightAccumulator = 0;
int dstIndex = 0;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
for (; dstIndex < items.Count; dstIndex++)
{
// Using BoundingBox here takes care of scale, paddings, etc...
heightAccumulator += items[dstIndex].BoundingBox.Height;
if (heightAccumulator > itemsPos.Y)
break;
}
dstIndex = Math.Clamp(dstIndex, 0, items.Count - 1);
2018-04-13 17:19:50 +08:00
if (srcIndex == dstIndex)
return;
if (srcIndex < dstIndex)
{
for (int i = srcIndex + 1; i <= dstIndex; i++)
items.SetLayoutPosition(items[i], i - 1);
}
else
{
for (int i = dstIndex; i < srcIndex; i++)
items.SetLayoutPosition(items[i], i + 1);
}
items.SetLayoutPosition(draggedItem, dstIndex);
dragDestination = dstIndex;
2018-04-13 17:19:50 +08:00
}
private class ItemSearchContainer : FillFlowContainer<PlaylistItem>, IHasFilterableChildren
{
2019-11-28 21:41:29 +08:00
public IEnumerable<string> FilterTerms => Array.Empty<string>();
2018-04-13 17:19:50 +08:00
public bool MatchingFilter
{
set
{
if (value)
InvalidateLayout();
}
}
2019-03-28 23:29:07 +08:00
public bool FilteringActive
{
set { }
}
2018-04-13 17:19:50 +08:00
public IEnumerable<IFilterable> FilterableChildren => Children;
public ItemSearchContainer()
{
LayoutDuration = 200;
LayoutEasing = Easing.OutQuint;
}
}
}
}
}