1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 22:22:55 +08:00
osu-lazer/osu.Game/Overlays/NotificationOverlay.cs

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

203 lines
7.0 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 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
using System.Linq;
2022-06-16 16:28:08 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Bindables;
2017-02-10 15:26:43 +08:00
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Localisation;
using osu.Framework.Logging;
using osu.Framework.Threading;
using osu.Game.Graphics;
2022-06-16 16:28:08 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Overlays.Notifications;
using osu.Game.Resources.Localisation.Web;
using NotificationsStrings = osu.Game.Localisation.NotificationsStrings;
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
namespace osu.Game.Overlays
{
public class NotificationOverlay : OsuFocusedOverlayContainer, INamedOverlayComponent, INotificationOverlay
2017-02-10 15:26:43 +08:00
{
2020-09-03 16:05:45 +08:00
public string IconTexture => "Icons/Hexacons/notification";
public LocalisableString Title => NotificationsStrings.HeaderTitle;
public LocalisableString Description => NotificationsStrings.HeaderDescription;
2021-08-07 03:36:40 +08:00
public const float WIDTH = 320;
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
public const float TRANSITION_LENGTH = 600;
2018-04-13 17:19:50 +08:00
2022-06-16 16:28:08 +08:00
private FlowContainer<NotificationSection> sections = null!;
2018-04-13 17:19:50 +08:00
[Resolved]
2022-06-16 16:28:08 +08:00
private AudioManager audio { get; set; } = null!;
2022-06-16 16:26:43 +08:00
private readonly IBindable<Visibility> firstRunSetupVisibility = new Bindable<Visibility>();
2022-06-16 16:28:08 +08:00
[BackgroundDependencyLoader]
private void load(FirstRunSetupOverlay? firstRunSetup)
2017-02-10 15:26:43 +08:00
{
X = WIDTH;
2021-08-07 03:36:40 +08:00
Width = WIDTH;
2017-02-10 15:26:43 +08:00
RelativeSizeAxes = Axes.Y;
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = OsuColour.Gray(0.05f),
2017-02-10 15:26:43 +08:00
},
new OsuScrollContainer
2017-02-10 15:26:43 +08:00
{
Masking = true,
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(AccountsStrings.NotificationsTitle, "Clear All")
2017-02-10 15:26:43 +08:00
{
AcceptTypes = new[] { typeof(SimpleNotification) }
2017-02-10 15:26:43 +08:00
},
new NotificationSection(@"Running Tasks", @"Cancel All")
2017-02-10 15:26:43 +08:00
{
AcceptTypes = new[] { typeof(ProgressNotification) }
}
2017-02-10 15:26:43 +08:00
}
}
}
}
};
2022-06-16 16:26:43 +08:00
if (firstRunSetup != null)
firstRunSetupVisibility.BindTo(firstRunSetup.State);
2017-02-10 15:26:43 +08:00
}
2018-04-13 17:19:50 +08:00
2022-06-16 16:28:08 +08:00
private ScheduledDelegate? notificationsEnabler;
2019-02-28 12:31:40 +08:00
private void updateProcessingMode()
{
2022-06-16 16:28:08 +08:00
bool enabled = (OverlayActivationMode.Value == OverlayActivation.All && firstRunSetupVisibility.Value != Visibility.Visible) || 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 : 1000);
else
processingPosts = false;
}
protected override void LoadComplete()
{
base.LoadComplete();
2022-06-16 16:26:43 +08:00
State.BindValueChanged(_ => updateProcessingMode());
firstRunSetupVisibility.BindValueChanged(_ => updateProcessingMode());
OverlayActivationMode.BindValueChanged(_ => updateProcessingMode(), true);
}
public IBindable<int> UnreadCount => unreadCount;
private readonly BindableInt unreadCount = new BindableInt();
2018-04-13 17:19:50 +08:00
2017-03-07 09:59:19 +08:00
private int runningDepth;
2018-04-13 17:19:50 +08:00
private readonly Scheduler postScheduler = new Scheduler();
2018-04-13 17:19:50 +08:00
public override bool IsPresent => base.IsPresent || postScheduler.HasPendingTasks;
private bool processingPosts = true;
2018-04-13 17:19:50 +08:00
private double? lastSamplePlayback;
public void Post(Notification notification) => postScheduler.Add(() =>
2017-02-10 15:26:43 +08:00
{
2017-12-26 00:50:05 +08:00
++runningDepth;
2018-04-13 17:19:50 +08:00
Logger.Log($"⚠️ {notification.Text}");
2017-12-26 00:50:05 +08:00
notification.Closed += notificationClosed;
2018-04-13 17:19:50 +08:00
2019-02-28 13:35:00 +08:00
if (notification is IHasCompletionTarget hasCompletionTarget)
2017-12-26 00:50:05 +08:00
hasCompletionTarget.CompletionTarget = Post;
2018-04-13 17:19:50 +08:00
2017-12-26 00:50:05 +08:00
var ourType = notification.GetType();
2018-04-13 17:19:50 +08:00
var section = sections.Children.FirstOrDefault(s => s.AcceptTypes.Any(accept => accept.IsAssignableFrom(ourType)));
section?.Add(notification, notification.DisplayOnTop ? -runningDepth : runningDepth);
2018-04-13 17:19:50 +08:00
if (notification.IsImportant)
Show();
2018-04-13 17:19:50 +08:00
2017-12-26 00:50:05 +08:00
updateCounts();
playDebouncedSample(notification.PopInSampleName);
2017-12-26 00:50:05 +08:00
});
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
if (processingPosts)
postScheduler.Update();
}
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
protected override void PopIn()
{
base.PopIn();
2018-04-13 17:19:50 +08:00
2017-07-23 02:50:25 +08:00
this.MoveToX(0, TRANSITION_LENGTH, Easing.OutQuint);
this.FadeTo(1, TRANSITION_LENGTH, Easing.OutQuint);
2017-02-10 15:26:43 +08:00
}
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
protected override void PopOut()
{
base.PopOut();
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
markAllRead();
2018-04-13 17:19:50 +08:00
2021-08-07 03:36:40 +08:00
this.MoveToX(WIDTH, TRANSITION_LENGTH, Easing.OutQuint);
this.FadeTo(0, TRANSITION_LENGTH, Easing.OutQuint);
}
2018-04-13 17:19:50 +08:00
private void notificationClosed()
{
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("UI/overlay-pop-out");
}
private void playDebouncedSample(string sampleName)
{
if (lastSamplePlayback == null || Time.Current - lastSamplePlayback > OsuGameBase.SAMPLE_DEBOUNCE_TIME)
{
audio.Samples.Get(sampleName)?.Play();
lastSamplePlayback = Time.Current;
}
}
2017-12-26 00:50:05 +08:00
private void updateCounts()
{
unreadCount.Value = sections.Select(c => c.UnreadCount).Sum();
2017-12-26 00:50:05 +08:00
}
2018-04-13 17:19:50 +08:00
private void markAllRead()
{
sections.Children.ForEach(s => s.MarkAllRead());
2018-04-13 17:19:50 +08:00
2017-12-26 00:50:05 +08:00
updateCounts();
}
2017-02-10 15:26:43 +08:00
}
}