1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 10:17:38 +08:00
osu-lazer/osu.Game/Screens/Menu/MainMenu.cs

168 lines
5.3 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-08-26 11:28:23 +08:00
2017-05-06 15:37:53 +08:00
using System.Threading.Tasks;
using OpenTK;
using OpenTK.Input;
2016-11-14 16:23:33 +08:00
using osu.Framework.Allocation;
2017-04-23 13:36:23 +08:00
using osu.Framework.Audio.Track;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
2017-05-06 15:37:53 +08:00
using osu.Framework.Input;
2017-04-23 13:36:23 +08:00
using osu.Framework.MathUtils;
2017-03-28 20:26:20 +08:00
using osu.Framework.Screens;
2017-04-23 13:36:23 +08:00
using osu.Game.Configuration;
using osu.Game.Database;
using osu.Game.Graphics.Containers;
2016-11-14 16:23:33 +08:00
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Charts;
using osu.Game.Screens.Direct;
using osu.Game.Screens.Edit;
2016-11-14 16:23:33 +08:00
using osu.Game.Screens.Multiplayer;
using osu.Game.Screens.Select;
2017-02-27 22:38:29 +08:00
using osu.Game.Screens.Tournament;
2016-08-26 11:28:23 +08:00
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens.Menu
2016-08-26 11:28:23 +08:00
{
2017-02-17 17:59:30 +08:00
public class MainMenu : OsuScreen
2016-08-26 11:28:23 +08:00
{
private readonly ButtonSystem buttons;
2016-08-26 11:28:23 +08:00
internal override bool ShowOverlays => buttons.State != MenuState.Initial;
private readonly BackgroundScreen background;
2017-03-17 19:09:33 +08:00
private Screen songSelect;
2016-10-05 15:35:10 +08:00
2017-02-17 17:59:30 +08:00
protected override BackgroundScreen CreateBackground() => background;
2016-11-01 22:24:14 +08:00
public MainMenu()
{
2017-02-17 17:59:30 +08:00
background = new BackgroundScreenDefault();
2016-09-30 12:31:05 +08:00
Children = new Drawable[]
{
2016-10-01 16:02:20 +08:00
new ParallaxContainer
2016-09-29 19:13:58 +08:00
{
2016-10-01 16:02:20 +08:00
ParallaxAmount = 0.01f,
Children = new Drawable[]
{
buttons = new ButtonSystem
2016-10-01 16:02:20 +08:00
{
OnChart = delegate { Push(new ChartListing()); },
OnDirect = delegate { Push(new OnlineListing()); },
OnEdit = delegate { Push(new Editor()); },
2017-03-17 19:09:33 +08:00
OnSolo = delegate { Push(consumeSongSelect()); },
2016-10-01 16:02:20 +08:00
OnMulti = delegate { Push(new Lobby()); },
OnExit = delegate { Exit(); },
2016-10-01 16:02:20 +08:00
}
}
2016-09-29 19:13:58 +08:00
}
2016-09-30 12:31:05 +08:00
};
2016-11-01 22:24:14 +08:00
}
2017-04-23 13:36:23 +08:00
private Bindable<bool> menuMusic;
private TrackManager trackManager;
[BackgroundDependencyLoader]
2017-04-23 13:36:23 +08:00
private void load(OsuGame game, OsuConfigManager config, BeatmapDatabase beatmaps)
2016-11-01 22:24:14 +08:00
{
2017-04-23 13:36:23 +08:00
menuMusic = config.GetBindable<bool>(OsuConfig.MenuMusic);
2017-04-02 14:56:12 +08:00
LoadComponentAsync(background);
2016-11-01 22:24:14 +08:00
2017-04-23 13:36:23 +08:00
if (!menuMusic)
{
trackManager = game.Audio.Track;
2017-05-06 15:37:53 +08:00
var query = beatmaps.Query<BeatmapSetInfo>().Where(b => !b.DeletePending);
int count = query.Count();
if (count > 0)
2017-04-23 13:36:23 +08:00
{
2017-05-06 15:37:53 +08:00
var beatmap = query.ElementAt(RNG.Next(0, count - 1));
beatmaps.GetChildren(beatmap);
2017-05-06 15:37:53 +08:00
Beatmap = beatmaps.GetWorkingBeatmap(beatmap.Beatmaps[0]);
2017-04-23 13:36:23 +08:00
}
}
buttons.OnSettings = game.ToggleSettings;
2017-03-17 19:09:33 +08:00
preloadSongSelect();
}
private void preloadSongSelect()
{
if (songSelect == null)
2017-04-02 14:56:12 +08:00
LoadComponentAsync(songSelect = new PlaySongSelect());
2017-03-17 19:09:33 +08:00
}
private Screen consumeSongSelect()
{
var s = songSelect;
songSelect = null;
return s;
2016-11-01 22:24:14 +08:00
}
2017-02-17 17:59:30 +08:00
protected override void OnEntering(Screen last)
2016-11-01 22:24:14 +08:00
{
base.OnEntering(last);
2016-11-01 22:24:14 +08:00
buttons.FadeInFromZero(500);
2017-05-06 14:03:08 +08:00
if (last is Intro && Beatmap != null)
2017-04-25 12:33:34 +08:00
{
2017-04-23 13:36:23 +08:00
Task.Run(() =>
{
2017-05-06 14:03:08 +08:00
trackManager.SetExclusive(Beatmap.Track);
Beatmap.Track.Seek(Beatmap.Metadata.PreviewTime);
if (Beatmap.Metadata.PreviewTime == -1)
Beatmap.Track.Seek(Beatmap.Track.Length * 0.4f);
Beatmap.Track.Start();
2017-04-23 13:36:23 +08:00
});
2017-04-25 12:33:34 +08:00
}
2016-09-30 12:31:05 +08:00
}
2017-02-17 17:59:30 +08:00
protected override void OnSuspending(Screen next)
{
2016-10-05 15:35:10 +08:00
base.OnSuspending(next);
const float length = 400;
buttons.State = MenuState.EnteringMode;
Content.FadeOut(length, EasingTypes.InSine);
Content.MoveTo(new Vector2(-800, 0), length, EasingTypes.InSine);
}
2017-02-17 17:59:30 +08:00
protected override void OnResuming(Screen last)
{
2016-10-05 15:35:10 +08:00
base.OnResuming(last);
2017-03-17 19:09:33 +08:00
//we may have consumed our preloaded instance, so let's make another.
preloadSongSelect();
const float length = 300;
buttons.State = MenuState.TopLevel;
Content.FadeIn(length, EasingTypes.OutQuint);
Content.MoveTo(new Vector2(0, 0), length, EasingTypes.OutQuint);
}
2017-02-17 17:59:30 +08:00
protected override bool OnExiting(Screen next)
{
buttons.State = MenuState.Exit;
2017-02-17 14:33:08 +08:00
Content.FadeOut(3000);
return base.OnExiting(next);
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (!args.Repeat && state.Keyboard.ControlPressed && state.Keyboard.ShiftPressed && args.Key == Key.D)
{
Push(new Drawings());
return true;
}
return base.OnKeyDown(state, args);
}
2016-08-26 11:28:23 +08:00
}
}