1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 22:07:28 +08:00

Merge pull request #1202 from 2yangk23/rearrange-playlist

Rearranging support in playlist
This commit is contained in:
Dean Herbert 2017-09-05 11:09:24 +09:00 committed by GitHub
commit 770d04956f
3 changed files with 255 additions and 89 deletions

View File

@ -8,6 +8,7 @@ using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics; using osu.Game.Graphics;
@ -16,7 +17,7 @@ using OpenTK;
namespace osu.Game.Overlays.Music namespace osu.Game.Overlays.Music
{ {
internal class PlaylistItem : Container, IFilterable internal class PlaylistItem : Container, IFilterable, IDraggable
{ {
private const float fade_duration = 100; private const float fade_duration = 100;
@ -33,6 +34,8 @@ namespace osu.Game.Overlays.Music
public Action<BeatmapSetInfo> OnSelect; public Action<BeatmapSetInfo> OnSelect;
public bool IsDraggable => handle.IsHovered;
private bool selected; private bool selected;
public bool Selected public bool Selected
{ {
@ -68,15 +71,9 @@ namespace osu.Game.Overlays.Music
Children = new Drawable[] Children = new Drawable[]
{ {
handle = new SpriteIcon handle = new PlaylistItemHandle
{ {
Anchor = Anchor.TopLeft, Colour = colours.Gray5
Origin = Anchor.TopLeft,
Size = new Vector2(12),
Colour = colours.Gray5,
Icon = FontAwesome.fa_bars,
Alpha = 0f,
Margin = new MarginPadding { Left = 5, Top = 2 },
}, },
text = new OsuTextFlowContainer text = new OsuTextFlowContainer
{ {
@ -114,19 +111,19 @@ namespace osu.Game.Overlays.Music
}); });
} }
protected override bool OnHover(Framework.Input.InputState state) protected override bool OnHover(InputState state)
{ {
handle.FadeIn(fade_duration); handle.FadeIn(fade_duration);
return base.OnHover(state); return base.OnHover(state);
} }
protected override void OnHoverLost(Framework.Input.InputState state) protected override void OnHoverLost(InputState state)
{ {
handle.FadeOut(fade_duration); handle.FadeOut(fade_duration);
} }
protected override bool OnClick(Framework.Input.InputState state) protected override bool OnClick(InputState state)
{ {
OnSelect?.Invoke(BeatmapSetInfo); OnSelect?.Invoke(BeatmapSetInfo);
return true; return true;
@ -148,5 +145,27 @@ namespace osu.Game.Overlays.Music
this.FadeTo(matching ? 1 : 0, 200); this.FadeTo(matching ? 1 : 0, 200);
} }
} }
private class PlaylistItemHandle : SpriteIcon
{
public PlaylistItemHandle()
{
Anchor = Anchor.TopLeft;
Origin = Anchor.TopLeft;
Size = new Vector2(12);
Icon = FontAwesome.fa_bars;
Alpha = 0f;
Margin = new MarginPadding { Left = 5, Top = 2 };
}
}
}
public interface IDraggable : IDrawable
{
/// <summary>
/// Whether this <see cref="IDraggable"/> can be dragged in its current state.
/// </summary>
bool IsDraggable { get; }
} }
} }

View File

