mirror of
https://github.com/ppy/osu.git
synced 2025-01-15 07:22:55 +08:00
Rewrite PlaylistList as CompositeDrawable and remove all backwards PlaylistList references
Now handles drag at a PlaylistList.ItemsScrollContainer level (private class), and PlaylistList itself is no longer a Container so it only supports adding BeatmapSets. Sorry for the rewrite x.x.
This commit is contained in:
parent
9b0309e683
commit
3b575444be
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using OpenTK.Graphics;
|
using OpenTK.Graphics;
|
||||||
using osu.Framework.Allocation;
|
using osu.Framework.Allocation;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
@ -18,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;
|
||||||
|
|
||||||
@ -31,11 +30,12 @@ namespace osu.Game.Overlays.Music
|
|||||||
private UnicodeBindableString titleBind;
|
private UnicodeBindableString titleBind;
|
||||||
private UnicodeBindableString artistBind;
|
private UnicodeBindableString artistBind;
|
||||||
|
|
||||||
private readonly FillFlowContainer<PlaylistItem> playlist;
|
|
||||||
public readonly BeatmapSetInfo BeatmapSetInfo;
|
public readonly BeatmapSetInfo BeatmapSetInfo;
|
||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
@ -51,9 +51,8 @@ namespace osu.Game.Overlays.Music
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public PlaylistItem(FillFlowContainer<PlaylistItem> playlist, BeatmapSetInfo setInfo)
|
public PlaylistItem(BeatmapSetInfo setInfo)
|
||||||
{
|
{
|
||||||
this.playlist = playlist;
|
|
||||||
BeatmapSetInfo = setInfo;
|
BeatmapSetInfo = setInfo;
|
||||||
|
|
||||||
RelativeSizeAxes = Axes.X;
|
RelativeSizeAxes = Axes.X;
|
||||||
@ -72,9 +71,9 @@ namespace osu.Game.Overlays.Music
|
|||||||
|
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
handle = new PlaylistItemHandle(playlist)
|
handle = new PlaylistItemHandle
|
||||||
{
|
{
|
||||||
Colour = colours.Gray5,
|
Colour = colours.Gray5
|
||||||
},
|
},
|
||||||
text = new OsuTextFlowContainer
|
text = new OsuTextFlowContainer
|
||||||
{
|
{
|
||||||
@ -149,11 +148,9 @@ namespace osu.Game.Overlays.Music
|
|||||||
|
|
||||||
private class PlaylistItemHandle : SpriteIcon
|
private class PlaylistItemHandle : SpriteIcon
|
||||||
{
|
{
|
||||||
private readonly FillFlowContainer<PlaylistItem> playlist;
|
|
||||||
|
|
||||||
public PlaylistItemHandle(FillFlowContainer<PlaylistItem> playlist)
|
public PlaylistItemHandle()
|
||||||
{
|
{
|
||||||
this.playlist = playlist;
|
|
||||||
Anchor = Anchor.TopLeft;
|
Anchor = Anchor.TopLeft;
|
||||||
Origin = Anchor.TopLeft;
|
Origin = Anchor.TopLeft;
|
||||||
Size = new Vector2(12);
|
Size = new Vector2(12);
|
||||||
@ -161,47 +158,14 @@ namespace osu.Game.Overlays.Music
|
|||||||
Alpha = 0f;
|
Alpha = 0f;
|
||||||
Margin = new MarginPadding { Left = 5, Top = 2 };
|
Margin = new MarginPadding { Left = 5, Top = 2 };
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected override bool OnDragStart(InputState state) => true;
|
public interface IDraggable : IDrawable
|
||||||
|
|
||||||
protected override bool OnDrag(InputState state)
|
|
||||||
{
|
{
|
||||||
int src = (int)Parent.Depth;
|
/// <summary>
|
||||||
|
/// Whether this <see cref="IDraggable"/> can be dragged in its current state.
|
||||||
var matchingItem = playlist.Children.LastOrDefault(c => c.Position.Y < state.Mouse.Position.Y + Parent.Position.Y);
|
/// </summary>
|
||||||
if (matchingItem == null)
|
bool IsDraggable { get; }
|
||||||
return true;
|
|
||||||
|
|
||||||
int dst = (int)matchingItem.Depth;
|
|
||||||
|
|
||||||
// Due to the position predicate above, there is an edge case to consider when an item is moved upwards:
|
|
||||||
// At the point where the two items cross there will be two items sharing the same condition, and the items will jump back
|
|
||||||
// and forth between the two positions because of this. This is accentuated if the items span differing line heights.
|
|
||||||
// The easiest way to avoid this is to ensure the movement direction matches the expected mouse delta
|
|
||||||
|
|
||||||
if (state.Mouse.Delta.Y <= 0 && dst > src)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (state.Mouse.Delta.Y >= 0 && dst < src)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (src == dst)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (src < dst)
|
|
||||||
{
|
|
||||||
for (int i = src + 1; i <= dst; i++)
|
|
||||||
playlist.ChangeChildDepth(playlist[i], i - 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
for (int i = dst; i < src; i++)
|
|
||||||
playlist.ChangeChildDepth(playlist[i], i + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
playlist.ChangeChildDepth(Parent as PlaylistItem, dst);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,73 +4,65 @@
|
|||||||
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, index) => new PlaylistItem(items, item) { OnSelect = itemSelected, Depth = index }).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 BeatmapSetInfo NextItem
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var available = items.Children;
|
|
||||||
return (available.SkipWhile(i => !i.Selected).Skip(1).FirstOrDefault() ?? available.FirstOrDefault())?.BeatmapSetInfo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public BeatmapSetInfo PreviousItem
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
var available = items.Children;
|
|
||||||
return (available.TakeWhile(i => !i.Selected).LastOrDefault() ?? available.LastOrDefault())?.BeatmapSetInfo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public PlaylistList()
|
public PlaylistList()
|
||||||
{
|
{
|
||||||
Children = new Drawable[]
|
InternalChild = items = new ItemsScrollContainer
|
||||||
{
|
|
||||||
new OsuScrollContainer
|
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Both,
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
OnSelect = set => OnSelect?.Invoke(set)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public new MarginPadding Padding
|
||||||
|
{
|
||||||
|
get { return base.Padding; }
|
||||||
|
set { base.Padding = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
{
|
||||||
|
get { return items.SelectedSet; }
|
||||||
|
set { items.SelectedSet = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
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 Action<BeatmapSetInfo> OnSelect;
|
||||||
|
|
||||||
|
private readonly SearchContainer search;
|
||||||
|
private readonly FillFlowContainer<PlaylistItem> items;
|
||||||
|
|
||||||
|
private PlaylistItem draggedItem;
|
||||||
|
|
||||||
|
public ItemsScrollContainer()
|
||||||
|
{
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
search = new SearchContainer
|
search = new SearchContainer
|
||||||
@ -86,22 +78,109 @@ namespace osu.Game.Overlays.Music
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public IEnumerable<BeatmapSetInfo> Sets
|
||||||
|
{
|
||||||
|
set
|
||||||
|
{
|
||||||
|
items.Clear();
|
||||||
|
value.ForEach(AddBeatmapSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string SearchTerm
|
||||||
|
{
|
||||||
|
get { return search.SearchTerm; }
|
||||||
|
set { search.SearchTerm = value; }
|
||||||
|
}
|
||||||
|
|
||||||
public void AddBeatmapSet(BeatmapSetInfo beatmapSet)
|
public void AddBeatmapSet(BeatmapSetInfo beatmapSet)
|
||||||
{
|
{
|
||||||
items.Add(new PlaylistItem(items, beatmapSet) { OnSelect = itemSelected, Depth = items.Count });
|
items.Add(new PlaylistItem(beatmapSet)
|
||||||
|
{
|
||||||
|
OnSelect = set => OnSelect?.Invoke(set),
|
||||||
|
Depth = items.Count
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet)
|
public bool RemoveBeatmapSet(BeatmapSetInfo beatmapSet)
|
||||||
{
|
{
|
||||||
PlaylistItem itemToRemove = items.Children.FirstOrDefault(item => item.BeatmapSetInfo.ID == beatmapSet.ID);
|
var itemToRemove = items.FirstOrDefault(i => i.BeatmapSetInfo.ID == beatmapSet.ID);
|
||||||
if (itemToRemove != null) items.Remove(itemToRemove);
|
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;
|
||||||
|
|
||||||
|
protected override bool OnDragStart(InputState state)
|
||||||
|
{
|
||||||
|
draggedItem = items.FirstOrDefault(d => d.IsDraggable);
|
||||||
|
return draggedItem != null || base.OnDragStart(state);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool OnDrag(InputState state)
|
||||||
|
{
|
||||||
|
if (draggedItem == null)
|
||||||
|
return base.OnDrag(state);
|
||||||
|
|
||||||
|
// Mouse position in the position space of the items container
|
||||||
|
Vector2 itemsPos = items.ToLocalSpace(state.Mouse.NativeState.Position);
|
||||||
|
|
||||||
|
int src = (int)draggedItem.Depth;
|
||||||
|
|
||||||
|
var matchingItem = items.LastOrDefault(c => c.Position.Y < itemsPos.Y);
|
||||||
|
if (matchingItem == null)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
int dst = (int)matchingItem.Depth;
|
||||||
|
|
||||||
|
// Due to the position predicate above, there is an edge case to consider when an item is moved upwards:
|
||||||
|
// At the point where the two items cross there will be two items sharing the same condition, and the items will jump back
|
||||||
|
// and forth between the two positions because of this. This is accentuated if the items span differing line heights.
|
||||||
|
// The easiest way to avoid this is to ensure the movement direction matches the expected mouse delta
|
||||||
|
|
||||||
|
if (state.Mouse.Delta.Y <= 0 && dst > src)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (state.Mouse.Delta.Y >= 0 && dst < src)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (src == dst)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (src < dst)
|
||||||
|
{
|
||||||
|
for (int i = src + 1; i <= dst; i++)
|
||||||
|
items.ChangeChildDepth(items[i], i - 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
for (int i = dst; i < src; i++)
|
||||||
|
items.ChangeChildDepth(items[i], i + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
items.ChangeChildDepth(draggedItem, dst);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override bool OnDragEnd(InputState state) => draggedItem != null || base.OnDragEnd(state);
|
||||||
|
|
||||||
private class ItemSearchContainer : FillFlowContainer<PlaylistItem>, IHasFilterableChildren
|
private class ItemSearchContainer : FillFlowContainer<PlaylistItem>, IHasFilterableChildren
|
||||||
{
|
{
|
||||||
public string[] FilterTerms => new string[] { };
|
public string[] FilterTerms => new string[] { };
|
||||||
@ -126,4 +205,5 @@ namespace osu.Game.Overlays.Music
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -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,23 +126,23 @@ namespace osu.Game.Overlays.Music
|
|||||||
|
|
||||||
public void PlayPrevious()
|
public void PlayPrevious()
|
||||||
{
|
{
|
||||||
var playable = list.PreviousItem;
|
var playable = list.PreviousSet;
|
||||||
|
|
||||||
if (playable != null)
|
if (playable != null)
|
||||||
{
|
{
|
||||||
playSpecified(playable.Beatmaps[0]);
|
playSpecified(playable.Beatmaps[0]);
|
||||||
list.SelectedItem = playable;
|
list.SelectedSet = playable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlayNext()
|
public void PlayNext()
|
||||||
{
|
{
|
||||||
var playable = list.NextItem;
|
var playable = list.NextSet;
|
||||||
|
|
||||||
if (playable != null)
|
if (playable != null)
|
||||||
{
|
{
|
||||||
playSpecified(playable.Beatmaps[0]);
|
playSpecified(playable.Beatmaps[0]);
|
||||||
list.SelectedItem = playable;
|
list.SelectedSet = playable;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user