2019-01-24 08:43:03 +00: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 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
using System.Linq ;
using osu.Framework.Extensions.IEnumerableExtensions ;
using osu.Framework.Graphics ;
using osu.Framework.Graphics.Containers ;
using osu.Game.Overlays.Notifications ;
2017-06-20 05:54:23 +00:00
using osu.Framework.Graphics.Shapes ;
2017-06-28 17:18:12 +00:00
using osu.Game.Graphics.Containers ;
2017-12-25 09:52:09 +00:00
using osu.Framework.Allocation ;
2021-09-05 04:22:37 +00:00
using osu.Framework.Audio ;
2019-02-21 10:04:31 +00:00
using osu.Framework.Bindables ;
2021-04-21 05:37:11 +00:00
using osu.Framework.Localisation ;
2022-01-25 14:38:46 +00:00
using osu.Framework.Logging ;
2017-12-25 09:52:09 +00:00
using osu.Framework.Threading ;
2020-11-30 08:18:29 +00:00
using osu.Game.Graphics ;
2021-04-21 05:37:11 +00:00
using osu.Game.Localisation ;
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
namespace osu.Game.Overlays
{
2020-09-03 06:46:56 +00:00
public class NotificationOverlay : OsuFocusedOverlayContainer , INamedOverlayComponent
2017-02-10 07:26:43 +00:00
{
2020-09-03 08:05:45 +00:00
public string IconTexture = > "Icons/Hexacons/notification" ;
2021-04-21 05:37:11 +00:00
public LocalisableString Title = > NotificationsStrings . HeaderTitle ;
public LocalisableString Description = > NotificationsStrings . HeaderDescription ;
2020-09-03 06:46:56 +00:00
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
2017-02-10 07:26:43 +00:00
private FlowContainer < NotificationSection > sections ;
2018-04-13 09:19:50 +00:00
2021-09-05 04:22:37 +00:00
[Resolved]
private AudioManager audio { get ; set ; }
2017-08-22 10:51:42 +00:00
[BackgroundDependencyLoader]
2017-02-27 14:32:32 +00:00
private void load ( )
2017-02-10 07:26:43 +00:00
{
2021-08-06 22:27:54 +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 [ ]
{
new Box
{
RelativeSizeAxes = Axes . Both ,
2020-11-30 08:18:29 +00:00
Colour = OsuColour . Gray ( 0.05f ) ,
2017-02-10 07:26:43 +00:00
} ,
2017-12-23 13:33:43 +00:00
new OsuScrollContainer
2017-02-10 07:26:43 +00:00
{
2017-12-23 13:33:43 +00:00
Masking = true ,
2017-02-17 19:08:28 +00:00
RelativeSizeAxes = Axes . Both ,
2017-02-10 07:26:43 +00:00
Children = new [ ]
{
2017-03-01 18:33:01 +00:00
sections = new FillFlowContainer < NotificationSection >
2017-02-10 07:26:43 +00:00
{
2017-03-04 10:00:17 +00:00
Direction = FillDirection . Vertical ,
2017-02-10 07:26:43 +00:00
AutoSizeAxes = Axes . Y ,
RelativeSizeAxes = Axes . X ,
2017-06-08 07:27:35 +00:00
Children = new [ ]
2017-02-10 07:26:43 +00:00
{
2019-06-20 08:41:12 +00:00
new NotificationSection ( @"Notifications" , @"Clear All" )
2017-02-10 07:26:43 +00:00
{
2017-12-23 13:33:43 +00:00
AcceptTypes = new [ ] { typeof ( SimpleNotification ) }
2017-02-10 07:26:43 +00:00
} ,
2019-06-20 08:41:12 +00:00
new NotificationSection ( @"Running Tasks" , @"Cancel All" )
2017-02-10 07:26:43 +00:00
{
2017-12-23 13:33:43 +00:00
AcceptTypes = new [ ] { typeof ( ProgressNotification ) }
}
2017-02-10 07:26:43 +00:00
}
}
}
}
} ;
}
2018-04-13 09:19:50 +00:00
2018-06-06 06:49:58 +00:00
private ScheduledDelegate notificationsEnabler ;
2019-02-28 04:31:40 +00:00
2018-06-06 06:49:58 +00:00
private void updateProcessingMode ( )
{
2019-06-11 05:28:52 +00:00
bool enabled = OverlayActivationMode . Value = = OverlayActivation . All | | State . Value = = Visibility . Visible ;
2018-06-06 06:49:58 +00:00
notificationsEnabler ? . Cancel ( ) ;
if ( enabled )
// we want a slight delay before toggling notifications on to avoid the user becoming overwhelmed.
2019-06-11 05:28:52 +00:00
notificationsEnabler = Scheduler . AddDelayed ( ( ) = > processingPosts = true , State . Value = = Visibility . Visible ? 0 : 1000 ) ;
2018-06-06 06:49:58 +00:00
else
processingPosts = false ;
}
protected override void LoadComplete ( )
{
base . LoadComplete ( ) ;
2019-06-11 05:28:52 +00:00
State . ValueChanged + = _ = > updateProcessingMode ( ) ;
2018-08-23 02:20:20 +00:00
OverlayActivationMode . BindValueChanged ( _ = > updateProcessingMode ( ) , true ) ;
2018-06-06 06:49:58 +00:00
}
2017-12-25 16:50:05 +00:00
public 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
2017-12-25 09:52:09 +00:00
private readonly Scheduler postScheduler = new Scheduler ( ) ;
2018-04-13 09:19:50 +00:00
2019-06-20 10:06:21 +00:00
public override bool IsPresent = > base . IsPresent | | postScheduler . HasPendingTasks ;
2017-12-25 09:52:09 +00:00
private bool processingPosts = true ;
2018-04-13 09:19:50 +00:00
2021-09-05 04:22:37 +00:00
private double? lastSamplePlayback ;
2021-09-05 03:54:21 +00:00
/// <summary>
/// Post a new notification for display.
/// </summary>
/// <param name="notification">The notification to display.</param>
2017-12-26 07:10:57 +00:00
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
2022-01-25 14:38:46 +00:00
Logger . Log ( $"⚠️ {notification.Text}" ) ;
2017-12-25 16:50:05 +00:00
notification . Closed + = notificationClosed ;
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
2017-12-25 16:50:05 +00:00
var ourType = notification . GetType ( ) ;
2018-04-13 09:19:50 +00:00
2018-03-06 08:20:58 +00:00
var section = sections . Children . FirstOrDefault ( s = > s . AcceptTypes . Any ( accept = > accept . IsAssignableFrom ( ourType ) ) ) ;
section ? . Add ( notification , notification . DisplayOnTop ? - runningDepth : runningDepth ) ;
2018-04-13 09:19:50 +00:00
2018-07-13 12:25:08 +00:00
if ( notification . IsImportant )
2019-06-11 05:28:52 +00:00
Show ( ) ;
2018-04-13 09:19:50 +00:00
2017-12-25 16:50:05 +00:00
updateCounts ( ) ;
2021-09-05 04:22:37 +00:00
playDebouncedSample ( notification . PopInSampleName ) ;
2017-12-25 16:50:05 +00:00
} ) ;
2018-04-13 09:19:50 +00:00
2017-12-25 09:52:09 +00:00
protected override void Update ( )
{
base . Update ( ) ;
2021-09-05 03:54:21 +00:00
2017-12-25 09:52:09 +00:00
if ( processingPosts )
postScheduler . Update ( ) ;
}
2018-04-13 09:19:50 +00:00
2017-02-10 07:26:43 +00:00
protected override void PopIn ( )
{
base . PopIn ( ) ;
2018-04-13 09:19:50 +00:00
2017-07-22 18:50:25 +00:00
this . MoveToX ( 0 , TRANSITION_LENGTH , Easing . OutQuint ) ;
2017-12-23 13:33:43 +00:00
this . FadeTo ( 1 , TRANSITION_LENGTH , Easing . OutQuint ) ;
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 ) ;
2017-12-23 13:33:43 +00:00
this . FadeTo ( 0 , TRANSITION_LENGTH , Easing . OutQuint ) ;
}
2018-04-13 09:19:50 +00:00
2021-09-05 04:22:37 +00: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 )
{
2021-09-05 04:25:10 +00:00
if ( lastSamplePlayback = = null | | Time . Current - lastSamplePlayback > OsuGameBase . SAMPLE_DEBOUNCE_TIME )
2021-09-05 04:22:37 +00:00
{
audio . Samples . Get ( sampleName ) ? . Play ( ) ;
lastSamplePlayback = Time . Current ;
}
}
2021-09-05 03:54:21 +00:00
2017-12-25 16:50:05 +00:00
private void updateCounts ( )
{
2019-06-20 10:06:21 +00:00
UnreadCount . Value = sections . Select ( c = > c . UnreadCount ) . Sum ( ) ;
2017-12-25 16:50:05 +00:00
}
2018-04-13 09:19:50 +00:00
2017-12-23 13:33:43 +00:00
private void markAllRead ( )
{
sections . Children . ForEach ( s = > s . MarkAllRead ( ) ) ;
2018-04-13 09:19:50 +00:00
2017-12-25 16:50:05 +00:00
updateCounts ( ) ;
2017-12-23 13:33:43 +00:00
}
2017-02-10 07:26:43 +00:00
}
2017-07-28 05:51:49 +00:00
}