osu/osu.Game/Graphics/UserInterface/LoadingLayer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
3.3 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.
2022-06-17 07:37:17 +00:00
#nullable disable
using System;
using JetBrains.Annotations;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osuTK;
using osuTK.Graphics;
namespace osu.Game.Graphics.UserInterface
{
/// <summary>
2020-02-21 06:35:40 +00:00
/// A layer that will show a loading spinner and completely block input to an area.
/// Also optionally dims target elements.
/// Useful for disabling all elements in a form and showing we are waiting on a response, for instance.
/// </summary>
public partial class LoadingLayer : LoadingSpinner
{
private readonly bool blockInput;
[CanBeNull]
2021-01-05 21:40:16 +00:00
protected Box BackgroundDimLayer { get; }
2020-02-21 06:35:40 +00:00
/// <summary>
/// Construct a new loading spinner.
2020-02-21 06:35:40 +00:00
/// </summary>
/// <param name="dimBackground">Whether the full background area should be dimmed while loading.</param>
2020-02-21 06:35:40 +00:00
/// <param name="withBox">Whether the spinner should have a surrounding black box for visibility.</param>
/// <param name="blockInput">Whether to block input of components behind the loading layer.</param>
public LoadingLayer(bool dimBackground = false, bool withBox = true, bool blockInput = true)
: base(withBox)
{
this.blockInput = blockInput;
RelativeSizeAxes = Axes.Both;
Size = new Vector2(1);
MainContents.RelativeSizeAxes = Axes.None;
if (dimBackground)
{
AddInternal(BackgroundDimLayer = new Box
{
Depth = float.MaxValue,
Colour = Color4.Black,
Alpha = 0,
RelativeSizeAxes = Axes.Both,
});
}
}
2020-02-21 07:31:16 +00:00
public override bool HandleNonPositionalInput => false;
protected override bool Handle(UIEvent e)
{
if (!blockInput)
return false;
switch (e)
{
// blocking scroll can cause weird behaviour when this layer is used within a ScrollContainer.
2022-06-24 12:25:23 +00:00
case ScrollEvent:
return false;
// blocking touch events causes the ISourcedFromTouch versions to not be fired, potentially impeding behaviour of drawables *above* the loading layer that may utilise these.
2020-10-21 15:17:23 +00:00
// note that this will not work well if touch handling elements are beneath this loading layer (something to consider for the future).
2022-06-24 12:25:23 +00:00
case TouchEvent:
return false;
}
return true;
}
protected override void PopIn()
{
BackgroundDimLayer?.FadeTo(0.5f, TRANSITION_DURATION * 2, Easing.OutQuint);
base.PopIn();
}
protected override void PopOut()
{
BackgroundDimLayer?.FadeOut(TRANSITION_DURATION, Easing.OutQuint);
base.PopOut();
}
protected override void Update()
{
base.Update();
MainContents.Size = new Vector2(Math.Clamp(Math.Min(DrawWidth, DrawHeight) * 0.25f, 20, 100));
}
}
}