// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using osu.Framework.Allocation; using osu.Framework.Configuration; using osu.Framework.Screens; using osu.Game.Beatmaps; using osu.Game.Database; using osu.Game.Graphics.Containers; using OpenTK; using osu.Framework.Audio.Sample; using osu.Framework.Audio; namespace osu.Game.Screens { public abstract class OsuScreen : Screen { internal BackgroundScreen Background { get; private set; } /// /// 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. /// protected virtual BackgroundScreen CreateBackground() => null; internal virtual bool ShowOverlays => true; protected new OsuGameBase Game => base.Game as OsuGameBase; internal virtual bool HasLocalCursorDisplayed => false; /// /// 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. /// internal virtual bool AllowBeatmapRulesetChange => true; private readonly Bindable beatmap = new Bindable(); private readonly Bindable ruleset = new Bindable(); private SampleChannel sampleExit; public WorkingBeatmap Beatmap { get { return beatmap.Value; } set { beatmap.Value = value ?? new DummyWorkingBeatmap(); } } [BackgroundDependencyLoader(permitNulls: true)] private void load(OsuGameBase game, OsuGame osuGame, AudioManager audio) { if (game != null) { //if we were given a beatmap at ctor time, we want to pass this on to the game-wide beatmap. var localMap = beatmap.Value; beatmap.BindTo(game.Beatmap); if (localMap != null) beatmap.Value = localMap; } if (osuGame != null) ruleset.BindTo(osuGame.Ruleset); sampleExit = audio.Sample.Get(@"UI/melodic-1"); } protected override void LoadComplete() { base.LoadComplete(); beatmap.ValueChanged += OnBeatmapChanged; } /// /// The global Beatmap was changed. /// protected virtual void OnBeatmapChanged(WorkingBeatmap beatmap) { } protected override void Update() { if (!IsCurrentScreen) return; if (ParentScreen != null) { // we only want to apply these restrictions when we are inside a screen stack. // the use case for not applying is in visual/unit tests. ruleset.Disabled = !AllowBeatmapRulesetChange; beatmap.Disabled = !AllowBeatmapRulesetChange; } } protected override void OnResuming(Screen last) { base.OnResuming(last); sampleExit?.Play(); } protected override void OnEntering(Screen last) { OsuScreen lastOsu = last as OsuScreen; BackgroundScreen bg = CreateBackground(); OnBeatmapChanged(Beatmap); 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 { Depth = float.MaxValue, Children = new[] { Background = bg } }); } base.OnEntering(last); } protected override bool OnExiting(Screen next) { OsuScreen nextOsu = next as OsuScreen; if (Background != null && !Background.Equals(nextOsu?.Background)) { if (nextOsu != null) //We need to use MakeCurrent in case we are jumping up multiple game screens. nextOsu.Background?.MakeCurrent(); else Background.Exit(); } if (base.OnExiting(next)) return true; // while this is not necessary as we are constructing our own bindable, there are cases where // the GC doesn't run as fast as expected and this is triggered post-exit. // added to resolve https://github.com/ppy/osu/issues/829 beatmap.ValueChanged -= OnBeatmapChanged; return false; } } }