1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 07:53:38 +08:00
osu-lazer/osu.Game/Overlays/Comments/CommentMarkdownContainer.cs

112 lines
4.2 KiB
C#
Raw Normal View History

2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2021-07-26 19:18:33 +08:00
// See the LICENCE file in the repository root for full licence text.
2021-07-26 22:07:35 +08:00
using Markdig.Syntax;
2023-09-23 12:04:51 +08:00
using Markdig.Syntax.Inlines;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2021-07-26 19:18:33 +08:00
using osu.Framework.Graphics.Containers.Markdown;
2023-09-23 12:04:51 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.Textures;
2021-07-26 19:18:33 +08:00
using osu.Game.Graphics.Containers.Markdown;
2023-09-23 12:04:51 +08:00
using osuTK;
2021-07-26 19:18:33 +08:00
namespace osu.Game.Overlays.Comments
{
2022-11-24 13:32:20 +08:00
public partial class CommentMarkdownContainer : OsuMarkdownContainer
2021-07-26 19:18:33 +08:00
{
2022-12-20 03:16:36 +08:00
protected override OsuMarkdownContainerOptions Options => new OsuMarkdownContainerOptions
{
Autolinks = true
};
2021-07-26 22:07:35 +08:00
protected override MarkdownHeading CreateHeading(HeadingBlock headingBlock) => new CommentMarkdownHeading(headingBlock);
2023-11-16 11:43:25 +08:00
public override OsuMarkdownTextFlowContainer CreateTextFlow() => new CommentMarkdownTextFlowContainer();
2023-09-23 12:04:51 +08:00
2022-11-24 13:32:20 +08:00
private partial class CommentMarkdownHeading : OsuMarkdownHeading
2021-07-26 22:07:35 +08:00
{
public CommentMarkdownHeading(HeadingBlock headingBlock)
: base(headingBlock)
{
}
protected override float GetFontSizeByLevel(int level)
{
float defaultFontSize = base.GetFontSizeByLevel(6);
2021-07-26 22:07:35 +08:00
switch (level)
{
case 1:
return 1.2f * defaultFontSize;
case 2:
return 1.1f * defaultFontSize;
default:
return defaultFontSize;
}
}
}
2023-09-23 12:04:51 +08:00
2023-11-16 11:43:25 +08:00
private partial class CommentMarkdownTextFlowContainer : OsuMarkdownTextFlowContainer
2023-09-23 12:04:51 +08:00
{
protected override void AddImage(LinkInline linkInline) => AddDrawable(new CommentMarkdownImage(linkInline));
2023-09-23 12:04:51 +08:00
private partial class CommentMarkdownImage : OsuMarkdownImage
2023-09-23 12:04:51 +08:00
{
public CommentMarkdownImage(LinkInline linkInline)
: base(linkInline)
2023-09-23 12:04:51 +08:00
{
}
2023-10-23 18:29:46 +08:00
private DelayedLoadWrapper wrapper = null!;
2023-09-23 12:04:51 +08:00
protected override Drawable CreateContent(string url) => wrapper = new DelayedLoadWrapper(CreateImageContainer(url));
protected override ImageContainer CreateImageContainer(string url)
{
var container = new CommentImageContainer(url);
container.OnLoadComplete += d =>
{
// The size of DelayedLoadWrapper depends on AutoSizeAxes of it's content.
// But since it's set to None, we need to specify the size here manually.
wrapper.Size = container.Size;
d.FadeInFromZero(300, Easing.OutQuint);
};
return container;
}
private partial class CommentImageContainer : ImageContainer
{
// https://github.com/ppy/osu-web/blob/3bd0f406dc78d60b356d955cd4201f8c3e1cca09/resources/css/bem/osu-md.less#L36
2023-10-23 18:26:59 +08:00
// Web version defines max height in em units (6 em), which assuming default font size as 14 results in 84 px,
// which also seems to match observations upon inspecting the web element.
2023-09-23 12:04:51 +08:00
private const float max_height = 84f;
public CommentImageContainer(string url)
: base(url)
{
AutoSizeAxes = Axes.None;
}
protected override Sprite CreateImageSprite() => new Sprite
{
RelativeSizeAxes = Axes.Both
};
protected override Texture GetImageTexture(TextureStore textures, string url)
{
Texture t = base.GetImageTexture(textures, url);
if (t != null)
2023-10-22 05:33:26 +08:00
Size = t.Height > max_height ? new Vector2(max_height / t.Height * t.Width, max_height) : t.Size;
2023-09-23 12:04:51 +08:00
2023-10-23 18:29:46 +08:00
return t!;
2023-09-23 12:04:51 +08:00
}
}
}
}
2021-07-26 19:18:33 +08:00
}
}