diff --git a/osu.Game.Tests/Visual/TestCaseChatLink.cs b/osu.Game.Tests/Visual/TestCaseChatLink.cs index 15a5e12546..31544c9364 100644 --- a/osu.Game.Tests/Visual/TestCaseChatLink.cs +++ b/osu.Game.Tests/Visual/TestCaseChatLink.cs @@ -6,6 +6,7 @@ using osu.Framework.Allocation; using osu.Framework.Graphics; using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Containers; +using osu.Game.Graphics.Containers; using osu.Game.Online.Chat; using osu.Game.Overlays; using osu.Game.Overlays.Chat; @@ -51,13 +52,11 @@ namespace osu.Game.Tests.Visual var newLine = new ChatLine(new DummyMessage(text, isAction)); textContainer.Add(newLine); - AddAssert($"check msg having {linkAmount} link(s)", () => newLine.Message.Links.Count == linkAmount); - // todo: tidy this lambda up - AddAssert("check link(s) displaying", () => newLine.ContentFlow.Any() - && newLine.ContentFlow - .Cast() - .All(sprite => sprite.HandleInput && !sprite.TextColour.Equals((SRGBColour)Color4.White) - || !sprite.HandleInput && sprite.TextColour.Equals((SRGBColour)Color4.White))); + AddAssert($"msg #{textContainer.Count} has {linkAmount} link(s)", () => newLine.Message.Links.Count == linkAmount); + AddAssert($"msg #{textContainer.Count} shows link(s)", () => newLine.ContentFlow.Any() && isShowingLinks(newLine.ContentFlow)); + + bool isShowingLinks(OsuTextFlowContainer c) => c.Cast().All(sprite => sprite.HandleInput && !sprite.TextColour.Equals((SRGBColour)Color4.White) + || !sprite.HandleInput && sprite.TextColour.Equals((SRGBColour)Color4.White)); } private void testLinksGeneral() diff --git a/osu.Game/Graphics/Containers/OsuLinkFlowContainer.cs b/osu.Game/Graphics/Containers/OsuLinkFlowContainer.cs new file mode 100644 index 0000000000..5f318668a2 --- /dev/null +++ b/osu.Game/Graphics/Containers/OsuLinkFlowContainer.cs @@ -0,0 +1,59 @@ +// Copyright (c) 2007-2017 ppy Pty Ltd . +// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK.Graphics; +using osu.Framework.Graphics.Colour; +using osu.Framework.Graphics.Sprites; +using osu.Game.Graphics.Sprites; +using System; +using System.Collections.Generic; + +namespace osu.Game.Graphics.Containers +{ + public class OsuLinkFlowContainer : OsuLinkFlowContainer + { + public OsuLinkFlowContainer(Action defaultCreationParameters = null) + : base(defaultCreationParameters) + { + } + } + + public class OsuLinkFlowContainer : OsuTextFlowContainer + where T : OsuSpriteLink, new() + { + public override bool HandleInput => true; + + public OsuLinkFlowContainer(Action defaultCreationParameters = null) : base(defaultCreationParameters) + { + } + + protected override SpriteText CreateSpriteText() => new T(); + + /// + /// The colour for text (links override this). Will only be used for new text elements. + /// + public ColourInfo TextColour = Color4.White; + + public IEnumerable AddLink(string text, string url, Action creationParameters = null) + { + // TODO: Remove this and get word wrapping working + text = text.Replace(' ', '_'); + + return AddText(text, link => + { + creationParameters?.Invoke(link); + ((T)link).Url = url; + }); + } + + public new IEnumerable AddText(string text, Action creationParameters = null) + { + return base.AddText(text, sprite => + { + ((OsuSpriteLink)sprite).TextColour = TextColour; + + creationParameters?.Invoke(sprite); + }); + } + } +} diff --git a/osu.Game/Graphics/Containers/OsuLinkTextFlowContainer.cs b/osu.Game/Graphics/Containers/OsuLinkTextFlowContainer.cs deleted file mode 100644 index bc23d128f2..0000000000 --- a/osu.Game/Graphics/Containers/OsuLinkTextFlowContainer.cs +++ /dev/null @@ -1,60 +0,0 @@ -// Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE - -using osu.Framework.Graphics.Colour; -using osu.Framework.Graphics.Sprites; -using osu.Game.Graphics.Sprites; -using System; -using System.Collections.Generic; - -namespace osu.Game.Graphics.Containers -{ - public class OsuLinkTextFlowContainer : OsuLinkTextFlowContainer - { - public OsuLinkTextFlowContainer(Action defaultCreationParameters = null) - : base(defaultCreationParameters) - { - } - } - - public class OsuLinkTextFlowContainer : OsuTextFlowContainer - where T : OsuLinkSpriteText, new() - { - public override bool HandleInput => true; - - public OsuLinkTextFlowContainer(Action defaultCreationParameters = null) : base(defaultCreationParameters) - { - } - - protected override SpriteText CreateSpriteText() => new T(); - - /// - /// The colour for normal text (links ignore this). Will only be used for new text elements. - /// Default is white. - /// - public ColourInfo? TextColour; - - public void AddLink(string text, string url, Action creationParameters = null) - { - // TODO: Remove this and get word wrapping working - text = text.Replace(' ', '_'); - - AddText(text, link => - { - creationParameters?.Invoke(link); - LoadComponentAsync(link, d => ((T)d).Url = url); - }); - } - - public IEnumerable AddText(string text, Action creationParameters = null) - { - return base.AddText(text, sprite => - { - if (TextColour.HasValue) - ((OsuLinkSpriteText)sprite).TextColour = TextColour.Value; - - creationParameters?.Invoke(sprite); - }); - } - } -} diff --git a/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs b/osu.Game/Graphics/Sprites/OsuSpriteLink.cs similarity index 90% rename from osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs rename to osu.Game/Graphics/Sprites/OsuSpriteLink.cs index ccf7c80d07..e42337dff3 100644 --- a/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs +++ b/osu.Game/Graphics/Sprites/OsuSpriteLink.cs @@ -10,7 +10,7 @@ using osu.Game.Graphics.Containers; namespace osu.Game.Graphics.Sprites { - public class OsuLinkSpriteText : OsuSpriteText + public class OsuSpriteLink : OsuSpriteText { protected override IEnumerable FlowingChildren => Children; @@ -18,7 +18,7 @@ namespace osu.Game.Graphics.Sprites private readonly Container content; - public OsuLinkSpriteText() + public OsuSpriteLink() { AddInternal(content = new OsuHoverContainer { diff --git a/osu.Game/Online/Chat/ChatLink.cs b/osu.Game/Online/Chat/ChatLink.cs index ccf146286b..aa5a0e100b 100644 --- a/osu.Game/Online/Chat/ChatLink.cs +++ b/osu.Game/Online/Chat/ChatLink.cs @@ -12,7 +12,7 @@ using System.Text.RegularExpressions; namespace osu.Game.Online.Chat { - public class ChatLink : OsuLinkSpriteText, IHasTooltip + public class ChatLink : OsuSpriteLink, IHasTooltip { private APIAccess api; private BeatmapSetOverlay beatmapSetOverlay; @@ -20,9 +20,13 @@ namespace osu.Game.Online.Chat public override bool HandleInput => !string.IsNullOrEmpty(Url); + // 'protocol' -> 'https', 'http', 'osu', 'osump' etc. + // 'content' -> everything after '://' + private Match getUrlMatch() => Regex.Match(Url, @"^(?osu(?:mp)?|https?):\/\/(?.*)"); + protected override void OnLinkClicked() { - var urlMatch = Regex.Matches(Url, @"^(?osu(?:mp)?|https?):\/\/(?.*)")[0]; + var urlMatch = getUrlMatch(); if (urlMatch.Success) { var args = urlMatch.Groups["content"].Value.Split('/'); @@ -41,11 +45,8 @@ namespace osu.Game.Online.Chat case "chan": var foundChannel = chat.AvailableChannels.Find(channel => channel.Name == args[1]); - if (foundChannel == null) - throw new ArgumentException($"Unknown channel name ({args[1]})."); - else - chat.OpenChannel(foundChannel); - + // links should be filtered out by now if a channel doesn't exist + chat.OpenChannel(foundChannel ?? throw new ArgumentException($"Unknown channel name ({args[1]}).")); break; case "edit": chat.Game?.LoadEditorTimestamp(); @@ -130,9 +131,10 @@ namespace osu.Game.Online.Chat if (Url == Text) return null; - if (Url.StartsWith("osu://")) + var urlMatch = getUrlMatch(); + if (urlMatch.Success && urlMatch.Groups["protocol"].Value == "osu") { - var args = Url.Substring(6).Split('/'); + var args = urlMatch.Groups["content"].Value.Split('/'); if (args.Length < 2) return Url; @@ -140,7 +142,7 @@ namespace osu.Game.Online.Chat if (args[0] == "chan") return "Switch to channel " + args[1]; if (args[0] == "edit") - return "Go to " + args[1].Remove(9).TrimEnd(); + return "Go to " + args[1]; } return Url; diff --git a/osu.Game/Overlays/Chat/ChatLine.cs b/osu.Game/Overlays/Chat/ChatLine.cs index 26759825ee..add0bb1fd8 100644 --- a/osu.Game/Overlays/Chat/ChatLine.cs +++ b/osu.Game/Overlays/Chat/ChatLine.cs @@ -83,7 +83,10 @@ namespace osu.Game.Overlays.Chat private Message message; private OsuSpriteText username; - private OsuLinkTextFlowContainer contentFlow; + private OsuLinkFlowContainer contentFlow; + + // this is only used for testing + public OsuTextFlowContainer ContentFlow => contentFlow; public Message Message { @@ -101,9 +104,6 @@ namespace osu.Game.Overlays.Chat } } - // this is only used for testing - public OsuTextFlowContainer ContentFlow => contentFlow; - [BackgroundDependencyLoader(true)] private void load(OsuColour colours, ChatOverlay chat) { @@ -193,7 +193,7 @@ namespace osu.Game.Overlays.Chat Padding = new MarginPadding { Left = message_padding + padding }, Children = new Drawable[] { - contentFlow = new OsuLinkTextFlowContainer(t => + contentFlow = new OsuLinkFlowContainer(t => { if (Message.IsAction) t.Font = "Exo2.0-MediumItalic"; diff --git a/osu.Game/Overlays/Profile/ProfileHeader.cs b/osu.Game/Overlays/Profile/ProfileHeader.cs index 2a682aa654..f1efbbc54a 100644 --- a/osu.Game/Overlays/Profile/ProfileHeader.cs +++ b/osu.Game/Overlays/Profile/ProfileHeader.cs @@ -23,7 +23,7 @@ namespace osu.Game.Overlays.Profile public class ProfileHeader : Container { private readonly OsuTextFlowContainer infoTextLeft; - private readonly OsuLinkTextFlowContainer infoTextRight; + private readonly OsuLinkFlowContainer infoTextRight; private readonly FillFlowContainer scoreText, scoreNumberText; private readonly RankGraph rankGraph; @@ -142,7 +142,7 @@ namespace osu.Game.Overlays.Profile ParagraphSpacing = 0.8f, LineSpacing = 0.2f }, - infoTextRight = new OsuLinkTextFlowContainer(t => + infoTextRight = new OsuLinkFlowContainer(t => { t.TextSize = 14; t.Font = @"Exo2.0-RegularItalic"; @@ -473,7 +473,7 @@ namespace osu.Game.Overlays.Profile } } - private class ProfileLink : OsuLinkSpriteText, IHasTooltip + private class ProfileLink : OsuSpriteLink, IHasTooltip { public string TooltipText => "View Profile in Browser"; diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index bc7df963da..d32c458ccf 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -270,8 +270,8 @@ - - + +