osu/osu.Game/Screens/Menu/MainMenu.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

390 lines
13 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 09:19:50 +00:00
2022-06-17 07:37:17 +00:00
#nullable disable
using System;
using System.Diagnostics;
using JetBrains.Annotations;
2016-11-14 08:23:33 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Framework.Platform;
2017-03-28 12:26:20 +00:00
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Configuration;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
2020-07-06 13:03:09 +00:00
using osu.Game.IO;
2019-09-08 05:36:58 +00:00
using osu.Game.Online.API;
using osu.Game.Overlays;
2023-11-24 01:56:59 +00:00
using osu.Game.Overlays.SkinEditor;
using osu.Game.Rulesets;
2016-11-14 08:23:33 +00:00
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Edit;
using osu.Game.Screens.OnlinePlay.Multiplayer;
using osu.Game.Screens.OnlinePlay.Playlists;
using osu.Game.Screens.Select;
2021-05-19 07:27:38 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
2016-11-14 08:23:33 +00:00
namespace osu.Game.Screens.Menu
2016-08-26 03:28:23 +00:00
{
public partial class MainMenu : OsuScreen, IHandlePresentBeatmap, IKeyBindingHandler<GlobalAction>
2016-08-26 03:28:23 +00:00
{
public const float FADE_IN_DURATION = 300;
public const float FADE_OUT_DURATION = 400;
2018-04-13 09:19:50 +00:00
2022-04-19 06:46:03 +00:00
public override bool HideOverlaysOnEnter => Buttons == null || Buttons.State == ButtonSystemState.Initial;
2018-04-13 09:19:50 +00:00
2019-06-25 09:38:14 +00:00
public override bool AllowBackButton => false;
public override bool AllowExternalScreenChange => true;
2017-03-17 11:09:33 +00:00
private Screen songSelect;
2018-04-13 09:19:50 +00:00
private MenuSideFlashes sideFlashes;
2018-04-13 09:19:50 +00:00
2022-04-19 06:46:03 +00:00
protected ButtonSystem Buttons;
[Resolved]
private GameHost host { get; set; }
2018-04-13 09:19:50 +00:00
[Resolved]
private INotificationOverlay notifications { get; set; }
2020-08-11 03:40:58 +00:00
[Resolved]
2020-08-16 14:04:49 +00:00
private MusicController musicController { get; set; }
2019-09-08 05:36:58 +00:00
[Resolved]
private IAPIProvider api { get; set; }
[Resolved]
private Storage storage { get; set; }
[Resolved(canBeNull: true)]
private LoginOverlay login { get; set; }
2019-09-19 08:40:46 +00:00
[Resolved(canBeNull: true)]
private IDialogOverlay dialogOverlay { get; set; }
protected override BackgroundScreen CreateBackground() => new BackgroundScreenDefault();
2018-04-13 09:19:50 +00:00
protected override bool PlayExitSound => false;
private Bindable<double> holdDelay;
2019-09-25 12:56:47 +00:00
private Bindable<bool> loginDisplayed;
private HoldToExitGameOverlay holdToExitGameOverlay;
2019-09-19 11:17:58 +00:00
private bool exitConfirmedViaDialog;
private bool exitConfirmedViaHoldOrClick;
private ParallaxContainer buttonsContainer;
2020-01-11 04:17:13 +00:00
private SongTicker songTicker;
private Container logoTarget;
private Sample reappearSampleSwoosh;
2023-11-24 01:56:59 +00:00
[Resolved(canBeNull: true)]
private SkinEditorOverlay skinEditor { get; set; }
[BackgroundDependencyLoader(true)]
private void load(BeatmapListingOverlay beatmapListing, SettingsOverlay settings, OsuConfigManager config, SessionStatics statics, AudioManager audio)
2016-11-01 14:24:14 +00:00
{
holdDelay = config.GetBindable<double>(OsuSetting.UIHoldActivationDelay);
2019-09-28 12:21:51 +00:00
loginDisplayed = statics.GetBindable<bool>(Static.LoginOverlayDisplayed);
2020-01-24 07:34:13 +00:00
if (host.CanExit)
{
AddInternal(holdToExitGameOverlay = new HoldToExitGameOverlay
2020-01-24 07:34:13 +00:00
{
Action = () =>
{
exitConfirmedViaHoldOrClick = holdDelay.Value > 0;
this.Exit();
2020-01-24 07:34:13 +00:00
}
});
}
AddRangeInternal(new[]
2016-09-30 04:31:05 +00:00
{
buttonsContainer = new ParallaxContainer
2016-09-29 11:13:58 +00:00
{
2016-10-01 08:02:20 +00:00
ParallaxAmount = 0.01f,
Children = new Drawable[]
{
2022-04-19 06:46:03 +00:00
Buttons = new ButtonSystem
2016-10-01 08:02:20 +00:00
{
2023-11-24 01:56:59 +00:00
OnEditBeatmap = () =>
{
Beatmap.SetDefault();
this.Push(new EditorLoader());
},
2023-11-24 01:56:59 +00:00
OnEditSkin = () =>
{
skinEditor?.Show();
},
OnSolo = loadSoloSongSelect,
2020-12-25 04:38:11 +00:00
OnMultiplayer = () => this.Push(new Multiplayer()),
2020-12-25 14:45:44 +00:00
OnPlaylists = () => this.Push(new Playlists()),
OnExit = () =>
{
exitConfirmedViaHoldOrClick = true;
this.Exit();
}
}
2016-10-01 08:02:20 +00:00
}
},
logoTarget = new Container { RelativeSizeAxes = Axes.Both, },
sideFlashes = new MenuSideFlashes(),
songTicker = new SongTicker
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding { Right = 15, Top = 5 }
2020-01-24 07:34:13 +00:00
},
2023-07-13 11:31:00 +00:00
new KiaiMenuFountains(),
holdToExitGameOverlay?.CreateProxy() ?? Empty()
});
2022-04-19 06:46:03 +00: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 03:44:46 +00:00
default:
ApplyToBackground(b => b.FadeColour(OsuColour.Gray(0.8f), 500, Easing.OutSine));
break;
}
};
2018-04-13 09:19:50 +00:00
2022-04-19 06:46:03 +00:00
Buttons.OnSettings = () => settings?.ToggleVisibility();
Buttons.OnBeatmapListing = () => beatmapListing?.ToggleVisibility();
2018-04-13 09:19:50 +00:00
reappearSampleSwoosh = audio.Samples.Get(@"Menu/reappear-swoosh");
2017-03-17 11:09:33 +00:00
preloadSongSelect();
}
2018-04-13 09:19:50 +00:00
public void ReturnToOsuLogo() => Buttons.State = ButtonSystemState.Initial;
2017-03-17 11:09:33 +00:00
private void preloadSongSelect()
{
if (songSelect == null)
2017-04-02 06:56:12 +00:00
LoadComponentAsync(songSelect = new PlaySongSelect());
2017-03-17 11:09:33 +00:00
}
2018-04-13 09:19:50 +00:00
private void loadSoloSongSelect() => this.Push(consumeSongSelect());
2017-03-17 11:09:33 +00:00
private Screen consumeSongSelect()
{
var s = songSelect;
songSelect = null;
return s;
2016-11-01 14:24:14 +00:00
}
2018-04-13 09:19:50 +00:00
public override void OnEntering(ScreenTransitionEvent e)
2016-11-01 14:24:14 +00:00
{
base.OnEntering(e);
2022-04-19 06:46:03 +00:00
Buttons.FadeInFromZero(500);
2018-04-13 09:19:50 +00:00
if (e.Last is IntroScreen && musicController.TrackLoaded)
2017-04-25 04:33:34 +00: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)
2017-04-23 05:36:23 +00:00
{
Beatmap.Value.PrepareTrackForPreview(false);
track.Restart();
2017-05-23 07:26:51 +00:00
}
2017-04-25 04:33:34 +00:00
}
2020-07-06 13:03:09 +00:00
if (storage is OsuStorage osuStorage && osuStorage.Error != OsuStorageError.None)
dialogOverlay?.Push(new StorageErrorDialog(osuStorage, osuStorage.Error));
2017-07-19 04:32:16 +00:00
}
2018-04-13 09:19:50 +00:00
[CanBeNull]
private Drawable proxiedLogo;
2017-11-09 08:38:20 +00:00
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
2017-11-09 08:38:20 +00:00
base.LogoArriving(logo, resuming);
2018-04-13 09:19:50 +00:00
2022-04-19 06:46:03 +00:00
Buttons.SetOsuLogo(logo);
2018-04-13 09:19:50 +00:00
logo.FadeColour(Color4.White, 100, Easing.OutQuint);
logo.FadeIn(100, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
proxiedLogo = logo.ProxyToContainer(logoTarget);
if (resuming)
{
2022-04-19 06:46:03 +00:00
Buttons.State = ButtonSystemState.TopLevel;
2018-04-13 09:19:50 +00:00
this.FadeIn(FADE_IN_DURATION, Easing.OutQuint);
buttonsContainer.MoveTo(new Vector2(0, 0), FADE_IN_DURATION, Easing.OutQuint);
2018-04-13 09:19:50 +00:00
sideFlashes.Delay(FADE_IN_DURATION).FadeIn(64, Easing.InQuint);
}
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 12:56:47 +00:00
if (!loginDisplayed.Value)
{
Scheduler.AddDelayed(() => login?.Show(), 500);
2019-09-25 12:56:47 +00:00
loginDisplayed.Value = true;
}
return originalAction.Invoke();
}
}
2018-04-13 09:19:50 +00:00
2017-11-09 08:38:20 +00:00
protected override void LogoSuspending(OsuLogo logo)
{
var seq = logo.FadeOut(300, Easing.InSine)
.ScaleTo(0.2f, 300, Easing.InSine);
if (proxiedLogo != null)
{
logo.ReturnProxy();
proxiedLogo = null;
}
2022-04-19 06:46:03 +00:00
seq.OnComplete(_ => Buttons.SetOsuLogo(null));
seq.OnAbort(_ => Buttons.SetOsuLogo(null));
}
2018-04-13 09:19:50 +00:00
protected override void LogoExiting(OsuLogo logo)
{
base.LogoExiting(logo);
if (proxiedLogo != null)
{
logo.ReturnProxy();
proxiedLogo = null;
}
}
public override void OnSuspending(ScreenTransitionEvent e)
{
base.OnSuspending(e);
2018-04-13 09:19:50 +00:00
2022-04-19 06:46:03 +00:00
Buttons.State = ButtonSystemState.EnteringMode;
2018-04-13 09:19:50 +00:00
this.FadeOut(FADE_OUT_DURATION, Easing.InSine);
buttonsContainer.MoveTo(new Vector2(-800, 0), FADE_OUT_DURATION, Easing.InSine);
2018-04-13 09:19:50 +00:00
sideFlashes.FadeOut(64, Easing.OutQuint);
}
2018-04-13 09:19:50 +00:00
public override void OnResuming(ScreenTransitionEvent e)
{
base.OnResuming(e);
2020-01-11 15:27:22 +00:00
// Ensures any playing `ButtonSystem` samples are stopped when returning to MainMenu (as to not overlap with the 'back' sample)
Buttons.StopSamplePlayback();
reappearSampleSwoosh?.Play();
ApplyToBackground(b => (b as BackgroundScreenDefault)?.Next());
2018-04-13 09:19:50 +00:00
2020-05-05 01:31:11 +00:00
// we may have consumed our preloaded instance, so let's make another.
2017-03-17 11:09:33 +00:00
preloadSongSelect();
2020-08-16 14:04:49 +00:00
musicController.EnsurePlayingSomething();
}
2018-04-13 09:19:50 +00:00
public override bool OnExiting(ScreenExitEvent e)
{
bool requiresConfirmation =
// we need to have a dialog overlay to confirm in the first place.
dialogOverlay != null
// if the dialog has already displayed and been accepted by the user, we are good.
&& !exitConfirmedViaDialog
// Only require confirmation if there is either an ongoing operation or the user exited via a non-hold escape press.
&& (notifications.HasOngoingOperations || !exitConfirmedViaHoldOrClick);
if (requiresConfirmation)
{
2019-12-22 13:39:25 +00:00
if (dialogOverlay.CurrentDialog is ConfirmExitDialog exitDialog)
2021-05-19 07:27:38 +00:00
exitDialog.PerformOkAction();
2019-12-22 13:39:25 +00:00
else
{
dialogOverlay.Push(new ConfirmExitDialog(() =>
{
exitConfirmedViaDialog = true;
this.Exit();
}, () =>
{
holdToExitGameOverlay.Abort();
}));
}
2021-05-19 07:27:38 +00:00
return true;
2019-12-22 12:52:00 +00:00
}
2022-04-19 06:46:03 +00:00
Buttons.State = ButtonSystemState.Exit;
OverlayActivationMode.Value = OverlayActivation.Disabled;
2020-01-11 04:17:13 +00:00
songTicker.Hide();
this.FadeOut(3000);
return base.OnExiting(e);
}
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)
{
}
2016-08-26 03:28:23 +00:00
}
}