From 2129d6cede8ec2ffdfc30ded337afacd36eda80d Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Thu, 7 Dec 2017 16:23:43 +0100 Subject: [PATCH] Renamed the "on click" method to OnClick() and moved most of the implementation to ChatLink. Also fixed the tooltip text up --- .../Graphics/Sprites/OsuLinkSpriteText.cs | 125 +---------------- osu.Game/Online/Chat/ChatLink.cs | 130 +++++++++++++++++- 2 files changed, 127 insertions(+), 128 deletions(-) diff --git a/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs b/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs index ad5045ff10..cc19a50f24 100644 --- a/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs +++ b/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs @@ -25,13 +25,8 @@ namespace osu.Game.Graphics.Sprites { public class OsuLinkSpriteText : OsuSpriteText { - private ChatOverlay chat; - private readonly OsuHoverContainer content; - private APIAccess api; - private BeatmapSetOverlay beatmapSetOverlay; - public override bool HandleInput => content.Action != null; protected override Container Content => content ?? (Container)this; @@ -52,11 +47,7 @@ namespace osu.Game.Graphics.Sprites { url = value; - content.Action = onClickAction; - - // For inheriting classes - if (Content is OsuHoverContainer hover) - hover.Action = onClickAction; + content.Action = OnClick; } } } @@ -75,118 +66,6 @@ namespace osu.Game.Graphics.Sprites set { Content.Colour = value; } } - [BackgroundDependencyLoader] - private void load(APIAccess api, BeatmapSetOverlay beatmapSetOverlay, ChatOverlay chat) - { - this.api = api; - this.beatmapSetOverlay = beatmapSetOverlay; - this.chat = chat; - } - - private void onClickAction() - { - var url = Url; - - if (url.StartsWith("osu://")) - { - url = url.Substring(6); - var args = url.Split('/'); - - switch (args[0]) - { - case "chan": - var foundChannel = chat.AvailableChannels.Find(channel => channel.Name == args[1]); - - if (foundChannel == null) - TextColour = Color4.White; - else - chat.OpenChannel(foundChannel); - - break; - case "edit": - chat.Game?.LoadEditorTimestamp(); - break; - case "b": - if (args.Length > 1 && int.TryParse(args[1], out int mapId)) - beatmapSetOverlay.ShowBeatmap(mapId); - - break; - case "s": - case "dl": - if (args.Length > 1 && int.TryParse(args[1], out int mapSetId)) - beatmapSetOverlay.ShowBeatmapSet(mapSetId); - - break; - case "spectate": - GetUserRequest req; - if (int.TryParse(args[1], out int userId)) - req = new GetUserRequest(userId); - else - // Get by username instead - req = new GetUserRequest(args[1]); - - req.Success += user => - { - chat.Game?.LoadSpectatorScreen(); - }; - api.Queue(req); - - break; - default: - throw new ArgumentException($"Unknown osu:// link at {nameof(OsuLinkSpriteText)} (https://osu.ppy.sh/{args[0]})."); - } - } - else if (url.StartsWith("osump://")) - { - url = url.Substring(8); - if (!int.TryParse(url.Split('/').ElementAtOrDefault(1), out int multiId)) - return; - - chat.Game?.LoadMultiplayerLobby(); - } - else if (url.StartsWith("http://") || url.StartsWith("https://")) - { - var osuUrlIndex = url.IndexOf("osu.ppy.sh/"); - if (osuUrlIndex == -1) - { - Process.Start(url); - return; - } - - url = url.Substring(osuUrlIndex + 11); - if (url.StartsWith("s/") || url.StartsWith("beatmapsets/") || url.StartsWith("d/")) - { - var id = getIdFromUrl(url); - beatmapSetOverlay.ShowBeatmapSet(id); - } - else if (url.StartsWith("b/") || url.StartsWith("beatmaps/")) - { - var id = getIdFromUrl(url); - beatmapSetOverlay.ShowBeatmap(id); - } - else - Process.Start($"https://osu.ppy.sh/{url}"); - } - else - Process.Start(url); - } - - private int getIdFromUrl(string url) - { - var lastSlashIndex = url.LastIndexOf('/'); - // Remove possible trailing slash - if (lastSlashIndex == url.Length) - { - url = url.Remove(url.Length - 1); - lastSlashIndex = url.LastIndexOf('/'); - } - - var lastQuestionMarkIndex = url.LastIndexOf('?'); - // Filter out possible queries like mode specifications (e.g. /b/252238?m=0) - if (lastQuestionMarkIndex > lastSlashIndex) - url = url.Remove(lastQuestionMarkIndex); - - return int.Parse(url.Substring(lastSlashIndex + 1)); - } + protected virtual void OnClick() => Process.Start(Url); } } diff --git a/osu.Game/Online/Chat/ChatLink.cs b/osu.Game/Online/Chat/ChatLink.cs index bfee170fd1..4aa182b994 100644 --- a/osu.Game/Online/Chat/ChatLink.cs +++ b/osu.Game/Online/Chat/ChatLink.cs @@ -11,9 +11,13 @@ using osu.Game.Graphics; using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests; +using osu.Game.Overlays; using osu.Game.Overlays.Chat; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; namespace osu.Game.Online.Chat @@ -22,6 +26,10 @@ namespace osu.Game.Online.Chat { public int LinkId = -1; + private APIAccess api; + private BeatmapSetOverlay beatmapSetOverlay; + private ChatOverlay chat; + private Color4 hoverColour; private Color4 urlColour; @@ -34,6 +42,107 @@ namespace osu.Game.Online.Chat protected override Container Content => content ?? base.Content; + protected override void OnClick() + { + var url = Url; + + if (url.StartsWith("osu://")) + { + url = url.Substring(6); + var args = url.Split('/'); + + switch (args[0]) + { + 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); + + break; + case "edit": + chat.Game?.LoadEditorTimestamp(); + break; + case "b": + if (args.Length > 1 && int.TryParse(args[1], out int mapId)) + beatmapSetOverlay.ShowBeatmap(mapId); + + break; + case "s": + case "dl": + if (args.Length > 1 && int.TryParse(args[1], out int mapSetId)) + beatmapSetOverlay.ShowBeatmapSet(mapSetId); + + break; + case "spectate": + GetUserRequest req; + if (int.TryParse(args[1], out int userId)) + req = new GetUserRequest(userId); + else + // Get by username instead + req = new GetUserRequest(args[1]); + + req.Success += user => + { + chat.Game?.LoadSpectatorScreen(); + }; + api.Queue(req); + + break; + default: + throw new ArgumentException($"Unknown osu:// link at {nameof(OsuLinkSpriteText)} (https://osu.ppy.sh/{args[0]})."); + } + } + else if (url.StartsWith("osump://")) + { + url = url.Substring(8); + if (!int.TryParse(url.Split('/').ElementAtOrDefault(1), out int multiId)) + return; + + chat.Game?.LoadMultiplayerLobby(); + } + else if (url.StartsWith("http://") || url.StartsWith("https://") && url.IndexOf("osu.ppy.sh/") != -1) + { + var osuUrlIndex = url.IndexOf("osu.ppy.sh/"); + + url = url.Substring(osuUrlIndex + 11); + if (url.StartsWith("s/") || url.StartsWith("beatmapsets/") || url.StartsWith("d/")) + { + var id = getIdFromUrl(url); + beatmapSetOverlay.ShowBeatmapSet(id); + } + else if (url.StartsWith("b/") || url.StartsWith("beatmaps/")) + { + var id = getIdFromUrl(url); + beatmapSetOverlay.ShowBeatmap(id); + } + else + base.OnClick(); + } + else + base.OnClick(); + } + + private int getIdFromUrl(string url) + { + var lastSlashIndex = url.LastIndexOf('/'); + // Remove possible trailing slash + if (lastSlashIndex == url.Length) + { + url = url.Remove(url.Length - 1); + lastSlashIndex = url.LastIndexOf('/'); + } + + var lastQuestionMarkIndex = url.LastIndexOf('?'); + // Filter out possible queries like mode specifications (e.g. /b/252238?m=0) + if (lastQuestionMarkIndex > lastSlashIndex) + url = url.Remove(lastQuestionMarkIndex); + + return int.Parse(url.Substring(lastSlashIndex + 1)); + } + public string TooltipText { get @@ -41,11 +150,18 @@ namespace osu.Game.Online.Chat if (LinkId == -1 || Url == Text) return null; - if (Url.StartsWith("osu://chan/")) - return "Switch to channel " + Url.Substring(11); + if (Url.StartsWith("osu://")) + { + var args = Url.Substring(6).Split('/'); - if (Url.StartsWith("osu://edit/") && TimeSpan.TryParse(Url.Substring(11).Split(' ')[0], out TimeSpan editorTimeStamp)) - return "Go to " + editorTimeStamp.ToString(); + if (args.Length < 2) + return Url; + + if (args[0] == "chan") + return "Switch to channel " + args[1]; + if (args[0] == "edit") + return "Go to " + args[1].Remove(9).TrimEnd(); + } return Url; } @@ -96,8 +212,12 @@ namespace osu.Game.Online.Chat } [BackgroundDependencyLoader] - private void load(OsuColour colours) + private void load(APIAccess api, BeatmapSetOverlay beatmapSetOverlay, ChatOverlay chat, OsuColour colours) { + this.api = api; + this.beatmapSetOverlay = beatmapSetOverlay; + this.chat = chat; + hoverColour = colours.Yellow; urlColour = colours.Blue; if (LinkId != -1)