2018-01-05 11:21:19 +00:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-02-10 07:26:43 +00:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
2017-06-20 05:54:23 +00:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using osu.Game.Graphics;
|
2017-12-25 15:44:35 +00:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Notifications
|
|
|
|
|
{
|
|
|
|
|
public class ProgressNotification : Notification, IHasCompletionTarget
|
|
|
|
|
{
|
2017-02-12 05:50:42 +00:00
|
|
|
|
public string Text
|
|
|
|
|
{
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-07-28 07:52:29 +00:00
|
|
|
|
Schedule(() => textDrawable.Text = value);
|
2017-02-12 05:50:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-02-10 07:26:43 +00:00
|
|
|
|
|
2017-12-18 09:59:25 +00:00
|
|
|
|
public string CompletionText { get; set; } = "Task has completed!";
|
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public float Progress
|
|
|
|
|
{
|
2017-02-12 10:39:54 +00:00
|
|
|
|
get { return progressBar.Progress; }
|
2017-10-18 01:07:20 +00:00
|
|
|
|
set { Schedule(() => progressBar.Progress = value); }
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-12 05:50:42 +00:00
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
|
|
//we may have received changes before we were displayed.
|
|
|
|
|
State = state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual ProgressNotificationState State
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
get { return state; }
|
|
|
|
|
set
|
|
|
|
|
{
|
2017-10-18 01:07:20 +00:00
|
|
|
|
Schedule(() =>
|
2017-02-12 05:50:42 +00:00
|
|
|
|
{
|
2017-10-18 01:07:20 +00:00
|
|
|
|
bool stateChanged = state != value;
|
|
|
|
|
state = value;
|
|
|
|
|
|
|
|
|
|
if (IsLoaded)
|
2017-02-12 05:50:42 +00:00
|
|
|
|
{
|
2017-10-18 01:07:20 +00:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ProgressNotificationState.Queued:
|
|
|
|
|
Light.Colour = colourQueued;
|
|
|
|
|
Light.Pulsate = false;
|
|
|
|
|
progressBar.Active = false;
|
|
|
|
|
break;
|
|
|
|
|
case ProgressNotificationState.Active:
|
|
|
|
|
Light.Colour = colourActive;
|
|
|
|
|
Light.Pulsate = true;
|
|
|
|
|
progressBar.Active = true;
|
|
|
|
|
break;
|
|
|
|
|
case ProgressNotificationState.Cancelled:
|
|
|
|
|
Light.Colour = colourCancelled;
|
|
|
|
|
Light.Pulsate = false;
|
|
|
|
|
progressBar.Active = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-02-12 05:50:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-18 01:07:20 +00:00
|
|
|
|
if (stateChanged)
|
2017-02-12 05:50:42 +00:00
|
|
|
|
{
|
2017-10-18 01:07:20 +00:00
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case ProgressNotificationState.Completed:
|
|
|
|
|
NotificationContent.MoveToY(-DrawSize.Y / 2, 200, Easing.OutQuint);
|
|
|
|
|
this.FadeOut(200).Finally(d => Completed());
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-02-12 05:50:42 +00:00
|
|
|
|
}
|
2017-10-18 01:07:20 +00:00
|
|
|
|
});
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ProgressNotificationState state;
|
|
|
|
|
|
2017-07-13 06:02:45 +00:00
|
|
|
|
protected virtual Notification CreateCompletionNotification() => new ProgressCompletionNotification
|
2017-02-12 05:50:42 +00:00
|
|
|
|
{
|
|
|
|
|
Activated = CompletionClickAction,
|
2017-12-18 09:59:25 +00:00
|
|
|
|
Text = CompletionText
|
2017-02-12 05:50:42 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
protected virtual void Completed()
|
|
|
|
|
{
|
|
|
|
|
CompletionTarget?.Invoke(CreateCompletionNotification());
|
2017-12-27 04:25:42 +00:00
|
|
|
|
base.Close();
|
2017-02-12 05:50:42 +00:00
|
|
|
|
}
|
2017-02-10 07:26:43 +00:00
|
|
|
|
|
|
|
|
|
public override bool DisplayOnTop => false;
|
|
|
|
|
|
2017-03-23 04:41:50 +00:00
|
|
|
|
private readonly ProgressBar progressBar;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
private Color4 colourQueued;
|
|
|
|
|
private Color4 colourActive;
|
|
|
|
|
private Color4 colourCancelled;
|
|
|
|
|
|
2017-07-28 07:52:29 +00:00
|
|
|
|
private readonly TextFlowContainer textDrawable;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
|
2017-02-12 10:39:54 +00:00
|
|
|
|
public ProgressNotification()
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
IconContent.Add(new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
});
|
|
|
|
|
|
2017-12-25 15:44:35 +00:00
|
|
|
|
Content.Add(textDrawable = new OsuTextFlowContainer(t =>
|
2017-07-28 07:52:29 +00:00
|
|
|
|
{
|
|
|
|
|
t.TextSize = 16;
|
|
|
|
|
})
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
Colour = OsuColour.Gray(128),
|
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
NotificationContent.Add(progressBar = new ProgressBar
|
|
|
|
|
{
|
|
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
State = ProgressNotificationState.Queued;
|
2017-07-28 07:53:49 +00:00
|
|
|
|
|
|
|
|
|
// don't close on click by default.
|
|
|
|
|
Activated = () => false;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-12 10:39:54 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
colourQueued = colours.YellowDark;
|
|
|
|
|
colourActive = colours.Blue;
|
|
|
|
|
colourCancelled = colours.Red;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public override void Close()
|
|
|
|
|
{
|
|
|
|
|
switch (State)
|
|
|
|
|
{
|
|
|
|
|
case ProgressNotificationState.Cancelled:
|
|
|
|
|
base.Close();
|
|
|
|
|
break;
|
|
|
|
|
case ProgressNotificationState.Active:
|
|
|
|
|
case ProgressNotificationState.Queued:
|
2017-08-24 11:15:45 +00:00
|
|
|
|
if (CancelRequested?.Invoke() != false)
|
|
|
|
|
State = ProgressNotificationState.Cancelled;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-24 11:15:45 +00:00
|
|
|
|
public Func<bool> CancelRequested { get; set; }
|
|
|
|
|
|
2017-02-12 05:50:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The function to post completion notifications back to.
|
|
|
|
|
/// </summary>
|
2017-02-10 07:26:43 +00:00
|
|
|
|
public Action<Notification> CompletionTarget { get; set; }
|
|
|
|
|
|
2017-02-12 05:50:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// An action to complete when the completion notification is clicked.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<bool> CompletionClickAction;
|
|
|
|
|
|
2017-03-07 01:59:19 +00:00
|
|
|
|
private class ProgressBar : Container
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
2017-07-28 07:53:37 +00:00
|
|
|
|
private readonly Box box;
|
2017-02-10 07:26:43 +00:00
|
|
|
|
|
|
|
|
|
private Color4 colourActive;
|
|
|
|
|
private Color4 colourInactive;
|
|
|
|
|
|
|
|
|
|
private float progress;
|
|
|
|
|
public float Progress
|
|
|
|
|
{
|
|
|
|
|
get { return progress; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
if (progress == value) return;
|
|
|
|
|
|
|
|
|
|
progress = value;
|
2017-07-22 18:50:25 +00:00
|
|
|
|
box.ResizeTo(new Vector2(progress, 1), 100, Easing.OutQuad);
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool active;
|
|
|
|
|
|
|
|
|
|
public bool Active
|
|
|
|
|
{
|
|
|
|
|
get { return active; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
active = value;
|
2017-07-14 16:18:12 +00:00
|
|
|
|
this.FadeColour(active ? colourActive : colourInactive, 100);
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-28 07:53:37 +00:00
|
|
|
|
public ProgressBar()
|
2017-02-10 07:26:43 +00:00
|
|
|
|
{
|
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
|
|
|
|
box = new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Width = 0,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-07-28 07:53:37 +00:00
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
|
{
|
|
|
|
|
colourActive = colours.Blue;
|
|
|
|
|
Colour = colourInactive = OsuColour.Gray(0.5f);
|
|
|
|
|
Height = 5;
|
|
|
|
|
}
|
2017-02-10 07:26:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum ProgressNotificationState
|
|
|
|
|
{
|
|
|
|
|
Queued,
|
|
|
|
|
Active,
|
|
|
|
|
Completed,
|
|
|
|
|
Cancelled
|
|
|
|
|
}
|
2017-10-18 01:07:20 +00:00
|
|
|
|
}
|