1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-27 15:32:57 +08:00
osu-lazer/osu.Game/Overlays/Notifications/Notification.cs

329 lines
11 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 osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
2019-04-02 13:51:28 +08:00
using osu.Framework.Graphics.Effects;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Game.Graphics.Containers;
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays.Notifications
{
public abstract class Notification : Container
{
/// <summary>
/// User requested close.
/// </summary>
public event Action? Closed;
2018-04-13 17:19:50 +08:00
public abstract LocalisableString Text { get; set; }
2018-07-13 20:25:08 +08:00
/// <summary>
/// Whether this notification should forcefully display itself.
/// </summary>
2018-07-16 12:00:21 +08:00
public virtual bool IsImportant => true;
2018-07-13 20:25:08 +08:00
2018-04-13 17:19:50 +08:00
/// <summary>
/// Run on user activating the notification. Return true to close.
/// </summary>
public Func<bool>? Activated;
2018-04-13 17:19:50 +08:00
/// <summary>
/// Should we show at the top of our section on display?
/// </summary>
public virtual bool DisplayOnTop => true;
public virtual string PopInSampleName => "UI/notification-pop-in";
2021-01-15 14:21:50 +08:00
2018-04-13 17:19:50 +08:00
protected NotificationLight Light;
2022-08-30 16:33:08 +08:00
2018-04-13 17:19:50 +08:00
protected Container IconContent;
2022-08-30 16:33:08 +08:00
2018-04-13 17:19:50 +08:00
private readonly Container content;
protected override Container<Drawable> Content => content;
protected Container MainContent;
2018-04-13 17:19:50 +08:00
public virtual bool Read { get; set; }
protected virtual IconUsage CloseButtonIcon => FontAwesome.Solid.Check;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
private readonly Box initialFlash;
private Box background = null!;
2018-04-13 17:19:50 +08:00
protected Notification()
{
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
2022-08-30 16:33:08 +08:00
InternalChildren = new Drawable[]
2018-04-13 17:19:50 +08:00
{
Light = new NotificationLight
{
Margin = new MarginPadding { Right = 5 },
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreRight,
},
MainContent = new Container
2018-04-13 17:19:50 +08:00
{
CornerRadius = 6,
2018-04-13 17:19:50 +08:00
Masking = true,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
AutoSizeDuration = 400,
AutoSizeEasing = Easing.OutQuint,
Children = new Drawable[]
{
2022-08-30 16:33:08 +08:00
new GridContainer
2018-04-13 17:19:50 +08:00
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
2022-08-30 16:33:08 +08:00
RowDimensions = new[]
2018-04-13 17:19:50 +08:00
{
2022-08-30 16:33:08 +08:00
new Dimension(GridSizeMode.AutoSize, minSize: 60)
},
ColumnDimensions = new[]
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(),
new Dimension(GridSizeMode.AutoSize),
},
Content = new[]
{
new Drawable[]
2018-04-13 17:19:50 +08:00
{
2022-08-30 16:33:08 +08:00
IconContent = new Container
{
Width = 40,
RelativeSizeAxes = Axes.Y,
},
new Container
2018-04-13 17:19:50 +08:00
{
2022-08-30 16:33:08 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding(10),
Children = new Drawable[]
{
content = new Container
{
Masking = true,
2022-08-30 16:33:08 +08:00
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
},
}
2018-04-13 17:19:50 +08:00
},
new CloseButton(CloseButtonIcon)
2022-08-30 16:33:08 +08:00
{
Action = Close,
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
}
2018-04-13 17:19:50 +08:00
}
},
2022-08-30 16:33:08 +08:00
},
initialFlash = new Box
{
Colour = Color4.White.Opacity(0.8f),
RelativeSizeAxes = Axes.Both,
Blending = BlendingParameters.Additive,
},
2018-04-13 17:19:50 +08:00
}
}
2022-08-30 16:33:08 +08:00
};
2018-04-13 17:19:50 +08:00
}
2022-08-30 16:33:08 +08:00
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
MainContent.Add(background = new Box
2022-08-30 16:33:08 +08:00
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background3,
Depth = float.MaxValue
});
2018-04-13 17:19:50 +08:00
}
protected override bool OnHover(HoverEvent e)
{
background.FadeColour(colourProvider.Background2, 200, Easing.OutQuint);
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
background.FadeColour(colourProvider.Background3, 200, Easing.OutQuint);
base.OnHoverLost(e);
}
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
if (Activated?.Invoke() ?? true)
Close();
return true;
}
protected override void LoadComplete()
{
base.LoadComplete();
2021-01-15 14:21:50 +08:00
2018-04-13 17:19:50 +08:00
this.FadeInFromZero(200);
2022-08-30 16:33:08 +08:00
MainContent.MoveToX(DrawSize.X);
MainContent.MoveToX(0, 500, Easing.OutQuint);
initialFlash.FadeOutFromOne(2000, Easing.OutQuart);
2018-04-13 17:19:50 +08:00
}
public bool WasClosed;
public virtual void Close()
2018-04-13 17:19:50 +08:00
{
if (WasClosed) return;
2019-02-28 12:31:40 +08:00
2018-04-13 17:19:50 +08:00
WasClosed = true;
Closed?.Invoke();
this.FadeOut(100);
Expire();
}
private class CloseButton : OsuClickableContainer
{
2022-08-30 16:33:08 +08:00
private SpriteIcon icon = null!;
private Box background = null!;
2018-04-13 17:19:50 +08:00
private readonly IconUsage iconUsage;
2022-08-30 16:33:08 +08:00
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
public CloseButton(IconUsage iconUsage)
{
this.iconUsage = iconUsage;
}
2022-08-30 16:33:08 +08:00
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
2022-08-30 16:33:08 +08:00
RelativeSizeAxes = Axes.Y;
Width = 28;
2018-04-13 17:19:50 +08:00
2022-08-30 16:33:08 +08:00
Children = new Drawable[]
2018-04-13 17:19:50 +08:00
{
2022-08-30 16:33:08 +08:00
background = new Box
{
Colour = OsuColour.Gray(0).Opacity(0.15f),
2022-08-30 16:33:08 +08:00
Alpha = 0,
RelativeSizeAxes = Axes.Both,
},
icon = new SpriteIcon
2018-04-13 17:19:50 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Icon = iconUsage,
2022-08-30 16:33:08 +08:00
Size = new Vector2(12),
Colour = colourProvider.Foreground1,
2018-04-13 17:19:50 +08:00
}
};
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
background.FadeIn(200, Easing.OutQuint);
icon.FadeColour(colourProvider.Content1, 200, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
background.FadeOut(200, Easing.OutQuint);
icon.FadeColour(colourProvider.Foreground1, 200, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
}
public class NotificationLight : Container
{
private bool pulsate;
private Container pulsateLayer = null!;
2018-04-13 17:19:50 +08:00
public bool Pulsate
{
get => pulsate;
2018-04-13 17:19:50 +08:00
set
{
if (pulsate == value) return;
pulsate = value;
pulsateLayer.ClearTransforms();
pulsateLayer.Alpha = 1;
if (pulsate)
{
const float length = 1000;
pulsateLayer.Loop(length / 2,
p => p.FadeTo(0.4f, length, Easing.In).Then().FadeTo(1, length, Easing.Out)
);
}
}
}
public new SRGBColour Colour
{
set
{
base.Colour = value;
pulsateLayer.EdgeEffect = new EdgeEffectParameters
{
Colour = ((Color4)value).Opacity(0.5f), //todo: avoid cast
Type = EdgeEffectType.Glow,
Radius = 12,
Roundness = 12,
};
}
}
[BackgroundDependencyLoader]
private void load()
{
Size = new Vector2(6, 15);
Children = new[]
{
pulsateLayer = new CircularContainer
{
Anchor = Anchor.CentreLeft,
Origin = Anchor.CentreLeft,
Masking = true,
RelativeSizeAxes = Axes.Both,
Children = new[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
},
}
}
};
}
}
}
}