Split OnlinePlayComposite to remove if-statement

This commit is contained in:
smoogipoo 2021-02-18 15:47:33 +09:00
parent 56e9e10ff5
commit e911760318
3 changed files with 46 additions and 30 deletions

View File

@ -11,7 +11,7 @@
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{
public class BeatmapSelectionControl : OnlinePlayComposite
public class BeatmapSelectionControl : RoomSubScreenComposite
{
[Resolved]
private MultiplayerMatchSubScreen matchSubScreen { get; set; }

View File

@ -2,9 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Specialized;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
@ -14,6 +12,9 @@
namespace osu.Game.Screens.OnlinePlay
{
/// <summary>
/// A <see cref="CompositeDrawable"/> that exposes bindables for <see cref="Room"/> properties.
/// </summary>
public class OnlinePlayComposite : CompositeDrawable
{
[Resolved(typeof(Room))]
@ -62,41 +63,18 @@ public class OnlinePlayComposite : CompositeDrawable
/// The currently selected item in the <see cref="RoomSubScreen"/>, or the first item from <see cref="Playlist"/>
/// if this <see cref="OnlinePlayComposite"/> is not within a <see cref="RoomSubScreen"/>.
/// </summary>
protected IBindable<PlaylistItem> SelectedItem => selectedItem;
private readonly Bindable<PlaylistItem> selectedItem = new Bindable<PlaylistItem>();
[CanBeNull]
[Resolved(CanBeNull = true)]
private IBindable<PlaylistItem> subScreenSelectedItem { get; set; }
protected readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
protected override void LoadComplete()
{
base.LoadComplete();
if (subScreenSelectedItem != null)
subScreenSelectedItem.BindValueChanged(onSelectedItemChanged, true);
else
Playlist.BindCollectionChanged(onPlaylistChanged, true);
Playlist.BindCollectionChanged((_, __) => UpdateSelectedItem(), true);
}
/// <summary>
/// Invoked when the selected item from within a <see cref="RoomSubScreen"/> changes.
/// Does not occur when this <see cref="OnlinePlayComposite"/> is outside a <see cref="RoomSubScreen"/>.
/// </summary>
private void onSelectedItemChanged(ValueChangedEvent<PlaylistItem> item)
protected virtual void UpdateSelectedItem()
{
// If the room hasn't been created yet, fall-back to the first item from the playlist.
selectedItem.Value = RoomID.Value == null ? Playlist.FirstOrDefault() : item.NewValue;
}
/// <summary>
/// Invoked when the playlist changes.
/// Does not occur when this <see cref="OnlinePlayComposite"/> is inside a <see cref="RoomSubScreen"/>.
/// </summary>
private void onPlaylistChanged(object sender, NotifyCollectionChangedEventArgs e)
{
selectedItem.Value = Playlist.FirstOrDefault();
SelectedItem.Value = Playlist.FirstOrDefault();
}
}
}

View File

@ -0,0 +1,38 @@
// 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Game.Online.Rooms;
using osu.Game.Screens.OnlinePlay.Match;
namespace osu.Game.Screens.OnlinePlay
{
/// <summary>
/// An <see cref="OnlinePlayComposite"/> with additional logic tracking the currently-selected <see cref="PlaylistItem"/> inside a <see cref="RoomSubScreen"/>.
/// </summary>
public class RoomSubScreenComposite : OnlinePlayComposite
{
[Resolved]
private IBindable<PlaylistItem> subScreenSelectedItem { get; set; }
protected override void LoadComplete()
{
base.LoadComplete();
subScreenSelectedItem.BindValueChanged(_ => UpdateSelectedItem(), true);
}
protected override void UpdateSelectedItem()
{
if (RoomID.Value == null)
{
// If the room hasn't been created yet, fall-back to the base logic.
base.UpdateSelectedItem();
return;
}
SelectedItem.Value = subScreenSelectedItem.Value;
}
}
}