2017-05-01 14:03:11 +08:00
|
|
|
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-05-01 14:09:14 +08:00
|
|
|
|
using System.Linq;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
2017-05-01 14:03:11 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
using osu.Framework.Input;
|
2017-07-26 12:22:46 +08:00
|
|
|
|
using osu.Game.Beatmaps;
|
2017-07-12 17:57:44 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
using OpenTK;
|
2017-05-01 14:03:11 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Music
|
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
internal class PlaylistList : CompositeDrawable
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public Action<BeatmapSetInfo> OnSelect;
|
|
|
|
|
|
|
|
|
|
private readonly ItemsScrollContainer items;
|
2017-05-01 14:03:11 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public PlaylistList()
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
InternalChild = items = new ItemsScrollContainer
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
OnSelect = set => OnSelect?.Invoke(set)
|
|
|
|
|
};
|
2017-05-01 14:03:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public new MarginPadding Padding
|
2017-05-01 14:09:14 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
get { return base.Padding; }
|
|
|
|
|
set { base.Padding = value; }
|
2017-05-01 14:09:14 +08:00
|
|
|
|
}
|
2017-05-01 14:03:11 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public IEnumerable<BeatmapSetInfo> BeatmapSets { set { items.Sets = value; } }
|
2017-05-02 09:45:55 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public BeatmapSetInfo FirstVisibleSet => items.FirstVisibleSet;
|
|
|
|
|
public BeatmapSetInfo NextSet => items.NextSet;
|
|
|
|
|
public BeatmapSetInfo PreviousSet => items.PreviousSet;
|
2017-05-02 09:45:55 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public BeatmapSetInfo SelectedSet
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
get { return items.SelectedSet; }
|
|
|
|
|
set { items.SelectedSet = value; }
|
2017-05-01 14:03:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public void AddBeatmapSet(BeatmapSetInfo beatmapSet) => items.AddBeatmapSet(beatmapSet);
|
|
|
|
|
public bool RemoveBeatmapSet(BeatmapSetInfo beatmapSet) => items.RemoveBeatmapSet(beatmapSet);
|
2017-08-28 15:56:03 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public void Filter(string searchTerm) => items.SearchTerm = searchTerm;
|
2017-08-28 15:56:03 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
private class ItemsScrollContainer : OsuScrollContainer
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public Action<BeatmapSetInfo> OnSelect;
|
|
|
|
|
|
|
|
|
|
private readonly SearchContainer search;
|
|
|
|
|
private readonly FillFlowContainer<PlaylistItem> items;
|
|
|
|
|
|
|
|
|
|
public ItemsScrollContainer()
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
Children = new Drawable[]
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
search = new SearchContainer
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
Children = new Drawable[]
|
2017-05-01 14:03:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
items = new ItemSearchContainer
|
2017-05-02 09:45:55 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
},
|
2017-05-02 09:45:55 +08:00
|
|
|
|
}
|
2017-09-04 12:12:12 +08:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-05-02 09:45:55 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public IEnumerable<BeatmapSetInfo> Sets
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
items.Clear();
|
|
|
|
|
value.ForEach(AddBeatmapSet);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-22 15:26:47 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public string SearchTerm
|
|
|
|
|
{
|
|
|
|
|
get { return search.SearchTerm; }
|
|
|
|
|
set { search.SearchTerm = value; }
|
|
|
|
|
}
|
2017-07-22 15:26:47 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public void AddBeatmapSet(BeatmapSetInfo beatmapSet)
|
2017-05-02 15:26:11 +08:00
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
items.Add(new PlaylistItem(beatmapSet)
|
|
|
|
|
{
|
|
|
|
|
OnSelect = set => OnSelect?.Invoke(set),
|
|
|
|
|
Depth = items.Count
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool RemoveBeatmapSet(BeatmapSetInfo beatmapSet)
|
|
|
|
|
{
|
|
|
|
|
var itemToRemove = items.FirstOrDefault(i => i.BeatmapSetInfo.ID == beatmapSet.ID);
|
|
|
|
|
if (itemToRemove == null)
|
|
|
|
|
return false;
|
|
|
|
|
return items.Remove(itemToRemove);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public BeatmapSetInfo SelectedSet
|
|
|
|
|
{
|
|
|
|
|
get { return items.FirstOrDefault(i => i.Selected)?.BeatmapSetInfo; }
|
2017-05-02 15:26:11 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
2017-09-04 12:12:12 +08:00
|
|
|
|
foreach (PlaylistItem s in items.Children)
|
|
|
|
|
s.Selected = s.BeatmapSetInfo.ID == value?.ID;
|
2017-05-02 15:26:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-02 09:45:55 +08:00
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public BeatmapSetInfo FirstVisibleSet => items.FirstOrDefault(i => i.MatchingFilter)?.BeatmapSetInfo;
|
|
|
|
|
public BeatmapSetInfo NextSet => (items.SkipWhile(i => !i.Selected).Skip(1).FirstOrDefault() ?? items.FirstOrDefault())?.BeatmapSetInfo;
|
|
|
|
|
public BeatmapSetInfo PreviousSet => (items.TakeWhile(i => !i.Selected).LastOrDefault() ?? items.LastOrDefault())?.BeatmapSetInfo;
|
|
|
|
|
|
2017-09-05 09:37:49 +08:00
|
|
|
|
private Vector2 nativeDragPosition;
|
2017-09-05 09:29:51 +08:00
|
|
|
|
private PlaylistItem draggedItem;
|
|
|
|
|
|
2017-09-04 12:12:12 +08:00
|
|
|
|
protected override bool OnDragStart(InputState state)
|
|
|
|
|
{
|
2017-09-05 09:37:49 +08:00
|
|
|
|
nativeDragPosition = state.Mouse.NativeState.Position;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
draggedItem = items.FirstOrDefault(d => d.IsDraggable);
|
|
|
|
|
return draggedItem != null || base.OnDragStart(state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnDrag(InputState state)
|
|
|
|
|
{
|
2017-09-05 09:37:49 +08:00
|
|
|
|
nativeDragPosition = state.Mouse.NativeState.Position;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
if (draggedItem == null)
|
|
|
|
|
return base.OnDrag(state);
|
2017-09-04 13:58:28 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override bool OnDragEnd(InputState state)
|
|
|
|
|
{
|
2017-09-05 09:37:49 +08:00
|
|
|
|
nativeDragPosition = state.Mouse.NativeState.Position;
|
2017-09-04 13:58:28 +08:00
|
|
|
|
var handled = draggedItem != null || base.OnDragEnd(state);
|
|
|
|
|
draggedItem = null;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
return handled;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2017-09-04 12:12:12 +08:00
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
if (draggedItem == null)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-09-05 09:29:51 +08:00
|
|
|
|
updateScrollPosition();
|
|
|
|
|
updateDragPosition();
|
2017-09-04 13:58:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 09:29:51 +08:00
|
|
|
|
private void updateScrollPosition()
|
2017-09-04 13:58:28 +08:00
|
|
|
|
{
|
|
|
|
|
const float start_offset = 10;
|
|
|
|
|
const double max_power = 50;
|
|
|
|
|
const double exp_base = 1.05;
|
|
|
|
|
|
2017-09-05 09:37:49 +08:00
|
|
|
|
var localPos = ToLocalSpace(nativeDragPosition);
|
2017-09-04 13:58:28 +08:00
|
|
|
|
|
|
|
|
|
if (localPos.Y < start_offset)
|
|
|
|
|
{
|
2017-09-04 14:06:21 +08:00
|
|
|
|
if (Current <= 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
2017-09-04 14:06:21 +08:00
|
|
|
|
if (IsScrolledToEnd())
|
|
|
|
|
return;
|
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
var power = Math.Min(max_power, Math.Abs(DrawHeight - start_offset - localPos.Y));
|
|
|
|
|
ScrollBy((float)Math.Pow(exp_base, power));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-05 09:29:51 +08:00
|
|
|
|
private void updateDragPosition()
|
2017-09-04 13:58:28 +08:00
|
|
|
|
{
|
2017-09-05 09:37:49 +08:00
|
|
|
|
var itemsPos = items.ToLocalSpace(nativeDragPosition);
|
2017-09-04 13:58:28 +08:00
|
|
|
|
|
|
|
|
|
int srcIndex = (int)draggedItem.Depth;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
|
2017-09-04 13:20:40 +08:00
|
|
|
|
// 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;
|
2017-09-04 13:58:28 +08:00
|
|
|
|
int dstIndex = 0;
|
|
|
|
|
for (; dstIndex < items.Count; dstIndex++)
|
2017-09-04 13:20:40 +08:00
|
|
|
|
{
|
|
|
|
|
// Using BoundingBox here takes care of scale, paddings, etc...
|
2017-09-04 13:58:28 +08:00
|
|
|
|
heightAccumulator += items[dstIndex].BoundingBox.Height;
|
2017-09-04 13:20:40 +08:00
|
|
|
|
if (heightAccumulator > itemsPos.Y)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-08-28 15:22:18 +08:00
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
dstIndex = MathHelper.Clamp(dstIndex, 0, items.Count - 1);
|
2017-05-02 09:45:55 +08:00
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
if (srcIndex == dstIndex)
|
|
|
|
|
return;
|
2017-09-04 12:12:12 +08:00
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
if (srcIndex < dstIndex)
|
2017-09-04 12:12:12 +08:00
|
|
|
|
{
|
2017-09-04 13:58:28 +08:00
|
|
|
|
for (int i = srcIndex + 1; i <= dstIndex; i++)
|
2017-09-04 12:12:12 +08:00
|
|
|
|
items.ChangeChildDepth(items[i], i - 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-09-04 13:58:28 +08:00
|
|
|
|
for (int i = dstIndex; i < srcIndex; i++)
|
2017-09-04 12:12:12 +08:00
|
|
|
|
items.ChangeChildDepth(items[i], i + 1);
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-04 13:58:28 +08:00
|
|
|
|
items.ChangeChildDepth(draggedItem, dstIndex);
|
2017-09-04 12:12:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private class ItemSearchContainer : FillFlowContainer<PlaylistItem>, IHasFilterableChildren
|
2017-05-02 09:45:55 +08:00
|
|
|
|
{
|
2017-09-13 22:18:02 +08:00
|
|
|
|
public IEnumerable<string> FilterTerms => new string[] { };
|
2017-09-04 12:12:12 +08:00
|
|
|
|
public bool MatchingFilter
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (value)
|
|
|
|
|
InvalidateLayout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Compare with reversed ChildID and Depth
|
|
|
|
|
protected override int Compare(Drawable x, Drawable y) => base.Compare(y, x);
|
|
|
|
|
|
|
|
|
|
public IEnumerable<IFilterable> FilterableChildren => Children;
|
|
|
|
|
|
|
|
|
|
public ItemSearchContainer()
|
|
|
|
|
{
|
|
|
|
|
LayoutDuration = 200;
|
|
|
|
|
LayoutEasing = Easing.OutQuint;
|
|
|
|
|
}
|
2017-05-02 09:45:55 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-01 14:03:11 +08:00
|
|
|
|
}
|
2017-09-05 09:29:51 +08:00
|
|
|
|
}
|