2018-01-09 19:22:23 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using osu.Game.Online.Chat;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
2018-01-09 23:11:45 +08:00
|
|
|
|
using System.Collections.Generic;
|
2018-01-09 19:22:23 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Graphics.Containers
|
|
|
|
|
{
|
|
|
|
|
public class LinkFlowContainer : OsuTextFlowContainer
|
|
|
|
|
{
|
|
|
|
|
public LinkFlowContainer(Action<SpriteText> defaultCreationParameters = null)
|
|
|
|
|
: base(defaultCreationParameters)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-17 15:51:47 +08:00
|
|
|
|
public override bool HandleMouseInput => true;
|
2018-01-09 19:22:23 +08:00
|
|
|
|
|
|
|
|
|
private OsuGame game;
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader(true)]
|
2018-01-09 23:11:45 +08:00
|
|
|
|
private void load(OsuGame game)
|
2018-01-09 19:22:23 +08:00
|
|
|
|
{
|
2018-01-09 23:11:45 +08:00
|
|
|
|
// will be null in tests
|
2018-01-09 19:22:23 +08:00
|
|
|
|
this.game = game;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 23:11:45 +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;
|
|
|
|
|
foreach (var link in links)
|
|
|
|
|
{
|
|
|
|
|
AddText(text.Substring(previousLinkEnd, link.Index - previousLinkEnd));
|
|
|
|
|
|
|
|
|
|
AddLink(text.Substring(link.Index, link.Length), link.Url, link.Action, link.Argument);
|
|
|
|
|
previousLinkEnd = link.Index + link.Length;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 19:22:23 +08:00
|
|
|
|
public void AddLink(string text, string url, LinkAction linkType = LinkAction.External, string linkArgument = null, string tooltipText = null)
|
|
|
|
|
{
|
|
|
|
|
AddInternal(new DrawableLinkCompiler(AddText(text).ToList())
|
|
|
|
|
{
|
|
|
|
|
TooltipText = tooltipText ?? (url != text ? url : string.Empty),
|
|
|
|
|
Action = () =>
|
|
|
|
|
{
|
|
|
|
|
switch (linkType)
|
|
|
|
|
{
|
|
|
|
|
case LinkAction.OpenBeatmap:
|
2018-01-17 18:37:20 +08:00
|
|
|
|
// todo: replace this with overlay.ShowBeatmap(id) once an appropriate API call is implemented.
|
|
|
|
|
if (int.TryParse(linkArgument, out int beatmapId))
|
|
|
|
|
Process.Start($"https://osu.ppy.sh/b/{beatmapId}");
|
2018-01-09 19:22:23 +08:00
|
|
|
|
break;
|
|
|
|
|
case LinkAction.OpenBeatmapSet:
|
|
|
|
|
if (int.TryParse(linkArgument, out int setId))
|
2018-01-09 23:11:45 +08:00
|
|
|
|
game?.ShowBeatmapSet(setId);
|
2018-01-09 19:22:23 +08:00
|
|
|
|
break;
|
|
|
|
|
case LinkAction.OpenChannel:
|
2018-01-09 23:11:45 +08:00
|
|
|
|
game?.OpenChannel(linkArgument);
|
2018-01-09 19:22:23 +08:00
|
|
|
|
break;
|
|
|
|
|
case LinkAction.OpenEditorTimestamp:
|
|
|
|
|
game?.LoadEditorTimestamp();
|
|
|
|
|
break;
|
|
|
|
|
case LinkAction.JoinMultiplayerMatch:
|
|
|
|
|
if (int.TryParse(linkArgument, out int matchId))
|
|
|
|
|
game?.JoinMultiplayerMatch(matchId);
|
|
|
|
|
break;
|
|
|
|
|
case LinkAction.Spectate:
|
|
|
|
|
// todo: implement this when spectating exists
|
|
|
|
|
break;
|
|
|
|
|
case LinkAction.External:
|
|
|
|
|
Process.Start(url);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new NotImplementedException($"This {nameof(LinkAction)} ({linkType.ToString()}) is missing an associated action.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|