1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-15 16:27:43 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/Match/RoomSubScreen.cs

558 lines
21 KiB
C#
Raw Normal View History

// 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Bindables;
2021-08-12 18:51:03 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2022-03-17 18:05:28 +08:00
using osu.Framework.Graphics.Cursor;
2021-08-12 18:51:03 +08:00
using osu.Framework.Graphics.Shapes;
2022-05-21 15:55:42 +08:00
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Screens;
using osu.Game.Audio;
using osu.Game.Beatmaps;
2022-05-21 15:55:42 +08:00
using osu.Game.Input.Bindings;
using osu.Game.Online.API;
2020-12-25 12:38:11 +08:00
using osu.Game.Online.Rooms;
using osu.Game.Overlays;
using osu.Game.Overlays.Mods;
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens.Menu;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.OnlinePlay.Multiplayer;
namespace osu.Game.Screens.OnlinePlay.Match
{
[Cached(typeof(IPreviewTrackOwner))]
2022-11-24 13:32:20 +08:00
public abstract partial class RoomSubScreen : OnlinePlaySubScreen, IPreviewTrackOwner
{
[Cached(typeof(IBindable<PlaylistItem>))]
public readonly Bindable<PlaylistItem> SelectedItem = new Bindable<PlaylistItem>();
public override bool? ApplyModTrackAdjustments => true;
2021-08-20 20:40:35 +08:00
protected override BackgroundScreen CreateBackground() => new RoomBackgroundScreen(Room.Playlist.FirstOrDefault())
{
SelectedItem = { BindTarget = SelectedItem }
};
2021-08-20 17:14:12 +08:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
/// <summary>
/// A container that provides controls for selection of user mods.
/// This will be shown/hidden automatically when applicable.
/// </summary>
protected Drawable UserModsSection;
2021-01-19 16:11:40 +08:00
private Sample sampleStart;
/// <summary>
/// Any mods applied by/to the local user.
/// </summary>
2021-02-01 16:57:32 +08:00
protected readonly Bindable<IReadOnlyList<Mod>> UserMods = new Bindable<IReadOnlyList<Mod>>(Array.Empty<Mod>());
protected readonly IBindable<long?> RoomId = new Bindable<long?>();
[Resolved(CanBeNull = true)]
private IOverlayManager overlayManager { get; set; }
[Resolved]
private MusicController music { get; set; }
[Resolved]
private BeatmapManager beatmapManager { get; set; }
[Resolved]
protected RulesetStore Rulesets { get; private set; }
[Resolved]
private IAPIProvider api { get; set; } = null!;
[Resolved(canBeNull: true)]
protected OnlinePlayScreen ParentScreen { get; private set; }
[Cached]
private readonly OnlinePlayBeatmapAvailabilityTracker beatmapAvailabilityTracker = new OnlinePlayBeatmapAvailabilityTracker();
protected IBindable<BeatmapAvailability> BeatmapAvailability => beatmapAvailabilityTracker.Availability;
public readonly Room Room;
2021-08-18 15:35:18 +08:00
private readonly bool allowEdit;
2021-08-17 16:05:20 +08:00
2022-07-01 15:51:15 +08:00
internal ModSelectOverlay UserModsSelectOverlay { get; private set; }
[CanBeNull]
private IDisposable userModsSelectOverlayRegistration;
2021-08-18 14:16:48 +08:00
private RoomSettingsOverlay settingsOverlay;
private Drawable mainContent;
2021-08-17 16:05:20 +08:00
2021-08-18 15:35:18 +08:00
/// <summary>
/// Creates a new <see cref="RoomSubScreen"/>.
/// </summary>
/// <param name="room">The <see cref="Room"/>.</param>
/// <param name="allowEdit">Whether to allow editing room settings post-creation.</param>
protected RoomSubScreen(Room room, bool allowEdit = true)
{
Room = room;
2021-08-18 15:35:18 +08:00
this.allowEdit = allowEdit;
2021-08-17 16:05:20 +08:00
2021-08-12 17:02:00 +08:00
Padding = new MarginPadding { Top = Header.HEIGHT };
RoomId.BindTo(room.RoomID);
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
sampleStart = audio.Samples.Get(@"SongSelect/confirm-selection");
2021-08-17 16:05:20 +08:00
2022-03-17 18:05:28 +08:00
InternalChild = new PopoverContainer
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
beatmapAvailabilityTracker,
new MultiplayerRoomSounds(),
new GridContainer
2021-08-17 16:13:25 +08:00
{
2022-03-17 18:05:28 +08:00
RelativeSizeAxes = Axes.Both,
RowDimensions = new[]
{
new Dimension(),
new Dimension(GridSizeMode.Absolute, 50)
},
Content = new[]
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
// Padded main content (drawable room + main content)
new Drawable[]
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
new Container
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
Horizontal = WaveOverlayContainer.WIDTH_PADDING,
Bottom = 30
},
Children = new[]
{
mainContent = new GridContainer
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
RelativeSizeAxes = Axes.Both,
RowDimensions = new[]
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.Absolute, 10)
},
2022-03-17 18:05:28 +08:00
Content = new[]
{
2022-03-17 18:05:28 +08:00
new Drawable[]
{
new DrawableMatchRoom(Room, allowEdit)
{
OnEdit = () => settingsOverlay.Show(),
SelectedItem = { BindTarget = SelectedItem }
}
},
null,
new Drawable[]
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
new Container
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
RelativeSizeAxes = Axes.Both,
Children = new[]
2021-08-17 16:05:20 +08:00
{
2022-03-17 18:05:28 +08:00
new Container
{
RelativeSizeAxes = Axes.Both,
2022-03-17 18:05:28 +08:00
Masking = true,
CornerRadius = 10,
Child = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex(@"3e3a44") // Temporary.
},
},
2022-03-17 18:05:28 +08:00
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(20),
Child = CreateMainContent(),
},
new Container
{
2022-03-17 18:05:28 +08:00
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
}
}
2021-08-17 16:05:20 +08:00
}
}
2022-03-17 18:05:28 +08:00
},
new Container
{
RelativeSizeAxes = Axes.Both,
// Resolves 1px masking errors between the settings overlay and the room panel.
Padding = new MarginPadding(-1),
Child = settingsOverlay = CreateRoomSettingsOverlay(Room)
2021-08-17 16:05:20 +08:00
}
},
},
},
2022-03-17 18:05:28 +08:00
// Footer
new Drawable[]
2021-08-18 19:21:29 +08:00
{
2022-03-17 18:05:28 +08:00
new Container
2021-08-18 19:21:29 +08:00
{
2022-03-17 18:05:28 +08:00
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2021-08-18 19:21:29 +08:00
{
2022-03-17 18:05:28 +08:00
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4Extensions.FromHex(@"28242d") // Temporary.
},
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(5),
Child = CreateFooter()
},
}
2021-08-18 19:21:29 +08:00
}
}
2021-08-17 16:05:20 +08:00
}
}
}
};
2024-02-18 09:28:24 +08:00
LoadComponent(UserModsSelectOverlay = new MultiplayerModSelectOverlay
{
SelectedMods = { BindTarget = UserMods },
2024-02-18 09:13:57 +08:00
IsValidMod = _ => false
});
}
protected override void LoadComplete()
{
base.LoadComplete();
RoomId.BindValueChanged(id =>
{
if (id.NewValue == null)
{
// A new room is being created.
// The main content should be hidden until the settings overlay is hidden, signaling the room is ready to be displayed.
mainContent.Hide();
settingsOverlay.Show();
}
else
{
mainContent.Show();
settingsOverlay.Hide();
}
}, true);
SelectedItem.BindValueChanged(_ => Scheduler.AddOnce(selectedItemChanged));
UserMods.BindValueChanged(_ => Scheduler.AddOnce(UpdateMods));
beatmapAvailabilityTracker.SelectedItem.BindTo(SelectedItem);
beatmapAvailabilityTracker.Availability.BindValueChanged(_ => updateWorkingBeatmap());
userModsSelectOverlayRegistration = overlayManager?.RegisterBlockingOverlay(UserModsSelectOverlay);
}
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
return new CachedModelDependencyContainer<Room>(base.CreateChildDependencies(parent))
{
Model = { Value = Room }
};
}
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
protected virtual bool IsConnected => api.State.Value == APIState.Online;
public override bool OnBackButton()
{
if (Room.RoomID.Value == null)
{
if (!ensureExitConfirmed())
return true;
settingsOverlay.Hide();
return base.OnBackButton();
}
if (UserModsSelectOverlay.State.Value == Visibility.Visible)
{
UserModsSelectOverlay.Hide();
return true;
}
2021-08-18 14:16:48 +08:00
if (settingsOverlay.State.Value == Visibility.Visible)
{
2021-08-18 14:16:48 +08:00
settingsOverlay.Hide();
return true;
}
return base.OnBackButton();
}
protected void ShowUserModSelect() => UserModsSelectOverlay.Show();
public override void OnEntering(ScreenTransitionEvent e)
{
base.OnEntering(e);
beginHandlingTrack();
}
public override void OnSuspending(ScreenTransitionEvent e)
{
// Should be a noop in most cases, but let's ensure beyond doubt that the beatmap is in a correct state.
updateWorkingBeatmap();
onLeaving();
base.OnSuspending(e);
}
public override void OnResuming(ScreenTransitionEvent e)
{
base.OnResuming(e);
updateWorkingBeatmap();
beginHandlingTrack();
Scheduler.AddOnce(UpdateMods);
Scheduler.AddOnce(updateRuleset);
}
2022-07-30 16:49:33 +08:00
protected bool ExitConfirmed { get; private set; }
public override bool OnExiting(ScreenExitEvent e)
{
if (!ensureExitConfirmed())
return true;
RoomManager?.PartRoom();
Mods.Value = Array.Empty<Mod>();
onLeaving();
return base.OnExiting(e);
}
private bool ensureExitConfirmed()
{
2022-07-30 16:49:33 +08:00
if (ExitConfirmed)
return true;
if (!IsConnected)
return true;
bool hasUnsavedChanges = Room.RoomID.Value == null && Room.Playlist.Count > 0;
if (dialogOverlay == null || !hasUnsavedChanges)
return true;
// if the dialog is already displayed, block exiting until the user explicitly makes a decision.
if (dialogOverlay.CurrentDialog is ConfirmDiscardChangesDialog discardChangesDialog)
{
discardChangesDialog.Flash();
return false;
}
dialogOverlay.Push(new ConfirmDiscardChangesDialog(() =>
{
2022-07-30 16:49:33 +08:00
ExitConfirmed = true;
settingsOverlay.Hide();
this.Exit();
}));
return false;
}
2021-04-22 22:37:33 +08:00
protected void StartPlay()
{
// User may be at song select or otherwise when the host starts gameplay.
// Ensure that they first return to this screen, else global bindables (beatmap etc.) may be in a bad state.
if (!this.IsCurrentScreen())
{
this.MakeCurrent();
Schedule(StartPlay);
return;
}
sampleStart?.Play();
// fallback is to allow this class to operate when there is no parent OnlineScreen (testing purposes).
var targetScreen = (Screen)ParentScreen ?? this;
2021-05-14 14:32:56 +08:00
targetScreen.Push(CreateGameplayScreen());
}
2021-04-22 22:37:33 +08:00
/// <summary>
/// Creates the gameplay screen to be entered.
/// </summary>
/// <returns>The screen to enter.</returns>
protected abstract Screen CreateGameplayScreen();
private void selectedItemChanged()
{
updateWorkingBeatmap();
2021-02-19 13:36:51 +08:00
var selected = SelectedItem.Value;
if (selected == null)
return;
var rulesetInstance = Rulesets.GetRuleset(SelectedItem.Value.RulesetID)?.CreateInstance();
Debug.Assert(rulesetInstance != null);
var allowedMods = SelectedItem.Value.AllowedMods.Select(m => m.ToMod(rulesetInstance));
2021-02-01 17:50:32 +08:00
// Remove any user mods that are no longer allowed.
UserMods.Value = UserMods.Value.Where(m => allowedMods.Any(a => m.GetType() == a.GetType())).ToList();
UpdateMods();
updateRuleset();
if (!selected.AllowedMods.Any())
{
UserModsSection?.Hide();
UserModsSelectOverlay.Hide();
UserModsSelectOverlay.IsValidMod = _ => false;
}
else
{
UserModsSection?.Show();
UserModsSelectOverlay.IsValidMod = m => allowedMods.Any(a => a.GetType() == m.GetType());
}
}
private void updateWorkingBeatmap()
{
if (SelectedItem.Value == null || !this.IsCurrentScreen())
return;
var beatmap = SelectedItem.Value?.Beatmap;
// Retrieve the corresponding local beatmap, since we can't directly use the playlist's beatmap info
var localBeatmap = beatmap == null ? null : beatmapManager.QueryBeatmap(b => b.OnlineID == beatmap.OnlineID);
UserModsSelectOverlay.Beatmap = Beatmap.Value = beatmapManager.GetWorkingBeatmap(localBeatmap);
}
protected virtual void UpdateMods()
{
if (SelectedItem.Value == null || !this.IsCurrentScreen())
return;
var rulesetInstance = Rulesets.GetRuleset(SelectedItem.Value.RulesetID)?.CreateInstance();
Debug.Assert(rulesetInstance != null);
Mods.Value = UserMods.Value.Concat(SelectedItem.Value.RequiredMods.Select(m => m.ToMod(rulesetInstance))).ToList();
}
private void updateRuleset()
{
if (SelectedItem.Value == null || !this.IsCurrentScreen())
return;
Ruleset.Value = Rulesets.GetRuleset(SelectedItem.Value.RulesetID);
}
private void beginHandlingTrack()
{
Beatmap.BindValueChanged(applyLoopingToTrack, true);
}
private void onLeaving()
{
UserModsSelectOverlay.Hide();
endHandlingTrack();
}
private void endHandlingTrack()
{
Beatmap.ValueChanged -= applyLoopingToTrack;
cancelTrackLooping();
}
private void applyLoopingToTrack(ValueChangedEvent<WorkingBeatmap> _ = null)
{
if (!this.IsCurrentScreen())
return;
var track = Beatmap.Value?.Track;
if (track != null)
{
Beatmap.Value.PrepareTrackForPreview(true);
music?.EnsurePlayingSomething();
}
}
private void cancelTrackLooping()
{
2024-02-02 18:48:13 +08:00
var track = Beatmap.Value?.Track;
if (track != null)
track.Looping = false;
}
2021-08-18 19:21:29 +08:00
/// <summary>
/// Creates the main centred content.
/// </summary>
2021-08-17 16:05:20 +08:00
protected abstract Drawable CreateMainContent();
2021-08-18 19:21:29 +08:00
/// <summary>
/// Creates the footer content.
/// </summary>
2021-08-17 16:05:20 +08:00
protected abstract Drawable CreateFooter();
2021-08-18 19:21:29 +08:00
/// <summary>
/// Creates the room settings overlay.
/// </summary>
2021-08-20 20:45:24 +08:00
/// <param name="room">The room to change the settings of.</param>
2021-08-19 15:40:27 +08:00
protected abstract RoomSettingsOverlay CreateRoomSettingsOverlay(Room room);
public partial class UserModSelectButton : PurpleRoundedButton, IKeyBindingHandler<GlobalAction>
{
2022-05-22 05:16:29 +08:00
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
2022-05-21 15:55:42 +08:00
{
if (e.Action == GlobalAction.ToggleModSelection && !e.Repeat)
{
TriggerClick();
return true;
}
2022-05-21 16:07:24 +08:00
2022-05-21 15:55:42 +08:00
return false;
}
2022-05-22 05:16:29 +08:00
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e) { }
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
userModsSelectOverlayRegistration?.Dispose();
}
}
}