1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-16 04:22:54 +08:00
osu-lazer/osu.Game/Screens/OsuScreen.cs

206 lines
6.6 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-10-05 15:35:10 +08:00
2017-07-19 12:32:16 +08:00
using System;
2016-11-11 06:40:42 +08:00
using osu.Framework.Allocation;
2016-10-28 18:55:48 +08:00
using osu.Framework.Configuration;
2017-02-17 17:59:30 +08:00
using osu.Framework.Screens;
2016-10-28 18:55:48 +08:00
using osu.Game.Beatmaps;
2016-10-05 19:03:52 +08:00
using osu.Game.Graphics.Containers;
using OpenTK;
using osu.Framework.Audio.Sample;
using osu.Framework.Audio;
using osu.Framework.Graphics;
2017-07-26 12:22:46 +08:00
using osu.Game.Rulesets;
using osu.Game.Screens.Menu;
2017-12-04 10:30:25 +08:00
using osu.Framework.Input;
using OpenTK.Input;
2016-10-05 15:35:10 +08:00
2016-11-14 16:23:33 +08:00
namespace osu.Game.Screens
2016-10-05 15:35:10 +08:00
{
2017-02-17 17:59:30 +08:00
public abstract class OsuScreen : Screen
2016-10-05 15:35:10 +08:00
{
public BackgroundScreen Background { get; private set; }
2016-10-05 15:35:10 +08:00
/// <summary>
2017-04-18 15:05:58 +08:00
/// Override to create a BackgroundMode for the current screen.
2016-10-05 15:35:10 +08:00
/// Note that the instance created may not be the used instance if it matches the BackgroundMode equality clause.
/// </summary>
2017-02-17 17:59:30 +08:00
protected virtual BackgroundScreen CreateBackground() => null;
2016-10-05 15:35:10 +08:00
public virtual bool ShowOverlays => true;
protected new OsuGameBase Game => base.Game as OsuGameBase;
public virtual bool HasLocalCursorDisplayed => false;
2017-03-16 22:58:36 +08:00
private OsuLogo logo;
/// <summary>
/// Whether the beatmap or ruleset should be allowed to be changed by the user or game.
/// Used to mark exclusive areas where this is strongly prohibited, like gameplay.
/// </summary>
public virtual bool AllowBeatmapRulesetChange => true;
2017-07-19 12:32:16 +08:00
protected readonly Bindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
2016-10-28 18:55:48 +08:00
2017-07-19 12:32:16 +08:00
public WorkingBeatmap InitialBeatmap
2016-10-28 18:55:48 +08:00
{
set
{
2017-07-19 12:32:16 +08:00
if (IsLoaded) throw new InvalidOperationException($"Cannot set {nameof(InitialBeatmap)} post-load.");
Beatmap.Value = value;
2016-10-28 18:55:48 +08:00
}
}
2017-07-24 21:47:31 +08:00
protected readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
2017-07-19 12:32:16 +08:00
private SampleChannel sampleExit;
[BackgroundDependencyLoader(permitNulls: true)]
2017-07-19 12:32:16 +08:00
private void load(OsuGameBase game, OsuGame osuGame, AudioManager audio)
{
2017-02-27 11:24:50 +08:00
if (game != null)
2017-02-27 17:08:05 +08:00
{
//if we were given a beatmap at ctor time, we want to pass this on to the game-wide beatmap.
2017-07-19 12:32:16 +08:00
var localMap = Beatmap.Value;
Beatmap.BindTo(game.Beatmap);
2017-02-27 17:08:05 +08:00
if (localMap != null)
2017-07-19 12:32:16 +08:00
Beatmap.Value = localMap;
2017-02-27 17:08:05 +08:00
}
if (osuGame != null)
2017-07-24 21:47:31 +08:00
Ruleset.BindTo(osuGame.Ruleset);
2017-11-25 22:11:18 +08:00
sampleExit = audio.Sample.Get(@"UI/screen-back");
2016-10-28 18:55:48 +08:00
}
2017-05-22 07:53:36 +08:00
2017-12-04 10:30:25 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
if (args.Repeat || !IsCurrentScreen) return false;
switch (args.Key)
{
case Key.Escape:
Exit();
return true;
}
return base.OnKeyDown(state, args);
}
protected override void OnResuming(Screen last)
{
base.OnResuming(last);
logo.AppendAnimatingAction(() => LogoArriving(logo, true), true);
sampleExit?.Play();
}
protected override void OnSuspending(Screen next)
{
base.OnSuspending(next);
2017-11-08 15:34:03 +08:00
onSuspendingLogo();
}
2017-02-17 17:59:30 +08:00
protected override void OnEntering(Screen last)
2016-10-05 15:35:10 +08:00
{
2017-02-17 17:59:30 +08:00
OsuScreen lastOsu = last as OsuScreen;
2016-10-05 15:35:10 +08:00
2017-02-17 17:59:30 +08:00
BackgroundScreen bg = CreateBackground();
2016-10-05 15:35:10 +08:00
if (lastOsu?.Background != null)
{
if (bg == null || lastOsu.Background.Equals(bg))
//we can keep the previous mode's background.
Background = lastOsu.Background;
else
{
lastOsu.Background.Push(Background = bg);
}
}
else if (bg != null)
{
// this makes up for the fact our padding changes when the global toolbar is visible.
bg.Scale = new Vector2(1.06f);
AddInternal(new ParallaxContainer
2016-10-05 19:03:52 +08:00
{
2016-11-30 03:50:12 +08:00
Depth = float.MaxValue,
2016-10-05 19:03:52 +08:00
Children = new[]
{
Background = bg
}
});
2016-10-05 15:35:10 +08:00
}
if ((logo = lastOsu?.logo) == null)
LoadComponentAsync(logo = new OsuLogo { Alpha = 0 }, AddInternal);
logo.AppendAnimatingAction(() => LogoArriving(logo, false), true);
base.OnEntering(last);
2016-10-05 15:35:10 +08:00
}
2017-02-17 17:59:30 +08:00
protected override bool OnExiting(Screen next)
2016-10-05 15:35:10 +08:00
{
if (ValidForResume && logo != null)
2017-11-08 15:34:03 +08:00
onExitingLogo();
2017-02-17 17:59:30 +08:00
OsuScreen nextOsu = next as OsuScreen;
2016-10-05 15:35:10 +08:00
if (Background != null && !Background.Equals(nextOsu?.Background))
{
if (nextOsu != null)
2017-04-18 15:05:58 +08:00
//We need to use MakeCurrent in case we are jumping up multiple game screens.
nextOsu.Background?.MakeCurrent();
else
Background.Exit();
}
2016-10-05 15:35:10 +08:00
if (base.OnExiting(next))
return true;
2017-07-19 12:32:16 +08:00
Beatmap.UnbindAll();
return false;
2016-10-05 15:35:10 +08:00
}
2017-11-08 15:34:03 +08:00
/// <summary>
/// Fired when this screen was entered or resumed and the logo state is required to be adjusted.
/// </summary>
2017-11-09 16:38:20 +08:00
protected virtual void LogoArriving(OsuLogo logo, bool resuming)
{
logo.Action = null;
logo.FadeOut(300, Easing.OutQuint);
logo.Anchor = Anchor.TopLeft;
logo.Origin = Anchor.Centre;
logo.RelativePositionAxes = Axes.None;
logo.Triangles = true;
logo.Ripple = true;
}
2017-11-08 15:34:03 +08:00
private void onExitingLogo()
{
logo.AppendAnimatingAction(() => { LogoExiting(logo); }, false);
}
2017-11-08 15:34:03 +08:00
/// <summary>
/// Fired when this screen was exited to add any outwards transition to the logo.
/// </summary>
2017-11-09 16:38:20 +08:00
protected virtual void LogoExiting(OsuLogo logo)
{
}
2017-11-08 15:34:03 +08:00
private void onSuspendingLogo()
{
logo.AppendAnimatingAction(() => { LogoSuspending(logo); }, false);
}
2017-11-08 15:34:03 +08:00
/// <summary>
/// Fired when this screen was suspended to add any outwards transition to the logo.
/// </summary>
2017-11-09 16:38:20 +08:00
protected virtual void LogoSuspending(OsuLogo logo)
{
}
2016-10-05 15:35:10 +08:00
}
}