Implement notifications.

This commit is contained in:
Dean Herbert
2017-02-10 16:26:43 +09:00
parent 60e206e587
commit 8ec927899f
17 changed files with 1033 additions and 9 deletions

View File

@ -0,0 +1,12 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
namespace osu.Game.Overlays.Notifications
{
public interface IHasCompletionTarget
{
Action<Notification> CompletionTarget { get; set; }
}
}

View File

@ -0,0 +1,285 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// 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.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Framework.Input;
using osu.Game.Graphics;
using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Overlays.Notifications
{
public abstract class Notification : Container
{
/// <summary>
/// Use requested close.
/// </summary>
public Action Closed;
/// <summary>
/// Run on user activating the notification. Return true to close.
/// </summary>
public Func<bool> Activated;
/// <summary>
/// Should we show at the top of our section on display?
/// </summary>
public virtual bool DisplayOnTop => true;
protected NotificationLight Light;
private CloseButton closeButton;
protected Container IconContent;
private Container content;
protected override Container<Drawable> Content => content;
protected Container NotificationContent;
private bool read;
public virtual bool Read
{
get { return read; }
set
{
read = value;
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
AddInternal(new Drawable[]
{
Light = new NotificationLight
{
Margin = new MarginPadding { Right = 5 },
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreRight,
},
NotificationContent = new Container
{
CornerRadius = 8,
Masking = true,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White,
},
new Container
{
RelativeSizeAxes = Axes.X,
Padding = new MarginPadding(5),
Height = 60,
Children = new Drawable[]
{
IconContent = new Container
{
Size = new Vector2(40),
Colour = Color4.DarkGray,
Masking = true,
CornerRadius = 5,
},
content = new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Top = 5,
Left = 45,
Right = 30
},
}
}
},
closeButton = new CloseButton
{
Alpha = 0,
Action = Close,
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding
{
Right = 5
},
}
}
}
});
}
protected override bool OnHover(InputState state)
{
closeButton.FadeIn(75);
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
closeButton.FadeOut(75);
base.OnHoverLost(state);
}
protected override bool OnClick(InputState state)
{
if (Activated?.Invoke() ?? true)
Close();
return true;
}
protected override void LoadComplete()
{
base.LoadComplete();
FadeInFromZero(200);
NotificationContent.MoveToX(DrawSize.X);
NotificationContent.MoveToX(0, 500, EasingTypes.OutQuint);
}
private bool wasClosed;
public virtual void Close()
{
if (wasClosed) return;
wasClosed = true;
Closed?.Invoke();
FadeOut(100);
Expire();
}
class CloseButton : ClickableContainer
{
private Color4 hoverColour;
public CloseButton()
{
Colour = OsuColour.Gray(0.2f);
AutoSizeAxes = Axes.Both;
Children = new[]
{
new TextAwesome
{
Anchor = Anchor.Centre,
Icon = FontAwesome.fa_times_circle,
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
hoverColour = colours.Yellow;
}
protected override bool OnHover(InputState state)
{
FadeColour(hoverColour, 200);
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
FadeColour(OsuColour.Gray(0.2f), 200);
base.OnHoverLost(state);
}
}
public class NotificationLight : Container
{
private bool pulsate;
private Container pulsateLayer;
public bool Pulsate
{
get { return pulsate; }
set
{
pulsate = value;
pulsateLayer.ClearTransformations();
pulsateLayer.Alpha = 1;
if (pulsate)
{
const float length = 1000;
pulsateLayer.Transforms.Add(new TransformAlpha
{
StartTime = Time.Current,
EndTime = Time.Current + length,
StartValue = 1,
EndValue = 0.4f,
Easing = EasingTypes.In
});
pulsateLayer.Transforms.Add(new TransformAlpha
{
StartTime = Time.Current + length,
EndTime = Time.Current + length * 2,
StartValue = 0.4f,
EndValue = 1,
Easing = EasingTypes.Out
});
//todo: figure why we can't add arbitrary delays at the end of loop.
pulsateLayer.Loop(length * 2);
}
}
}
public new SRGBColour Colour
{
set
{
base.Colour = value;
pulsateLayer.EdgeEffect = new EdgeEffect
{
Colour = ((Color4)value).Opacity(0.5f), //todo: avoid cast
Type = EdgeEffectType.Glow,
Radius = 12,
Roundness = 12,
};
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
Size = new Vector2(6, 15);
Children = new[]
{
pulsateLayer = new CircularContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Masking = true,
RelativeSizeAxes = Axes.Both,
Children = new[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
},
}
}
};
}
}
}
}

View File

