1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 11:28:00 +08:00
osu-lazer/osu.Game/GameModes/OsuGameMode.cs

150 lines
4.3 KiB
C#
Raw Normal View History

2016-10-05 15:35:10 +08:00
//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>.
//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
2016-10-28 18:55:48 +08:00
using System.Diagnostics;
2016-10-05 15:35:10 +08:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using osu.Framework;
2016-11-11 06:40:42 +08:00
using osu.Framework.Allocation;
2016-10-28 18:55:48 +08:00
using osu.Framework.Configuration;
2016-10-05 15:35:10 +08:00
using osu.Framework.GameModes;
using osu.Framework.Graphics.Containers;
2016-10-28 18:55:48 +08:00
using osu.Game.Beatmaps;
2016-10-05 15:35:10 +08:00
using osu.Game.Graphics.Background;
2016-10-05 19:03:52 +08:00
using osu.Game.Graphics.Containers;
2016-10-05 15:35:10 +08:00
namespace osu.Game.GameModes
{
2016-10-28 18:55:48 +08:00
public abstract class OsuGameMode : GameMode
2016-10-05 15:35:10 +08:00
{
internal BackgroundMode Background { get; private set; }
/// <summary>
/// Override to create a BackgroundMode for the current GameMode.
/// Note that the instance created may not be the used instance if it matches the BackgroundMode equality clause.
/// </summary>
protected virtual BackgroundMode CreateBackground() => null;
internal virtual bool ShowToolbar => true;
protected new OsuGameBase Game => base.Game as OsuGameBase;
protected float ToolbarPadding => ShowToolbar ? (Game as OsuGame)?.Toolbar.DrawHeight ?? 0 : 0;
2016-10-28 18:55:48 +08:00
private bool boundToBeatmap;
private Bindable<WorkingBeatmap> beatmap;
public WorkingBeatmap Beatmap
{
get
{
bindBeatmap();
return beatmap.Value;
}
set
{
bindBeatmap();
beatmap.Value = value;
}
}
private void bindBeatmap()
{
if (beatmap == null)
beatmap = new Bindable<WorkingBeatmap>();
if (!boundToBeatmap)
{
beatmap.ValueChanged += beatmap_ValueChanged;
boundToBeatmap = true;
}
}
protected override void Dispose(bool isDisposing)
{
if (boundToBeatmap)
beatmap.ValueChanged -= beatmap_ValueChanged;
base.Dispose(isDisposing);
}
private void beatmap_ValueChanged(object sender, EventArgs e)
{
OnBeatmapChanged(beatmap.Value);
}
[BackgroundDependencyLoader(permitNulls: true)]
private void load(OsuGameBase game)
{
if (beatmap == null)
beatmap = game?.Beatmap;
}
2016-10-28 18:55:48 +08:00
public override bool Push(GameMode mode)
{
OsuGameMode nextOsu = mode as OsuGameMode;
if (nextOsu != null)
{
nextOsu.beatmap = beatmap;
}
return base.Push(mode);
}
protected virtual void OnBeatmapChanged(WorkingBeatmap beatmap)
{
}
2016-10-05 15:35:10 +08:00
protected override void OnEntering(GameMode last)
{
OsuGameMode lastOsu = last as OsuGameMode;
BackgroundMode bg = CreateBackground();
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)
{
AddInternal(new ParallaxContainer
2016-10-05 19:03:52 +08:00
{
Depth = float.MinValue,
Children = new[]
{
Background = bg
}
});
2016-10-05 15:35:10 +08:00
}
base.OnEntering(last);
}
protected override bool OnExiting(GameMode next)
2016-10-05 15:35:10 +08:00
{
OsuGameMode nextOsu = next as OsuGameMode;
if (Background != null && !Background.Equals(nextOsu?.Background))
{
if (nextOsu != null)
//We need to use MakeCurrent in case we are jumping up multiple game modes.
nextOsu.Background.MakeCurrent();
else
Background.Exit();
}
2016-10-05 15:35:10 +08:00
return base.OnExiting(next);
2016-10-05 15:35:10 +08:00
}
}
}