1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-18 05:06:14 +08:00
osu-lazer/osu.Game/Screens/OsuScreen.cs

251 lines
8.0 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2018-06-06 19:32:37 +08:00
using System;
2018-05-28 12:02:06 +08:00
using Microsoft.EntityFrameworkCore.Internal;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
2018-04-30 01:15:09 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2018-04-13 17:19:50 +08:00
using osu.Framework.Configuration;
2018-04-30 01:15:09 +08:00
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
2018-04-13 17:19:50 +08:00
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Graphics.Containers;
using osu.Game.Input.Bindings;
2018-04-13 17:19:50 +08:00
using osu.Game.Rulesets;
using osu.Game.Screens.Menu;
2018-04-30 01:15:09 +08:00
using OpenTK;
using osu.Game.Overlays;
using osu.Framework.Graphics.Containers;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens
{
2018-05-28 12:02:06 +08:00
public abstract class OsuScreen : Screen, IKeyBindingHandler<GlobalAction>, IHasDescription
2018-04-13 17:19:50 +08:00
{
public BackgroundScreen Background { get; private set; }
2018-05-28 12:02:06 +08:00
/// <summary>
/// A user-facing title for this screen.
/// </summary>
public virtual string Title => GetType().ShortDisplayName();
public string Description => Title;
protected virtual bool AllowBackButton => true;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Override to create a BackgroundMode for the current screen.
/// Note that the instance created may not be the used instance if it matches the BackgroundMode equality clause.
/// </summary>
protected virtual BackgroundScreen CreateBackground() => null;
private Action updateOverlayStates;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Whether all overlays should be hidden when this screen is entered or resumed.
2018-04-13 17:19:50 +08:00
/// </summary>
protected virtual bool HideOverlaysOnEnter => false;
2018-06-06 15:17:51 +08:00
protected readonly Bindable<OverlayActivation> OverlayActivationMode = new Bindable<OverlayActivation>();
/// <summary>
/// Whether overlays should be able to be opened once this screen is entered or resumed.
/// </summary>
2018-06-06 15:17:51 +08:00
protected virtual OverlayActivation InitialOverlayActivationMode => OverlayActivation.All;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Whether this <see cref="OsuScreen"/> allows the cursor to be displayed.
/// </summary>
public virtual bool CursorVisible => true;
protected new OsuGameBase Game => base.Game as OsuGameBase;
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;
protected readonly Bindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
2018-04-13 17:19:50 +08:00
protected virtual float BackgroundParallaxAmount => 1;
private ParallaxContainer backgroundParallaxContainer;
protected readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
private SampleChannel sampleExit;
[BackgroundDependencyLoader(true)]
2018-08-31 06:04:40 +08:00
private void load(BindableBeatmap beatmap, OsuGame osu, AudioManager audio, Bindable<RulesetInfo> ruleset)
2018-04-13 17:19:50 +08:00
{
Beatmap.BindTo(beatmap);
Ruleset.BindTo(ruleset);
if (osu != null)
2018-04-13 17:19:50 +08:00
{
OverlayActivationMode.BindTo(osu.OverlayActivationMode);
updateOverlayStates = () =>
{
if (HideOverlaysOnEnter)
osu.CloseAllOverlays();
else
osu.Toolbar.State = Visibility.Visible;
};
2018-04-13 17:19:50 +08:00
}
2018-08-31 06:04:40 +08:00
sampleExit = audio.Sample.Get(@"UI/screen-back");
2018-04-13 17:19:50 +08:00
}
public virtual bool OnPressed(GlobalAction action)
2018-04-13 17:19:50 +08:00
{
2018-06-27 15:06:26 +08:00
if (!IsCurrentScreen) return false;
if (action == GlobalAction.Back && AllowBackButton)
2018-04-13 17:19:50 +08:00
{
Exit();
return true;
2018-04-13 17:19:50 +08:00
}
return false;
2018-04-13 17:19:50 +08:00
}
public bool OnReleased(GlobalAction action) => action == GlobalAction.Back && AllowBackButton;
2018-04-13 17:19:50 +08:00
protected override void OnResuming(Screen last)
{
sampleExit?.Play();
applyArrivingDefaults(true);
base.OnResuming(last);
}
protected override void OnSuspending(Screen next)
{
base.OnSuspending(next);
onSuspendingLogo();
}
protected override void OnEntering(Screen last)
{
OsuScreen lastOsu = last as OsuScreen;
BackgroundScreen bg = CreateBackground();
if (lastOsu?.Background != null)
{
backgroundParallaxContainer = lastOsu.backgroundParallaxContainer;
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(backgroundParallaxContainer = new ParallaxContainer
{
Depth = float.MaxValue,
Children = new[]
{
Background = bg
}
});
}
if ((logo = lastOsu?.logo) == null)
LoadComponentAsync(logo = new OsuLogo { Alpha = 0 }, AddInternal);
applyArrivingDefaults(false);
base.OnEntering(last);
}
protected override bool OnExiting(Screen next)
{
if (ValidForResume && logo != null)
onExitingLogo();
OsuScreen nextOsu = next as OsuScreen;
if (Background != null && !Background.Equals(nextOsu?.Background))
{
Background.Exit();
//We need to use MakeCurrent in case we are jumping up multiple game screens.
nextOsu?.Background?.MakeCurrent();
2018-04-13 17:19:50 +08:00
}
if (base.OnExiting(next))
return true;
Beatmap.UnbindAll();
return false;
}
/// <summary>
/// Fired when this screen was entered or resumed and the logo state is required to be adjusted.
/// </summary>
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;
2018-05-31 16:14:47 +08:00
logo.BeatMatching = true;
2018-04-13 17:19:50 +08:00
logo.Triangles = true;
logo.Ripple = true;
}
private void applyArrivingDefaults(bool isResuming)
{
logo.AppendAnimatingAction(() =>
{
if (IsCurrentScreen) LogoArriving(logo, isResuming);
}, true);
2018-04-13 17:19:50 +08:00
if (backgroundParallaxContainer != null)
backgroundParallaxContainer.ParallaxAmount = ParallaxContainer.DEFAULT_PARALLAX_AMOUNT * BackgroundParallaxAmount;
2018-06-06 15:17:51 +08:00
OverlayActivationMode.Value = InitialOverlayActivationMode;
2018-06-03 18:02:43 +08:00
updateOverlayStates?.Invoke();
2018-04-13 17:19:50 +08:00
}
private void onExitingLogo()
{
logo.AppendAnimatingAction(() => { LogoExiting(logo); }, false);
}
/// <summary>
/// Fired when this screen was exited to add any outwards transition to the logo.
/// </summary>
protected virtual void LogoExiting(OsuLogo logo)
{
}
private void onSuspendingLogo()
{
logo.AppendAnimatingAction(() => { LogoSuspending(logo); }, false);
}
/// <summary>
/// Fired when this screen was suspended to add any outwards transition to the logo.
/// </summary>
protected virtual void LogoSuspending(OsuLogo logo)
{
}
}
}