From b04ddba2a0b6062b90e5bc45dc7eb52a075d2c20 Mon Sep 17 00:00:00 2001 From: FreezyLemon Date: Wed, 6 Dec 2017 10:27:30 +0100 Subject: [PATCH] Added basic "on click" actions to the in-chat links --- .../Graphics/Sprites/OsuLinkSpriteText.cs | 102 ++++++++++++------ .../Online/API/Requests/GetUserRequest.cs | 11 +- 2 files changed, 82 insertions(+), 31 deletions(-) diff --git a/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs b/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs index 17f7cf88d5..da11bc5043 100644 --- a/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs +++ b/osu.Game/Graphics/Sprites/OsuLinkSpriteText.cs @@ -7,9 +7,13 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Colour; using osu.Framework.Graphics.Containers; using osu.Framework.Input; +using osu.Framework.Screens; using osu.Game.Beatmaps; using osu.Game.Graphics.Containers; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests; using osu.Game.Overlays; +using osu.Game.Screens.Edit; using System; using System.Collections.Generic; using System.Diagnostics; @@ -25,6 +29,7 @@ namespace osu.Game.Graphics.Sprites private readonly OsuHoverContainer content; + private APIAccess api; private BeatmapSetOverlay beatmapSetOverlay; public override bool HandleInput => content.Action != null; @@ -43,10 +48,10 @@ namespace osu.Game.Graphics.Sprites } set { - if (value != null) + if (!string.IsNullOrEmpty(value)) { url = value; - loadAction(); + content.Action = onClickAction; } } } @@ -66,63 +71,100 @@ namespace osu.Game.Graphics.Sprites } [BackgroundDependencyLoader] - private void load(BeatmapSetOverlay beatmapSetOverlay, ChatOverlay chat) + private void load(APIAccess api, BeatmapSetOverlay beatmapSetOverlay, ChatOverlay chat) { + this.api = api; this.beatmapSetOverlay = beatmapSetOverlay; this.chat = chat; } - private void loadAction() + private void onClickAction() { - if (Url == null || String.IsNullOrEmpty(Url)) - return; - var url = Url; - // Client-internal stuff if (url.StartsWith("osu://")) { - var firstPath = url.Substring(6, 5); - url = url.Substring(11); + url = url.Substring(6); + var args = url.Split('/'); - if (firstPath == "chan/") + switch (args[0]) { - var nextSlashIndex = url.IndexOf('/'); - var channelName = nextSlashIndex != -1 ? url.Remove(nextSlashIndex) : url; + case "chan": + var foundChannel = chat.AvailableChannels.Find(channel => channel.Name == args[1]); - var foundChannel = chat.AvailableChannels.Find(channel => channel.Name == channelName); + if (foundChannel == null) + TextColour = Color4.White; + else + chat.OpenChannel(foundChannel); - if (foundChannel != null) - content.Action = () => chat.OpenChannel(foundChannel); - } - else if (firstPath == "edit/") - { - // Open editor here, then goto specified time - // how to push new screen from here? we'll see - content.Action = () => new Framework.Screens.Screen().Push(new Screens.Edit.Editor()); - } - else - throw new ArgumentException($"Unknown osu:// link at {nameof(OsuLinkSpriteText)} ({firstPath})."); + break; + case "edit": + // TODO: Change screen to editor + 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 => + { + // TODO: Open spectator screen and start spectating + + }; + // 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; + + // TODO: Join the specified multiplayer lobby here with multiId } else if (url.StartsWith("http://") || url.StartsWith("https://")) { var osuUrlIndex = url.IndexOf("osu.ppy.sh/"); if (osuUrlIndex == -1) { - content.Action = () => Process.Start(url); + Process.Start(url); return; } url = url.Substring(osuUrlIndex + 11); if (url.StartsWith("s/") || url.StartsWith("beatmapsets/") || url.StartsWith("d/")) - content.Action = () => beatmapSetOverlay.ShowBeatmapSet(getIdFromUrl(url)); + { + var id = getIdFromUrl(url); + beatmapSetOverlay.ShowBeatmapSet(id); + } else if (url.StartsWith("b/") || url.StartsWith("beatmaps/")) - content.Action = () => beatmapSetOverlay.ShowBeatmap(getIdFromUrl(url)); + { + var id = getIdFromUrl(url); + beatmapSetOverlay.ShowBeatmap(id); + } else - content.Action = () => Process.Start($"https://osu.ppy.sh/{url}"); + Process.Start($"https://osu.ppy.sh/{url}"); } else - content.Action = () => Process.Start(url); + Process.Start(url); } private int getIdFromUrl(string url) diff --git a/osu.Game/Online/API/Requests/GetUserRequest.cs b/osu.Game/Online/API/Requests/GetUserRequest.cs index 2e3e7b01c8..47038b8ccb 100644 --- a/osu.Game/Online/API/Requests/GetUserRequest.cs +++ b/osu.Game/Online/API/Requests/GetUserRequest.cs @@ -8,12 +8,21 @@ namespace osu.Game.Online.API.Requests public class GetUserRequest : APIRequest { private long? userId; + private string userName; + /// The user's ID. public GetUserRequest(long? userId = null) { this.userId = userId; } - protected override string Target => userId.HasValue ? $@"users/{userId}" : @"me"; + /// The user's username. + public GetUserRequest(string userName) + { + this.userName = userName; + } + + // Prefer ID over name + protected override string Target => userId.HasValue ? $@"users/{userId}" : ((!string.IsNullOrEmpty(userName)) ? $@"users/{userName}" : @"me"); } }