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

292 lines
9.1 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
2019-12-22 12:52:00 +00:00
using System.Linq;
2018-11-20 07:51:59 +00:00
using osuTK;
using osuTK.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-13 09:19:50 +00:00
using osu.Framework.Graphics;
using osu.Framework.Platform;
2018-04-13 09:19:50 +00:00
using osu.Framework.Screens;
using osu.Game.Configuration;
using osu.Game.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Game.Graphics.Containers;
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;
2018-04-13 09:19:50 +00:00
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Edit;
2018-05-16 00:14:10 +00:00
using osu.Game.Screens.Multi;
2018-04-13 09:19:50 +00:00
using osu.Game.Screens.Select;
namespace osu.Game.Screens.Menu
{
public class MainMenu : OsuScreen
{
public const float FADE_IN_DURATION = 300;
public const float FADE_OUT_DURATION = 400;
2018-04-13 09:19:50 +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;
public override bool AllowRateAdjustments => false;
2018-04-13 09:19:50 +00:00
private Screen songSelect;
private MenuSideFlashes sideFlashes;
2018-04-13 09:19:50 +00:00
private ButtonSystem buttons;
[Resolved]
private GameHost host { get; set; }
2018-04-13 09:19:50 +00:00
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(canBeNull: true)]
private LoginOverlay login { get; set; }
[Resolved]
private IAPIProvider api { get; set; }
2019-09-19 08:40:46 +00:00
[Resolved(canBeNull: true)]
private DialogOverlay dialogOverlay { get; set; }
2019-03-11 15:04:19 +00:00
private BackgroundScreenDefault background;
protected override BackgroundScreen CreateBackground() => background;
2018-04-13 09:19:50 +00:00
2019-10-02 04:26:46 +00:00
private Bindable<float> holdDelay;
2019-09-25 12:56:47 +00:00
private Bindable<bool> loginDisplayed;
2019-09-19 11:17:58 +00:00
private ExitConfirmOverlay exitConfirmOverlay;
private ParallaxContainer buttonsContainer;
2020-01-11 04:17:13 +00:00
private SongTicker songTicker;
[BackgroundDependencyLoader(true)]
private void load(BeatmapListingOverlay beatmapListing, SettingsOverlay settings, RankingsOverlay rankings, OsuConfigManager config, SessionStatics statics)
2018-04-13 09:19:50 +00:00
{
2019-10-02 04:26:46 +00:00
holdDelay = config.GetBindable<float>(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(exitConfirmOverlay = new ExitConfirmOverlay
{
Action = () =>
{
if (holdDelay.Value > 0)
confirmAndExit();
else
this.Exit();
}
});
}
AddRangeInternal(new[]
2018-04-13 09:19:50 +00:00
{
buttonsContainer = new ParallaxContainer
2018-04-13 09:19:50 +00:00
{
ParallaxAmount = 0.01f,
Children = new Drawable[]
{
buttons = new ButtonSystem
{
OnEdit = delegate
{
Beatmap.SetDefault();
this.Push(new Editor());
},
OnSolo = onSolo,
OnMulti = delegate { this.Push(new Multiplayer()); },
2019-09-19 13:46:21 +00:00
OnExit = confirmAndExit,
2018-04-13 09:19:50 +00:00
}
}
},
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
},
exitConfirmOverlay?.CreateProxy() ?? Drawable.Empty()
});
buttons.StateChanged += state =>
{
switch (state)
{
case ButtonSystemState.Initial:
case ButtonSystemState.Exit:
2019-01-23 11:52:00 +00:00
Background.FadeColour(Color4.White, 500, Easing.OutSine);
break;
2019-04-01 03:44:46 +00:00
default:
2019-01-23 11:52:00 +00:00
Background.FadeColour(OsuColour.Gray(0.8f), 500, Easing.OutSine);
break;
}
};
2018-04-13 09:19:50 +00:00
buttons.OnSettings = () => settings?.ToggleVisibility();
buttons.OnBeatmapListing = () => beatmapListing?.ToggleVisibility();
buttons.OnChart = () => rankings?.ShowSpotlights();
2018-04-13 09:19:50 +00:00
2019-03-11 15:04:19 +00:00
LoadComponentAsync(background = new BackgroundScreenDefault());
2018-04-13 09:19:50 +00:00
preloadSongSelect();
}
[Resolved(canBeNull: true)]
private OsuGame game { get; set; }
2019-09-19 13:46:21 +00:00
private void confirmAndExit()
{
2019-12-22 12:52:00 +00:00
if (exitConfirmed) return;
2019-09-19 13:46:21 +00:00
exitConfirmed = true;
game?.PerformFromScreen(menu => menu.Exit());
2019-09-19 13:46:21 +00:00
}
2018-04-13 09:19:50 +00:00
private void preloadSongSelect()
{
if (songSelect == null)
LoadComponentAsync(songSelect = new PlaySongSelect());
}
public void LoadToSolo() => Schedule(onSolo);
2019-02-25 10:19:28 +00:00
private void onSolo() => this.Push(consumeSongSelect());
2018-04-13 09:19:50 +00:00
private Screen consumeSongSelect()
{
var s = songSelect;
songSelect = null;
return s;
}
2020-07-06 13:03:09 +00:00
[Resolved]
private Storage storage { get; set; }
2019-01-23 11:52:00 +00:00
public override void OnEntering(IScreen last)
2018-04-13 09:19:50 +00:00
{
base.OnEntering(last);
buttons.FadeInFromZero(500);
var metadata = Beatmap.Value.Metadata;
2020-08-16 14:04:49 +00:00
if (last is IntroScreen && musicController.TrackLoaded)
2018-04-13 09:19:50 +00:00
{
2020-08-16 14:04:49 +00:00
if (!musicController.CurrentTrack.IsRunning)
2018-04-13 09:19:50 +00:00
{
2020-08-16 14:04:49 +00:00
musicController.CurrentTrack.Seek(metadata.PreviewTime != -1 ? metadata.PreviewTime : 0.4f * musicController.CurrentTrack.Length);
musicController.CurrentTrack.Start();
2018-04-13 09:19:50 +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));
2018-04-13 09:19:50 +00:00
}
private bool exitConfirmed;
2018-04-13 09:19:50 +00:00
protected override void LogoArriving(OsuLogo logo, bool resuming)
{
base.LogoArriving(logo, resuming);
buttons.SetOsuLogo(logo);
logo.FadeColour(Color4.White, 100, Easing.OutQuint);
logo.FadeIn(100, Easing.OutQuint);
if (resuming)
{
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);
2018-04-13 09:19:50 +00:00
}
else if (!api.IsLoggedIn)
{
logo.Action += displayLogin;
}
bool displayLogin()
{
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 true;
}
2018-04-13 09:19:50 +00:00
}
protected override void LogoSuspending(OsuLogo logo)
{
var seq = logo.FadeOut(300, Easing.InSine)
.ScaleTo(0.2f, 300, Easing.InSine);
seq.OnComplete(_ => buttons.SetOsuLogo(null));
seq.OnAbort(_ => buttons.SetOsuLogo(null));
2018-04-13 09:19:50 +00:00
}
2019-01-23 11:52:00 +00:00
public override void OnSuspending(IScreen next)
2018-04-13 09:19:50 +00:00
{
base.OnSuspending(next);
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);
}
2019-01-23 11:52:00 +00:00
public override void OnResuming(IScreen last)
2018-04-13 09:19:50 +00:00
{
base.OnResuming(last);
2020-01-11 15:27:22 +00:00
2019-02-25 10:19:28 +00:00
(Background 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.
2018-04-13 09:19:50 +00:00
preloadSongSelect();
2020-08-16 14:04:49 +00:00
musicController.EnsurePlayingSomething();
2018-04-13 09:19:50 +00:00
}
2019-01-23 11:52:00 +00:00
public override bool OnExiting(IScreen next)
2018-04-13 09:19:50 +00:00
{
if (!exitConfirmed && dialogOverlay != null)
{
2019-12-22 13:39:25 +00:00
if (dialogOverlay.CurrentDialog is ConfirmExitDialog exitDialog)
{
exitConfirmed = true;
exitDialog.Buttons.First().Click();
}
else
{
dialogOverlay.Push(new ConfirmExitDialog(confirmAndExit, () => exitConfirmOverlay.Abort()));
return true;
}
2019-12-22 12:52:00 +00:00
}
buttons.State = ButtonSystemState.Exit;
OverlayActivationMode.Value = OverlayActivation.Disabled;
2020-01-11 04:17:13 +00:00
songTicker.Hide();
this.FadeOut(3000);
2018-04-13 09:19:50 +00:00
return base.OnExiting(next);
}
}
}