1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 15:33:22 +08:00
osu-lazer/osu.Game/Screens/Menu/MainMenu.cs

333 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-04-13 17:19:50 +08:00
2022-06-17 15:37:17 +08:00
#nullable disable
using System;
using System.Diagnostics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Framework.Platform;
2018-04-13 17:19:50 +08:00
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
2020-07-06 21:03:09 +08:00
using osu.Game.IO;
2019-09-08 13:36:58 +08:00
using osu.Game.Online.API;
using osu.Game.Overlays;
using osu.Game.Rulesets;
2018-04-13 17:19:50 +08:00
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Edit;
using osu.Game.Screens.OnlinePlay.Multiplayer;
using osu.Game.Screens.OnlinePlay.Playlists;
2018-04-13 17:19:50 +08:00
using osu.Game.Screens.Select;
2021-05-19 15:27:38 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Menu
{
public class MainMenu : OsuScreen, IHandlePresentBeatmap, IKeyBindingHandler<GlobalAction>
2018-04-13 17:19:50 +08:00
{
public const float FADE_IN_DURATION = 300;
public const float FADE_OUT_DURATION = 400;
2018-04-13 17:19:50 +08:00
2022-04-19 14:46:03 +08:00
public override bool HideOverlaysOnEnter => Buttons == null || Buttons.State == ButtonSystemState.Initial;
2018-04-13 17:19:50 +08:00
2019-06-25 17:38:14 +08:00
public override bool AllowBackButton => false;
public override bool AllowExternalScreenChange => true;
2018-04-13 17:19:50 +08:00
private Screen songSelect;
private MenuSideFlashes sideFlashes;
2018-04-13 17:19:50 +08:00
2022-04-19 14:46:03 +08:00
protected ButtonSystem Buttons;
[Resolved]
private GameHost host { get; set; }
2018-04-13 17:19:50 +08:00
2020-08-11 11:40:58 +08:00
[Resolved]
2020-08-16 22:04:49 +08:00
private MusicController musicController { get; set; }
2019-09-08 13:36:58 +08:00
[Resolved(canBeNull: true)]
private LoginOverlay login { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
2019-09-19 16:40:46 +08:00
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
2018-04-13 17:19:50 +08:00
protected override bool PlayExitSound => false;
private Bindable<double> holdDelay;
2019-09-25 20:56:47 +08:00
private Bindable<bool> loginDisplayed;
2019-09-19 19:17:58 +08:00
private ExitConfirmOverlay exitConfirmOverlay;
private ParallaxContainer buttonsContainer;
2020-01-11 12:17:13 +08:00
private SongTicker songTicker;
[BackgroundDependencyLoader(true)]
2022-01-15 08:06:39 +08:00
private void load(BeatmapListingOverlay beatmapListing, SettingsOverlay settings, OsuConfigManager config, SessionStatics statics)
2018-04-13 17:19:50 +08:00
{
holdDelay = config.GetBindable<double>(OsuSetting.UIHoldActivationDelay);
2019-09-28 20:21:51 +08:00
loginDisplayed = statics.GetBindable<bool>(Static.LoginOverlayDisplayed);
2020-01-24 15:34:13 +08:00
if (host.CanExit)
{
AddInternal(exitConfirmOverlay = new ExitConfirmOverlay
{
Action = () =>
{
if (holdDelay.Value > 0)
confirmAndExit();
else
this.Exit();
}
});
}
AddRangeInternal(new[]
2018-04-13 17:19:50 +08:00
{
buttonsContainer = new ParallaxContainer
2018-04-13 17:19:50 +08:00
{
ParallaxAmount = 0.01f,
Children = new Drawable[]
{
2022-04-19 14:46:03 +08:00
Buttons = new ButtonSystem
2018-04-13 17:19:50 +08:00
{
OnEdit = delegate
{
Beatmap.SetDefault();
this.Push(new EditorLoader());
},
OnSolo = loadSoloSongSelect,
2020-12-25 12:38:11 +08:00
OnMultiplayer = () => this.Push(new Multiplayer()),
2020-12-25 22:45:44 +08:00
OnPlaylists = () => this.Push(new Playlists()),
2019-09-19 21:46:21 +08:00
OnExit = confirmAndExit,
2018-04-13 17:19:50 +08:00
}
}
},
sideFlashes = new MenuSideFlashes(),
songTicker = new SongTicker
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding { Right = 15, Top = 5 }
2020-01-24 15:34:13 +08:00
},
2021-05-19 15:27:38 +08:00
exitConfirmOverlay?.CreateProxy() ?? Empty()
});
2022-04-19 14:46:03 +08:00
Buttons.StateChanged += state =>
{
switch (state)
{
case ButtonSystemState.Initial:
case ButtonSystemState.Exit:
ApplyToBackground(b => b.FadeColour(Color4.White, 500, Easing.OutSine));
break;
2019-04-01 11:44:46 +08:00
default:
ApplyToBackground(b => b.FadeColour(OsuColour.Gray(0.8f), 500, Easing.OutSine));
break;
}
};
2018-04-13 17:19:50 +08:00
2022-04-19 14:46:03 +08:00
Buttons.OnSettings = () => settings?.ToggleVisibility();
Buttons.OnBeatmapListing = () => beatmapListing?.ToggleVisibility();
2018-04-13 17:19:50 +08:00
preloadSongSelect();
}
[Resolved(canBeNull: true)]
private IPerformFromScreenRunner performer { get; set; }
public void ReturnToOsuLogo() => Buttons.State = ButtonSystemState.Initial;
2019-09-19 21:46:21 +08:00
private void confirmAndExit()
{
2019-12-22 20:52:00 +08:00
if (exitConfirmed) return;
2019-09-19 21:46:21 +08:00
exitConfirmed = true;
performer?.PerformFromScreen(menu => menu.Exit());
2019-09-19 21:46:21 +08:00
}
2018-04-13 17:19:50 +08:00
private void preloadSongSelect()
{
if (songSelect == null)
LoadComponentAsync(songSelect = new PlaySongSelect());
}
private void loadSoloSongSelect() => this.Push(consumeSongSelect());
2018-04-13 17:19:50 +08:00
private Screen consumeSongSelect()
{
var s = songSelect;
songSelect = null;
return s;
}
2020-07-06 21:03:09 +08:00
[Resolved]
private Storage storage { get; set; }
public override void OnEntering(ScreenTransitionEvent e)
2018-04-13 17:19:50 +08:00
{
base.OnEntering(e);
2022-04-19 14:46:03 +08:00
Buttons.FadeInFromZero(500);
2018-04-13 17:19:50 +08:00
if (e.Last is IntroScreen && musicController.TrackLoaded)
2018-04-13 17:19:50 +08:00
{
var track = musicController.CurrentTrack;
// presume the track is the current beatmap's track. not sure how correct this assumption is but it has worked until now.
if (!track.IsRunning)
2018-04-13 17:19:50 +08:00
{
Beatmap.Value.PrepareTrackForPreviewLooping();
track.Restart();
2018-04-13 17:19:50 +08:00
}
}
2020-07-06 21:03:09 +08:00
if (storage is OsuStorage osuStorage && osuStorage.Error != OsuStorageError.None)
dialogOverlay?.Push(new StorageErrorDialog(osuStorage, osuStorage.Error));
2018-04-13 17:19:50 +08:00
}
private bool exitConfirmed;
2018-04-13 17:19:50 +08:00
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
base.LogoArriving(logo, resuming);
2022-04-19 14:46:03 +08:00
Buttons.SetOsuLogo(logo);
2018-04-13 17:19:50 +08:00
logo.FadeColour(Color4.White, 100, Easing.OutQuint);
logo.FadeIn(100, Easing.OutQuint);
if (resuming)
{
2022-04-19 14:46:03 +08:00
Buttons.State = ButtonSystemState.TopLevel;
2018-04-13 17:19:50 +08:00
this.FadeIn(FADE_IN_DURATION, Easing.OutQuint);
buttonsContainer.MoveTo(new Vector2(0, 0), FADE_IN_DURATION, Easing.OutQuint);
2018-04-13 17:19:50 +08:00
sideFlashes.Delay(FADE_IN_DURATION).FadeIn(64, Easing.InQuint);
2018-04-13 17:19:50 +08:00
}
else if (!api.IsLoggedIn)
{
// copy out old action to avoid accidentally capturing logo.Action in closure, causing a self-reference loop.
var previousAction = logo.Action;
// we want to hook into logo.Action to display the login overlay, but also preserve the return value of the old action.
// therefore pass the old action to displayLogin, so that it can return that value.
// this ensures that the OsuLogo sample does not play when it is not desired.
logo.Action = () => displayLogin(previousAction);
}
bool displayLogin(Func<bool> originalAction)
{
2019-09-25 20:56:47 +08:00
if (!loginDisplayed.Value)
{
Scheduler.AddDelayed(() => login?.Show(), 500);
2019-09-25 20:56:47 +08:00
loginDisplayed.Value = true;
}
return originalAction.Invoke();
}
2018-04-13 17:19:50 +08:00
}
protected override void LogoSuspending(OsuLogo logo)
{
var seq = logo.FadeOut(300, Easing.InSine)
.ScaleTo(0.2f, 300, Easing.InSine);
2022-04-19 14:46:03 +08:00
seq.OnComplete(_ => Buttons.SetOsuLogo(null));
seq.OnAbort(_ => Buttons.SetOsuLogo(null));
2018-04-13 17:19:50 +08:00
}
public override void OnSuspending(ScreenTransitionEvent e)
2018-04-13 17:19:50 +08:00
{
base.OnSuspending(e);
2018-04-13 17:19:50 +08:00
2022-04-19 14:46:03 +08:00
Buttons.State = ButtonSystemState.EnteringMode;
2018-04-13 17:19:50 +08:00
this.FadeOut(FADE_OUT_DURATION, Easing.InSine);
buttonsContainer.MoveTo(new Vector2(-800, 0), FADE_OUT_DURATION, Easing.InSine);
2018-04-13 17:19:50 +08:00
sideFlashes.FadeOut(64, Easing.OutQuint);
}
public override void OnResuming(ScreenTransitionEvent e)
2018-04-13 17:19:50 +08:00
{
base.OnResuming(e);
2020-01-11 23:27:22 +08:00
ApplyToBackground(b => (b as BackgroundScreenDefault)?.Next());
2018-04-13 17:19:50 +08:00
2020-05-05 09:31:11 +08:00
// we may have consumed our preloaded instance, so let's make another.
2018-04-13 17:19:50 +08:00
preloadSongSelect();
2020-08-16 22:04:49 +08:00
musicController.EnsurePlayingSomething();
2018-04-13 17:19:50 +08:00
}
public override bool OnExiting(ScreenExitEvent e)
2018-04-13 17:19:50 +08:00
{
if (!exitConfirmed && dialogOverlay != null)
{
2019-12-22 21:39:25 +08:00
if (dialogOverlay.CurrentDialog is ConfirmExitDialog exitDialog)
2021-05-19 15:27:38 +08:00
exitDialog.PerformOkAction();
2019-12-22 21:39:25 +08:00
else
dialogOverlay.Push(new ConfirmExitDialog(confirmAndExit, () => exitConfirmOverlay.Abort()));
2021-05-19 15:27:38 +08:00
return true;
2019-12-22 20:52:00 +08:00
}
2022-04-19 14:46:03 +08:00
Buttons.State = ButtonSystemState.Exit;
OverlayActivationMode.Value = OverlayActivation.Disabled;
2020-01-11 12:17:13 +08:00
songTicker.Hide();
this.FadeOut(3000);
return base.OnExiting(e);
2018-04-13 17:19:50 +08:00
}
public void PresentBeatmap(WorkingBeatmap beatmap, RulesetInfo ruleset)
{
Logger.Log($"{nameof(MainMenu)} completing {nameof(PresentBeatmap)} with beatmap {beatmap} ruleset {ruleset}");
Beatmap.Value = beatmap;
Ruleset.Value = ruleset;
Schedule(loadSoloSongSelect);
}
public bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
if (e.Repeat)
return false;
switch (e.Action)
{
case GlobalAction.Back:
// In the case of a host being able to exit, the back action is handled by ExitConfirmOverlay.
Debug.Assert(!host.CanExit);
return host.SuspendToBackground();
}
return false;
}
public void OnReleased(KeyBindingReleaseEvent<GlobalAction> e)
{
}
2018-04-13 17:19:50 +08:00
}
}