1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-18 19:32:58 +08:00
osu-lazer/osu.Game/Graphics/Backgrounds/Background.cs

85 lines
2.8 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 17:19:50 +08:00
using System;
2018-04-13 17:19:50 +08:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
using osu.Framework.Graphics.Transforms;
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.Backgrounds
{
/// <summary>
2019-07-02 16:47:19 +08:00
/// A background which offers blurring via a <see cref="BufferedContainer"/> on demand.
/// </summary>
public class Background : CompositeDrawable, IEquatable<Background>
2018-04-13 17:19:50 +08:00
{
2019-11-27 14:04:09 +08:00
private const float blur_scale = 0.5f;
2019-07-15 10:45:15 +08:00
public readonly Sprite Sprite;
2018-04-13 17:19:50 +08:00
private readonly string textureName;
private BufferedContainer bufferedContainer;
2018-04-13 17:19:50 +08:00
public Background(string textureName = @"")
{
this.textureName = textureName;
RelativeSizeAxes = Axes.Both;
AddInternal(Sprite = new Sprite
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.Both,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fill,
});
}
[BackgroundDependencyLoader]
2018-08-31 06:04:40 +08:00
private void load(LargeTextureStore textures)
2018-04-13 17:19:50 +08:00
{
if (!string.IsNullOrEmpty(textureName))
2018-08-31 06:04:40 +08:00
Sprite.Texture = textures.Get(textureName);
2018-04-13 17:19:50 +08:00
}
2019-11-29 00:16:26 +08:00
public Vector2 BlurSigma => bufferedContainer?.BlurSigma / blur_scale ?? Vector2.Zero;
/// <summary>
/// Smoothly adjusts <see cref="IBufferedContainer.BlurSigma"/> over time.
/// </summary>
/// <returns>A <see cref="TransformSequence{T}"/> to which further transforms can be added.</returns>
public void BlurTo(Vector2 newBlurSigma, double duration = 0, Easing easing = Easing.None)
{
if (bufferedContainer == null && newBlurSigma != Vector2.Zero)
{
RemoveInternal(Sprite);
AddInternal(bufferedContainer = new BufferedContainer(cachedFrameBuffer: true)
{
RelativeSizeAxes = Axes.Both,
RedrawOnScale = false,
Child = Sprite
});
}
2019-11-27 14:04:09 +08:00
if (bufferedContainer != null)
bufferedContainer.FrameBufferScale = newBlurSigma == Vector2.Zero ? Vector2.One : new Vector2(blur_scale);
bufferedContainer?.BlurTo(newBlurSigma * blur_scale, duration, easing);
}
public virtual bool Equals(Background other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return other.GetType() == GetType()
&& other.textureName == textureName;
}
2018-04-13 17:19:50 +08:00
}
}