1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-29 18:12:55 +08:00
osu-lazer/osu.Game/Overlays/Notifications/NotificationSection.cs

181 lines
5.9 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
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Extensions.LocalisationExtensions;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Sprites;
using osu.Game.Localisation;
2018-11-20 15:51:59 +08:00
using osuTK;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Notifications
{
2022-11-24 13:32:20 +08:00
public partial class NotificationSection : AlwaysUpdateFillFlowContainer<Drawable>
2018-04-13 17:19:50 +08:00
{
/// <summary>
/// All notifications currently being displayed in this section.
/// </summary>
public IEnumerable<Notification> Notifications => notifications;
private OsuSpriteText countDrawable = null!;
2018-04-13 17:19:50 +08:00
private FlowContainer<Notification> notifications = null!;
2018-04-13 17:19:50 +08:00
public int DisplayedCount => notifications.Count(n => !n.WasClosed);
public int UnreadCount => notifications.Count(n => !n.WasClosed && !n.Read);
public void Add(Notification notification, float position)
{
2019-07-01 23:41:40 +08:00
notifications.Insert((int)position, notification);
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Enumerable of notification types accepted in this section.
/// If <see langword="null"/>, the section accepts any and all notifications.
/// </summary>
public IEnumerable<Type>? AcceptedNotificationTypes { get; }
2018-04-13 17:19:50 +08:00
private readonly LocalisableString titleText;
2018-04-13 17:19:50 +08:00
public NotificationSection(LocalisableString title, IEnumerable<Type>? acceptedNotificationTypes = null)
2018-04-13 17:19:50 +08:00
{
AcceptedNotificationTypes = acceptedNotificationTypes?.ToArray();
titleText = title;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Direction = FillDirection.Vertical;
Padding = new MarginPadding
{
Top = 10,
Bottom = 5,
Right = 20,
Left = 20,
};
AddRangeInternal(new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
new ClearAllButton
{
Text = NotificationsStrings.ClearAll.ToUpper(),
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Action = clearAll
},
2018-04-13 17:19:50 +08:00
new FillFlowContainer
{
Margin = new MarginPadding
{
Bottom = 5
},
Spacing = new Vector2(5, 0),
AutoSizeAxes = Axes.Both,
Children = new Drawable[]
{
new OsuSpriteText
2018-04-13 17:19:50 +08:00
{
Text = titleText.ToUpper(),
2020-03-17 07:32:25 +08:00
Font = OsuFont.GetFont(weight: FontWeight.Bold)
2018-04-13 17:19:50 +08:00
},
countDrawable = new OsuSpriteText
2018-04-13 17:19:50 +08:00
{
Text = "3",
Colour = colours.Yellow,
2020-03-17 07:32:25 +08:00
Font = OsuFont.GetFont(weight: FontWeight.Bold)
2018-04-13 17:19:50 +08:00
},
}
},
},
},
notifications = new AlwaysUpdateFillFlowContainer<Notification>
{
AutoSizeAxes = Axes.Y,
RelativeSizeAxes = Axes.X,
LayoutDuration = 150,
LayoutEasing = Easing.OutQuart,
}
});
}
private void clearAll() => notifications.Children.ForEach(c =>
{
if (c is not ProgressNotification p || !p.Ongoing)
c.Close(true);
});
2018-04-13 17:19:50 +08:00
protected override void Update()
{
base.Update();
countDrawable.Text = getVisibleCount().ToString();
}
private int getVisibleCount()
{
int count = 0;
foreach (var c in notifications)
{
if (c.Alpha > 0.99f)
count++;
}
return count;
2018-04-13 17:19:50 +08:00
}
2022-11-24 13:32:20 +08:00
private partial class ClearAllButton : OsuClickableContainer
2018-04-13 17:19:50 +08:00
{
private readonly OsuSpriteText text;
public ClearAllButton()
{
AutoSizeAxes = Axes.Both;
Children = new[]
{
text = new OsuSpriteText()
};
}
public LocalisableString Text
2018-04-13 17:19:50 +08:00
{
get => text.Text;
set => text.Text = value;
2018-04-13 17:19:50 +08:00
}
}
public void MarkAllRead()
{
notifications.Children.ForEach(n => n.Read = true);
2018-04-13 17:19:50 +08:00
}
}
2022-11-24 13:32:20 +08:00
public partial class AlwaysUpdateFillFlowContainer<T> : FillFlowContainer<T>
2018-04-13 17:19:50 +08:00
where T : Drawable
{
// this is required to ensure correct layout and scheduling on children.
// the layout portion of this is being tracked as a framework issue (https://github.com/ppy/osu-framework/issues/1297).
protected override bool RequiresChildrenUpdate => true;
}
}