Adjust game-side text flow containers to part-based model

This commit is contained in:
Bartłomiej Dach
2021-08-01 17:12:04 +02:00
parent fe86ae51b0
commit 6f863ca204
8 changed files with 73 additions and 37 deletions

View File

@ -9,6 +9,7 @@ using osu.Framework.Graphics.Sprites;
using System.Collections.Generic;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Localisation;
using osu.Framework.Platform;
using osu.Game.Graphics.Sprites;
@ -71,26 +72,20 @@ namespace osu.Game.Graphics.Containers
var spriteText = new OsuSpriteText { Text = text };
AddText(spriteText, creationParameters);
createLink(spriteText.Yield(), new LinkDetails(action, argument), tooltipText);
createLink(new TextPartManual(spriteText.Yield()), new LinkDetails(action, argument), tooltipText);
}
public void AddLink(IEnumerable<SpriteText> text, LinkAction action, string linkArgument, string tooltipText = null)
{
foreach (var t in text)
AddArbitraryDrawable(t);
createLink(text, new LinkDetails(action, linkArgument), tooltipText);
createLink(new TextPartManual(text), new LinkDetails(action, linkArgument), tooltipText);
}
public void AddUserLink(User user, Action<SpriteText> creationParameters = null)
=> createLink(AddText(user.Username, creationParameters), new LinkDetails(LinkAction.OpenUserProfile, user.Id.ToString()), "view profile");
private void createLink(IEnumerable<Drawable> drawables, LinkDetails link, string tooltipText, Action action = null)
private void createLink(ITextPart textPart, LinkDetails link, LocalisableString tooltipText, Action action = null)
{
var linkCompiler = CreateLinkCompiler(drawables.OfType<SpriteText>());
linkCompiler.RelativeSizeAxes = Axes.Both;
linkCompiler.TooltipText = tooltipText;
linkCompiler.Action = () =>
Action onClickAction = () =>
{
if (action != null)
action();
@ -101,10 +96,41 @@ namespace osu.Game.Graphics.Containers
host.OpenUrlExternally(link.Argument);
};
AddInternal(linkCompiler);
AddPart(new TextLink(textPart, tooltipText, onClickAction));
}
protected virtual DrawableLinkCompiler CreateLinkCompiler(IEnumerable<SpriteText> parts) => new DrawableLinkCompiler(parts);
private class TextLink : TextPart
{
private readonly ITextPart innerPart;
private readonly LocalisableString tooltipText;
private readonly Action action;
public TextLink(ITextPart innerPart, LocalisableString tooltipText, Action action)
{
this.innerPart = innerPart;
this.tooltipText = tooltipText;
this.action = action;
}
protected override IEnumerable<Drawable> CreateDrawablesFor(TextFlowContainer textFlowContainer)
{
var linkFlowContainer = (LinkFlowContainer)textFlowContainer;
innerPart.RecreateDrawablesFor(linkFlowContainer);
var drawables = innerPart.Drawables.ToList();
drawables.Add(linkFlowContainer.CreateLinkCompiler(innerPart).With(c =>
{
c.RelativeSizeAxes = Axes.Both;
c.TooltipText = tooltipText;
c.Action = action;
}));
return drawables;
}
}
protected virtual DrawableLinkCompiler CreateLinkCompiler(ITextPart textPart) => new DrawableLinkCompiler(textPart);
// We want the compilers to always be visible no matter where they are, so RelativeSizeAxes is used.
// However due to https://github.com/ppy/osu-framework/issues/2073, it's possible for the compilers to be relative size in the flow's auto-size axes - an unsupported operation.