2019-01-24 16:43:03 +08:00
|
|
|
|
// 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
|
|
|
|
|
2022-06-17 15:37:17 +08:00
|
|
|
|
#nullable disable
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
using System;
|
2019-05-28 17:59:21 +08:00
|
|
|
|
using System.Threading;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
using osu.Framework.Graphics;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
using osu.Framework.Graphics.Colour;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-06-20 13:54:23 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2021-10-31 00:50:13 +08:00
|
|
|
|
using osu.Framework.Localisation;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
using osu.Game.Graphics;
|
2017-12-25 23:44:35 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
using osu.Game.Graphics.UserInterface;
|
2018-11-20 15:51:59 +08:00
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
namespace osu.Game.Overlays.Notifications
|
|
|
|
|
{
|
|
|
|
|
public class ProgressNotification : Notification, IHasCompletionTarget
|
|
|
|
|
{
|
2021-10-28 15:34:12 +08:00
|
|
|
|
private const float loading_spinner_size = 22;
|
|
|
|
|
|
2021-11-09 20:34:36 +08:00
|
|
|
|
private LocalisableString text;
|
|
|
|
|
|
2022-01-25 22:38:46 +08:00
|
|
|
|
public override LocalisableString Text
|
2017-02-12 13:50:42 +08:00
|
|
|
|
{
|
2021-11-09 20:34:36 +08:00
|
|
|
|
get => text;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
text = value;
|
|
|
|
|
Schedule(() => textDrawable.Text = text);
|
|
|
|
|
}
|
2017-02-12 13:50:42 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-12-18 17:59:25 +08:00
|
|
|
|
public string CompletionText { get; set; } = "Task has completed!";
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-06-20 18:05:33 +08:00
|
|
|
|
private float progress;
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public float Progress
|
|
|
|
|
{
|
2019-06-20 18:05:33 +08:00
|
|
|
|
get => progress;
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
progress = value;
|
2021-10-14 16:52:19 +08:00
|
|
|
|
Scheduler.AddOnce(updateProgress, progress);
|
2019-06-20 18:05:33 +08:00
|
|
|
|
}
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-14 16:52:19 +08:00
|
|
|
|
private void updateProgress(float progress) => progressBar.Progress = progress;
|
|
|
|
|
|
2017-02-12 13:50:42 +08:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2020-05-05 09:31:11 +08:00
|
|
|
|
// we may have received changes before we were displayed.
|
2019-06-20 18:05:33 +08:00
|
|
|
|
updateState();
|
2017-02-12 13:50:42 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2019-05-28 17:59:21 +08:00
|
|
|
|
private readonly CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
|
|
|
|
|
|
public CancellationToken CancellationToken => cancellationTokenSource.Token;
|
|
|
|
|
|
2019-06-20 18:05:33 +08:00
|
|
|
|
public ProgressNotificationState State
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => state;
|
2019-06-20 18:05:33 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (state == value) return;
|
|
|
|
|
|
|
|
|
|
state = value;
|
|
|
|
|
|
|
|
|
|
if (IsLoaded)
|
|
|
|
|
Schedule(updateState);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateState()
|
|
|
|
|
{
|
2021-10-28 15:34:12 +08:00
|
|
|
|
const double colour_fade_duration = 200;
|
|
|
|
|
|
2019-06-20 18:05:33 +08:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ProgressNotificationState.Queued:
|
|
|
|
|
Light.Colour = colourQueued;
|
|
|
|
|
Light.Pulsate = false;
|
|
|
|
|
progressBar.Active = false;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
|
|
|
|
|
iconBackground.FadeColour(ColourInfo.GradientVertical(colourQueued, colourQueued.Lighten(0.5f)), colour_fade_duration);
|
|
|
|
|
loadingSpinner.Show();
|
2019-06-20 18:05:33 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProgressNotificationState.Active:
|
|
|
|
|
Light.Colour = colourActive;
|
|
|
|
|
Light.Pulsate = true;
|
|
|
|
|
progressBar.Active = true;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
|
|
|
|
|
iconBackground.FadeColour(ColourInfo.GradientVertical(colourActive, colourActive.Lighten(0.5f)), colour_fade_duration);
|
|
|
|
|
loadingSpinner.Show();
|
2019-06-20 18:05:33 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProgressNotificationState.Cancelled:
|
|
|
|
|
cancellationTokenSource.Cancel();
|
|
|
|
|
|
2021-10-28 15:34:12 +08:00
|
|
|
|
iconBackground.FadeColour(ColourInfo.GradientVertical(Color4.Gray, Color4.Gray.Lighten(0.5f)), colour_fade_duration);
|
|
|
|
|
loadingSpinner.Hide();
|
|
|
|
|
|
|
|
|
|
var icon = new SpriteIcon
|
|
|
|
|
{
|
|
|
|
|
Icon = FontAwesome.Solid.Ban,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Size = new Vector2(loading_spinner_size),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
IconContent.Add(icon);
|
|
|
|
|
|
|
|
|
|
icon.FadeInFromZero(200, Easing.OutQuint);
|
|
|
|
|
|
2019-06-20 18:05:33 +08:00
|
|
|
|
Light.Colour = colourCancelled;
|
|
|
|
|
Light.Pulsate = false;
|
|
|
|
|
progressBar.Active = false;
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case ProgressNotificationState.Completed:
|
2021-10-28 15:34:12 +08:00
|
|
|
|
loadingSpinner.Hide();
|
2019-06-20 18:05:33 +08:00
|
|
|
|
NotificationContent.MoveToY(-DrawSize.Y / 2, 200, Easing.OutQuint);
|
|
|
|
|
this.FadeOut(200).Finally(d => Completed());
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
private ProgressNotificationState state;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-07-13 14:02:45 +08:00
|
|
|
|
protected virtual Notification CreateCompletionNotification() => new ProgressCompletionNotification
|
2017-02-12 13:50:42 +08:00
|
|
|
|
{
|
|
|
|
|
Activated = CompletionClickAction,
|
2017-12-18 17:59:25 +08:00
|
|
|
|
Text = CompletionText
|
2017-02-12 13:50:42 +08:00
|
|
|
|
};
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-12 13:50:42 +08:00
|
|
|
|
protected virtual void Completed()
|
|
|
|
|
{
|
|
|
|
|
CompletionTarget?.Invoke(CreateCompletionNotification());
|
2017-12-27 12:25:42 +08:00
|
|
|
|
base.Close();
|
2017-02-12 13:50:42 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public override bool DisplayOnTop => false;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-03-23 12:41:50 +08:00
|
|
|
|
private readonly ProgressBar progressBar;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
private Color4 colourQueued;
|
|
|
|
|
private Color4 colourActive;
|
|
|
|
|
private Color4 colourCancelled;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-28 15:34:12 +08:00
|
|
|
|
private Box iconBackground;
|
|
|
|
|
private LoadingSpinner loadingSpinner;
|
|
|
|
|
|
2017-07-28 15:52:29 +08:00
|
|
|
|
private readonly TextFlowContainer textDrawable;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-12 18:39:54 +08:00
|
|
|
|
public ProgressNotification()
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
2019-02-20 19:52:56 +08:00
|
|
|
|
Content.Add(textDrawable = new OsuTextFlowContainer
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
|
|
|
|
Colour = OsuColour.Gray(128),
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
NotificationContent.Add(progressBar = new ProgressBar
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
});
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-10-28 15:34:12 +08:00
|
|
|
|
// make some extra space for the progress bar.
|
|
|
|
|
IconContent.Margin = new MarginPadding { Bottom = 5 };
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
State = ProgressNotificationState.Queued;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-07-28 15:53:49 +08:00
|
|
|
|
// don't close on click by default.
|
|
|
|
|
Activated = () => false;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-12 18:39:54 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
colourQueued = colours.YellowDark;
|
|
|
|
|
colourActive = colours.Blue;
|
|
|
|
|
colourCancelled = colours.Red;
|
2021-10-28 15:34:12 +08:00
|
|
|
|
|
|
|
|
|
IconContent.AddRange(new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
iconBackground = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = Color4.White,
|
|
|
|
|
},
|
|
|
|
|
loadingSpinner = new LoadingSpinner
|
|
|
|
|
{
|
|
|
|
|
Size = new Vector2(loading_spinner_size),
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-02-12 18:39:54 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2021-09-05 12:22:37 +08:00
|
|
|
|
public override void Close()
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
|
|
|
|
switch (State)
|
|
|
|
|
{
|
|
|
|
|
case ProgressNotificationState.Cancelled:
|
2021-09-05 12:22:37 +08:00
|
|
|
|
base.Close();
|
2017-02-10 15:26:43 +08:00
|
|
|
|
break;
|
2019-04-01 11:44:46 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
case ProgressNotificationState.Active:
|
|
|
|
|
case ProgressNotificationState.Queued:
|
2017-08-24 19:15:45 +08:00
|
|
|
|
if (CancelRequested?.Invoke() != false)
|
|
|
|
|
State = ProgressNotificationState.Cancelled;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-08-24 19:15:45 +08:00
|
|
|
|
public Func<bool> CancelRequested { get; set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-12 13:50:42 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The function to post completion notifications back to.
|
|
|
|
|
/// </summary>
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public Action<Notification> CompletionTarget { get; set; }
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-12 13:50:42 +08:00
|
|
|
|
/// <summary>
|
2018-09-07 15:30:11 +08:00
|
|
|
|
/// An action to complete when the completion notification is clicked. Return true to close.
|
2017-02-12 13:50:42 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<bool> CompletionClickAction;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-03-07 09:59:19 +08:00
|
|
|
|
private class ProgressBar : Container
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
2017-07-28 15:53:37 +08:00
|
|
|
|
private readonly Box box;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
private Color4 colourActive;
|
|
|
|
|
private Color4 colourInactive;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
private float progress;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public float Progress
|
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => progress;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (progress == value) return;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
progress = value;
|
2017-07-23 02:50:25 +08:00
|
|
|
|
box.ResizeTo(new Vector2(progress, 1), 100, Easing.OutQuad);
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
private bool active;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public bool Active
|
|
|
|
|
{
|
2019-02-28 12:58:19 +08:00
|
|
|
|
get => active;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
active = value;
|
2017-07-15 00:18:12 +08:00
|
|
|
|
this.FadeColour(active ? colourActive : colourInactive, 100);
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-07-28 15:53:37 +08:00
|
|
|
|
public ProgressBar()
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
box = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Width = 0,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-07-28 15:53:37 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
colourActive = colours.Blue;
|
|
|
|
|
Colour = colourInactive = OsuColour.Gray(0.5f);
|
|
|
|
|
Height = 5;
|
|
|
|
|
}
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public enum ProgressNotificationState
|
|
|
|
|
{
|
|
|
|
|
Queued,
|
|
|
|
|
Active,
|
|
|
|
|
Completed,
|
|
|
|
|
Cancelled
|
|
|
|
|
}
|
2017-10-18 09:07:20 +08:00
|
|
|
|
}
|