1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-30 17:42:54 +08:00
osu-lazer/osu.Game/Overlays/News/NewsCard.cs

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

165 lines
6.2 KiB
C#
Raw Normal View History

2020-07-06 12:27:53 +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.
using System;
2020-07-16 20:18:01 +08:00
using System.Collections.Generic;
2020-07-06 12:27:53 +08:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Extensions.LocalisationExtensions;
2020-07-06 12:27:53 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
2020-07-06 12:27:53 +08:00
using osu.Game.Graphics;
2020-07-16 20:18:01 +08:00
using osu.Game.Graphics.Containers;
2020-07-06 12:27:53 +08:00
using osu.Game.Graphics.Sprites;
2023-10-19 16:19:45 +08:00
using osu.Game.Graphics.UserInterface;
2020-07-06 12:27:53 +08:00
using osu.Game.Online.API.Requests.Responses;
namespace osu.Game.Overlays.News
{
2020-07-16 20:18:01 +08:00
public partial class NewsCard : OsuHoverContainer
2020-07-06 12:27:53 +08:00
{
2020-07-16 20:18:01 +08:00
protected override IEnumerable<Drawable> EffectTargets => new[] { background };
2020-07-06 12:27:53 +08:00
2020-07-06 12:28:44 +08:00
private readonly APINewsPost post;
2020-07-06 12:27:53 +08:00
2023-05-22 10:27:20 +08:00
private Box background = null!;
private TextFlowContainer main = null!;
2020-07-06 12:27:53 +08:00
2020-07-06 12:28:44 +08:00
public NewsCard(APINewsPost post)
2023-10-19 16:19:45 +08:00
: base(HoverSampleSet.Button)
2020-07-06 12:27:53 +08:00
{
this.post = post;
RelativeSizeAxes = Axes.X;
AutoSizeAxes = Axes.Y;
Masking = true;
CornerRadius = 6;
2020-07-16 20:18:01 +08:00
}
2020-07-06 12:27:53 +08:00
2020-07-16 20:18:01 +08:00
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider, OsuGame? game)
2020-07-16 20:18:01 +08:00
{
if (post.Slug != null)
{
TooltipText = "view in browser";
Action = () => game?.OpenUrlExternally(@"/home/news/" + post.Slug);
2020-07-16 20:18:01 +08:00
}
2020-07-06 12:27:53 +08:00
2020-07-16 20:18:01 +08:00
AddRange(new Drawable[]
2020-07-06 12:27:53 +08:00
{
background = new Box
{
2020-07-16 20:18:01 +08:00
RelativeSizeAxes = Axes.Both
2020-07-06 12:27:53 +08:00
},
new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.X,
Height = 160,
Masking = true,
CornerRadius = 6,
Children = new Drawable[]
{
new DelayedLoadUnloadWrapper(() => new NewsPostBackground(post.FirstImage)
2020-07-06 12:27:53 +08:00
{
RelativeSizeAxes = Axes.Both,
FillMode = FillMode.Fill,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Alpha = 0
}, timeBeforeUnload: 5000)
2020-07-06 12:27:53 +08:00
{
RelativeSizeAxes = Axes.Both
},
new DateContainer(post.PublishedAt)
{
Anchor = Anchor.TopRight,
Origin = Anchor.TopRight,
Margin = new MarginPadding
{
Top = 10,
Right = 15
}
}
}
},
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding
{
Horizontal = 15,
Vertical = 10
},
Child = main = new TextFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y
}
}
}
2020-07-16 20:18:01 +08:00
}
});
IdleColour = colourProvider.Background4;
HoverColour = colourProvider.Background3;
2020-07-06 12:27:53 +08:00
main.AddParagraph(post.Title, t => t.Font = OsuFont.GetFont(size: 20, weight: FontWeight.SemiBold));
main.AddParagraph(post.Preview, t => t.Font = OsuFont.GetFont(size: 12)); // Should use sans-serif font
main.AddParagraph("by ", t => t.Font = OsuFont.GetFont(size: 12));
main.AddText(post.Author, t => t.Font = OsuFont.GetFont(size: 12, weight: FontWeight.SemiBold));
}
private partial class DateContainer : CircularContainer, IHasCustomTooltip<DateTimeOffset>
2020-07-06 12:27:53 +08:00
{
private readonly DateTimeOffset date;
2020-07-06 12:27:53 +08:00
2020-07-07 04:55:20 +08:00
public DateContainer(DateTimeOffset date)
2020-07-06 12:27:53 +08:00
{
this.date = date;
2020-07-06 12:27:53 +08:00
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
AutoSizeAxes = Axes.Both;
Masking = true;
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = colourProvider.Background6.Opacity(0.5f)
},
new OsuSpriteText
{
Text = date.ToLocalisableString(@"d MMM yyyy").ToUpper(),
2020-07-08 05:04:39 +08:00
Font = OsuFont.GetFont(size: 10, weight: FontWeight.SemiBold),
2020-07-06 12:27:53 +08:00
Margin = new MarginPadding
{
Horizontal = 20,
Vertical = 5
}
}
};
}
protected override bool OnClick(ClickEvent e) => true; // Protects the NewsCard from clicks while hovering DateContainer
ITooltip<DateTimeOffset> IHasCustomTooltip<DateTimeOffset>.GetCustomTooltip() => new DateTooltip();
DateTimeOffset IHasCustomTooltip<DateTimeOffset>.TooltipContent => date;
2020-07-06 12:27:53 +08:00
}
}
}