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.

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
@ -19,8 +19,8 @@ namespace osu.Game.Graphics.Containers
protected override SpriteText CreateSpriteText() => new OsuSpriteText();
public void AddArbitraryDrawable(Drawable drawable) => AddInternal(drawable);
public ITextPart AddArbitraryDrawable(Drawable drawable) => AddPart(new TextPartManual(drawable.Yield()));
public IEnumerable<Drawable> AddIcon(IconUsage icon, Action<SpriteText> creationParameters = null) => AddText(icon.Icon.ToString(), creationParameters);
public ITextPart AddIcon(IconUsage icon, Action<SpriteText> creationParameters = null) => AddText(icon.Icon.ToString(), creationParameters);
}
}

View File

@ -2,7 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Containers;
using osuTK.Graphics;
@ -10,7 +10,7 @@ namespace osu.Game.Graphics
{
public class ErrorTextFlowContainer : OsuTextFlowContainer
{
private readonly List<Drawable> errorDrawables = new List<Drawable>();
private readonly List<ITextPart> errorTextParts = new List<ITextPart>();
public ErrorTextFlowContainer()
: base(cp => cp.Font = cp.Font.With(size: 12))
@ -19,7 +19,8 @@ namespace osu.Game.Graphics
public void ClearErrors()
{
errorDrawables.ForEach(d => d.Expire());
foreach (var textPart in errorTextParts)
RemovePart(textPart);
}
public void AddErrors(string[] errors)
@ -29,7 +30,7 @@ namespace osu.Game.Graphics
if (errors == null) return;
foreach (string error in errors)
errorDrawables.AddRange(AddParagraph(error, cp => cp.Colour = Color4.Red));
errorTextParts.Add(AddParagraph(error, cp => cp.Colour = Color4.Red));
}
}
}