1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-15 09:22:54 +08:00

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 @@ using osu.Game.Screens.OnlinePlay.Match.Components;
namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match namespace osu.Game.Screens.OnlinePlay.Multiplayer.Match
{ {
public class BeatmapSelectionControl : OnlinePlayComposite public class BeatmapSelectionControl : RoomSubScreenComposite
{ {
[Resolved] [Resolved]
private MultiplayerMatchSubScreen matchSubScreen { get; set; } private MultiplayerMatchSubScreen matchSubScreen { get; set; }

View File

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

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;
}
}
}