diff --git a/osu.Game/Overlays/Music/PlaylistItem.cs b/osu.Game/Overlays/Music/PlaylistItem.cs index 4145a8d1f0..2aaa182685 100644 --- a/osu.Game/Overlays/Music/PlaylistItem.cs +++ b/osu.Game/Overlays/Music/PlaylistItem.cs @@ -8,6 +8,7 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; +using osu.Framework.Input; using osu.Framework.Localisation; using osu.Game.Beatmaps; using osu.Game.Graphics; @@ -16,7 +17,7 @@ namespace osu.Game.Overlays.Music { - internal class PlaylistItem : Container, IFilterable + internal class PlaylistItem : Container, IFilterable, IDraggable { private const float fade_duration = 100; @@ -33,6 +34,8 @@ internal class PlaylistItem : Container, IFilterable public Action OnSelect; + public bool IsDraggable => handle.IsHovered; + private bool selected; public bool Selected { @@ -68,15 +71,9 @@ private void load(OsuColour colours, LocalisationEngine localisation) Children = new Drawable[] { - handle = new SpriteIcon + handle = new PlaylistItemHandle { - Anchor = Anchor.TopLeft, - Origin = Anchor.TopLeft, - Size = new Vector2(12), - Colour = colours.Gray5, - Icon = FontAwesome.fa_bars, - Alpha = 0f, - Margin = new MarginPadding { Left = 5, Top = 2 }, + Colour = colours.Gray5 }, text = new OsuTextFlowContainer { @@ -114,19 +111,19 @@ private void recreateText() }); } - protected override bool OnHover(Framework.Input.InputState state) + protected override bool OnHover(InputState state) { handle.FadeIn(fade_duration); return base.OnHover(state); } - protected override void OnHoverLost(Framework.Input.InputState state) + protected override void OnHoverLost(InputState state) { handle.FadeOut(fade_duration); } - protected override bool OnClick(Framework.Input.InputState state) + protected override bool OnClick(InputState state) { OnSelect?.Invoke(BeatmapSetInfo); return true; @@ -148,5 +145,27 @@ public bool MatchingFilter 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 + { + /// + /// Whether this can be dragged in its current state. + /// + bool IsDraggable { get; } } } diff --git a/osu.Game/Overlays/Music/PlaylistList.cs b/osu.Game/Overlays/Music/PlaylistList.cs index 3dd514edeb..360e2ad843 100644 --- a/osu.Game/Overlays/Music/PlaylistList.cs +++ b/osu.Game/Overlays/Music/PlaylistList.cs @@ -4,105 +4,252 @@ using System; using System.Collections.Generic; using System.Linq; +using osu.Framework.Extensions.IEnumerableExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Framework.Input; using osu.Game.Beatmaps; using osu.Game.Graphics.Containers; +using OpenTK; namespace osu.Game.Overlays.Music { - internal class PlaylistList : Container + internal class PlaylistList : CompositeDrawable { - private readonly FillFlowContainer items; - - public IEnumerable 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 OnSelect; - private readonly SearchContainer search; - - 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; - } - } + private readonly ItemsScrollContainer items; public PlaylistList() { - Children = new Drawable[] + InternalChild = items = new ItemsScrollContainer { - new OsuScrollContainer - { - 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, - }, - } - } - }, - }, + RelativeSizeAxes = Axes.Both, + OnSelect = set => OnSelect?.Invoke(set) }; } - 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 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); - if (itemToRemove != null) items.Remove(itemToRemove); + get { return items.SelectedSet; } + set { items.SelectedSet = value; } } - private class ItemSearchContainer : FillFlowContainer, 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 bool MatchingFilter + public Action OnSelect; + + private readonly SearchContainer search; + private readonly FillFlowContainer 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 Sets { set { - if (value) - InvalidateLayout(); + items.Clear(); + value.ForEach(AddBeatmapSet); } } - public IEnumerable FilterableChildren => Children; - - public ItemSearchContainer() + public string SearchTerm { - LayoutDuration = 200; - LayoutEasing = Easing.OutQuint; + get { return search.SearchTerm; } + 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, 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 FilterableChildren => Children; + + public ItemSearchContainer() + { + LayoutDuration = 200; + LayoutEasing = Easing.OutQuint; + } } } } -} \ No newline at end of file +} diff --git a/osu.Game/Overlays/Music/PlaylistOverlay.cs b/osu.Game/Overlays/Music/PlaylistOverlay.cs index 83e92c5554..d05ad85726 100644 --- a/osu.Game/Overlays/Music/PlaylistOverlay.cs +++ b/osu.Game/Overlays/Music/PlaylistOverlay.cs @@ -92,7 +92,7 @@ private void load(OsuGameBase game, BeatmapManager beatmaps, OsuColour colours) protected override void LoadComplete() { base.LoadComplete(); - beatmapBacking.ValueChanged += b => list.SelectedItem = b?.BeatmapSetInfo; + beatmapBacking.ValueChanged += b => list.SelectedSet = b?.BeatmapSetInfo; beatmapBacking.TriggerChange(); } @@ -126,24 +126,24 @@ private void itemSelected(BeatmapSetInfo set) public void PlayPrevious() { - var currentID = beatmapBacking.Value?.BeatmapSetInfo.ID ?? -1; - var available = BeatmapSets.Reverse(); - - var playable = available.SkipWhile(b => b.ID != currentID).Skip(1).FirstOrDefault() ?? available.FirstOrDefault(); + var playable = list.PreviousSet; if (playable != null) + { playSpecified(playable.Beatmaps[0]); + list.SelectedSet = playable; + } } public void PlayNext() { - var currentID = beatmapBacking.Value?.BeatmapSetInfo.ID ?? -1; - var available = BeatmapSets; - - var playable = available.SkipWhile(b => b.ID != currentID).Skip(1).FirstOrDefault() ?? available.FirstOrDefault(); + var playable = list.NextSet; if (playable != null) + { playSpecified(playable.Beatmaps[0]); + list.SelectedSet = playable; + } } private void playSpecified(BeatmapInfo info)