@ -0,0 +1,163 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Transformations;
using osu.Game.Graphics;
using osu.Game.Graphics.Sprites;
using OpenTK;
namespace osu.Game.Overlays.Notifications
{
public class NotificationSection : FlowContainer
{
private OsuSpriteText titleText;
private OsuSpriteText countText;
private ClearAllButton clearButton;
private FlowContainer<Notification> notifications;
public void Add(Notification notification)
{
notifications.Add(notification);
}
public IEnumerable<Type> AcceptTypes;
private string clearText;
public string ClearText
{
get { return clearText; }
set
{
clearText = value;
if (clearButton != null) clearButton.Text = clearText;
}
}
private string title;
public string Title
{
get { return title; }
set
{
title = value;
if (titleText != null) titleText.Text = title.ToUpper();
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Direction = FlowDirection.VerticalOnly;
Padding = new MarginPadding
{
Top = 10,
Bottom = 5,
Right = 20,
Left = 20,
};
AddInternal(new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
clearButton = new ClearAllButton
{
Text = clearText,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Action = clearAll
},
new FlowContainer
{
Margin = new MarginPadding
{
Bottom = 5
},
Spacing = new Vector2(5, 0),
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
titleText = new OsuSpriteText
{
Text = title.ToUpper(),
Font = @"Exo2.0-Black",
},
countText = new OsuSpriteText
{
Text = "3",
Colour = colours.Yellow,
Font = @"Exo2.0-Black",
},
}
},
},
},
notifications = new FlowContainer<Notification>
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
LayoutDuration = 150,
LayoutEasing = EasingTypes.OutQuart,
Spacing = new Vector2(3),
}
});
}
private void clearAll()
{
notifications.Children.ForEach(c => c.Close());
}
protected override void Update()
{
base.Update();
countText.Text = notifications.Children.Count(c => c.Alpha > 0.99f).ToString();
}
class ClearAllButton : ClickableContainer
{
private OsuSpriteText text;
public ClearAllButton()
{
AutoSizeAxes = Axes.Both;
Children = new[]
{
text = new OsuSpriteText()
};
}
public string Text
{
get { return text.Text; }
set { text.Text = value.ToUpper(); }
}
}
public void MarkAllRead()
{
notifications.Children.ForEach(n => n.Read = true);
}
}
}

View File

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

View File

@ -0,0 +1,198 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Diagnostics;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Transformations;
using osu.Game.Graphics;
using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Overlays.Notifications
{
public class ProgressNotification : Notification, IHasCompletionTarget
{
private string text;
private float progress;
public float Progress
{
get { return progress; }
set
{
Debug.Assert(state == ProgressNotificationState.Active);
progress = value;
progressBar.Progress = progress;
}
}
public ProgressNotificationState State
{
get { return state; }
set
{
state = value;
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;
}
}
}
private ProgressNotificationState state;
public Action Completed;
public override bool DisplayOnTop => false;
private ProgressBar progressBar;
private Color4 colourQueued;
private Color4 colourActive;
private Color4 colourCancelled;
public ProgressNotification(string text)
{
this.text = text;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
colourQueued = colours.YellowDark;
colourActive = colours.Blue;
colourCancelled = colours.Red;
IconContent.Add(new Box
{
RelativeSizeAxes = Axes.Both,
});
Content.Add(new SpriteText
{
TextSize = 16,
Colour = OsuColour.Gray(128),
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Text = text
});
NotificationContent.Add(progressBar = new ProgressBar
{
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
});
State = ProgressNotificationState.Queued;
}
public void Complete()
{
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)
{
case ProgressNotificationState.Cancelled:
base.Close();
break;
case ProgressNotificationState.Active:
case ProgressNotificationState.Queued:
State = ProgressNotificationState.Cancelled;
break;
}
}
public Action<Notification> CompletionTarget { get; set; }
class ProgressBar : Container
{
private Box box;
private Color4 colourActive;
private Color4 colourInactive;
private float progress;
public float Progress
{
get { return progress; }
set
{
if (progress == value) return;
progress = value;
box.ResizeTo(new Vector2(progress, 1), 100, EasingTypes.OutQuad);
}
}
private bool active;
public bool Active
{
get { return active; }
set
{
active = value;
FadeColour(active ? colourActive : colourInactive, 100);
}
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
colourActive = colours.Blue;
Colour = colourInactive = OsuColour.Gray(0.5f);
Height = 5;
Children = new[]
{
box = new Box
{
RelativeSizeAxes = Axes.Both,
Width = 0,
}
};
}
}
}
public enum ProgressNotificationState
{
Queued,
Active,
Completed,
Cancelled
}
}

View File

@ -0,0 +1,66 @@
// 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.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Sprites;
using osu.Game.Graphics;
namespace osu.Game.Overlays.Notifications
{
public class SimpleNotification : Notification
{
private string text;
public SimpleNotification(string text)
{
this.text = text;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
IconContent.Add(new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
ColourInfo = ColourInfo.GradientVertical(OsuColour.Gray(0.2f), OsuColour.Gray(0.5f))
},
new TextAwesome
{
Anchor = Anchor.Centre,
Icon = FontAwesome.fa_info_circle,
}
});
Content.Add(new SpriteText
{
TextSize = 16,
Colour = OsuColour.Gray(128),
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Text = text
});
Light.Colour = colours.Green;
}
public override bool Read
{
get
{
return base.Read;
}
set
{
if (base.Read = value)
Light.FadeOut(100);
else
Light.FadeIn(100);
}
}
}
}