2019-07-15 02:46:41 +00:00
|
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
2019-01-24 08:43:03 +00:00
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-09-06 04:15:43 +00:00
|
|
|
|
using osu.Framework.Graphics.Textures;
|
2019-07-02 08:45:46 +00:00
|
|
|
|
using osu.Framework.Graphics.Transforms;
|
|
|
|
|
using osuTK;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Backgrounds
|
|
|
|
|
{
|
2019-07-02 08:45:46 +00:00
|
|
|
|
/// <summary>
|
2019-07-02 08:47:19 +00:00
|
|
|
|
/// A background which offers blurring via a <see cref="BufferedContainer"/> on demand.
|
2019-07-02 08:45:46 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class Background : CompositeDrawable
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
2019-11-27 06:04:09 +00:00
|
|
|
|
private const float blur_scale = 0.5f;
|
|
|
|
|
|
2019-07-15 02:45:15 +00:00
|
|
|
|
public readonly Sprite Sprite;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
|
|
|
|
private readonly string textureName;
|
|
|
|
|
|
2019-07-02 08:45:46 +00:00
|
|
|
|
private BufferedContainer bufferedContainer;
|
|
|
|
|
|
2018-04-13 09:19:50 +00:00
|
|
|
|
public Background(string textureName = @"")
|
|
|
|
|
{
|
|
|
|
|
this.textureName = textureName;
|
|
|
|
|
RelativeSizeAxes = Axes.Both;
|
|
|
|
|
|
2019-07-02 08:45:46 +00:00
|
|
|
|
AddInternal(Sprite = new Sprite
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
FillMode = FillMode.Fill,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
2018-08-30 22:04:40 +00:00
|
|
|
|
private void load(LargeTextureStore textures)
|
2018-04-13 09:19:50 +00:00
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(textureName))
|
2018-08-30 22:04:40 +00:00
|
|
|
|
Sprite.Texture = textures.Get(textureName);
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
2019-07-02 08:45:46 +00:00
|
|
|
|
|
2019-11-28 16:16:26 +00:00
|
|
|
|
public Vector2 BlurSigma => bufferedContainer?.BlurSigma / blur_scale ?? Vector2.Zero;
|
2019-07-02 08:45:46 +00:00
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
2019-07-15 02:46:41 +00:00
|
|
|
|
if (bufferedContainer == null && newBlurSigma != Vector2.Zero)
|
2019-07-02 08:45:46 +00:00
|
|
|
|
{
|
|
|
|
|
RemoveInternal(Sprite);
|
|
|
|
|
|
|
|
|
|
AddInternal(bufferedContainer = new BufferedContainer
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2019-09-04 10:38:12 +00:00
|
|
|
|
CacheDrawnFrameBuffer = true,
|
|
|
|
|
RedrawOnScale = false,
|
2019-07-02 08:45:46 +00:00
|
|
|
|
Child = Sprite
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-27 06:04:09 +00:00
|
|
|
|
if (bufferedContainer != null)
|
|
|
|
|
bufferedContainer.FrameBufferScale = newBlurSigma == Vector2.Zero ? Vector2.One : new Vector2(blur_scale);
|
|
|
|
|
|
|
|
|
|
bufferedContainer?.BlurTo(newBlurSigma * blur_scale, duration, easing);
|
2019-07-02 08:45:46 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|