osu/osu.Game/Screens/OsuScreen.cs

204 lines
6.1 KiB
C#
Raw Normal View History

2018-04-13 09:19:50 +00: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 11:32:37 +00:00
using System;
2018-05-28 04:02:06 +00:00
using Microsoft.EntityFrameworkCore.Internal;
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;
2018-04-13 09:19:50 +00:00
using osu.Framework.Configuration;
2018-04-29 17:15:09 +00:00
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
2018-04-13 09:19:50 +00:00
using osu.Framework.Screens;
using osu.Game.Beatmaps;
using osu.Game.Input.Bindings;
2018-04-13 09:19:50 +00:00
using osu.Game.Rulesets;
using osu.Game.Screens.Menu;
using osu.Game.Overlays;
using osu.Framework.Graphics.Containers;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Screens
{
2019-01-23 11:52:00 +00:00
public abstract class OsuScreen : Screen, IOsuScreen, IKeyBindingHandler<GlobalAction>, IHasDescription
2018-04-13 09:19:50 +00:00
{
2019-01-23 11:52:00 +00:00
public BackgroundScreen Background { get; }
2018-04-13 09:19:50 +00:00
2018-05-28 04:02:06 +00: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;
public virtual bool AllowExternalScreenChange => false;
2018-04-13 09:19:50 +00: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 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>
protected virtual bool HideOverlaysOnEnter => false;
2018-06-06 07:17:51 +00: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 07:17:51 +00:00
protected virtual OverlayActivation InitialOverlayActivationMode => OverlayActivation.All;
2018-04-13 09:19:50 +00:00
public virtual bool CursorVisible => true;
protected new OsuGameBase Game => base.Game as OsuGameBase;
2019-01-23 11:52:00 +00:00
[Resolved]
private OsuLogo logo { get; set; }
2018-04-13 09:19:50 +00:00
public virtual bool AllowBeatmapRulesetChange => true;
protected readonly Bindable<WorkingBeatmap> Beatmap = new Bindable<WorkingBeatmap>();
2018-04-13 09:19:50 +00:00
2019-01-23 11:52:00 +00:00
public virtual float BackgroundParallaxAmount => 1;
2018-04-13 09:19:50 +00:00
protected readonly Bindable<RulesetInfo> Ruleset = new Bindable<RulesetInfo>();
private SampleChannel sampleExit;
2019-01-23 11:52:00 +00:00
protected OsuScreen()
{
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
Background = CreateBackground();
}
[BackgroundDependencyLoader(true)]
2018-08-30 22:04:40 +00:00
private void load(BindableBeatmap beatmap, OsuGame osu, AudioManager audio, Bindable<RulesetInfo> ruleset)
2018-04-13 09:19:50 +00:00
{
Beatmap.BindTo(beatmap);
Ruleset.BindTo(ruleset);
if (osu != null)
2018-04-13 09:19:50 +00:00
{
OverlayActivationMode.BindTo(osu.OverlayActivationMode);
updateOverlayStates = () =>
{
if (HideOverlaysOnEnter)
osu.CloseAllOverlays();
else
osu.Toolbar.State = Visibility.Visible;
};
2018-04-13 09:19:50 +00:00
}
2018-08-30 22:04:40 +00:00
sampleExit = audio.Sample.Get(@"UI/screen-back");
2018-04-13 09:19:50 +00:00
}
public virtual bool OnPressed(GlobalAction action)
2018-04-13 09:19:50 +00:00
{
2019-01-23 11:52:00 +00:00
if (!this.IsCurrentScreen()) return false;
2018-06-27 07:06:26 +00:00
if (action == GlobalAction.Back && AllowBackButton)
2018-04-13 09:19:50 +00:00
{
2019-01-23 11:52:00 +00:00
this.Exit();
return true;
2018-04-13 09:19:50 +00:00
}
return false;
2018-04-13 09:19:50 +00:00
}
public bool OnReleased(GlobalAction action) => action == GlobalAction.Back && AllowBackButton;
2019-01-23 11:52:00 +00:00
public override void OnResuming(IScreen last)
2018-04-13 09:19:50 +00:00
{
sampleExit?.Play();
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);
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);
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;
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 08:14:47 +00:00
logo.BeatMatching = true;
2018-04-13 09:19:50 +00:00
logo.Triangles = true;
logo.Ripple = true;
}
private void applyArrivingDefaults(bool isResuming)
{
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
2018-06-06 07:17:51 +00:00
OverlayActivationMode.Value = InitialOverlayActivationMode;
2018-06-03 10:02:43 +00:00
updateOverlayStates?.Invoke();
2018-04-13 09:19:50 +00:00
}
private void onExitingLogo()
{
2019-01-23 11:52:00 +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-23 11:52:00 +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)
{
}
}
}