Clean up selection handling

This commit is contained in:
smoogipoo 2020-02-14 16:55:05 +09:00
parent 6a466ea2f5
commit aceba8791c
3 changed files with 40 additions and 43 deletions

View File

@ -46,6 +46,9 @@ namespace osu.Game.Tests.Visual.Multiplayer
moveToItem(0);
assertHandleVisibility(0, false);
assertDeleteButtonVisibility(0, false);
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("no item selected", () => playlist.SelectedItem.Value == null);
}
[Test]
@ -56,6 +59,9 @@ namespace osu.Game.Tests.Visual.Multiplayer
moveToItem(0);
assertHandleVisibility(0, true);
assertDeleteButtonVisibility(0, true);
AddStep("click", () => InputManager.Click(MouseButton.Left));
AddAssert("no item selected", () => playlist.SelectedItem.Value == null);
}
[Test]
@ -165,7 +171,7 @@ namespace osu.Game.Tests.Visual.Multiplayer
moveToDeleteButton(0);
AddStep("click delete button", () => InputManager.Click(MouseButton.Left));
AddAssert("selected item is null", () => playlist.SelectedItem.Value == null);
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)

View File

@ -28,15 +28,7 @@ namespace osu.Game.Screens.Multi
{
base.LoadComplete();
SelectedItem.BindValueChanged(item =>
{
if (item.OldValue != null && ItemMap.TryGetValue(item.OldValue, out var oldItem))
((DrawableRoomPlaylistItem)oldItem).Deselect();
if (item.NewValue != null && ItemMap.TryGetValue(item.NewValue, out var newItem))
((DrawableRoomPlaylistItem)newItem).Select();
}, true);
// Scheduled since items are removed and re-added upon rearrangement
Items.ItemsRemoved += items => Schedule(() =>
{
if (!Items.Contains(SelectedItem.Value))
@ -58,7 +50,7 @@ namespace osu.Game.Screens.Multi
protected override OsuRearrangeableListItem<PlaylistItem> CreateOsuDrawable(PlaylistItem item) => new DrawableRoomPlaylistItem(item, allowEdit, allowSelection)
{
RequestSelection = requestSelection,
SelectedItem = { BindTarget = SelectedItem },
RequestDeletion = requestDeletion
};

View File

@ -31,9 +31,10 @@ namespace osu.Game.Screens.Multi
{
public class DrawableRoomPlaylistItem : OsuRearrangeableListItem<PlaylistItem>
{
public Action<PlaylistItem> RequestSelection;
public Action<PlaylistItem> RequestDeletion;
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
protected override bool ShowDragHandle => allowEdit;
private Container maskingContainer;
@ -75,6 +76,8 @@ namespace osu.Game.Screens.Multi
{
base.LoadComplete();
SelectedItem.BindValueChanged(selected => maskingContainer.BorderThickness = selected.NewValue == Model ? 5 : 0);
beatmap.BindValueChanged(_ => scheduleRefresh());
ruleset.BindValueChanged(_ => scheduleRefresh());
@ -84,6 +87,32 @@ namespace osu.Game.Screens.Multi
refresh();
}
private ScheduledDelegate scheduledRefresh;
private void scheduleRefresh()
{
scheduledRefresh?.Cancel();
scheduledRefresh = Schedule(refresh);
}
private void refresh()
{
difficultyIconContainer.Child = new DifficultyIcon(beatmap.Value, ruleset.Value) { Size = new Vector2(32) };
beatmapText.Clear();
beatmapText.AddLink(item.Beatmap.ToString(), LinkAction.OpenBeatmap, item.Beatmap.Value.OnlineBeatmapID.ToString());
authorText.Clear();
if (item.Beatmap?.Value?.Metadata?.Author != null)
{
authorText.AddText("mapped by ");
authorText.AddUserLink(item.Beatmap.Value?.Metadata.Author);
}
modDisplay.Current.Value = requiredMods.ToArray();
}
protected override Drawable CreateContent() => maskingContainer = new Container
{
RelativeSizeAxes = Axes.X,
@ -173,43 +202,13 @@ namespace osu.Game.Screens.Multi
}
};
private ScheduledDelegate scheduledRefresh;
private void scheduleRefresh()
{
scheduledRefresh?.Cancel();
scheduledRefresh = Schedule(refresh);
}
private void refresh()
{
difficultyIconContainer.Child = new DifficultyIcon(beatmap.Value, ruleset.Value) { Size = new Vector2(32) };
beatmapText.Clear();
beatmapText.AddLink(item.Beatmap.ToString(), LinkAction.OpenBeatmap, item.Beatmap.Value.OnlineBeatmapID.ToString());
authorText.Clear();
if (item.Beatmap?.Value?.Metadata?.Author != null)
{
authorText.AddText("mapped by ");
authorText.AddUserLink(item.Beatmap.Value?.Metadata.Author);
}
modDisplay.Current.Value = requiredMods.ToArray();
}
protected override bool OnClick(ClickEvent e)
{
if (allowSelection)
RequestSelection?.Invoke(Model);
SelectedItem.Value = Model;
return true;
}
public void Select() => maskingContainer.BorderThickness = 5;
public void Deselect() => maskingContainer.BorderThickness = 0;
// 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)
{