osu/osu.Game/Screens/OsuScreen.cs

283 lines
9.7 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
using System;
2019-04-08 09:32:05 +00:00
using System.Collections.Generic;
2021-01-06 06:25:53 +00:00
using JetBrains.Annotations;
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
2018-04-29 17:15:09 +00:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables;
2018-04-29 17:15:09 +00:00
using osu.Framework.Graphics;
2018-04-13 09:19:50 +00:00
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Rulesets;
using osu.Game.Screens.Menu;
using osu.Game.Overlays;
using osu.Game.Users;
2019-04-08 09:32:05 +00:00
using osu.Game.Rulesets.Mods;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Screens
{
public abstract class OsuScreen : Screen, IOsuScreen, IHasDescription
2018-04-13 09:19:50 +00:00
{
2019-01-25 05:10:59 +00:00
/// <summary>
/// The amount of negative padding that should be applied to game background content which touches both the left and right sides of the screen.
2020-05-05 01:26:12 +00:00
/// This allows for the game content to be pushed by the options/notification overlays without causing black areas to appear.
2019-01-25 05:10:59 +00:00
/// </summary>
public const float HORIZONTAL_OVERFLOW_PADDING = 50;
2018-05-28 04:02:06 +00:00
/// <summary>
/// A user-facing title for this screen.
/// </summary>
public virtual string Title => GetType().Name;
2018-05-28 04:02:06 +00:00
public string Description => Title;
2019-06-25 07:55:49 +00:00
public virtual bool AllowBackButton => true;
public virtual bool AllowExternalScreenChange => false;
2018-04-13 09:19:50 +00:00
/// <summary>
/// Whether all overlays should be hidden when this screen is entered or resumed.
2018-04-13 09:19:50 +00:00
/// </summary>
public virtual bool HideOverlaysOnEnter => false;
/// <summary>
2020-09-02 18:55:26 +00:00
/// The initial overlay activation mode to use when this screen is entered for the first time.
/// </summary>
protected virtual OverlayActivation InitialOverlayActivationMode => OverlayActivation.All;
2018-04-13 09:19:50 +00:00
protected readonly Bindable<OverlayActivation> OverlayActivationMode;
IBindable<OverlayActivation> IOsuScreen.OverlayActivationMode => OverlayActivationMode;
2018-04-13 09:19:50 +00:00
public virtual bool CursorVisible => true;
protected new OsuGameBase Game => base.Game as OsuGameBase;
/// <summary>
2020-11-08 13:39:56 +00:00
/// The <see cref="UserActivity"/> to set the user's activity automatically to when this screen is entered.
/// <para>This <see cref="Activity"/> will be automatically set to <see cref="InitialActivity"/> for this screen on entering for the first time
/// unless <see cref="Activity"/> is manually set before.</para>
/// </summary>
protected virtual UserActivity InitialActivity => null;
/// <summary>
/// The current <see cref="UserActivity"/> for this screen.
/// </summary>
2020-11-09 12:39:50 +00:00
protected readonly Bindable<UserActivity> Activity = new Bindable<UserActivity>();
2020-11-08 11:29:52 +00:00
IBindable<UserActivity> IOsuScreen.Activity => Activity;
2019-02-01 06:42:15 +00:00
/// <summary>
/// Whether to disallow changes to game-wise Beatmap/Ruleset bindables for this screen (and all children).
2019-02-01 06:42:15 +00:00
/// </summary>
public virtual bool DisallowExternalBeatmapRulesetChanges => false;
2021-01-19 08:11:40 +00:00
private Sample sampleExit;
2018-04-13 09:19:50 +00:00
protected virtual bool PlayResumeSound => true;
2019-01-23 11:52:00 +00:00
public virtual float BackgroundParallaxAmount => 1;
2018-04-13 09:19:50 +00:00
2019-12-13 11:06:12 +00:00
public virtual bool AllowRateAdjustments => true;
public Bindable<WorkingBeatmap> Beatmap { get; private set; }
2019-02-02 08:11:25 +00:00
public Bindable<RulesetInfo> Ruleset { get; private set; }
2018-04-13 09:19:50 +00:00
public Bindable<IReadOnlyList<Mod>> Mods { get; private set; }
2018-04-13 09:19:50 +00:00
private OsuScreenDependencies screenDependencies;
internal void CreateLeasedDependencies(IReadOnlyDependencyContainer dependencies) => createDependencies(dependencies);
2019-02-02 08:11:25 +00:00
protected override IReadOnlyDependencyContainer CreateChildDependencies(IReadOnlyDependencyContainer parent)
{
if (screenDependencies == null)
{
if (DisallowExternalBeatmapRulesetChanges)
throw new InvalidOperationException($"Screens that specify {nameof(DisallowExternalBeatmapRulesetChanges)} must be pushed immediately.");
createDependencies(parent);
}
return base.CreateChildDependencies(screenDependencies);
}
private void createDependencies(IReadOnlyDependencyContainer dependencies)
{
screenDependencies = new OsuScreenDependencies(DisallowExternalBeatmapRulesetChanges, dependencies);
2019-02-01 06:42:15 +00:00
Beatmap = screenDependencies.Beatmap;
Ruleset = screenDependencies.Ruleset;
2019-04-10 03:03:57 +00:00
Mods = screenDependencies.Mods;
2019-02-02 08:11:25 +00:00
}
2018-04-13 09:19:50 +00:00
/// <summary>
/// The background created and owned by this screen. May be null if the background didn't change.
/// </summary>
2021-01-06 06:25:53 +00:00
[CanBeNull]
private BackgroundScreen ownedBackground;
2021-01-06 06:25:53 +00:00
[CanBeNull]
private BackgroundScreen background;
2019-01-31 02:00:33 +00:00
[Resolved(canBeNull: true)]
[CanBeNull]
private BackgroundScreenStack backgroundStack { get; set; }
2019-01-31 02:00:33 +00:00
[Resolved(canBeNull: true)]
private OsuLogo logo { get; set; }
2019-01-23 11:52:00 +00:00
protected OsuScreen()
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
OverlayActivationMode = new Bindable<OverlayActivation>(InitialOverlayActivationMode);
2019-01-23 11:52:00 +00:00
}
[BackgroundDependencyLoader(true)]
2019-06-09 18:01:19 +00:00
private void load(OsuGame osu, AudioManager audio)
2018-04-13 09:19:50 +00:00
{
sampleExit = audio.Samples.Get(@"UI/screen-back");
}
2020-11-08 11:53:19 +00:00
protected override void LoadComplete()
{
base.LoadComplete();
Activity.Value ??= InitialActivity;
2018-04-13 09:19:50 +00:00
}
/// <summary>
/// Apply arbitrary changes to the current background screen in a thread safe manner.
/// </summary>
/// <param name="action">The operation to perform.</param>
public void ApplyToBackground(Action<BackgroundScreen> action)
{
if (backgroundStack == null)
throw new InvalidOperationException("Attempted to apply to background without a background stack being available.");
if (background == null)
throw new InvalidOperationException("Attempted to apply to background before screen is pushed.");
background.ApplyToBackground(action);
}
2019-01-23 11:52:00 +00:00
public override void OnResuming(IScreen last)
2018-04-13 09:19:50 +00:00
{
if (PlayResumeSound)
sampleExit?.Play();
2018-04-13 09:19:50 +00:00
applyArrivingDefaults(true);
base.OnResuming(last);
}
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);
2018-04-13 09:19:50 +00:00
onSuspendingLogo();
}
2019-01-23 11:52:00 +00:00
public override void OnEntering(IScreen last)
2018-04-13 09:19:50 +00:00
{
applyArrivingDefaults(false);
backgroundStack?.Push(ownedBackground = CreateBackground());
background = backgroundStack?.CurrentScreen as BackgroundScreen;
if (background != ownedBackground)
{
// background may have not been replaced, at which point we don't want to track the background lifetime.
ownedBackground?.Dispose();
ownedBackground = null;
}
2018-04-13 09:19:50 +00:00
base.OnEntering(last);
}
2019-01-23 11:52:00 +00:00
public override bool OnExiting(IScreen next)
2018-04-13 09:19:50 +00:00
{
if (ValidForResume && logo != null)
onExitingLogo();
if (base.OnExiting(next))
return true;
if (ownedBackground != null && backgroundStack?.CurrentScreen == ownedBackground)
backgroundStack?.Exit();
2018-04-13 09:19:50 +00:00
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)
{
2019-01-25 12:02:35 +00:00
ApplyLogoArrivingDefaults(logo);
2018-04-13 09:19:50 +00:00
}
private void applyArrivingDefaults(bool isResuming)
{
2019-01-31 02:00:33 +00:00
logo?.AppendAnimatingAction(() =>
{
2019-01-23 11:52:00 +00:00
if (this.IsCurrentScreen()) LogoArriving(logo, isResuming);
}, true);
2018-04-13 09:19:50 +00:00
}
2019-01-25 12:02:35 +00:00
/// <summary>
/// Applies default animations to an arriving logo.
/// Todo: This should not exist.
/// </summary>
/// <param name="logo">The logo to apply animations to.</param>
public static void ApplyLogoArrivingDefaults(OsuLogo logo)
{
logo.Action = null;
logo.FadeOut(300, Easing.OutQuint);
logo.Anchor = Anchor.TopLeft;
logo.Origin = Anchor.Centre;
logo.RelativePositionAxes = Axes.Both;
2019-01-25 12:02:35 +00:00
logo.BeatMatching = true;
logo.Triangles = true;
logo.Ripple = true;
}
2018-04-13 09:19:50 +00:00
private void onExitingLogo()
{
2019-01-31 02:00:33 +00:00
logo?.AppendAnimatingAction(() => LogoExiting(logo), false);
2018-04-13 09:19:50 +00:00
}
/// <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()
{
2019-01-31 02:00:33 +00:00
logo?.AppendAnimatingAction(() => LogoSuspending(logo), false);
2018-04-13 09:19:50 +00:00
}
/// <summary>
/// Fired when this screen was suspended to add any outwards transition to the logo.
/// </summary>
protected virtual void LogoSuspending(OsuLogo logo)
{
}
/// <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;
2020-07-14 06:59:20 +00:00
public virtual bool OnBackButton() => false;
2018-04-13 09:19:50 +00:00
}
}