2020-02-14 14:01:45 +08:00
|
|
|
// 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.
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
#nullable disable
|
|
|
|
|
2021-12-08 19:38:18 +08:00
|
|
|
using System;
|
2021-12-09 00:16:37 +08:00
|
|
|
using System.Linq;
|
2020-02-14 14:01:45 +08:00
|
|
|
using osu.Framework.Bindables;
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2022-05-04 18:09:40 +08:00
|
|
|
using osu.Framework.Input.Bindings;
|
|
|
|
using osu.Framework.Input.Events;
|
2020-02-14 14:01:45 +08:00
|
|
|
using osu.Game.Graphics.Containers;
|
2022-05-04 18:09:40 +08:00
|
|
|
using osu.Game.Input.Bindings;
|
2020-12-25 12:38:11 +08:00
|
|
|
using osu.Game.Online.Rooms;
|
2020-02-14 14:01:45 +08:00
|
|
|
using osuTK;
|
|
|
|
|
2020-12-25 23:50:00 +08:00
|
|
|
namespace osu.Game.Screens.OnlinePlay
|
2020-02-14 14:01:45 +08:00
|
|
|
{
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
2021-12-09 16:33:36 +08:00
|
|
|
/// A scrollable list which displays the <see cref="PlaylistItem"/>s in a <see cref="Room"/>.
|
2021-12-09 00:29:45 +08:00
|
|
|
/// </summary>
|
2022-05-04 18:09:40 +08:00
|
|
|
public class DrawableRoomPlaylist : OsuRearrangeableListContainer<PlaylistItem>, IKeyBindingHandler<GlobalAction>
|
2020-02-14 14:01:45 +08:00
|
|
|
{
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
2021-12-09 16:33:36 +08:00
|
|
|
/// The currently-selected item. Selection is visually represented with a border.
|
|
|
|
/// May be updated by clicking playlist items if <see cref="AllowSelection"/> is <c>true</c>.
|
2021-12-09 00:29:45 +08:00
|
|
|
/// </summary>
|
2020-02-14 14:01:45 +08:00
|
|
|
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
|
|
|
|
|
2021-12-08 19:38:18 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Invoked when an item is requested to be deleted.
|
|
|
|
/// </summary>
|
2021-12-09 00:52:59 +08:00
|
|
|
public Action<PlaylistItem> RequestDeletion;
|
2021-12-08 19:38:18 +08:00
|
|
|
|
2021-12-09 00:17:25 +08:00
|
|
|
/// <summary>
|
2021-12-09 00:29:45 +08:00
|
|
|
/// Invoked when an item requests its results to be shown.
|
2021-12-09 00:17:25 +08:00
|
|
|
/// </summary>
|
2021-12-09 00:52:59 +08:00
|
|
|
public Action<PlaylistItem> RequestResults;
|
2021-12-09 00:17:25 +08:00
|
|
|
|
2021-12-10 00:08:54 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Invoked when an item requests to be edited.
|
|
|
|
/// </summary>
|
|
|
|
public Action<PlaylistItem> RequestEdit;
|
|
|
|
|
2021-12-09 00:16:37 +08:00
|
|
|
private bool allowReordering;
|
|
|
|
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to allow reordering items in the playlist.
|
|
|
|
/// </summary>
|
2021-12-09 00:16:37 +08:00
|
|
|
public bool AllowReordering
|
|
|
|
{
|
|
|
|
get => allowReordering;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
allowReordering = value;
|
|
|
|
|
|
|
|
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
|
|
|
|
item.AllowReordering = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool allowDeletion;
|
|
|
|
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to allow deleting items from the playlist.
|
2021-12-09 00:52:59 +08:00
|
|
|
/// If <c>true</c>, requests to delete items may be satisfied via <see cref="RequestDeletion"/>.
|
2021-12-09 00:29:45 +08:00
|
|
|
/// </summary>
|
2021-12-09 00:16:37 +08:00
|
|
|
public bool AllowDeletion
|
|
|
|
{
|
|
|
|
get => allowDeletion;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
allowDeletion = value;
|
|
|
|
|
|
|
|
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
|
|
|
|
item.AllowDeletion = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool allowSelection;
|
|
|
|
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to allow selecting items from the playlist.
|
|
|
|
/// If <c>true</c>, clicking on items in the playlist will change the value of <see cref="SelectedItem"/>.
|
|
|
|
/// </summary>
|
2021-12-09 00:16:37 +08:00
|
|
|
public bool AllowSelection
|
|
|
|
{
|
|
|
|
get => allowSelection;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
allowSelection = value;
|
|
|
|
|
|
|
|
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
|
|
|
|
item.AllowSelection = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 00:17:25 +08:00
|
|
|
private bool allowShowingResults;
|
|
|
|
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to allow items to request their results to be shown.
|
2021-12-09 00:52:59 +08:00
|
|
|
/// If <c>true</c>, requests to show the results may be satisfied via <see cref="RequestResults"/>.
|
2021-12-09 00:29:45 +08:00
|
|
|
/// </summary>
|
2021-12-09 00:17:25 +08:00
|
|
|
public bool AllowShowingResults
|
|
|
|
{
|
|
|
|
get => allowShowingResults;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
allowShowingResults = value;
|
|
|
|
|
|
|
|
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
|
|
|
|
item.AllowShowingResults = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-10 00:08:54 +08:00
|
|
|
private bool allowEditing;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Whether to allow items to be edited.
|
|
|
|
/// If <c>true</c>, requests to edit items may be satisfied via <see cref="RequestEdit"/>.
|
|
|
|
/// </summary>
|
|
|
|
public bool AllowEditing
|
|
|
|
{
|
|
|
|
get => allowEditing;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
allowEditing = value;
|
|
|
|
|
|
|
|
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
|
|
|
|
item.AllowEditing = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 00:16:37 +08:00
|
|
|
private bool showItemOwners;
|
|
|
|
|
2021-12-09 00:29:45 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Whether to show the avatar of users which own each playlist item.
|
|
|
|
/// </summary>
|
2021-12-09 00:16:37 +08:00
|
|
|
public bool ShowItemOwners
|
|
|
|
{
|
|
|
|
get => showItemOwners;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
showItemOwners = value;
|
|
|
|
|
|
|
|
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
|
|
|
|
item.ShowItemOwner = value;
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 14:01:45 +08:00
|
|
|
|
2020-02-14 14:36:47 +08:00
|
|
|
protected override ScrollContainer<Drawable> CreateScrollContainer() => base.CreateScrollContainer().With(d =>
|
2020-02-14 14:01:45 +08:00
|
|
|
{
|
2020-02-14 14:36:47 +08:00
|
|
|
d.ScrollbarVisible = false;
|
|
|
|
});
|
2020-02-14 14:01:45 +08:00
|
|
|
|
2021-11-22 17:39:50 +08:00
|
|
|
protected override FillFlowContainer<RearrangeableListItem<PlaylistItem>> CreateListFillFlowContainer() => new FillFlowContainer<RearrangeableListItem<PlaylistItem>>
|
2020-02-14 14:01:45 +08:00
|
|
|
{
|
|
|
|
Spacing = new Vector2(0, 2)
|
|
|
|
};
|
|
|
|
|
2021-12-09 00:47:46 +08:00
|
|
|
protected sealed override OsuRearrangeableListItem<PlaylistItem> CreateOsuDrawable(PlaylistItem item) => CreateDrawablePlaylistItem(item).With(d =>
|
2020-02-14 14:01:45 +08:00
|
|
|
{
|
2021-12-09 00:47:46 +08:00
|
|
|
d.SelectedItem.BindTarget = SelectedItem;
|
2021-12-09 00:52:59 +08:00
|
|
|
d.RequestDeletion = i => RequestDeletion?.Invoke(i);
|
2021-12-10 00:08:54 +08:00
|
|
|
d.RequestResults = i => RequestResults?.Invoke(i);
|
|
|
|
d.RequestEdit = i => RequestEdit?.Invoke(i);
|
2021-12-09 00:47:46 +08:00
|
|
|
d.AllowReordering = AllowReordering;
|
|
|
|
d.AllowDeletion = AllowDeletion;
|
|
|
|
d.AllowSelection = AllowSelection;
|
|
|
|
d.AllowShowingResults = AllowShowingResults;
|
2021-12-10 00:08:54 +08:00
|
|
|
d.AllowEditing = AllowEditing;
|
2021-12-09 00:47:46 +08:00
|
|
|
d.ShowItemOwner = ShowItemOwners;
|
|
|
|
});
|
|
|
|
|
|
|
|
protected virtual DrawableRoomPlaylistItem CreateDrawablePlaylistItem(PlaylistItem item) => new DrawableRoomPlaylistItem(item);
|
2022-05-04 18:09:40 +08:00
|
|
|
|
2022-05-04 19:15:26 +08:00
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-05-04 19:51:47 +08:00
|
|
|
// schedules added as the properties may change value while the drawable items haven't been created yet.
|
|
|
|
SelectedItem.BindValueChanged(_ => Scheduler.AddOnce(scrollToSelection));
|
2022-06-24 20:25:23 +08:00
|
|
|
Items.BindCollectionChanged((_, _) => Scheduler.AddOnce(scrollToSelection), true);
|
2022-05-04 19:15:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void scrollToSelection()
|
|
|
|
{
|
2022-05-04 19:51:47 +08:00
|
|
|
// SelectedItem and ItemMap/drawable items are managed separately,
|
|
|
|
// so if the item can't be unmapped to a drawable, don't try to scroll to it.
|
|
|
|
// best effort is made to not drop any updates, by subscribing to both sources.
|
|
|
|
if (SelectedItem.Value == null || !ItemMap.TryGetValue(SelectedItem.Value, out var drawableItem))
|
|
|
|
return;
|
2022-05-04 19:15:26 +08:00
|
|
|
|
2022-05-05 15:16:16 +08:00
|
|
|
// ScrollIntoView does not handle non-loaded items appropriately, delay scroll until the item finishes loading.
|
|
|
|
// see: https://github.com/ppy/osu-framework/issues/5158
|
|
|
|
if (!drawableItem.IsLoaded)
|
|
|
|
drawableItem.OnLoadComplete += _ => ScrollContainer.ScrollIntoView(drawableItem);
|
|
|
|
else
|
|
|
|
ScrollContainer.ScrollIntoView(drawableItem);
|
2022-05-04 19:15:26 +08:00
|
|
|
}
|
|
|
|
|
2022-05-04 18:09:40 +08:00
|
|
|
#region Key selection logic (shared with BeatmapCarousel and RoomsContainer)
|
|
|
|
|
|
|
|
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
|
|
|
|
{
|
2022-08-01 01:12:29 +08:00
|
|
|
if (!AllowSelection)
|
|
|
|
return false;
|
|
|
|
|
2022-05-04 18:09:40 +08:00
|
|
|
switch (e.Action)
|
|
|
|
{
|
|
|
|
case GlobalAction.SelectNext:
|
|
|
|
selectNext(1);
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case GlobalAction.SelectPrevious:
|
|
|
|
selectNext(-1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
private void selectNext(int direction)
|
|
|
|
{
|
2022-05-05 10:42:20 +08:00
|
|
|
var visibleItems = ListContainer.AsEnumerable().Where(r => r.IsPresent);
|
2022-05-04 18:09:40 +08:00
|
|
|
|
|
|
|
PlaylistItem item;
|
|
|
|
|
|
|
|
if (SelectedItem.Value == null)
|
2022-05-05 10:42:20 +08:00
|
|
|
item = visibleItems.FirstOrDefault()?.Model;
|
2022-05-04 18:09:40 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (direction < 0)
|
2022-05-05 10:42:20 +08:00
|
|
|
visibleItems = visibleItems.Reverse();
|
2022-05-04 18:09:40 +08:00
|
|
|
|
2022-05-05 10:42:20 +08:00
|
|
|
item = visibleItems.SkipWhile(r => r.Model != SelectedItem.Value).Skip(1).FirstOrDefault()?.Model;
|
2022-05-04 18:09:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// we already have a valid selection only change selection if we still have a room to switch to.
|
|
|
|
if (item != null)
|
|
|
|
SelectedItem.Value = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2020-02-14 14:01:45 +08:00
|
|
|
}
|
|
|
|
}
|