osu/osu.Game/Screens/BackgroundScreen.cs

88 lines
2.6 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;
using osu.Framework.Screens;
using osu.Framework.Graphics;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2018-11-20 07:51:59 +00:00
using osuTK;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Screens
{
public abstract class BackgroundScreen : Screen, IEquatable<BackgroundScreen>
{
2019-08-09 11:05:28 +00:00
private readonly bool animateOnEnter;
protected BackgroundScreen(bool animateOnEnter = true)
2019-01-23 11:52:00 +00:00
{
2019-08-09 11:05:28 +00:00
this.animateOnEnter = animateOnEnter;
2019-01-23 11:52:00 +00:00
Anchor = Anchor.Centre;
Origin = Anchor.Centre;
}
2018-04-13 09:19:50 +00:00
public virtual bool Equals(BackgroundScreen other)
{
return other?.GetType() == GetType();
}
private const float transition_length = 500;
private const float x_movement_amount = 50;
2018-10-02 03:02:47 +00:00
protected override bool OnKeyDown(KeyDownEvent e)
2018-04-13 09:19:50 +00:00
{
2020-05-05 01:31:11 +00:00
// we don't want to handle escape key.
2018-04-13 09:19:50 +00:00
return false;
}
/// <summary>
/// Apply arbitrary changes to this background in a thread safe manner.
/// </summary>
/// <param name="action">The operation to perform.</param>
public void ApplyToBackground(Action<BackgroundScreen> action) => Schedule(() => action.Invoke(this));
2018-04-13 09:19:50 +00:00
protected override void Update()
{
base.Update();
2019-01-23 11:52:00 +00:00
Scale = new Vector2(1 + x_movement_amount / DrawSize.X * 2);
2018-04-13 09:19:50 +00:00
}
2019-01-23 11:52:00 +00:00
public override void OnEntering(IScreen last)
2018-04-13 09:19:50 +00:00
{
2019-08-09 11:05:28 +00:00
if (animateOnEnter)
{
this.FadeOut();
this.MoveToX(x_movement_amount);
2018-04-13 09:19:50 +00:00
2019-08-09 11:05:28 +00:00
this.FadeIn(transition_length, Easing.InOutQuart);
this.MoveToX(0, transition_length, Easing.InOutQuart);
}
2018-04-13 09:19:50 +00:00
base.OnEntering(last);
}
2019-01-23 11:52:00 +00:00
public override void OnSuspending(IScreen next)
2018-04-13 09:19:50 +00:00
{
2019-01-23 11:52:00 +00:00
this.MoveToX(-x_movement_amount, transition_length, Easing.InOutQuart);
2018-04-13 09:19:50 +00:00
base.OnSuspending(next);
}
2019-01-23 11:52:00 +00:00
public override bool OnExiting(IScreen next)
2018-04-13 09:19:50 +00:00
{
if (IsLoaded)
{
this.FadeOut(transition_length, Easing.OutExpo);
this.MoveToX(x_movement_amount, transition_length, Easing.OutExpo);
}
2018-04-13 09:19:50 +00:00
return base.OnExiting(next);
}
2019-01-23 11:52:00 +00:00
public override void OnResuming(IScreen last)
2018-04-13 09:19:50 +00:00
{
if (IsLoaded)
this.MoveToX(0, transition_length, Easing.OutExpo);
2018-04-13 09:19:50 +00:00
base.OnResuming(last);
}
}
}