1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-07 02:37:19 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/OnlinePlayScreen.cs

236 lines
8.0 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.
2018-05-15 21:14:10 -03:00
2022-06-17 16:37:17 +09:00
#nullable disable
using System.Diagnostics;
using osu.Framework.Allocation;
2019-02-21 19:04:31 +09:00
using osu.Framework.Bindables;
2018-05-15 21:14:10 -03:00
using osu.Framework.Graphics;
using osu.Framework.Logging;
2018-05-15 21:14:10 -03:00
using osu.Framework.Screens;
using osu.Game.Graphics.Containers;
2018-12-26 16:46:50 +09:00
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Screens.Menu;
using osu.Game.Screens.OnlinePlay.Components;
using osu.Game.Screens.OnlinePlay.Lounge;
using osu.Game.Users;
2018-05-15 21:14:10 -03:00
namespace osu.Game.Screens.OnlinePlay
2018-05-15 21:14:10 -03:00
{
[Cached]
2022-11-24 14:32:20 +09:00
public abstract partial class OnlinePlayScreen : OsuScreen, IHasSubScreenStack
2018-05-15 21:14:10 -03:00
{
[Cached]
protected readonly OverlayColourProvider ColourProvider = new OverlayColourProvider(OverlayColourScheme.Plum);
public IScreen CurrentSubScreen => screenStack.CurrentScreen;
public override bool CursorVisible => (screenStack?.CurrentScreen as IOnlinePlaySubScreen)?.CursorVisible ?? true;
2018-05-15 21:14:10 -03:00
// this is required due to PlayerLoader eventually being pushed to the main stack
// while leases may be taken out by a subscreen.
2019-02-12 11:19:34 +09:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
2019-01-25 20:25:55 +09:00
2023-10-13 09:56:46 +02:00
protected LoungeSubScreen Lounge { get; private set; }
private OnlinePlayScreenWaveContainer waves;
private ScreenStack screenStack;
2018-12-14 17:35:18 +09:00
2020-12-19 00:15:41 +09:00
[Cached(Type = typeof(IRoomManager))]
protected RoomManager RoomManager { get; private set; }
2020-12-19 00:15:41 +09:00
[Cached]
private readonly OngoingOperationTracker ongoingOperationTracker = new OngoingOperationTracker();
2018-12-26 16:46:50 +09:00
[Resolved]
protected IAPIProvider API { get; private set; }
2018-12-26 16:46:50 +09:00
protected OnlinePlayScreen()
2018-05-15 21:14:10 -03:00
{
2019-01-25 20:47:31 +09:00
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
2019-01-25 20:25:55 +09:00
RelativeSizeAxes = Axes.Both;
2019-02-12 13:02:33 +09:00
Padding = new MarginPadding { Horizontal = -HORIZONTAL_OVERFLOW_PADDING };
2019-01-25 20:25:55 +09:00
RoomManager = CreateRoomManager();
}
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
[BackgroundDependencyLoader]
private void load()
{
InternalChild = waves = new OnlinePlayScreenWaveContainer
2018-05-15 21:14:10 -03:00
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2018-05-15 21:14:10 -03:00
{
2021-08-20 17:02:55 +09:00
screenStack = new OnlinePlaySubScreenStack { RelativeSizeAxes = Axes.Both },
new Header(ScreenTitle, screenStack),
2021-08-03 20:38:50 +09:00
RoomManager,
ongoingOperationTracker,
2019-02-12 13:02:33 +09:00
}
};
}
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
{
if (state.NewValue != APIState.Online)
Schedule(forcefullyExit);
});
protected override void LoadComplete()
{
base.LoadComplete();
screenStack.ScreenPushed += screenPushed;
screenStack.ScreenExited += screenExited;
screenStack.Push(Lounge = CreateLounge());
apiState.BindTo(API.State);
apiState.BindValueChanged(onlineStateChanged, true);
}
private void forcefullyExit()
{
2021-12-21 15:07:06 +09:00
Logger.Log($"{this} forcefully exiting due to loss of API connection");
// This is temporary since we don't currently have a way to force screens to be exited
// See also: `DailyChallenge.forcefullyExit()`
2019-01-23 20:52:00 +09:00
if (this.IsCurrentScreen())
{
while (this.IsCurrentScreen())
this.Exit();
}
// Also handle the case where a child screen is current (ie. gameplay).
else if (this.GetChildScreen() != null)
{
2019-01-23 20:52:00 +09:00
this.MakeCurrent();
Schedule(forcefullyExit);
}
}
public override void OnEntering(ScreenTransitionEvent e)
2018-05-15 21:14:10 -03:00
{
2019-01-23 20:52:00 +09:00
this.FadeIn();
2018-05-15 21:14:10 -03:00
waves.Show();
Mods.SetDefault();
if (Lounge.IsCurrentScreen())
Lounge.OnEntering(e);
else
Lounge.MakeCurrent();
}
public override void OnResuming(ScreenTransitionEvent e)
{
this.FadeIn(250);
this.ScaleTo(1, 250, Easing.OutSine);
Debug.Assert(screenStack.CurrentScreen != null);
// if a subscreen was pushed to the nested stack while the stack was not present, this path will proxy `OnResuming()`
// to the subscreen before `OnEntering()` can even be called for the subscreen, breaking ordering expectations.
// to work around this, do not proxy resume to screens that haven't loaded yet.
if ((screenStack.CurrentScreen as Drawable)?.IsLoaded == true)
screenStack.CurrentScreen.OnResuming(e);
base.OnResuming(e);
}
public override void OnSuspending(ScreenTransitionEvent e)
{
this.ScaleTo(1.1f, 250, Easing.InSine);
this.FadeOut(250);
Debug.Assert(screenStack.CurrentScreen != null);
// if a subscreen was pushed to the nested stack while the stack was not present, this path will proxy `OnSuspending()`
// to the subscreen before `OnEntering()` can even be called for the subscreen, breaking ordering expectations.
// to work around this, do not proxy suspend to screens that haven't loaded yet.
if ((screenStack.CurrentScreen as Drawable)?.IsLoaded == true)
screenStack.CurrentScreen.OnSuspending(e);
2018-05-15 21:14:10 -03:00
}
public override bool OnExiting(ScreenExitEvent e)
2018-05-15 21:14:10 -03:00
{
while (screenStack.CurrentScreen != null && screenStack.CurrentScreen is not LoungeSubScreen)
{
var subScreen = (Screen)screenStack.CurrentScreen;
if (subScreen.IsLoaded && subScreen.OnExiting(e))
return true;
subScreen.Exit();
}
2020-12-19 00:15:41 +09:00
RoomManager.PartRoom();
2018-05-15 21:14:10 -03:00
waves.Hide();
2018-12-18 17:07:47 +09:00
2019-01-23 20:52:00 +09:00
this.Delay(WaveContainer.DISAPPEAR_DURATION).FadeOut();
2018-12-26 20:05:57 +09:00
base.OnExiting(e);
2019-01-25 20:25:55 +09:00
return false;
2018-05-15 21:14:10 -03:00
}
public override bool OnBackButton()
{
if (!(screenStack.CurrentScreen is IOnlinePlaySubScreen onlineSubScreen))
return false;
if (((Drawable)onlineSubScreen).IsLoaded && onlineSubScreen.AllowBackButton && onlineSubScreen.OnBackButton())
return true;
if (screenStack.CurrentScreen != null && !(screenStack.CurrentScreen is LoungeSubScreen))
{
screenStack.Exit();
return true;
}
return false;
}
protected override void LogoExiting(OsuLogo logo)
{
base.LogoExiting(logo);
// the wave overlay transition takes longer than expected to run.
logo.Delay(WaveContainer.DISAPPEAR_DURATION / 2).FadeOut();
}
2020-02-19 19:42:25 +09:00
private void screenPushed(IScreen lastScreen, IScreen newScreen)
{
subScreenChanged(lastScreen, newScreen);
2020-02-19 19:42:25 +09:00
}
private void screenExited(IScreen lastScreen, IScreen newScreen)
2018-05-15 21:14:10 -03:00
{
subScreenChanged(lastScreen, newScreen);
if (screenStack.CurrentScreen == null && this.IsCurrentScreen())
this.Exit();
2018-05-15 21:14:10 -03:00
}
private void subScreenChanged(IScreen lastScreen, IScreen newScreen)
2018-12-26 22:37:09 +09:00
{
if (lastScreen is IOsuScreen lastOsuScreen)
Activity.UnbindFrom(lastOsuScreen.Activity);
if (newScreen is IOsuScreen newOsuScreen)
((IBindable<UserActivity>)Activity).BindTo(newOsuScreen.Activity);
2018-12-26 22:37:09 +09:00
}
protected abstract string ScreenTitle { get; }
protected virtual RoomManager CreateRoomManager() => new RoomManager();
2020-12-19 00:15:41 +09:00
2020-12-20 23:36:56 +09:00
protected abstract LoungeSubScreen CreateLounge();
ScreenStack IHasSubScreenStack.SubScreenStack => screenStack;
2018-05-15 21:14:10 -03:00
}
}