1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 21:47:25 +08:00
osu-lazer/osu.Game/Screens/Multi/Multiplayer.cs

295 lines
9.5 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 osu.Framework.Allocation;
using osu.Framework.Configuration;
2018-05-16 08:14:10 +08:00
using osu.Framework.Graphics;
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;
2019-01-25 19:25:55 +08:00
using osu.Game.Beatmaps;
2018-05-16 08:14:10 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Game.Graphics.Containers;
2018-12-19 15:56:51 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Input;
2018-12-26 15:46:50 +08:00
using osu.Game.Online.API;
2018-12-19 15:56:51 +08:00
using osu.Game.Online.Multiplayer;
using osu.Game.Overlays;
2018-12-19 15:56:51 +08:00
using osu.Game.Overlays.BeatmapSet.Buttons;
using osu.Game.Screens.Menu;
2018-12-10 18:20:41 +08:00
using osu.Game.Screens.Multi.Lounge;
using osu.Game.Screens.Multi.Lounge.Components;
2018-12-18 16:07:47 +08:00
using osu.Game.Screens.Multi.Match;
2018-12-19 15:56:51 +08:00
using osuTK;
2018-05-16 08:14:10 +08:00
namespace osu.Game.Screens.Multi
{
[Cached]
2019-01-25 19:25:55 +08:00
public class Multiplayer : CompositeDrawable, IOsuScreen, IOnlineComponent
2018-05-16 08:14:10 +08:00
{
2019-01-25 19:25:55 +08:00
public bool AllowBeatmapRulesetChange => (screenStack.CurrentScreen as IMultiplayerSubScreen)?.AllowBeatmapRulesetChange ?? true;
public bool AllowExternalScreenChange => (screenStack.CurrentScreen as IMultiplayerSubScreen)?.AllowExternalScreenChange ?? true;
public bool CursorVisible => (screenStack.CurrentScreen as IMultiplayerSubScreen)?.AllowExternalScreenChange ?? true;
public bool HideOverlaysOnEnter => false;
public OverlayActivation InitialOverlayActivationMode => OverlayActivation.All;
public float BackgroundParallaxAmount => 1;
2019-01-25 19:25:55 +08:00
public bool ValidForResume { get; set; } = true;
public bool ValidForPush { get; set; } = true;
2018-05-16 08:14:10 +08:00
2019-01-25 19:25:55 +08:00
public override bool RemoveWhenNotAlive => false;
private readonly MultiplayerWaveContainer waves;
2018-12-14 16:35:18 +08:00
2018-12-19 15:56:51 +08:00
private readonly OsuButton createButton;
2018-12-26 19:05:57 +08:00
private readonly LoungeSubScreen loungeSubScreen;
2019-01-23 19:52:00 +08:00
private readonly ScreenStack screenStack;
private readonly RoomPollingComponent pollingComponent;
[Cached]
private readonly Bindable<FilterCriteria> filter = new Bindable<FilterCriteria>();
2018-12-14 16:35:18 +08:00
[Cached(Type = typeof(IRoomManager))]
2018-12-22 11:45:36 +08:00
private RoomManager roomManager;
2019-01-25 19:25:55 +08:00
[Resolved]
private IBindableBeatmap beatmap { get; set; }
[Resolved]
private OsuGameBase game { get; set; }
2018-12-26 15:46:50 +08:00
[Resolved]
private APIAccess api { get; set; }
2019-01-31 12:50:37 +08:00
[Resolved(CanBeNull = true)]
2019-01-25 19:25:55 +08:00
private OsuLogo logo { get; set; }
2018-05-16 08:14:10 +08:00
public Multiplayer()
{
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-01-23 19:52:00 +08:00
InternalChild = waves = new MultiplayerWaveContainer
2018-05-16 08:14:10 +08:00
{
RelativeSizeAxes = Axes.Both,
};
2019-01-23 19:52:00 +08:00
screenStack = new ScreenStack(loungeSubScreen = new LoungeSubScreen(this.Push)) { RelativeSizeAxes = Axes.Both };
Padding = new MarginPadding { Horizontal = -OsuScreen.HORIZONTAL_OVERFLOW_PADDING };
2019-01-23 19:52:00 +08:00
waves.AddRange(new Drawable[]
2018-05-16 08:14:10 +08:00
{
new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.FromHex(@"3e3a44"),
},
new Triangles
{
RelativeSizeAxes = Axes.Both,
ColourLight = OsuColour.FromHex(@"3c3842"),
ColourDark = OsuColour.FromHex(@"393540"),
TriangleScale = 5,
},
},
},
roomManager = new RoomManager
2018-05-16 08:14:10 +08:00
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Top = Header.HEIGHT },
2019-01-23 19:52:00 +08:00
Child = screenStack
2018-05-16 08:14:10 +08:00
},
2019-01-23 19:52:00 +08:00
new Header(screenStack),
2018-12-19 15:56:51 +08:00
createButton = new HeaderButton
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
RelativeSizeAxes = Axes.None,
Size = new Vector2(150, Header.HEIGHT - 20),
Margin = new MarginPadding
{
Top = 10,
Right = 10 + OsuScreen.HORIZONTAL_OVERFLOW_PADDING,
2018-12-19 15:56:51 +08:00
},
Text = "Create room",
2018-12-26 19:21:30 +08:00
Action = () => loungeSubScreen.Push(new Room
2018-12-26 15:46:50 +08:00
{
Name = { Value = $"{api.LocalUser}'s awesome room" }
}),
2018-12-22 11:45:36 +08:00
},
pollingComponent = new RoomPollingComponent()
});
2018-12-26 19:05:57 +08:00
pollingComponent.Filter.BindTo(filter);
pollingComponent.RoomsRetrieved += roomManager.UpdateRooms;
2019-01-23 19:52:00 +08:00
screenStack.ScreenPushed += screenPushed;
screenStack.ScreenExited += screenExited;
2018-05-16 08:14:10 +08:00
}
private readonly IBindable<bool> isIdle = new BindableBool();
2018-12-27 19:17:27 +08:00
[BackgroundDependencyLoader(true)]
private void load(IdleTracker idleTracker)
{
api.Register(this);
2018-12-27 19:17:27 +08:00
if (idleTracker != null)
isIdle.BindTo(idleTracker.IsIdle);
}
protected override void LoadComplete()
{
base.LoadComplete();
isIdle.BindValueChanged(updatePollingRate, true);
}
private void updatePollingRate(bool idle)
{
pollingComponent.TimeBetweenPolls = !this.IsCurrentScreen() || !(screenStack.CurrentScreen is LoungeSubScreen) ? 0 : (idle ? 120000 : 15000);
Logger.Log($"Polling adjusted to {pollingComponent.TimeBetweenPolls}");
}
public void APIStateChanged(APIAccess api, APIState state)
{
if (state != APIState.Online)
forcefullyExit();
}
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())
this.Exit();
else
{
2019-01-23 19:52:00 +08:00
this.MakeCurrent();
Schedule(forcefullyExit);
}
}
2019-01-25 19:25:55 +08:00
public void OnEntering(IScreen last)
2018-05-16 08:14:10 +08:00
{
2019-01-23 19:52:00 +08:00
this.FadeIn();
2018-12-26 19:05:57 +08:00
2018-05-16 08:14:10 +08:00
waves.Show();
}
2019-01-25 19:25:55 +08:00
public bool OnExiting(IScreen next)
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
cancelLooping();
if (screenStack.CurrentScreen != null)
loungeSubScreen.MakeCurrent();
updatePollingRate(isIdle.Value);
2018-12-20 19:58:34 +08:00
2019-01-25 20:02:35 +08:00
// the wave overlay transition takes longer than expected to run.
logo?.AppendAnimatingAction(() => logo.Delay(WaveContainer.DISAPPEAR_DURATION / 2).FadeOut(), false);
2019-01-25 19:25:55 +08:00
return false;
2018-05-16 08:14:10 +08:00
}
2019-01-25 19:25:55 +08:00
public void OnResuming(IScreen last)
2018-05-16 08:14:10 +08:00
{
2019-01-23 19:52:00 +08:00
this.FadeIn(250);
this.ScaleTo(1, 250, Easing.OutSine);
2019-01-25 20:02:35 +08:00
logo?.AppendAnimatingAction(() => OsuScreen.ApplyLogoArrivingDefaults(logo), true);
updatePollingRate(isIdle.Value);
2018-05-16 08:14:10 +08:00
}
2019-01-25 19:25:55 +08:00
public void OnSuspending(IScreen next)
2018-05-16 08:14:10 +08:00
{
2019-01-23 19:52:00 +08:00
this.ScaleTo(1.1f, 250, Easing.InSine);
this.FadeOut(250);
2018-12-26 21:37:09 +08:00
cancelLooping();
pollingComponent.TimeBetweenPolls = 0;
2018-05-16 08:14:10 +08:00
}
2018-12-26 21:37:09 +08:00
private void cancelLooping()
{
2019-01-25 19:25:55 +08:00
var track = beatmap.Value.Track;
2018-12-26 21:37:09 +08:00
if (track != null)
track.Looping = false;
}
2018-12-18 16:07:47 +08:00
protected override void Update()
{
base.Update();
2019-01-23 19:52:00 +08:00
if (!this.IsCurrentScreen()) return;
2018-12-26 21:37:09 +08:00
2019-01-23 19:52:00 +08:00
if (screenStack.CurrentScreen is MatchSubScreen)
2018-12-18 16:07:47 +08:00
{
2019-01-25 19:25:55 +08:00
var track = beatmap.Value.Track;
2018-12-18 16:07:47 +08:00
if (track != null)
{
track.Looping = true;
if (!track.IsRunning)
{
2019-01-25 19:25:55 +08:00
game.Audio.AddItemToList(track);
track.Seek(beatmap.Value.Metadata.PreviewTime);
2018-12-18 16:07:47 +08:00
track.Start();
}
}
2018-12-19 15:56:51 +08:00
createButton.Hide();
2018-12-18 16:07:47 +08:00
}
2019-01-23 19:52:00 +08:00
else if (screenStack.CurrentScreen is LoungeSubScreen)
2018-12-19 15:56:51 +08:00
createButton.Show();
2018-12-18 16:07:47 +08:00
}
2019-01-23 19:52:00 +08:00
private void screenPushed(IScreen lastScreen, IScreen newScreen)
=> updatePollingRate(isIdle.Value);
2018-12-14 16:35:18 +08:00
2019-01-23 19:52:00 +08:00
private void screenExited(IScreen lastScreen, IScreen newScreen)
2018-12-14 16:35:18 +08:00
{
2019-01-23 19:52:00 +08:00
if (lastScreen is MatchSubScreen)
2018-12-26 21:37:09 +08:00
cancelLooping();
2018-12-18 16:07:47 +08:00
updatePollingRate(isIdle.Value);
2019-01-25 19:25:55 +08:00
if (screenStack.CurrentScreen == null)
this.Exit();
2018-12-14 16:35:18 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
api?.Unregister(this);
}
2018-05-16 08:14:10 +08:00
private class MultiplayerWaveContainer : WaveContainer
{
protected override bool StartHidden => true;
public MultiplayerWaveContainer()
{
FirstWaveColour = OsuColour.FromHex(@"654d8c");
SecondWaveColour = OsuColour.FromHex(@"554075");
ThirdWaveColour = OsuColour.FromHex(@"44325e");
FourthWaveColour = OsuColour.FromHex(@"392850");
}
}
}
}