2019-01-24 16:43:03 +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.
2018-04-13 17:19:50 +08:00
using osu.Game.Online.Chat ;
using System ;
using System.Linq ;
using osu.Framework.Allocation ;
using osu.Framework.Graphics.Sprites ;
using System.Collections.Generic ;
2021-04-19 14:29:26 +08:00
using osu.Framework.Extensions.IEnumerableExtensions ;
2018-12-26 17:05:12 +08:00
using osu.Framework.Graphics ;
2021-04-19 14:29:26 +08:00
using osu.Framework.Localisation ;
2021-07-30 12:21:26 +08:00
using osu.Framework.Platform ;
2021-04-19 14:29:26 +08:00
using osu.Game.Graphics.Sprites ;
2019-04-05 13:15:36 +08:00
using osu.Game.Users ;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Graphics.Containers
{
public class LinkFlowContainer : OsuTextFlowContainer
{
public LinkFlowContainer ( Action < SpriteText > defaultCreationParameters = null )
: base ( defaultCreationParameters )
{
}
2019-11-27 23:13:44 +08:00
[Resolved(CanBeNull = true)]
private OsuGame game { get ; set ; }
2018-04-13 17:19:50 +08:00
2021-07-30 12:21:26 +08:00
[Resolved]
private GameHost host { get ; set ; }
2018-04-13 17:19:50 +08:00
public void AddLinks ( string text , List < Link > links )
{
if ( string . IsNullOrEmpty ( text ) | | links = = null )
return ;
if ( links . Count = = 0 )
{
AddText ( text ) ;
return ;
}
int previousLinkEnd = 0 ;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
foreach ( var link in links )
{
2019-12-14 20:54:22 +08:00
AddText ( text [ previousLinkEnd . . link . Index ] ) ;
2021-02-12 14:18:16 +08:00
string displayText = text . Substring ( link . Index , link . Length ) ;
2021-06-01 15:46:29 +08:00
string linkArgument = link . Argument ;
2021-02-12 14:18:16 +08:00
string tooltip = displayText = = link . Url ? null : link . Url ;
AddLink ( displayText , link . Action , linkArgument , tooltip ) ;
2018-04-13 17:19:50 +08:00
previousLinkEnd = link . Index + link . Length ;
}
AddText ( text . Substring ( previousLinkEnd ) ) ;
}
2019-11-01 10:40:51 +08:00
public void AddLink ( string text , string url , Action < SpriteText > creationParameters = null ) = >
createLink ( AddText ( text , creationParameters ) , new LinkDetails ( LinkAction . External , url ) , url ) ;
2018-12-26 17:05:12 +08:00
2019-11-01 10:40:51 +08:00
public void AddLink ( string text , Action action , string tooltipText = null , Action < SpriteText > creationParameters = null )
2021-06-02 13:43:35 +08:00
= > createLink ( AddText ( text , creationParameters ) , new LinkDetails ( LinkAction . Custom , string . Empty ) , tooltipText , action ) ;
2018-12-26 17:05:12 +08:00
2019-11-01 10:40:51 +08:00
public void AddLink ( string text , LinkAction action , string argument , string tooltipText = null , Action < SpriteText > creationParameters = null )
2021-02-12 14:18:16 +08:00
= > createLink ( AddText ( text , creationParameters ) , new LinkDetails ( action , argument ) , tooltipText ) ;
2019-11-01 10:40:51 +08:00
2021-04-20 01:27:35 +08:00
public void AddLink ( LocalisableString text , LinkAction action , string argument , string tooltipText = null , Action < SpriteText > creationParameters = null )
2018-12-26 17:05:12 +08:00
{
2021-04-19 14:29:26 +08:00
var spriteText = new OsuSpriteText { Text = text } ;
2018-12-26 17:05:12 +08:00
2021-04-19 14:29:26 +08:00
AddText ( spriteText , creationParameters ) ;
createLink ( spriteText . Yield ( ) , new LinkDetails ( action , argument ) , tooltipText ) ;
2018-12-26 17:05:12 +08:00
}
2021-06-01 16:10:09 +08:00
public void AddLink ( IEnumerable < SpriteText > text , LinkAction action , string linkArgument , string tooltipText = null )
2021-04-18 20:58:08 +08:00
{
2021-04-19 14:29:26 +08:00
foreach ( var t in text )
AddArbitraryDrawable ( t ) ;
2021-04-18 20:58:08 +08:00
createLink ( text , new LinkDetails ( action , linkArgument ) , tooltipText ) ;
}
2019-11-01 10:40:51 +08:00
public void AddUserLink ( User user , Action < SpriteText > creationParameters = null )
2020-01-15 03:26:54 +08:00
= > createLink ( AddText ( user . Username , creationParameters ) , new LinkDetails ( LinkAction . OpenUserProfile , user . Id . ToString ( ) ) , "view profile" ) ;
2019-04-05 13:15:36 +08:00
2019-11-01 10:40:51 +08:00
private void createLink ( IEnumerable < Drawable > drawables , LinkDetails link , string tooltipText , Action action = null )
2018-04-13 17:19:50 +08:00
{
2021-09-08 14:50:19 +08:00
var linkCompiler = CreateLinkCompiler ( drawables . OfType < SpriteText > ( ) ) ;
linkCompiler . RelativeSizeAxes = Axes . Both ;
linkCompiler . TooltipText = tooltipText ;
linkCompiler . Action = ( ) = >
2018-04-13 17:19:50 +08:00
{
2021-09-08 14:50:19 +08:00
if ( action ! = null )
action ( ) ;
else if ( game ! = null )
game . HandleLink ( link ) ;
// fallback to handle cases where OsuGame is not available, ie. tournament client.
else if ( link . Action = = LinkAction . External )
host . OpenUrlExternally ( link . Argument ) ;
} ;
AddInternal ( linkCompiler ) ;
2018-04-13 17:19:50 +08:00
}
2019-01-08 17:58:44 +08:00
2021-09-08 14:50:19 +08:00
protected virtual DrawableLinkCompiler CreateLinkCompiler ( IEnumerable < SpriteText > parts ) = > new DrawableLinkCompiler ( parts ) ;
2019-01-10 13:55:36 +08:00
// 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.
// Since the compilers don't display any content and don't affect the layout, it's simplest to exclude them from the flow.
2019-01-08 17:58:44 +08:00
public override IEnumerable < Drawable > FlowingChildren = > base . FlowingChildren . Where ( c = > ! ( c is DrawableLinkCompiler ) ) ;
2018-04-13 17:19:50 +08:00
}
}