1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-20 16:12:54 +08:00

Rework notifications to be more flexible.

This commit is contained in:
Dean Herbert 2017-02-12 14:50:42 +09:00
parent a14b7eb598
commit ac548dc9ec
4 changed files with 113 additions and 55 deletions

View File

@ -88,13 +88,13 @@ namespace osu.Desktop.VisualTests.Tests
if (n.Progress < 1)
n.Progress += (float)(Time.Elapsed / 2000) * RNG.NextSingle();
else
n.Complete();
n.State = ProgressNotificationState.Completed;
}
}
private void sendProgress2()
{
var n = new ProgressNotification(@"Downloading Haitai...");
var n = new ProgressNotification { Text = @"Downloading Haitai..." };
manager.Post(n);
progressingNotifications.Add(n);
}
@ -103,19 +103,19 @@ namespace osu.Desktop.VisualTests.Tests
private void sendProgress1()
{
var n = new ProgressNotification(@"Uploading to BSS...");
var n = new ProgressNotification { Text = @"Uploading to BSS..." };
manager.Post(n);
progressingNotifications.Add(n);
}
private void sendNotification2()
{
manager.Post(new SimpleNotification(@"You are amazing"));
manager.Post(new SimpleNotification { Text = @"You are amazing" });
}
private void sendNotification1()
{
manager.Post(new SimpleNotification(@"Welcome to osu!. Enjoy your stay!"));
manager.Post(new SimpleNotification { Text = @"Welcome to osu!. Enjoy your stay!" });
}
}
}

View File

@ -1,6 +1,8 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Game.Graphics;
namespace osu.Game.Overlays.Notifications
{
public class ProgressCompletionNotification : SimpleNotification
@ -8,9 +10,9 @@ namespace osu.Game.Overlays.Notifications
private ProgressNotification progressNotification;
public ProgressCompletionNotification(ProgressNotification progressNotification)
: base(@"Task has completed!")
{
this.progressNotification = progressNotification;
Icon = FontAwesome.fa_check;
}
}
}

View File

@ -17,6 +17,16 @@ namespace osu.Game.Overlays.Notifications
public class ProgressNotification : Notification, IHasCompletionTarget
{
private string text;
public string Text
{
get { return text; }
set
{
text = value;
if (IsLoaded)
textDrawable.Text = text;
}
}
private float progress;
public float Progress
@ -24,42 +34,79 @@ namespace osu.Game.Overlays.Notifications
get { return progress; }
set
{
Debug.Assert(state == ProgressNotificationState.Active);
progress = value;
progressBar.Progress = progress;
if (IsLoaded)
progressBar.Progress = progress;
}
}
public ProgressNotificationState State
protected override void LoadComplete()
{
base.LoadComplete();
//we may have received changes before we were displayed.
State = state;
Progress = progress;
}
public virtual ProgressNotificationState State
{
get { return state; }
set
{
bool stateChanged = state != value;
state = value;
switch (state)
if (IsLoaded)
{
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;
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;
}
}
if (stateChanged)
{
switch (state)
{
case ProgressNotificationState.Completed:
NotificationContent.MoveToY(-DrawSize.Y / 2, 200, EasingTypes.OutQuint);
FadeTo(0.01f, 200); //don't completely fade out or our scheduled task won't run.
Delay(100);
Schedule(Completed);
break;
}
}
}
}
private ProgressNotificationState state;
public Action Completed;
protected virtual Notification CreateCompletionNotification() => new ProgressCompletionNotification(this)
{
Activated = CompletionClickAction,
Text = $"Task \"{Text}\" has completed!"
};
protected virtual void Completed()
{
CompletionTarget?.Invoke(CreateCompletionNotification());
}
public override bool DisplayOnTop => false;
@ -68,10 +115,7 @@ namespace osu.Game.Overlays.Notifications
private Color4 colourActive;
private Color4 colourCancelled;
public ProgressNotification(string text)
{
this.text = text;
}
private SpriteText textDrawable;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
@ -85,7 +129,7 @@ namespace osu.Game.Overlays.Notifications
RelativeSizeAxes = Axes.Both,
});
Content.Add(new SpriteText
Content.Add(textDrawable = new SpriteText
{
TextSize = 16,
Colour = OsuColour.Gray(128),
@ -104,23 +148,6 @@ namespace osu.Game.Overlays.Notifications
State = ProgressNotificationState.Queued;
}
public void Complete()
{
Debug.Assert(state != ProgressNotificationState.Completed);
state = ProgressNotificationState.Completed;
NotificationContent.MoveToY(-DrawSize.Y / 2, 200, EasingTypes.OutQuint);
FadeTo(0.01f, 200); //don't completely fade out or our scheduled task won't run.
Delay(100);
Schedule(() =>
{
CompletionTarget?.Invoke(new ProgressCompletionNotification(this));
base.Close();
});
}
public override void Close()
{
switch (State)
@ -135,8 +162,16 @@ namespace osu.Game.Overlays.Notifications
}
}
/// <summary>
/// The function to post completion notifications back to.
/// </summary>
public Action<Notification> CompletionTarget { get; set; }
/// <summary>
/// An action to complete when the completion notification is clicked.
/// </summary>
public Func<bool> CompletionClickAction;
class ProgressBar : Container
{
private Box box;
@ -175,7 +210,7 @@ namespace osu.Game.Overlays.Notifications
{
colourActive = colours.Blue;
Colour = colourInactive = OsuColour.Gray(0.5f);
Height = 5;
Children = new[]

View File

@ -12,12 +12,33 @@ namespace osu.Game.Overlays.Notifications
public class SimpleNotification : Notification
{
private string text;
public SimpleNotification(string text)
public string Text
{
this.text = text;
get { return text; }
set
{
text = value;
if (IsLoaded)
textDrawable.Text = text;
}
}
private FontAwesome icon = FontAwesome.fa_info_circle;
public FontAwesome Icon
{
get { return icon; }
set
{
icon = value;
if (IsLoaded)
iconDrawable.Icon = icon;
}
}
private SpriteText textDrawable;
private TextAwesome iconDrawable;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
@ -28,14 +49,14 @@ namespace osu.Game.Overlays.Notifications
RelativeSizeAxes = Axes.Both,
ColourInfo = ColourInfo.GradientVertical(OsuColour.Gray(0.2f), OsuColour.Gray(0.5f))
},
new TextAwesome
iconDrawable = new TextAwesome
{
Anchor = Anchor.Centre,
Icon = FontAwesome.fa_info_circle,
Icon = icon ,
}
});
Content.Add(new SpriteText
Content.Add(textDrawable = new SpriteText
{
TextSize = 16,
Colour = OsuColour.Gray(128),