@ -4,104 +4,251 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Game.Beatmaps; using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
using OpenTK;
namespace osu.Game.Overlays.Music namespace osu.Game.Overlays.Music
{ {
internal class PlaylistList : Container internal class PlaylistList : CompositeDrawable
{ {
private readonly FillFlowContainer<PlaylistItem> items;
public IEnumerable<BeatmapSetInfo> BeatmapSets
{
set
{
items.Children = value.Select(item => new PlaylistItem(item) { OnSelect = itemSelected }).ToList();
}
}
public BeatmapSetInfo FirstVisibleSet => items.Children.FirstOrDefault(i => i.MatchingFilter)?.BeatmapSetInfo;
private void itemSelected(BeatmapSetInfo b)
{
OnSelect?.Invoke(b);
}
public Action<BeatmapSetInfo> OnSelect; public Action<BeatmapSetInfo> OnSelect;
private readonly SearchContainer search; private readonly ItemsScrollContainer items;
public void Filter(string searchTerm) => search.SearchTerm = searchTerm;
public BeatmapSetInfo SelectedItem
{
get { return items.Children.FirstOrDefault(i => i.Selected)?.BeatmapSetInfo; }
set
{
foreach (PlaylistItem s in items.Children)
s.Selected = s.BeatmapSetInfo.ID == value?.ID;
}
}
public PlaylistList() public PlaylistList()
{ {
Children = new Drawable[] InternalChild = items = new ItemsScrollContainer
{ {
new OsuScrollContainer RelativeSizeAxes = Axes.Both,
{ OnSelect = set => OnSelect?.Invoke(set)
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
{
search = new SearchContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
items = new ItemSearchContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
}
}
},
},
}; };
} }
public void AddBeatmapSet(BeatmapSetInfo beatmapSet) public new MarginPadding Padding
{ {
items.Add(new PlaylistItem(beatmapSet) { OnSelect = itemSelected }); get { return base.Padding; }
set { base.Padding = value; }
} }
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) public IEnumerable<BeatmapSetInfo> BeatmapSets { set { items.Sets = value; } }
public BeatmapSetInfo FirstVisibleSet => items.FirstVisibleSet;
public BeatmapSetInfo NextSet => items.NextSet;
public BeatmapSetInfo PreviousSet => items.PreviousSet;
public BeatmapSetInfo SelectedSet
{ {
PlaylistItem itemToRemove = items.Children.FirstOrDefault(item => item.BeatmapSetInfo.ID == beatmapSet.ID); get { return items.SelectedSet; }
if (itemToRemove != null) items.Remove(itemToRemove); set { items.SelectedSet = value; }
} }
private class ItemSearchContainer : FillFlowContainer<PlaylistItem>, IHasFilterableChildren public void AddBeatmapSet(BeatmapSetInfo beatmapSet) => items.AddBeatmapSet(beatmapSet);
public bool RemoveBeatmapSet(BeatmapSetInfo beatmapSet) => items.RemoveBeatmapSet(beatmapSet);
public void Filter(string searchTerm) => items.SearchTerm = searchTerm;
private class ItemsScrollContainer : OsuScrollContainer
{ {
public string[] FilterTerms => new string[] { }; public Action<BeatmapSetInfo> OnSelect;
public bool MatchingFilter
private readonly SearchContainer search;
private readonly FillFlowContainer<PlaylistItem> items;
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,
},
}
}
};
}
public IEnumerable<BeatmapSetInfo> Sets
{ {
set set
{ {
if (value) items.Clear();
InvalidateLayout(); value.ForEach(AddBeatmapSet);
} }
} }
public IEnumerable<IFilterable> FilterableChildren => Children; public string SearchTerm
public ItemSearchContainer()
{ {
LayoutDuration = 200; get { return search.SearchTerm; }
LayoutEasing = Easing.OutQuint; set { search.SearchTerm = value; }
}
public void AddBeatmapSet(BeatmapSetInfo beatmapSet)
{
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; }
set
{
foreach (PlaylistItem s in items.Children)
s.Selected = s.BeatmapSetInfo.ID == value?.ID;
}
}
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;
private Vector2 nativeDragPosition;
private PlaylistItem draggedItem;
protected override bool OnDragStart(InputState state)
{
nativeDragPosition = state.Mouse.NativeState.Position;
draggedItem = items.FirstOrDefault(d => d.IsDraggable);
return draggedItem != null || base.OnDragStart(state);
}
protected override bool OnDrag(InputState state)
{
nativeDragPosition = state.Mouse.NativeState.Position;
if (draggedItem == null)
return base.OnDrag(state);
return true;
}
protected override bool OnDragEnd(InputState state)
{
nativeDragPosition = state.Mouse.NativeState.Position;
var handled = draggedItem != null || base.OnDragEnd(state);
draggedItem = null;
return handled;
}
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)draggedItem.Depth;
// 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;
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 = MathHelper.Clamp(dstIndex, 0, items.Count - 1);
if (srcIndex == dstIndex)
return;
if (srcIndex < dstIndex)
{
for (int i = srcIndex + 1; i <= dstIndex; i++)
items.ChangeChildDepth(items[i], i - 1);
}
else
{
for (int i = dstIndex; i < srcIndex; i++)
items.ChangeChildDepth(items[i], i + 1);
}
items.ChangeChildDepth(draggedItem, dstIndex);
}
private class ItemSearchContainer : FillFlowContainer<PlaylistItem>, IHasFilterableChildren
{
public string[] FilterTerms => new string[] { };
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;
}
} }
} }
} }

View File

@ -92,7 +92,7 @@ namespace osu.Game.Overlays.Music
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
beatmapBacking.ValueChanged += b => list.SelectedItem = b?.BeatmapSetInfo; beatmapBacking.ValueChanged += b => list.SelectedSet = b?.BeatmapSetInfo;
beatmapBacking.TriggerChange(); beatmapBacking.TriggerChange();
} }
@ -126,24 +126,24 @@ namespace osu.Game.Overlays.Music
public void PlayPrevious() public void PlayPrevious()
{ {
var currentID = beatmapBacking.Value?.BeatmapSetInfo.ID ?? -1; var playable = list.PreviousSet;
var available = BeatmapSets.Reverse();
var playable = available.SkipWhile(b => b.ID != currentID).Skip(1).FirstOrDefault() ?? available.FirstOrDefault();
if (playable != null) if (playable != null)
{
playSpecified(playable.Beatmaps[0]); playSpecified(playable.Beatmaps[0]);
list.SelectedSet = playable;
}
} }
public void PlayNext() public void PlayNext()
{ {
var currentID = beatmapBacking.Value?.BeatmapSetInfo.ID ?? -1; var playable = list.NextSet;
var available = BeatmapSets;
var playable = available.SkipWhile(b => b.ID != currentID).Skip(1).FirstOrDefault() ?? available.FirstOrDefault();
if (playable != null) if (playable != null)
{
playSpecified(playable.Beatmaps[0]); playSpecified(playable.Beatmaps[0]);
list.SelectedSet = playable;
}
} }
private void playSpecified(BeatmapInfo info) private void playSpecified(BeatmapInfo info)