1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 20:13:02 +08:00
osu-lazer/osu.Game/Screens/OnlinePlay/OnlinePlayScreen.cs

327 lines
11 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-16 08:14:10 +08:00
using System.Diagnostics;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2020-02-19 18:42:25 +08:00
using osu.Framework.Extensions.Color4Extensions;
2018-05-16 08:14:10 +08:00
using osu.Framework.Graphics;
2020-02-19 18:42:25 +08:00
using osu.Framework.Graphics.Colour;
2018-05-16 08:14:10 +08:00
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Logging;
2018-05-16 08:14:10 +08:00
using osu.Framework.Screens;
using osu.Game.Beatmaps;
2020-02-19 18:42:25 +08:00
using osu.Game.Beatmaps.Drawables;
2018-05-16 08:14:10 +08:00
using osu.Game.Graphics.Containers;
2018-12-26 15:46:50 +08:00
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.Screens.Menu;
using osu.Game.Screens.OnlinePlay.Components;
using osu.Game.Screens.OnlinePlay.Lounge;
using osu.Game.Users;
2021-08-12 19:01:53 +08:00
using osuTK;
2021-08-12 18:27:06 +08:00
using osuTK.Graphics;
2018-05-16 08:14:10 +08:00
namespace osu.Game.Screens.OnlinePlay
2018-05-16 08:14:10 +08:00
{
[Cached]
public abstract class OnlinePlayScreen : OsuScreen, IHasSubScreenStack
2018-05-16 08:14:10 +08:00
{
[Cached]
protected readonly OverlayColourProvider ColourProvider = new OverlayColourProvider(OverlayColourScheme.Plum);
public override bool CursorVisible => (screenStack?.CurrentScreen as IOnlinePlaySubScreen)?.CursorVisible ?? true;
2018-05-16 08:14:10 +08: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 10:19:34 +08:00
public override bool DisallowExternalBeatmapRulesetChanges => true;
2019-01-25 19:25:55 +08:00
private MultiplayerWaveContainer waves;
private LoungeSubScreen loungeSubScreen;
private ScreenStack screenStack;
2018-12-14 16:35:18 +08:00
2020-12-18 23:15:41 +08:00
[Cached(Type = typeof(IRoomManager))]
protected RoomManager RoomManager { get; private set; }
2020-12-18 23:15:41 +08:00
[Cached]
private readonly Bindable<Room> selectedRoom = new Bindable<Room>();
2019-02-08 13:57:51 +08:00
[Cached]
private readonly OngoingOperationTracker ongoingOperationTracker = new OngoingOperationTracker();
[Resolved(CanBeNull = true)]
private MusicController music { get; set; }
2019-01-25 19:25:55 +08:00
[Resolved]
private OsuGameBase game { get; set; }
2018-12-26 15:46:50 +08:00
[Resolved]
protected IAPIProvider API { get; private set; }
2018-12-26 15:46:50 +08:00
2019-01-31 12:50:37 +08:00
[Resolved(CanBeNull = true)]
2019-01-25 19:25:55 +08:00
private OsuLogo logo { get; set; }
protected OnlinePlayScreen()
2018-05-16 08:14:10 +08:00
{
2019-01-25 19:47:31 +08:00
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
2019-01-25 19:25:55 +08:00
RelativeSizeAxes = Axes.Both;
2019-02-12 12:02:33 +08:00
Padding = new MarginPadding { Horizontal = -HORIZONTAL_OVERFLOW_PADDING };
2019-01-25 19:25:55 +08:00
RoomManager = CreateRoomManager();
}
private readonly IBindable<APIState> apiState = new Bindable<APIState>();
[BackgroundDependencyLoader]
private void load()
{
var backgroundColour = Color4Extensions.FromHex(@"3e3a44");
2019-01-23 19:52:00 +08:00
InternalChild = waves = new MultiplayerWaveContainer
2018-05-16 08:14:10 +08:00
{
RelativeSizeAxes = Axes.Both,
Children = new Drawable[]
2018-05-16 08:14:10 +08:00
{
2020-02-19 18:42:25 +08:00
new Box
2018-05-16 08:14:10 +08:00
{
2019-02-12 12:02:33 +08:00
RelativeSizeAxes = Axes.Both,
Colour = backgroundColour,
2018-05-16 08:14:10 +08:00
},
2019-02-12 12:02:33 +08:00
new Container
2018-12-19 15:56:51 +08:00
{
2019-02-12 12:02:33 +08:00
RelativeSizeAxes = Axes.Both,
2021-08-12 18:27:06 +08:00
Children = new Drawable[]
2020-02-19 18:42:25 +08:00
{
new BeatmapBackgroundSprite
2021-08-12 18:27:06 +08:00
{
RelativeSizeAxes = Axes.Both
2021-08-12 18:27:06 +08:00
},
new Box
2020-02-19 18:42:25 +08:00
{
2021-08-12 18:27:06 +08:00
RelativeSizeAxes = Axes.Both,
Colour = ColourInfo.GradientVertical(Color4.Black.Opacity(0.9f), Color4.Black.Opacity(0.6f))
2020-02-19 18:42:25 +08:00
},
2021-08-12 18:27:06 +08:00
screenStack = new OnlinePlaySubScreenStack
{
RelativeSizeAxes = Axes.Both
}
2020-02-19 18:42:25 +08:00
}
2018-12-19 15:56:51 +08:00
},
new Header(ScreenTitle, screenStack),
2021-08-03 19:38:50 +08:00
RoomManager,
ongoingOperationTracker
2019-02-12 12:02:33 +08:00
}
};
}
private void onlineStateChanged(ValueChangedEvent<APIState> state) => Schedule(() =>
{
if (state.NewValue != APIState.Online)
{
Logger.Log("API connection was lost, can't continue with online play", LoggingTarget.Network, LogLevel.Important);
Schedule(forcefullyExit);
}
});
protected override void LoadComplete()
{
base.LoadComplete();
screenStack.ScreenPushed += screenPushed;
screenStack.ScreenExited += screenExited;
screenStack.Push(loungeSubScreen = CreateLounge());
apiState.BindTo(API.State);
apiState.BindValueChanged(onlineStateChanged, true);
}
2019-02-05 14:56:18 +08:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
2019-02-12 12:02:33 +08:00
var dependencies = new CachedModelDependencyContainer<Room>(base.CreateChildDependencies(parent));
dependencies.Model.BindTo(selectedRoom);
2019-02-05 14:56:18 +08:00
return dependencies;
}
private void forcefullyExit()
{
// This is temporary since we don't currently have a way to force screens to be exited
2019-01-23 19:52:00 +08:00
if (this.IsCurrentScreen())
{
while (this.IsCurrentScreen())
this.Exit();
}
else
{
2019-01-23 19:52:00 +08:00
this.MakeCurrent();
Schedule(forcefullyExit);
}
}
2019-02-11 18:11:34 +08:00
public override void OnEntering(IScreen last)
2018-05-16 08:14:10 +08:00
{
2019-01-23 19:52:00 +08:00
this.FadeIn();
2018-05-16 08:14:10 +08:00
waves.Show();
if (loungeSubScreen.IsCurrentScreen())
loungeSubScreen.OnEntering(last);
else
loungeSubScreen.MakeCurrent();
}
public override void OnResuming(IScreen last)
{
this.FadeIn(250);
this.ScaleTo(1, 250, Easing.OutSine);
Debug.Assert(screenStack.CurrentScreen != null);
screenStack.CurrentScreen.OnResuming(last);
base.OnResuming(last);
}
public override void OnSuspending(IScreen next)
{
this.ScaleTo(1.1f, 250, Easing.InSine);
this.FadeOut(250);
Debug.Assert(screenStack.CurrentScreen != null);
screenStack.CurrentScreen.OnSuspending(next);
2018-05-16 08:14:10 +08:00
}
2019-02-11 18:11:34 +08:00
public override bool OnExiting(IScreen next)
2018-05-16 08:14:10 +08:00
{
var subScreen = screenStack.CurrentScreen as Drawable;
if (subScreen?.IsLoaded == true && screenStack.CurrentScreen.OnExiting(next))
return true;
2020-12-18 23:15:41 +08:00
RoomManager.PartRoom();
2018-05-16 08:14:10 +08:00
waves.Hide();
2018-12-18 16:07:47 +08:00
2019-01-23 19:52:00 +08:00
this.Delay(WaveContainer.DISAPPEAR_DURATION).FadeOut();
2018-12-26 19:05:57 +08:00
base.OnExiting(next);
2019-01-25 19:25:55 +08:00
return false;
2018-05-16 08:14:10 +08: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 18:42:25 +08:00
private void screenPushed(IScreen lastScreen, IScreen newScreen)
{
subScreenChanged(lastScreen, newScreen);
2020-02-19 18:42:25 +08:00
}
private void screenExited(IScreen lastScreen, IScreen newScreen)
2018-05-16 08:14:10 +08:00
{
subScreenChanged(lastScreen, newScreen);
if (screenStack.CurrentScreen == null && this.IsCurrentScreen())
this.Exit();
2018-05-16 08:14:10 +08:00
}
private void subScreenChanged(IScreen lastScreen, IScreen newScreen)
2018-12-26 21:37:09 +08:00
{
if (lastScreen is IOsuScreen lastOsuScreen)
Activity.UnbindFrom(lastOsuScreen.Activity);
if (newScreen is IOsuScreen newOsuScreen)
((IBindable<UserActivity>)Activity).BindTo(newOsuScreen.Activity);
2018-12-26 21:37:09 +08:00
}
2020-12-18 23:15:41 +08:00
protected IScreen CurrentSubScreen => screenStack.CurrentScreen;
protected abstract string ScreenTitle { get; }
protected virtual RoomManager CreateRoomManager() => new RoomManager();
2020-12-18 23:15:41 +08:00
2020-12-20 22:36:56 +08:00
protected abstract LoungeSubScreen CreateLounge();
2018-05-16 08:14:10 +08:00
private class MultiplayerWaveContainer : WaveContainer
{
protected override bool StartHidden => true;
public MultiplayerWaveContainer()
{
FirstWaveColour = Color4Extensions.FromHex(@"654d8c");
SecondWaveColour = Color4Extensions.FromHex(@"554075");
ThirdWaveColour = Color4Extensions.FromHex(@"44325e");
FourthWaveColour = Color4Extensions.FromHex(@"392850");
2018-05-16 08:14:10 +08:00
}
}
2020-02-19 18:42:25 +08:00
private class BeatmapBackgroundSprite : OnlinePlayBackgroundSprite
2020-02-19 18:42:25 +08:00
{
protected override UpdateableBeatmapBackgroundSprite CreateBackgroundSprite() => new BlurredBackgroundSprite(BeatmapSetCoverType) { RelativeSizeAxes = Axes.Both };
2020-02-19 18:42:25 +08:00
public class BlurredBackgroundSprite : UpdateableBeatmapBackgroundSprite
2020-02-19 18:42:25 +08:00
{
public BlurredBackgroundSprite(BeatmapSetCoverType type)
: base(type)
{
}
protected override double LoadDelay => 200;
protected override Drawable CreateDrawable(BeatmapInfo model) =>
new BufferedLoader(base.CreateDrawable(model));
}
// This class is an unfortunate requirement due to `LongRunningLoad` requiring direct async loading.
// It means that if the web request fetching the beatmap background takes too long, it will suddenly appear.
internal class BufferedLoader : BufferedContainer
{
private readonly Drawable drawable;
public BufferedLoader(Drawable drawable)
{
this.drawable = drawable;
RelativeSizeAxes = Axes.Both;
BlurSigma = new Vector2(10);
FrameBufferScale = new Vector2(0.5f);
CacheDrawnFrameBuffer = true;
}
[BackgroundDependencyLoader]
private void load()
{
LoadComponentAsync(drawable, d =>
{
Add(d);
ForceRedraw();
});
}
2020-02-19 18:42:25 +08:00
}
}
ScreenStack IHasSubScreenStack.SubScreenStack => screenStack;
2018-05-16 08:14:10 +08:00
}
}