osu/osu.Game/Overlays/NotificationOverlay.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

277 lines
9.8 KiB
C#
Raw Normal View History

// 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 09:19:50 +00:00
2023-06-25 12:36:21 +00:00
using System;
using System.Collections.Generic;
2017-02-10 07:26:43 +00:00
using System.Linq;
2022-06-16 08:28:08 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
2017-02-10 07:26:43 +00:00
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Framework.Threading;
2022-06-16 08:28:08 +00:00
using osu.Game.Graphics.Containers;
using osu.Game.Overlays.Notifications;
using osu.Game.Resources.Localisation.Web;
2022-08-30 11:57:12 +00:00
using osuTK;
using osuTK.Graphics;
using NotificationsStrings = osu.Game.Localisation.NotificationsStrings;
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
namespace osu.Game.Overlays
{
public partial class NotificationOverlay : OsuFocusedOverlayContainer, INamedOverlayComponent, INotificationOverlay
2017-02-10 07:26:43 +00:00
{
2020-09-03 08:05:45 +00:00
public string IconTexture => "Icons/Hexacons/notification";
public LocalisableString Title => NotificationsStrings.HeaderTitle;
public LocalisableString Description => NotificationsStrings.HeaderDescription;
protected override double PopInOutSampleBalance => OsuGameBase.SFX_STEREO_STRENGTH;
2021-08-06 19:36:40 +00:00
public const float WIDTH = 320;
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
public const float TRANSITION_LENGTH = 600;
2018-04-13 09:19:50 +00:00
public IEnumerable<Notification> AllNotifications =>
IsLoaded ? toastTray.Notifications.Concat(sections.SelectMany(s => s.Notifications)) : Array.Empty<Notification>();
2022-06-16 08:28:08 +00:00
private FlowContainer<NotificationSection> sections = null!;
2018-04-13 09:19:50 +00:00
[Resolved]
2022-06-16 08:28:08 +00:00
private AudioManager audio { get; set; } = null!;
[Resolved]
2023-08-22 06:54:41 +00:00
private OsuGame? game { get; set; }
2022-08-30 08:33:08 +00:00
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider(OverlayColourScheme.Purple);
2022-08-30 11:57:12 +00:00
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos)
{
if (State.Value == Visibility.Visible)
return base.ReceivePositionalInputAt(screenSpacePos);
if (toastTray.IsDisplayingToasts)
return toastTray.ReceivePositionalInputAt(screenSpacePos);
return false;
}
public override bool PropagatePositionalInputSubTree => base.PropagatePositionalInputSubTree || toastTray.IsDisplayingToasts;
private NotificationOverlayToastTray toastTray = null!;
private Container mainContent = null!;
2022-06-16 08:28:08 +00:00
[BackgroundDependencyLoader]
private void load()
2017-02-10 07:26:43 +00:00
{
X = WIDTH;
2021-08-06 19:36:40 +00:00
Width = WIDTH;
2017-02-10 07:26:43 +00:00
RelativeSizeAxes = Axes.Y;
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
Children = new Drawable[]
{
2022-08-30 11:57:12 +00:00
toastTray = new NotificationOverlayToastTray
2017-02-10 07:26:43 +00:00
{
ForwardNotificationToPermanentStore = addPermanently,
2022-08-30 11:57:12 +00:00
Origin = Anchor.TopRight,
2017-02-10 07:26:43 +00:00
},
2022-08-30 11:57:12 +00:00
mainContent = new Container
2017-02-10 07:26:43 +00:00
{
RelativeSizeAxes = Axes.Both,
Masking = true,
EdgeEffect = new EdgeEffectParameters
{
Colour = Color4.Black.Opacity(0),
Type = EdgeEffectType.Shadow,
Radius = 10,
Hollow = true,
},
2022-08-30 11:57:12 +00:00
Children = new Drawable[]
2017-02-10 07:26:43 +00:00
{
2022-08-30 11:57:12 +00:00
new Box
2017-02-10 07:26:43 +00:00
{
2022-08-30 11:57:12 +00:00
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background4,
},
new OsuScrollContainer
{
Masking = true,
RelativeSizeAxes = Axes.Both,
2017-06-08 07:27:35 +00:00
Children = new[]
2017-02-10 07:26:43 +00:00
{
2022-08-30 11:57:12 +00:00
sections = new FillFlowContainer<NotificationSection>
{
Direction = FillDirection.Vertical,
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
Children = new[]
{
new NotificationSection(AccountsStrings.NotificationsTitle, new[] { typeof(SimpleNotification) }),
new NotificationSection(NotificationsStrings.RunningTasks, new[] { typeof(ProgressNotification) }),
2022-08-30 11:57:12 +00:00
}
}
2017-02-10 07:26:43 +00:00
}
}
}
2022-08-30 11:57:12 +00:00
},
2017-02-10 07:26:43 +00:00
};
}
2018-04-13 09:19:50 +00:00
2022-06-16 08:28:08 +00:00
private ScheduledDelegate? notificationsEnabler;
2019-02-28 04:31:40 +00:00
private void updateProcessingMode()
{
bool enabled = OverlayActivationMode.Value != OverlayActivation.Disabled || State.Value == Visibility.Visible;
notificationsEnabler?.Cancel();
if (enabled)
// we want a slight delay before toggling notifications on to avoid the user becoming overwhelmed.
notificationsEnabler = Scheduler.AddDelayed(() => processingPosts = true, State.Value == Visibility.Visible ? 0 : 250);
else
{
processingPosts = false;
toastTray.FlushAllToasts();
}
}
protected override void LoadComplete()
{
base.LoadComplete();
2022-06-16 08:26:43 +00:00
State.BindValueChanged(_ => updateProcessingMode());
OverlayActivationMode.BindValueChanged(_ => updateProcessingMode(), true);
}
public IBindable<int> UnreadCount => unreadCount;
public int ToastCount => toastTray.UnreadCount;
private readonly BindableInt unreadCount = new BindableInt();
2018-04-13 09:19:50 +00:00
2017-03-07 01:59:19 +00:00
private int runningDepth;
2018-04-13 09:19:50 +00:00
private readonly Scheduler postScheduler = new Scheduler();
2018-04-13 09:19:50 +00:00
public override bool IsPresent =>
// Delegate presence as we need to consider the toast tray in addition to the main overlay.
State.Value == Visibility.Visible || mainContent.IsPresent || toastTray.IsPresent || postScheduler.HasPendingTasks;
private bool processingPosts = true;
2018-04-13 09:19:50 +00:00
private double? lastSamplePlayback;
public void Post(Notification notification) => postScheduler.Add(() =>
2017-02-10 07:26:43 +00:00
{
2017-12-25 16:50:05 +00:00
++runningDepth;
2018-04-13 09:19:50 +00:00
Logger.Log($"⚠️ {notification.Text}");
notification.Closed += () => notificationClosed(notification);
2018-04-13 09:19:50 +00:00
2019-02-28 05:35:00 +00:00
if (notification is IHasCompletionTarget hasCompletionTarget)
2017-12-25 16:50:05 +00:00
hasCompletionTarget.CompletionTarget = Post;
2018-04-13 09:19:50 +00:00
playDebouncedSample(notification.PopInSampleName);
if (notification.IsImportant)
{
game?.Window?.Flash();
2023-08-22 06:54:41 +00:00
notification.Closed += () => game?.Window?.CancelFlash();
}
2022-08-30 11:57:12 +00:00
if (State.Value == Visibility.Hidden)
{
notification.IsInToastTray = true;
toastTray.Post(notification);
}
2022-08-30 11:57:12 +00:00
else
addPermanently(notification);
updateCounts();
});
2018-04-13 09:19:50 +00:00
private void addPermanently(Notification notification)
{
notification.IsInToastTray = false;
var ourType = notification.GetType();
int depth = notification.DisplayOnTop ? -runningDepth : runningDepth;
2022-08-30 11:57:12 +00:00
var section = sections.Children.First(s => s.AcceptedNotificationTypes.Any(accept => accept.IsAssignableFrom(ourType)));
2022-08-30 11:57:12 +00:00
section.Add(notification, depth);
updateCounts();
}
2018-04-13 09:19:50 +00:00
protected override void Update()
{
base.Update();
if (processingPosts)
postScheduler.Update();
}
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
protected override void PopIn()
{
2017-07-22 18:50:25 +00:00
this.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint);
2022-08-30 11:57:12 +00:00
mainContent.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);
mainContent.FadeEdgeEffectTo(WaveContainer.SHADOW_OPACITY, WaveContainer.APPEAR_DURATION, Easing.Out);
2022-08-30 11:57:12 +00:00
toastTray.FlushAllToasts();
2017-02-10 07:26:43 +00:00
}
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
protected override void PopOut()
{
base.PopOut();
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
markAllRead();
2018-04-13 09:19:50 +00:00
2021-08-06 19:36:40 +00:00
this.MoveToX(WIDTH, TRANSITION_LENGTH, Easing.OutQuint);
2022-08-30 11:57:12 +00:00
mainContent.FadeTo(0, TRANSITION_LENGTH, Easing.OutQuint);
mainContent.FadeEdgeEffectTo(0, WaveContainer.DISAPPEAR_DURATION, Easing.In);
}
2018-04-13 09:19:50 +00:00
private void notificationClosed(Notification notification) => Schedule(() =>
{
updateCounts();
// this debounce is currently shared between popin/popout sounds, which means one could potentially not play when the user is expecting it.
// popout is constant across all notification types, and should therefore be handled using playback concurrency instead, but seems broken at the moment.
playDebouncedSample(notification.PopOutSampleName);
});
private void playDebouncedSample(string sampleName)
{
if (string.IsNullOrEmpty(sampleName))
return;
if (lastSamplePlayback == null || Time.Current - lastSamplePlayback > OsuGameBase.SAMPLE_DEBOUNCE_TIME)
{
audio.Samples.Get(sampleName)?.Play();
lastSamplePlayback = Time.Current;
}
}
private void markAllRead()
{
sections.Children.ForEach(s => s.MarkAllRead());
toastTray.MarkAllRead();
2017-12-25 16:50:05 +00:00
updateCounts();
}
private void updateCounts()
{
unreadCount.Value = sections.Select(c => c.UnreadCount).Sum() + toastTray.UnreadCount;
}
2017-02-10 07:26:43 +00:00
}
}