2019-01-24 16:43:03 +08:00
// 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
2023-06-25 20:36:21 +08:00
using System ;
2023-06-23 13:58:19 +08:00
using System.Collections.Generic ;
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 ;
2023-05-23 17:47:56 +08:00
using osu.Framework.Extensions.Color4Extensions ;
2017-02-10 15:26:43 +08:00
using osu.Framework.Extensions.IEnumerableExtensions ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
2023-05-23 17:47:56 +08:00
using osu.Framework.Graphics.Effects ;
2017-06-20 13:54:23 +08:00
using osu.Framework.Graphics.Shapes ;
2023-11-24 12:16:04 +08:00
using osu.Framework.Graphics.Sprites ;
2021-04-21 13:37:11 +08:00
using osu.Framework.Localisation ;
2022-01-25 22:38:46 +08:00
using osu.Framework.Logging ;
2017-12-25 17:52:09 +08:00
using osu.Framework.Threading ;
2023-11-24 12:16:04 +08:00
using osu.Game.Graphics ;
2022-06-16 16:28:08 +08:00
using osu.Game.Graphics.Containers ;
using osu.Game.Overlays.Notifications ;
2022-01-28 12:53:48 +08:00
using osu.Game.Resources.Localisation.Web ;
2022-08-30 19:57:12 +08:00
using osuTK ;
2023-05-23 17:47:56 +08:00
using osuTK.Graphics ;
2022-01-28 12:53:48 +08:00
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
{
2022-04-18 18:59:57 +08:00
public partial class NotificationOverlay : OsuFocusedOverlayContainer , INamedOverlayComponent , INotificationOverlay
2017-02-10 15:26:43 +08:00
{
2023-12-27 22:35:03 +08:00
public IconUsage Icon = > OsuIcon . Notification ;
2021-04-21 13:37:11 +08:00
public LocalisableString Title = > NotificationsStrings . HeaderTitle ;
public LocalisableString Description = > NotificationsStrings . HeaderDescription ;
2020-09-03 14:46:56 +08:00
2023-08-17 10:37:07 +08:00
protected override double PopInOutSampleBalance = > OsuGameBase . SFX_STEREO_STRENGTH ;
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
2023-06-23 13:58:19 +08:00
public IEnumerable < Notification > AllNotifications = >
2023-06-25 20:25:07 +08:00
IsLoaded ? toastTray . Notifications . Concat ( sections . SelectMany ( s = > s . Notifications ) ) : Array . Empty < Notification > ( ) ;
2023-06-21 17:27:29 +08:00
2022-06-16 16:28:08 +08:00
private FlowContainer < NotificationSection > sections = null ! ;
2018-04-13 17:19:50 +08:00
2021-09-05 12:22:37 +08:00
[Resolved]
2022-06-16 16:28:08 +08:00
private AudioManager audio { get ; set ; } = null ! ;
2021-09-05 12:22:37 +08:00
2023-08-22 05:36:54 +08:00
[Resolved]
2023-08-22 14:54:41 +08:00
private OsuGame ? game { get ; set ; }
2023-08-22 05:36:54 +08:00
2022-08-30 16:33:08 +08:00
[Cached]
private OverlayColourProvider colourProvider = new OverlayColourProvider ( OverlayColourScheme . Purple ) ;
2022-08-30 19:57:12 +08: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 16:28:08 +08:00
[BackgroundDependencyLoader]
2022-08-30 23:57:18 +08:00
private void load ( )
2017-02-10 15:26:43 +08:00
{
2021-08-07 06:27:54 +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 [ ]
{
2022-08-30 19:57:12 +08:00
toastTray = new NotificationOverlayToastTray
2017-02-10 15:26:43 +08:00
{
2022-08-30 20:31:06 +08:00
ForwardNotificationToPermanentStore = addPermanently ,
2022-08-30 19:57:12 +08:00
Origin = Anchor . TopRight ,
2017-02-10 15:26:43 +08:00
} ,
2022-08-30 19:57:12 +08:00
mainContent = new Container
2017-02-10 15:26:43 +08:00
{
2017-02-18 03:08:28 +08:00
RelativeSizeAxes = Axes . Both ,
2023-05-23 17:47:56 +08:00
Masking = true ,
EdgeEffect = new EdgeEffectParameters
{
Colour = Color4 . Black . Opacity ( 0 ) ,
Type = EdgeEffectType . Shadow ,
Radius = 10 ,
Hollow = true ,
} ,
2022-08-30 19:57:12 +08:00
Children = new Drawable [ ]
2017-02-10 15:26:43 +08:00
{
2022-08-30 19:57:12 +08:00
new Box
2017-02-10 15:26:43 +08:00
{
2022-08-30 19:57:12 +08:00
RelativeSizeAxes = Axes . Both ,
Colour = colourProvider . Background4 ,
} ,
new OsuScrollContainer
{
Masking = true ,
RelativeSizeAxes = Axes . Both ,
2017-06-08 15:27:35 +08:00
Children = new [ ]
2017-02-10 15:26:43 +08:00
{
2022-08-30 19:57:12 +08:00
sections = new FillFlowContainer < NotificationSection >
{
Direction = FillDirection . Vertical ,
AutoSizeAxes = Axes . Y ,
RelativeSizeAxes = Axes . X ,
Children = new [ ]
{
2023-10-12 15:00:23 +08:00
// The main section adds as a catch-all for notifications which don't group into other sections.
2023-10-13 16:31:00 +08:00
new NotificationSection ( AccountsStrings . NotificationsTitle ) ,
2023-07-07 12:10:49 +08:00
new NotificationSection ( NotificationsStrings . RunningTasks , new [ ] { typeof ( ProgressNotification ) } ) ,
2022-08-30 19:57:12 +08:00
}
}
2017-02-10 15:26:43 +08:00
}
}
}
2022-08-30 19:57:12 +08:00
} ,
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
2018-06-06 14:49:58 +08:00
private void updateProcessingMode ( )
{
2023-06-20 15:56:41 +08:00
bool enabled = OverlayActivationMode . Value ! = OverlayActivation . Disabled | | State . Value = = Visibility . Visible ;
2018-06-06 14:49:58 +08:00
notificationsEnabler ? . Cancel ( ) ;
if ( enabled )
// we want a slight delay before toggling notifications on to avoid the user becoming overwhelmed.
2022-09-27 16:32:30 +08:00
notificationsEnabler = Scheduler . AddDelayed ( ( ) = > processingPosts = true , State . Value = = Visibility . Visible ? 0 : 250 ) ;
2018-06-06 14:49:58 +08:00
else
2022-09-27 16:32:53 +08:00
{
2018-06-06 14:49:58 +08:00
processingPosts = false ;
2022-09-27 16:32:53 +08:00
toastTray . FlushAllToasts ( ) ;
}
2018-06-06 14:49:58 +08:00
}
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2022-06-16 16:26:43 +08:00
State . BindValueChanged ( _ = > updateProcessingMode ( ) ) ;
2018-08-23 10:20:20 +08:00
OverlayActivationMode . BindValueChanged ( _ = > updateProcessingMode ( ) , true ) ;
2018-06-06 14:49:58 +08:00
}
2022-04-19 10:10:11 +08:00
public IBindable < int > UnreadCount = > unreadCount ;
2022-08-30 23:57:30 +08:00
public int ToastCount = > toastTray . UnreadCount ;
2022-04-19 10:10:11 +08:00
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
2017-12-25 17:52:09 +08:00
private readonly Scheduler postScheduler = new Scheduler ( ) ;
2018-04-13 17:19:50 +08:00
2022-09-05 17:57:33 +08: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 ;
2019-06-20 18:06:21 +08:00
2017-12-25 17:52:09 +08:00
private bool processingPosts = true ;
2018-04-13 17:19:50 +08:00
2021-09-05 12:22:37 +08:00
private double? lastSamplePlayback ;
2017-12-26 15:10:57 +08:00
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
2022-01-25 22:38:46 +08:00
Logger . Log ( $"⚠️ {notification.Text}" ) ;
2023-07-07 00:50:15 +08:00
notification . Closed + = ( ) = > notificationClosed ( notification ) ;
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
2022-08-30 20:13:27 +08:00
playDebouncedSample ( notification . PopInSampleName ) ;
2023-08-22 14:58:10 +08:00
if ( notification . IsImportant )
2023-08-22 05:36:54 +08:00
{
2023-08-22 14:58:10 +08:00
game ? . Window ? . Flash ( ) ;
2023-08-22 14:54:41 +08:00
notification . Closed + = ( ) = > game ? . Window ? . CancelFlash ( ) ;
2023-08-22 05:36:54 +08:00
}
2022-08-30 19:57:12 +08:00
if ( State . Value = = Visibility . Hidden )
2022-09-11 20:34:35 +08:00
{
2022-09-11 20:52:19 +08:00
notification . IsInToastTray = true ;
2022-08-30 20:31:06 +08:00
toastTray . Post ( notification ) ;
2022-09-11 20:34:35 +08:00
}
2022-08-30 19:57:12 +08:00
else
2022-08-30 20:31:06 +08:00
addPermanently ( notification ) ;
2022-08-31 00:40:27 +08:00
updateCounts ( ) ;
2022-08-30 20:31:06 +08:00
} ) ;
2018-04-13 17:19:50 +08:00
2022-08-30 20:31:06 +08:00
private void addPermanently ( Notification notification )
{
2022-09-11 20:52:19 +08:00
notification . IsInToastTray = false ;
2022-09-11 20:34:35 +08:00
2022-08-30 20:31:06 +08:00
var ourType = notification . GetType ( ) ;
int depth = notification . DisplayOnTop ? - runningDepth : runningDepth ;
2022-08-30 19:57:12 +08:00
2023-10-13 16:31:00 +08:00
var section = sections . Children . FirstOrDefault ( s = > s . AcceptedNotificationTypes ? . Any ( accept = > accept . IsAssignableFrom ( ourType ) ) = = true )
? ? sections . First ( ) ;
2022-08-30 19:57:12 +08:00
2022-08-30 20:31:06 +08:00
section . Add ( notification , depth ) ;
updateCounts ( ) ;
}
2018-04-13 17:19:50 +08:00
2017-12-25 17:52:09 +08:00
protected override void Update ( )
{
base . Update ( ) ;
2021-09-05 11:54:21 +08:00
2017-12-25 17:52:09 +08:00
if ( processingPosts )
postScheduler . Update ( ) ;
}
2018-04-13 17:19:50 +08:00
2017-02-10 15:26:43 +08:00
protected override void PopIn ( )
{
2017-07-23 02:50:25 +08:00
this . MoveToX ( 0 , TRANSITION_LENGTH , Easing . OutQuint ) ;
2023-12-27 12:50:47 +08:00
mainContent . FadeTo ( 1 , TRANSITION_LENGTH / 2 , Easing . OutQuint ) ;
2023-05-24 11:38:27 +08:00
mainContent . FadeEdgeEffectTo ( WaveContainer . SHADOW_OPACITY , WaveContainer . APPEAR_DURATION , Easing . Out ) ;
2022-08-30 19:57:12 +08:00
toastTray . FlushAllToasts ( ) ;
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 ) ;
2023-12-27 12:50:47 +08:00
mainContent . FadeTo ( 0 , TRANSITION_LENGTH / 2 , Easing . OutQuint ) ;
2023-05-23 17:47:56 +08:00
mainContent . FadeEdgeEffectTo ( 0 , WaveContainer . DISAPPEAR_DURATION , Easing . In ) ;
2017-12-23 21:33:43 +08:00
}
2018-04-13 17:19:50 +08:00
2023-07-07 00:50:15 +08:00
private void notificationClosed ( Notification notification ) = > Schedule ( ( ) = >
2021-09-05 12:22:37 +08:00
{
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.
2023-07-07 00:50:15 +08:00
playDebouncedSample ( notification . PopOutSampleName ) ;
2022-09-16 15:36:56 +08:00
} ) ;
2021-09-05 12:22:37 +08:00
private void playDebouncedSample ( string sampleName )
{
2023-07-07 00:50:15 +08:00
if ( string . IsNullOrEmpty ( sampleName ) )
return ;
2021-09-05 12:25:10 +08:00
if ( lastSamplePlayback = = null | | Time . Current - lastSamplePlayback > OsuGameBase . SAMPLE_DEBOUNCE_TIME )
2021-09-05 12:22:37 +08:00
{
audio . Samples . Get ( sampleName ) ? . Play ( ) ;
lastSamplePlayback = Time . Current ;
}
}
2021-09-05 11:54:21 +08:00
2017-12-23 21:33:43 +08:00
private void markAllRead ( )
{
sections . Children . ForEach ( s = > s . MarkAllRead ( ) ) ;
2022-08-31 11:46:03 +08:00
toastTray . MarkAllRead ( ) ;
2017-12-26 00:50:05 +08:00
updateCounts ( ) ;
2017-12-23 21:33:43 +08:00
}
2022-08-31 11:46:43 +08:00
private void updateCounts ( )
{
unreadCount . Value = sections . Select ( c = > c . UnreadCount ) . Sum ( ) + toastTray . UnreadCount ;
}
2017-02-10 15:26:43 +08:00
}
2017-07-28 13:51:49 +08:00
}