1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 18:27:26 +08:00

Merge pull request #16009 from smoogipoo/cleanup-drawable-playlist

Cleanup DrawableRoomPlaylist and DrawableRoomPlaylistItem
This commit is contained in:
Dean Herbert 2021-12-09 18:54:16 +09:00 committed by GitHub
commit b393f83028
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 508 additions and 278 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
@ -48,7 +49,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestNonEditableNonSelectable() public void TestNonEditableNonSelectable()
{ {
createPlaylist(false, false); createPlaylist();
moveToItem(0); moveToItem(0);
assertHandleVisibility(0, false); assertHandleVisibility(0, false);
@ -61,7 +62,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestEditable() public void TestEditable()
{ {
createPlaylist(true, false); createPlaylist(p =>
{
p.AllowReordering = true;
p.AllowDeletion = true;
});
moveToItem(0); moveToItem(0);
assertHandleVisibility(0, true); assertHandleVisibility(0, true);
@ -74,7 +79,12 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestMarkInvalid() public void TestMarkInvalid()
{ {
createPlaylist(true, true); createPlaylist(p =>
{
p.AllowReordering = true;
p.AllowDeletion = true;
p.AllowSelection = true;
});
AddStep("mark item 0 as invalid", () => playlist.Items[0].MarkInvalid()); AddStep("mark item 0 as invalid", () => playlist.Items[0].MarkInvalid());
@ -87,7 +97,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestSelectable() public void TestSelectable()
{ {
createPlaylist(false, true); createPlaylist(p => p.AllowSelection = true);
moveToItem(0); moveToItem(0);
assertHandleVisibility(0, false); assertHandleVisibility(0, false);
@ -101,7 +111,12 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestEditableSelectable() public void TestEditableSelectable()
{ {
createPlaylist(true, true); createPlaylist(p =>
{
p.AllowReordering = true;
p.AllowDeletion = true;
p.AllowSelection = true;
});
moveToItem(0); moveToItem(0);
assertHandleVisibility(0, true); assertHandleVisibility(0, true);
@ -115,7 +130,12 @@ namespace osu.Game.Tests.Visual.Multiplayer
[Test] [Test]
public void TestSelectionNotLostAfterRearrangement() public void TestSelectionNotLostAfterRearrangement()
{ {
createPlaylist(true, true); createPlaylist(p =>
{
p.AllowReordering = true;
p.AllowDeletion = true;
p.AllowSelection = true;
});
moveToItem(0); moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left)); AddStep("click", () => InputManager.Click(MouseButton.Left));
@ -128,95 +148,6 @@ namespace osu.Game.Tests.Visual.Multiplayer
AddAssert("item 1 is selected", () => playlist.SelectedItem.Value == playlist.Items[1]); AddAssert("item 1 is selected", () => playlist.SelectedItem.Value == playlist.Items[1]);
} }
[Test]
public void TestItemRemovedOnDeletion()
{
PlaylistItem selectedItem = null;
createPlaylist(true, true);
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddStep("retrieve selection", () => selectedItem = playlist.SelectedItem.Value);
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("item removed", () => !playlist.Items.Contains(selectedItem));
}
[Test]
public void TestNextItemSelectedAfterDeletion()
{
createPlaylist(true, true);
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("item 0 is selected", () => playlist.SelectedItem.Value == playlist.Items[0]);
}
[Test]
public void TestLastItemSelectedAfterLastItemDeleted()
{
createPlaylist(true, true);
AddWaitStep("wait for flow", 5); // Items may take 1 update frame to flow. A wait count of 5 is guaranteed to result in the flow being updated as desired.
AddStep("scroll to bottom", () => playlist.ChildrenOfType<ScrollContainer<Drawable>>().First().ScrollToEnd(false));
moveToItem(19);
AddStep("click", () => InputManager.Click(MouseButton.Left));
moveToDeleteButton(19);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("item 18 is selected", () => playlist.SelectedItem.Value == playlist.Items[18]);
}
[Test]
public void TestSelectionResetWhenAllItemsDeleted()
{
createPlaylist(true, true);
AddStep("remove all but one item", () =>
{
playlist.Items.RemoveRange(1, playlist.Items.Count - 1);
});
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("no item selected", () => playlist.SelectedItem.Value == null);
}
// Todo: currently not possible due to bindable list shortcomings (https://github.com/ppy/osu-framework/issues/3081)
// [Test]
public void TestNextItemSelectedAfterExternalDeletion()
{
createPlaylist(true, true);
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddStep("remove item 0", () => playlist.Items.RemoveAt(0));
AddAssert("item 0 is selected", () => playlist.SelectedItem.Value == playlist.Items[0]);
}
[Test]
public void TestChangeBeatmapAndRemove()
{
createPlaylist(true, true);
AddStep("change beatmap of first item", () => playlist.Items[0].BeatmapID = 30);
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
}
[Test] [Test]
public void TestDownloadButtonHiddenWhenBeatmapExists() public void TestDownloadButtonHiddenWhenBeatmapExists()
{ {
@ -224,7 +155,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
AddStep("import beatmap", () => manager.Import(beatmap.BeatmapSet).Wait()); AddStep("import beatmap", () => manager.Import(beatmap.BeatmapSet).Wait());
createPlaylist(beatmap); createPlaylistWithBeatmaps(beatmap);
assertDownloadButtonVisible(false); assertDownloadButtonVisible(false);
@ -247,7 +178,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
var byChecksum = CreateAPIBeatmap(); var byChecksum = CreateAPIBeatmap();
byChecksum.Checksum = "1337"; // Some random checksum that does not exist locally. byChecksum.Checksum = "1337"; // Some random checksum that does not exist locally.
createPlaylist(byOnlineId, byChecksum); createPlaylistWithBeatmaps(byOnlineId, byChecksum);
AddAssert("download buttons shown", () => playlist.ChildrenOfType<BeatmapDownloadButton>().All(d => d.IsPresent)); AddAssert("download buttons shown", () => playlist.ChildrenOfType<BeatmapDownloadButton>().All(d => d.IsPresent));
} }
@ -261,7 +192,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
beatmap.BeatmapSet.HasExplicitContent = true; beatmap.BeatmapSet.HasExplicitContent = true;
createPlaylist(beatmap); createPlaylistWithBeatmaps(beatmap);
} }
[Test] [Test]
@ -269,7 +200,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
{ {
AddStep("create playlist", () => AddStep("create playlist", () =>
{ {
Child = playlist = new TestPlaylist(false, false) Child = playlist = new TestPlaylist
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
@ -312,7 +243,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
[TestCase(true)] [TestCase(true)]
public void TestWithOwner(bool withOwner) public void TestWithOwner(bool withOwner)
{ {
createPlaylist(false, false, withOwner); createPlaylist(p => p.ShowItemOwners = withOwner);
AddAssert("owner visible", () => playlist.ChildrenOfType<UpdateableAvatar>().All(a => a.IsPresent == withOwner)); AddAssert("owner visible", () => playlist.ChildrenOfType<UpdateableAvatar>().All(a => a.IsPresent == withOwner));
} }
@ -326,12 +257,6 @@ namespace osu.Game.Tests.Visual.Multiplayer
InputManager.MoveMouseTo(item.ChildrenOfType<OsuRearrangeableListItem<PlaylistItem>.PlaylistItemHandle>().Single(), offset); InputManager.MoveMouseTo(item.ChildrenOfType<OsuRearrangeableListItem<PlaylistItem>.PlaylistItemHandle>().Single(), offset);
}); });
private void moveToDeleteButton(int index, Vector2? offset = null) => AddStep($"move mouse to delete button {index}", () =>
{
var item = playlist.ChildrenOfType<OsuRearrangeableListItem<PlaylistItem>>().ElementAt(index);
InputManager.MoveMouseTo(item.ChildrenOfType<DrawableRoomPlaylistItem.PlaylistRemoveButton>().ElementAt(0), offset);
});
private void assertHandleVisibility(int index, bool visible) private void assertHandleVisibility(int index, bool visible)
=> AddAssert($"handle {index} {(visible ? "is" : "is not")} visible", => AddAssert($"handle {index} {(visible ? "is" : "is not")} visible",
() => (playlist.ChildrenOfType<OsuRearrangeableListItem<PlaylistItem>.PlaylistItemHandle>().ElementAt(index).Alpha > 0) == visible); () => (playlist.ChildrenOfType<OsuRearrangeableListItem<PlaylistItem>.PlaylistItemHandle>().ElementAt(index).Alpha > 0) == visible);
@ -340,17 +265,19 @@ namespace osu.Game.Tests.Visual.Multiplayer
=> AddAssert($"delete button {index} {(visible ? "is" : "is not")} visible", => AddAssert($"delete button {index} {(visible ? "is" : "is not")} visible",
() => (playlist.ChildrenOfType<DrawableRoomPlaylistItem.PlaylistRemoveButton>().ElementAt(2 + index * 2).Alpha > 0) == visible); () => (playlist.ChildrenOfType<DrawableRoomPlaylistItem.PlaylistRemoveButton>().ElementAt(2 + index * 2).Alpha > 0) == visible);
private void createPlaylist(bool allowEdit, bool allowSelection, bool showItemOwner = false) private void createPlaylist(Action<TestPlaylist> setupPlaylist = null)
{ {
AddStep("create playlist", () => AddStep("create playlist", () =>
{ {
Child = playlist = new TestPlaylist(allowEdit, allowSelection, showItemOwner) Child = playlist = new TestPlaylist
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(500, 300) Size = new Vector2(500, 300)
}; };
setupPlaylist?.Invoke(playlist);
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
playlist.Items.Add(new PlaylistItem playlist.Items.Add(new PlaylistItem
@ -386,11 +313,11 @@ namespace osu.Game.Tests.Visual.Multiplayer
AddUntilStep("wait for items to load", () => playlist.ItemMap.Values.All(i => i.IsLoaded)); AddUntilStep("wait for items to load", () => playlist.ItemMap.Values.All(i => i.IsLoaded));
} }
private void createPlaylist(params IBeatmapInfo[] beatmaps) private void createPlaylistWithBeatmaps(params IBeatmapInfo[] beatmaps)
{ {
AddStep("create playlist", () => AddStep("create playlist", () =>
{ {
Child = playlist = new TestPlaylist(false, false) Child = playlist = new TestPlaylist
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
@ -423,11 +350,6 @@ namespace osu.Game.Tests.Visual.Multiplayer
private class TestPlaylist : DrawableRoomPlaylist private class TestPlaylist : DrawableRoomPlaylist
{ {
public new IReadOnlyDictionary<PlaylistItem, RearrangeableListItem<PlaylistItem>> ItemMap => base.ItemMap; public new IReadOnlyDictionary<PlaylistItem, RearrangeableListItem<PlaylistItem>> ItemMap => base.ItemMap;
public TestPlaylist(bool allowEdit, bool allowSelection, bool showItemOwner = false)
: base(allowEdit, allowSelection, showItemOwner: showItemOwner)
{
}
} }
} }
} }

View File

@ -0,0 +1,188 @@
// 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.
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Database;
using osu.Game.Graphics.Containers;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Rooms;
using osu.Game.Rulesets.Osu;
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Screens.OnlinePlay;
using osu.Game.Screens.OnlinePlay.Playlists;
using osu.Game.Tests.Beatmaps;
using osuTK;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Multiplayer
{
public class TestScenePlaylistsRoomSettingsPlaylist : OsuManualInputManagerTestScene
{
private TestPlaylist playlist;
[Cached(typeof(UserLookupCache))]
private readonly TestUserLookupCache userLookupCache = new TestUserLookupCache();
[Test]
public void TestItemRemovedOnDeletion()
{
PlaylistItem selectedItem = null;
createPlaylist();
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddStep("retrieve selection", () => selectedItem = playlist.SelectedItem.Value);
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("item removed", () => !playlist.Items.Contains(selectedItem));
}
[Test]
public void TestNextItemSelectedAfterDeletion()
{
createPlaylist();
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("item 0 is selected", () => playlist.SelectedItem.Value == playlist.Items[0]);
}
[Test]
public void TestLastItemSelectedAfterLastItemDeleted()
{
createPlaylist();
AddWaitStep("wait for flow", 5); // Items may take 1 update frame to flow. A wait count of 5 is guaranteed to result in the flow being updated as desired.
AddStep("scroll to bottom", () => playlist.ChildrenOfType<ScrollContainer<Drawable>>().First().ScrollToEnd(false));
moveToItem(19);
AddStep("click", () => InputManager.Click(MouseButton.Left));
moveToDeleteButton(19);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("item 18 is selected", () => playlist.SelectedItem.Value == playlist.Items[18]);
}
[Test]
public void TestSelectionResetWhenAllItemsDeleted()
{
createPlaylist();
AddStep("remove all but one item", () =>
{
playlist.Items.RemoveRange(1, playlist.Items.Count - 1);
});
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("no item selected", () => playlist.SelectedItem.Value == null);
}
// Todo: currently not possible due to bindable list shortcomings (https://github.com/ppy/osu-framework/issues/3081)
// [Test]
public void TestNextItemSelectedAfterExternalDeletion()
{
createPlaylist();
moveToItem(0);
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddStep("remove item 0", () => playlist.Items.RemoveAt(0));
AddAssert("item 0 is selected", () => playlist.SelectedItem.Value == playlist.Items[0]);
}
[Test]
public void TestChangeBeatmapAndRemove()
{
createPlaylist();
AddStep("change beatmap of first item", () => playlist.Items[0].BeatmapID = 30);
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
}
private void moveToItem(int index, Vector2? offset = null)
=> AddStep($"move mouse to item {index}", () => InputManager.MoveMouseTo(playlist.ChildrenOfType<DifficultyIcon>().ElementAt(index), offset));
private void moveToDeleteButton(int index, Vector2? offset = null) => AddStep($"move mouse to delete button {index}", () =>
{
var item = playlist.ChildrenOfType<OsuRearrangeableListItem<PlaylistItem>>().ElementAt(index);
InputManager.MoveMouseTo(item.ChildrenOfType<DrawableRoomPlaylistItem.PlaylistRemoveButton>().ElementAt(0), offset);
});
private void createPlaylist()
{
AddStep("create playlist", () =>
{
Child = playlist = new TestPlaylist
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(500, 300)
};
for (int i = 0; i < 20; i++)
{
playlist.Items.Add(new PlaylistItem
{
ID = i,
OwnerID = 2,
Beatmap =
{
Value = i % 2 == 1
? new TestBeatmap(new OsuRuleset().RulesetInfo).BeatmapInfo
: new BeatmapInfo
{
Metadata = new BeatmapMetadata
{
Artist = "Artist",
Author = new APIUser { Username = "Creator name here" },
Title = "Long title used to check background colour",
},
BeatmapSet = new BeatmapSetInfo()
}
},
Ruleset = { Value = new OsuRuleset().RulesetInfo },
RequiredMods =
{
new OsuModHardRock(),
new OsuModDoubleTime(),
new OsuModAutoplay()
}
});
}
});
AddUntilStep("wait for items to load", () => playlist.ItemMap.Values.All(i => i.IsLoaded));
}
private class TestPlaylist : PlaylistsRoomSettingsPlaylist
{
public new IReadOnlyDictionary<PlaylistItem, RearrangeableListItem<PlaylistItem>> ItemMap => base.ItemMap;
public TestPlaylist()
{
AllowSelection = true;
}
}
}
}

View File

@ -9,6 +9,7 @@ using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.UserInterface; using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Playlists;
using osu.Game.Screens.Select; using osu.Game.Screens.Select;
using osuTK; using osuTK;
@ -43,9 +44,9 @@ namespace osu.Game.Screens.OnlinePlay.Components
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Bottom = 10 }, Padding = new MarginPadding { Bottom = 10 },
Child = playlist = new DrawableRoomPlaylist(true, false) Child = playlist = new PlaylistsRoomSettingsPlaylist
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both
} }
} }
}, },

View File

@ -1,9 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Specialized; using System;
using System.Linq;
using osu.Framework.Bindables; using osu.Framework.Bindables;
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.Game.Graphics.Containers; using osu.Game.Graphics.Containers;
@ -12,36 +12,113 @@ using osuTK;
namespace osu.Game.Screens.OnlinePlay namespace osu.Game.Screens.OnlinePlay
{ {
/// <summary>
/// A scrollable list which displays the <see cref="PlaylistItem"/>s in a <see cref="Room"/>.
/// </summary>
public class DrawableRoomPlaylist : OsuRearrangeableListContainer<PlaylistItem> public class DrawableRoomPlaylist : OsuRearrangeableListContainer<PlaylistItem>
{ {
/// <summary>
/// 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>.
/// </summary>
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>(); public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
private readonly bool allowEdit; /// <summary>
private readonly bool allowSelection; /// Invoked when an item is requested to be deleted.
private readonly bool showItemOwner; /// </summary>
public Action<PlaylistItem> RequestDeletion;
public DrawableRoomPlaylist(bool allowEdit, bool allowSelection, bool showItemOwner = false) /// <summary>
/// Invoked when an item requests its results to be shown.
/// </summary>
public Action<PlaylistItem> RequestResults;
private bool allowReordering;
/// <summary>
/// Whether to allow reordering items in the playlist.
/// </summary>
public bool AllowReordering
{ {
this.allowEdit = allowEdit; get => allowReordering;
this.allowSelection = allowSelection; set
this.showItemOwner = showItemOwner; {
allowReordering = value;
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
item.AllowReordering = value;
}
} }
protected override void LoadComplete() private bool allowDeletion;
{
base.LoadComplete();
// Scheduled since items are removed and re-added upon rearrangement /// <summary>
Items.CollectionChanged += (_, args) => Schedule(() => /// Whether to allow deleting items from the playlist.
/// If <c>true</c>, requests to delete items may be satisfied via <see cref="RequestDeletion"/>.
/// </summary>
public bool AllowDeletion
{
get => allowDeletion;
set
{ {
switch (args.Action) allowDeletion = value;
{
case NotifyCollectionChangedAction.Remove: foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
if (allowSelection && args.OldItems.Contains(SelectedItem)) item.AllowDeletion = value;
SelectedItem.Value = null; }
break; }
}
}); private bool allowSelection;
/// <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>
public bool AllowSelection
{
get => allowSelection;
set
{
allowSelection = value;
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
item.AllowSelection = value;
}
}
private bool allowShowingResults;
/// <summary>
/// Whether to allow items to request their results to be shown.
/// If <c>true</c>, requests to show the results may be satisfied via <see cref="RequestResults"/>.
/// </summary>
public bool AllowShowingResults
{
get => allowShowingResults;
set
{
allowShowingResults = value;
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
item.AllowShowingResults = value;
}
}
private bool showItemOwners;
/// <summary>
/// Whether to show the avatar of users which own each playlist item.
/// </summary>
public bool ShowItemOwners
{
get => showItemOwners;
set
{
showItemOwners = value;
foreach (var item in ListContainer.OfType<DrawableRoomPlaylistItem>())
item.ShowItemOwner = value;
}
} }
protected override ScrollContainer<Drawable> CreateScrollContainer() => base.CreateScrollContainer().With(d => protected override ScrollContainer<Drawable> CreateScrollContainer() => base.CreateScrollContainer().With(d =>
@ -54,23 +131,18 @@ namespace osu.Game.Screens.OnlinePlay
Spacing = new Vector2(0, 2) Spacing = new Vector2(0, 2)
}; };
protected override OsuRearrangeableListItem<PlaylistItem> CreateOsuDrawable(PlaylistItem item) => new DrawableRoomPlaylistItem(item, allowEdit, allowSelection, showItemOwner) protected sealed override OsuRearrangeableListItem<PlaylistItem> CreateOsuDrawable(PlaylistItem item) => CreateDrawablePlaylistItem(item).With(d =>
{ {
SelectedItem = { BindTarget = SelectedItem }, d.SelectedItem.BindTarget = SelectedItem;
RequestDeletion = requestDeletion d.RequestDeletion = i => RequestDeletion?.Invoke(i);
}; d.AllowReordering = AllowReordering;
d.AllowDeletion = AllowDeletion;
d.AllowSelection = AllowSelection;
d.AllowShowingResults = AllowShowingResults;
d.ShowItemOwner = ShowItemOwners;
d.RequestResults = i => RequestResults?.Invoke(i);
});
private void requestDeletion(PlaylistItem item) protected virtual DrawableRoomPlaylistItem CreateDrawablePlaylistItem(PlaylistItem item) => new DrawableRoomPlaylistItem(item);
{
if (allowSelection && SelectedItem.Value == item)
{
if (Items.Count == 1)
SelectedItem.Value = null;
else
SelectedItem.Value = Items.GetNext(item) ?? Items[^2];
}
Items.Remove(item);
}
} }
} }

View File

@ -7,7 +7,6 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -39,12 +38,33 @@ namespace osu.Game.Screens.OnlinePlay
public class DrawableRoomPlaylistItem : OsuRearrangeableListItem<PlaylistItem> public class DrawableRoomPlaylistItem : OsuRearrangeableListItem<PlaylistItem>
{ {
public const float HEIGHT = 50; public const float HEIGHT = 50;
public const float ICON_HEIGHT = 34;
private const float icon_height = 34;
/// <summary>
/// Invoked when this item requests to be deleted.
/// </summary>
public Action<PlaylistItem> RequestDeletion; public Action<PlaylistItem> RequestDeletion;
/// <summary>
/// Invoked when this item requests its results to be shown.
/// </summary>
public Action<PlaylistItem> RequestResults;
/// <summary>
/// The currently-selected item, used to show a border around this item.
/// May be updated by this item if <see cref="AllowSelection"/> is <c>true</c>.
/// </summary>
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>(); public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
public readonly PlaylistItem Item;
private readonly DelayedLoadWrapper onScreenLoader = new DelayedLoadWrapper(Empty) { RelativeSizeAxes = Axes.Both };
private readonly IBindable<bool> valid = new Bindable<bool>();
private readonly Bindable<IBeatmapInfo> beatmap = new Bindable<IBeatmapInfo>();
private readonly Bindable<IRulesetInfo> ruleset = new Bindable<IRulesetInfo>();
private readonly BindableList<Mod> requiredMods = new BindableList<Mod>();
private Container maskingContainer; private Container maskingContainer;
private Container difficultyIconContainer; private Container difficultyIconContainer;
private LinkFlowContainer beatmapText; private LinkFlowContainer beatmapText;
@ -53,14 +73,10 @@ namespace osu.Game.Screens.OnlinePlay
private ModDisplay modDisplay; private ModDisplay modDisplay;
private FillFlowContainer buttonsFlow; private FillFlowContainer buttonsFlow;
private UpdateableAvatar ownerAvatar; private UpdateableAvatar ownerAvatar;
private Drawable removeButton;
private readonly IBindable<bool> valid = new Bindable<bool>(); private Drawable showResultsButton;
private PanelBackground panelBackground;
private readonly Bindable<IBeatmapInfo> beatmap = new Bindable<IBeatmapInfo>(); private FillFlowContainer mainFillFlow;
private readonly Bindable<IRulesetInfo> ruleset = new Bindable<IRulesetInfo>();
private readonly BindableList<Mod> requiredMods = new BindableList<Mod>();
public readonly PlaylistItem Item;
[Resolved] [Resolved]
private OsuColour colours { get; set; } private OsuColour colours { get; set; }
@ -71,35 +87,18 @@ namespace osu.Game.Screens.OnlinePlay
[Resolved] [Resolved]
private BeatmapLookupCache beatmapLookupCache { get; set; } private BeatmapLookupCache beatmapLookupCache { get; set; }
private PanelBackground panelBackground; protected override bool ShouldBeConsideredForInput(Drawable child) => AllowReordering || AllowDeletion || !AllowSelection || SelectedItem.Value == Model;
private readonly DelayedLoadWrapper onScreenLoader = new DelayedLoadWrapper(Empty) { RelativeSizeAxes = Axes.Both }; public DrawableRoomPlaylistItem(PlaylistItem item)
private readonly bool allowEdit;
private readonly bool allowSelection;
private readonly bool showItemOwner;
private FillFlowContainer mainFillFlow;
protected override bool ShouldBeConsideredForInput(Drawable child) => allowEdit || !allowSelection || SelectedItem.Value == Model;
public DrawableRoomPlaylistItem(PlaylistItem item, bool allowEdit, bool allowSelection, bool showItemOwner)
: base(item) : base(item)
{ {
Item = item; Item = item;
// TODO: edit support should be moved out into a derived class
this.allowEdit = allowEdit;
this.allowSelection = allowSelection;
this.showItemOwner = showItemOwner;
beatmap.BindTo(item.Beatmap); beatmap.BindTo(item.Beatmap);
valid.BindTo(item.Valid); valid.BindTo(item.Valid);
ruleset.BindTo(item.Ruleset); ruleset.BindTo(item.Ruleset);
requiredMods.BindTo(item.RequiredMods); requiredMods.BindTo(item.RequiredMods);
ShowDragHandle.Value = allowEdit;
if (item.Expired) if (item.Expired)
Colour = OsuColour.Gray(0.5f); Colour = OsuColour.Gray(0.5f);
} }
@ -107,9 +106,6 @@ namespace osu.Game.Screens.OnlinePlay
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load() private void load()
{ {
if (!allowEdit)
HandleColour = HandleColour.Opacity(0);
maskingContainer.BorderColour = colours.Yellow; maskingContainer.BorderColour = colours.Yellow;
} }
@ -169,6 +165,71 @@ namespace osu.Game.Screens.OnlinePlay
refresh(); refresh();
} }
/// <summary>
/// Whether this item can be selected.
/// </summary>
public bool AllowSelection { get; set; }
/// <summary>
/// Whether this item can be reordered in the playlist.
/// </summary>
public bool AllowReordering
{
get => ShowDragHandle.Value;
set => ShowDragHandle.Value = value;
}
private bool allowDeletion;
/// <summary>
/// Whether this item can be deleted.
/// </summary>
public bool AllowDeletion
{
get => allowDeletion;
set
{
allowDeletion = value;
if (removeButton != null)
removeButton.Alpha = value ? 1 : 0;
}
}
private bool allowShowingResults;
/// <summary>
/// Whether this item can have results shown.
/// </summary>
public bool AllowShowingResults
{
get => allowShowingResults;
set
{
allowShowingResults = value;
if (showResultsButton != null)
showResultsButton.Alpha = value ? 1 : 0;
}
}
private bool showItemOwner;
/// <summary>
/// Whether to display the avatar of the user which owns this playlist item.
/// </summary>
public bool ShowItemOwner
{
get => showItemOwner;
set
{
showItemOwner = value;
if (ownerAvatar != null)
ownerAvatar.Alpha = value ? 1 : 0;
}
}
private void refresh() private void refresh()
{ {
if (!valid.Value) if (!valid.Value)
@ -178,7 +239,7 @@ namespace osu.Game.Screens.OnlinePlay
} }
if (Item.Beatmap.Value != null) if (Item.Beatmap.Value != null)
difficultyIconContainer.Child = new DifficultyIcon(Item.Beatmap.Value, ruleset.Value, requiredMods, performBackgroundDifficultyLookup: false) { Size = new Vector2(ICON_HEIGHT) }; difficultyIconContainer.Child = new DifficultyIcon(Item.Beatmap.Value, ruleset.Value, requiredMods, performBackgroundDifficultyLookup: false) { Size = new Vector2(icon_height) };
else else
difficultyIconContainer.Clear(); difficultyIconContainer.Clear();
@ -208,7 +269,7 @@ namespace osu.Game.Screens.OnlinePlay
modDisplay.Current.Value = requiredMods.ToArray(); modDisplay.Current.Value = requiredMods.ToArray();
buttonsFlow.Clear(); buttonsFlow.Clear();
buttonsFlow.ChildrenEnumerable = CreateButtons(); buttonsFlow.ChildrenEnumerable = createButtons();
difficultyIconContainer.FadeInFromZero(500, Easing.OutQuint); difficultyIconContainer.FadeInFromZero(500, Easing.OutQuint);
mainFillFlow.FadeInFromZero(500, Easing.OutQuint); mainFillFlow.FadeInFromZero(500, Easing.OutQuint);
@ -322,7 +383,7 @@ namespace osu.Game.Screens.OnlinePlay
Margin = new MarginPadding { Horizontal = 8 }, Margin = new MarginPadding { Horizontal = 8 },
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Spacing = new Vector2(5), Spacing = new Vector2(5),
ChildrenEnumerable = CreateButtons().Select(button => button.With(b => ChildrenEnumerable = createButtons().Select(button => button.With(b =>
{ {
b.Anchor = Anchor.Centre; b.Anchor = Anchor.Centre;
b.Origin = Anchor.Centre; b.Origin = Anchor.Centre;
@ -332,11 +393,11 @@ namespace osu.Game.Screens.OnlinePlay
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(ICON_HEIGHT), Size = new Vector2(icon_height),
Margin = new MarginPadding { Right = 8 }, Margin = new MarginPadding { Right = 8 },
Masking = true, Masking = true,
CornerRadius = 4, CornerRadius = 4,
Alpha = showItemOwner ? 1 : 0 Alpha = ShowItemOwner ? 1 : 0
}, },
} }
} }
@ -345,17 +406,21 @@ namespace osu.Game.Screens.OnlinePlay
}; };
} }
protected virtual IEnumerable<Drawable> CreateButtons() => private IEnumerable<Drawable> createButtons() => new[]
new[] {
showResultsButton = new ShowResultsButton
{ {
Item.Beatmap.Value == null ? Empty() : new PlaylistDownloadButton(Item), Action = () => RequestResults?.Invoke(Item),
new PlaylistRemoveButton Alpha = AllowShowingResults ? 1 : 0,
{ },
Size = new Vector2(30, 30), Item.Beatmap.Value == null ? Empty() : new PlaylistDownloadButton(Item),
Alpha = allowEdit ? 1 : 0, removeButton = new PlaylistRemoveButton
Action = () => RequestDeletion?.Invoke(Model), {
}, Size = new Vector2(30, 30),
}; Alpha = AllowDeletion ? 1 : 0,
Action = () => RequestDeletion?.Invoke(Item),
},
};
public class PlaylistRemoveButton : GrayButton public class PlaylistRemoveButton : GrayButton
{ {
@ -374,7 +439,7 @@ namespace osu.Game.Screens.OnlinePlay
protected override bool OnClick(ClickEvent e) protected override bool OnClick(ClickEvent e)
{ {
if (allowSelection && valid.Value) if (AllowSelection && valid.Value)
SelectedItem.Value = Model; SelectedItem.Value = Model;
return true; return true;
} }
@ -432,6 +497,23 @@ namespace osu.Game.Screens.OnlinePlay
} }
} }
private class ShowResultsButton : IconButton
{
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Icon = FontAwesome.Solid.ChartPie;
TooltipText = "View results";
Add(new Box
{
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue,
Colour = colours.Gray4,
});
}
}
// For now, this is the same implementation as in PanelBackground, but supports a beatmap info rather than a working beatmap // For now, this is the same implementation as in PanelBackground, but supports a beatmap info rather than a working beatmap
private class PanelBackground : Container // todo: should be a buffered container (https://github.com/ppy/osu-framework/issues/3222) private class PanelBackground : Container // todo: should be a buffered container (https://github.com/ppy/osu-framework/issues/3222)
{ {

View File

@ -1,69 +0,0 @@
// 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.
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Rooms;
namespace osu.Game.Screens.OnlinePlay
{
public class DrawableRoomPlaylistWithResults : DrawableRoomPlaylist
{
public Action<PlaylistItem> RequestShowResults;
private readonly bool showItemOwner;
public DrawableRoomPlaylistWithResults(bool showItemOwner = false)
: base(false, true, showItemOwner: showItemOwner)
{
this.showItemOwner = showItemOwner;
}
protected override OsuRearrangeableListItem<PlaylistItem> CreateOsuDrawable(PlaylistItem item) =>
new DrawableRoomPlaylistItemWithResults(item, false, true, showItemOwner)
{
RequestShowResults = () => RequestShowResults(item),
SelectedItem = { BindTarget = SelectedItem },
};
private class DrawableRoomPlaylistItemWithResults : DrawableRoomPlaylistItem
{
public Action RequestShowResults;
public DrawableRoomPlaylistItemWithResults(PlaylistItem item, bool allowEdit, bool allowSelection, bool showItemOwner)
: base(item, allowEdit, allowSelection, showItemOwner)
{
}
protected override IEnumerable<Drawable> CreateButtons() =>
base.CreateButtons().Prepend(new FilledIconButton
{
Icon = FontAwesome.Solid.ChartPie,
Action = () => RequestShowResults?.Invoke(),
TooltipText = "View results"
});
private class FilledIconButton : IconButton
{
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Add(new Box
{
RelativeSizeAxes = Axes.Both,
Depth = float.MaxValue,
Colour = colours.Gray4,
});
}
}
}
}
}

View File

@ -248,7 +248,7 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
Spacing = new Vector2(5), Spacing = new Vector2(5),
Children = new Drawable[] Children = new Drawable[]
{ {
drawablePlaylist = new DrawableRoomPlaylist(false, false) drawablePlaylist = new DrawableRoomPlaylist
{ {
RelativeSizeAxes = Axes.X, RelativeSizeAxes = Axes.X,
Height = DrawableRoomPlaylistItem.HEIGHT Height = DrawableRoomPlaylistItem.HEIGHT

View File

@ -16,8 +16,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match.Playlist
public class MultiplayerHistoryList : DrawableRoomPlaylist public class MultiplayerHistoryList : DrawableRoomPlaylist
{ {
public MultiplayerHistoryList() public MultiplayerHistoryList()
: base(false, false, true)
{ {
ShowItemOwners = true;
} }
protected override FillFlowContainer<RearrangeableListItem<PlaylistItem>> CreateListFillFlowContainer() => new HistoryFillFlowContainer protected override FillFlowContainer<RearrangeableListItem<PlaylistItem>> CreateListFillFlowContainer() => new HistoryFillFlowContainer

View File

@ -18,8 +18,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match.Playlist
public class MultiplayerQueueList : DrawableRoomPlaylist public class MultiplayerQueueList : DrawableRoomPlaylist
{ {
public MultiplayerQueueList() public MultiplayerQueueList()
: base(false, false, true)
{ {
ShowItemOwners = true;
} }
protected override FillFlowContainer<RearrangeableListItem<PlaylistItem>> CreateListFillFlowContainer() => new QueueFillFlowContainer protected override FillFlowContainer<RearrangeableListItem<PlaylistItem>> CreateListFillFlowContainer() => new QueueFillFlowContainer

View File

@ -205,7 +205,10 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
{ {
new Drawable[] new Drawable[]
{ {
playlist = new DrawableRoomPlaylist(true, false) { RelativeSizeAxes = Axes.Both } playlist = new PlaylistsRoomSettingsPlaylist
{
RelativeSizeAxes = Axes.Both,
}
}, },
new Drawable[] new Drawable[]
{ {

View File

@ -0,0 +1,29 @@
// 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.
using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
namespace osu.Game.Screens.OnlinePlay.Playlists
{
/// <summary>
/// A <see cref="DrawableRoomPlaylist"/> which is displayed during the setup stage of a playlists room.
/// </summary>
public class PlaylistsRoomSettingsPlaylist : DrawableRoomPlaylist
{
public PlaylistsRoomSettingsPlaylist()
{
AllowReordering = true;
AllowDeletion = true;
RequestDeletion = item =>
{
var nextItem = Items.GetNext(item);
Items.Remove(item);
SelectedItem.Value = nextItem ?? Items.LastOrDefault();
};
}
}
}

View File

@ -88,12 +88,14 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
new Drawable[] { new OverlinedPlaylistHeader(), }, new Drawable[] { new OverlinedPlaylistHeader(), },
new Drawable[] new Drawable[]
{ {
new DrawableRoomPlaylistWithResults new DrawableRoomPlaylist
{ {
RelativeSizeAxes = Axes.Both, RelativeSizeAxes = Axes.Both,
Items = { BindTarget = Room.Playlist }, Items = { BindTarget = Room.Playlist },
SelectedItem = { BindTarget = SelectedItem }, SelectedItem = { BindTarget = SelectedItem },
RequestShowResults = item => AllowSelection = true,
AllowShowingResults = true,
RequestResults = item =>
{ {
Debug.Assert(RoomId.Value != null); Debug.Assert(RoomId.Value != null);
ParentScreen?.Push(new PlaylistsResultsScreen(null, RoomId.Value.Value, item, false)); ParentScreen?.Push(new PlaylistsResultsScreen(null, RoomId.Value.Value, item, false));