1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 07:43:01 +08:00
osu-lazer/osu.Game/Screens/Play/SoloSpectatorScreen.cs

278 lines
10 KiB
C#
Raw Normal View History

2020-10-26 18:47:39 +08:00
// 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.Diagnostics;
2020-10-26 18:47:39 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2020-10-28 17:35:20 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Screens;
2021-04-02 20:27:20 +08:00
using osu.Framework.Threading;
using osu.Game.Audio;
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables.Cards;
using osu.Game.Configuration;
using osu.Game.Graphics;
2020-10-26 18:47:39 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Spectator;
using osu.Game.Overlays;
using osu.Game.Overlays.Settings;
using osu.Game.Screens.OnlinePlay.Match.Components;
using osu.Game.Screens.Spectate;
2020-10-26 18:47:39 +08:00
using osu.Game.Users;
using osuTK;
2020-10-26 18:47:39 +08:00
namespace osu.Game.Screens.Play
{
[Cached(typeof(IPreviewTrackOwner))]
public partial class SoloSpectatorScreen : SpectatorScreen, IPreviewTrackOwner
2020-10-26 18:47:39 +08:00
{
[Resolved]
2023-11-22 14:56:49 +08:00
private IAPIProvider api { get; set; } = null!;
[Resolved]
2023-11-22 14:56:49 +08:00
private PreviewTrackManager previewTrackManager { get; set; } = null!;
[Resolved]
2023-11-22 14:56:49 +08:00
private BeatmapManager beatmaps { get; set; } = null!;
[Resolved]
2023-11-22 14:56:49 +08:00
private BeatmapModelDownloader beatmapDownloader { get; set; } = null!;
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
2023-11-22 14:56:49 +08:00
private Container beatmapPanelContainer = null!;
private RoundedButton watchButton = null!;
private SettingsCheckbox automaticDownload = null!;
private readonly APIUser targetUser;
/// <summary>
/// The player's immediate online gameplay state.
2021-04-02 20:27:20 +08:00
/// This doesn't always reflect the gameplay state being watched.
/// </summary>
2023-11-22 14:56:49 +08:00
private SpectatorGameplayState? immediateSpectatorGameplayState;
2023-11-22 14:56:49 +08:00
private GetBeatmapSetRequest? onlineBeatmapRequest;
2023-11-22 14:56:49 +08:00
private APIBeatmapSet? beatmapSet;
2023-11-22 14:56:49 +08:00
public SoloSpectatorScreen(APIUser targetUser)
: base(targetUser.Id)
2020-10-26 18:47:39 +08:00
{
this.targetUser = targetUser;
2020-10-26 18:47:39 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuConfigManager config)
2020-10-26 18:47:39 +08:00
{
2020-10-28 17:35:20 +08:00
InternalChild = new Container
2020-10-26 18:47:39 +08:00
{
2020-10-28 17:35:20 +08:00
Masking = true,
CornerRadius = 20,
AutoSizeAxes = Axes.Both,
AutoSizeDuration = 500,
AutoSizeEasing = Easing.OutQuint,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Children = new Drawable[]
2020-10-26 18:47:39 +08:00
{
2020-10-28 17:35:20 +08:00
new Box
{
Colour = colourProvider.Background5,
2020-10-28 17:35:20 +08:00
RelativeSizeAxes = Axes.Both,
},
new FillFlowContainer
{
Margin = new MarginPadding(20),
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Vertical,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Spacing = new Vector2(15),
Children = new Drawable[]
{
2020-10-28 17:35:20 +08:00
new OsuSpriteText
{
Text = "Spectator Mode",
Font = OsuFont.Default.With(size: 30),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
new FillFlowContainer
{
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Spacing = new Vector2(15),
Children = new Drawable[]
{
new UserGridPanel(targetUser)
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Height = 145,
Width = 290,
},
new SpriteIcon
{
Size = new Vector2(40),
Icon = FontAwesome.Solid.ArrowRight,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
beatmapPanelContainer = new Container
{
AutoSizeAxes = Axes.Both,
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
},
}
},
automaticDownload = new SettingsCheckbox
{
LabelText = "Automatically download beatmaps",
Current = config.GetBindable<bool>(OsuSetting.AutomaticallyDownloadMissingBeatmaps),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
watchButton = new PurpleRoundedButton
2020-10-28 17:35:20 +08:00
{
Text = "Start Watching",
Width = 250,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = () => scheduleStart(immediateSpectatorGameplayState),
Enabled = { Value = false }
2020-10-28 17:35:20 +08:00
}
}
}
2020-10-28 17:35:20 +08:00
}
2020-10-26 18:47:39 +08:00
};
}
protected override void LoadComplete()
{
base.LoadComplete();
automaticDownload.Current.BindValueChanged(_ => checkForAutomaticDownload());
}
protected override void OnNewPlayingUserState(int userId, SpectatorState spectatorState) => Schedule(() =>
{
clearDisplay();
showBeatmapPanel(spectatorState);
});
protected override void StartGameplay(int userId, SpectatorGameplayState spectatorGameplayState) => Schedule(() =>
{
immediateSpectatorGameplayState = spectatorGameplayState;
watchButton.Enabled.Value = true;
scheduleStart(spectatorGameplayState);
});
2021-04-02 20:27:20 +08:00
protected override void FailGameplay(int userId)
{
if (this.GetChildScreen() is SpectatorPlayerLoader loader)
{
if (loader.GetChildScreen() is SpectatorPlayer player)
{
player.AllowFail();
resetStartState();
}
else
QuitGameplay(userId);
}
}
protected override void QuitGameplay(int userId)
{
// Importantly, don't schedule this call, as a child screen may be present (and will cause the schedule to not be run as expected).
this.MakeCurrent();
resetStartState();
}
private void resetStartState() => Schedule(() =>
{
scheduledStart?.Cancel();
immediateSpectatorGameplayState = null;
clearDisplay();
});
2020-10-28 17:35:20 +08:00
private void clearDisplay()
{
watchButton.Enabled.Value = false;
onlineBeatmapRequest?.Cancel();
2020-10-28 17:35:20 +08:00
beatmapPanelContainer.Clear();
previewTrackManager.StopAnyPlaying(this);
2020-10-28 17:35:20 +08:00
}
2023-11-22 14:56:49 +08:00
private ScheduledDelegate? scheduledStart;
2021-04-02 20:27:20 +08:00
2023-11-22 14:56:49 +08:00
private void scheduleStart(SpectatorGameplayState? spectatorGameplayState)
2020-10-28 17:35:20 +08:00
{
2023-11-22 14:56:49 +08:00
Debug.Assert(spectatorGameplayState != null);
2021-04-02 20:27:20 +08:00
// This function may be called multiple times in quick succession once the screen becomes current again.
scheduledStart?.Cancel();
scheduledStart = Schedule(() =>
{
if (this.IsCurrentScreen())
start();
else
scheduleStart(spectatorGameplayState);
2021-04-02 20:27:20 +08:00
});
2021-04-02 20:27:20 +08:00
void start()
{
Beatmap.Value = spectatorGameplayState.Beatmap;
Ruleset.Value = spectatorGameplayState.Ruleset.RulesetInfo;
this.Push(new SpectatorPlayerLoader(spectatorGameplayState.Score, () => new SoloSpectatorPlayer(spectatorGameplayState.Score)));
2021-04-02 20:27:20 +08:00
}
}
private void showBeatmapPanel(SpectatorState state)
{
Debug.Assert(state.BeatmapID != null);
onlineBeatmapRequest = new GetBeatmapSetRequest(state.BeatmapID.Value, BeatmapSetLookupType.BeatmapId);
onlineBeatmapRequest.Success += beatmapSet => Schedule(() =>
{
this.beatmapSet = beatmapSet;
2021-12-21 15:25:52 +08:00
beatmapPanelContainer.Child = new BeatmapCardNormal(this.beatmapSet, allowExpansion: false);
checkForAutomaticDownload();
});
api.Queue(onlineBeatmapRequest);
}
private void checkForAutomaticDownload()
{
if (beatmapSet == null)
return;
if (!automaticDownload.Current.Value)
return;
if (beatmaps.IsAvailableLocally(new BeatmapSetInfo { OnlineID = beatmapSet.OnlineID }))
return;
beatmapDownloader.Download(beatmapSet);
}
public override bool OnExiting(ScreenExitEvent e)
{
previewTrackManager.StopAnyPlaying(this);
return base.OnExiting(e);
}
2020-10-26 18:47:39 +08:00
}
}