2017-02-10 15:26:43 +08:00
|
|
|
|
// 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.Linq;
|
|
|
|
|
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
|
using OpenTK.Graphics;
|
2017-06-20 13:54:23 +08:00
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2017-06-29 01:18:12 +08:00
|
|
|
|
using osu.Game.Graphics.Containers;
|
2017-12-23 21:33:43 +08:00
|
|
|
|
using System;
|
2017-12-25 17:52:09 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
|
using osu.Framework.Threading;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays
|
|
|
|
|
{
|
2017-07-28 13:51:49 +08:00
|
|
|
|
public class NotificationOverlay : OsuFocusedOverlayContainer
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
|
|
|
|
private const float width = 320;
|
|
|
|
|
|
|
|
|
|
public const float TRANSITION_LENGTH = 600;
|
|
|
|
|
|
2017-12-25 17:52:09 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Whether posted notifications should be processed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public readonly BindableBool Enabled = new BindableBool(true);
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
private FlowContainer<NotificationSection> sections;
|
|
|
|
|
|
2017-12-23 21:33:43 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Provide a source for the toolbar height.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public Func<float> GetToolbarHeight;
|
|
|
|
|
|
2017-12-25 17:52:09 +08:00
|
|
|
|
public NotificationOverlay()
|
|
|
|
|
{
|
|
|
|
|
ScheduledDelegate notificationsEnabler = null;
|
|
|
|
|
Enabled.ValueChanged += v =>
|
|
|
|
|
{
|
|
|
|
|
if (!IsLoaded)
|
|
|
|
|
{
|
|
|
|
|
processingPosts = v;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notificationsEnabler?.Cancel();
|
|
|
|
|
|
|
|
|
|
if (v)
|
|
|
|
|
// we want a slight delay before toggling notifications on to avoid the user becoming overwhelmed.
|
|
|
|
|
notificationsEnabler = Scheduler.AddDelayed(() => processingPosts = true, 1000);
|
|
|
|
|
else
|
|
|
|
|
processingPosts = false;
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-22 18:51:42 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
2017-02-27 22:32:32 +08:00
|
|
|
|
private void load()
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
|
|
|
|
Width = width;
|
|
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
|
|
2017-07-28 15:54:29 +08:00
|
|
|
|
AlwaysPresent = true;
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new Box
|
|
|
|
|
{
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Colour = Color4.Black,
|
2017-12-23 21:33:43 +08:00
|
|
|
|
Alpha = 0.6f
|
2017-02-10 15:26:43 +08:00
|
|
|
|
},
|
2017-12-23 21:33:43 +08:00
|
|
|
|
new OsuScrollContainer
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
2017-12-23 21:33:43 +08:00
|
|
|
|
Masking = true,
|
2017-02-18 03:08:28 +08:00
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
2017-02-10 15:26:43 +08:00
|
|
|
|
Children = new[]
|
|
|
|
|
{
|
2017-03-02 02:33:01 +08:00
|
|
|
|
sections = new FillFlowContainer<NotificationSection>
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
2017-03-04 18:00:17 +08:00
|
|
|
|
Direction = FillDirection.Vertical,
|
2017-02-10 15:26:43 +08:00
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2017-06-08 15:27:35 +08:00
|
|
|
|
Children = new[]
|
2017-02-10 15:26:43 +08:00
|
|
|
|
{
|
|
|
|
|
new NotificationSection
|
|
|
|
|
{
|
|
|
|
|
Title = @"Notifications",
|
|
|
|
|
ClearText = @"Clear All",
|
2017-12-23 21:33:43 +08:00
|
|
|
|
AcceptTypes = new[] { typeof(SimpleNotification) }
|
2017-02-10 15:26:43 +08:00
|
|
|
|
},
|
|
|
|
|
new NotificationSection
|
|
|
|
|
{
|
|
|
|
|
Title = @"Running Tasks",
|
|
|
|
|
ClearText = @"Cancel All",
|
2017-12-23 21:33:43 +08:00
|
|
|
|
AcceptTypes = new[] { typeof(ProgressNotification) }
|
|
|
|
|
}
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 09:59:19 +08:00
|
|
|
|
private int runningDepth;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
|
2017-08-22 18:51:42 +08:00
|
|
|
|
private void notificationClosed()
|
|
|
|
|
{
|
|
|
|
|
// hide ourselves if all notifications have been dismissed.
|
2017-08-23 11:47:20 +08:00
|
|
|
|
if (sections.Select(c => c.DisplayedCount).Sum() == 0)
|
2017-08-22 18:51:42 +08:00
|
|
|
|
State = Visibility.Hidden;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-25 17:52:09 +08:00
|
|
|
|
private readonly Scheduler postScheduler = new Scheduler();
|
|
|
|
|
|
|
|
|
|
private bool processingPosts = true;
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
public void Post(Notification notification)
|
|
|
|
|
{
|
2017-12-25 17:52:09 +08:00
|
|
|
|
postScheduler.Add(() =>
|
2017-07-28 14:03:08 +08:00
|
|
|
|
{
|
|
|
|
|
State = Visibility.Visible;
|
2017-02-12 13:50:02 +08:00
|
|
|
|
|
2017-07-28 14:03:08 +08:00
|
|
|
|
++runningDepth;
|
|
|
|
|
notification.Depth = notification.DisplayOnTop ? runningDepth : -runningDepth;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
|
2017-08-22 18:51:42 +08:00
|
|
|
|
notification.Closed += notificationClosed;
|
|
|
|
|
|
2017-07-28 14:03:08 +08:00
|
|
|
|
var hasCompletionTarget = notification as IHasCompletionTarget;
|
|
|
|
|
if (hasCompletionTarget != null)
|
|
|
|
|
hasCompletionTarget.CompletionTarget = Post;
|
2017-02-10 15:26:43 +08:00
|
|
|
|
|
2017-07-28 14:03:08 +08:00
|
|
|
|
var ourType = notification.GetType();
|
|
|
|
|
sections.Children.FirstOrDefault(s => s.AcceptTypes.Any(accept => accept.IsAssignableFrom(ourType)))?.Add(notification);
|
|
|
|
|
});
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-25 17:52:09 +08:00
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
if (processingPosts)
|
|
|
|
|
postScheduler.Update();
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-10 15:26:43 +08:00
|
|
|
|
protected override void PopIn()
|
|
|
|
|
{
|
|
|
|
|
base.PopIn();
|
|
|
|
|
|
2017-07-23 02:50:25 +08:00
|
|
|
|
this.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint);
|
2017-12-23 21:33:43 +08:00
|
|
|
|
this.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void PopOut()
|
|
|
|
|
{
|
|
|
|
|
base.PopOut();
|
|
|
|
|
|
|
|
|
|
markAllRead();
|
|
|
|
|
|
2017-07-23 02:50:25 +08:00
|
|
|
|
this.MoveToX(width, TRANSITION_LENGTH, Easing.OutQuint);
|
2017-12-23 21:33:43 +08:00
|
|
|
|
this.FadeTo(0, TRANSITION_LENGTH, Easing.OutQuint);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void markAllRead()
|
|
|
|
|
{
|
|
|
|
|
sections.Children.ForEach(s => s.MarkAllRead());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void UpdateAfterChildren()
|
|
|
|
|
{
|
|
|
|
|
base.UpdateAfterChildren();
|
|
|
|
|
|
|
|
|
|
Padding = new MarginPadding { Top = GetToolbarHeight?.Invoke() ?? 0 };
|
2017-02-10 15:26:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-07-28 13:51:49 +08:00
|
|
|
|
}
|