osu/osu.Game/Overlays/Notifications/Notification.cs

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

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