1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 20:07:29 +08:00
osu-lazer/osu.Game/Graphics/Containers/LinkFlowContainer.cs

115 lines
4.5 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +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.Linq;
using osu.Framework.Allocation;
using osu.Framework.Graphics.Sprites;
using System.Collections.Generic;
2018-06-19 19:19:52 +08:00
using osu.Framework.Platform;
2018-04-13 17:19:50 +08:00
using osu.Game.Overlays;
using osu.Game.Overlays.Notifications;
namespace osu.Game.Graphics.Containers
{
public class LinkFlowContainer : OsuTextFlowContainer
{
public LinkFlowContainer(Action<SpriteText> defaultCreationParameters = null)
: base(defaultCreationParameters)
{
}
public override bool HandleMouseInput => true;
private OsuGame game;
private ChannelManager channelManager;
2018-04-13 17:19:50 +08:00
private Action showNotImplementedError;
2018-06-19 19:19:52 +08:00
private GameHost host;
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader(true)]
private void load(OsuGame game, NotificationOverlay notifications, GameHost host, ChannelManager channelManager)
2018-04-13 17:19:50 +08:00
{
// will be null in tests
this.game = game;
2018-06-19 19:19:52 +08:00
this.host = host;
this.channelManager = channelManager;
2018-04-13 17:19:50 +08:00
showNotImplementedError = () => notifications?.Post(new SimpleNotification
{
Text = @"This link type is not yet supported!",
Icon = FontAwesome.fa_life_saver,
});
}
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;
}
AddText(text.Substring(previousLinkEnd));
}
2018-04-20 12:01:23 +08:00
public void AddLink(string text, string url, LinkAction linkType = LinkAction.External, string linkArgument = null, string tooltipText = null, Action<SpriteText> creationParameters = null)
2018-04-13 17:19:50 +08:00
{
2018-04-20 12:01:23 +08:00
AddInternal(new DrawableLinkCompiler(AddText(text, creationParameters).ToList())
2018-04-13 17:19:50 +08:00
{
TooltipText = tooltipText ?? (url != text ? url : string.Empty),
Action = () =>
{
switch (linkType)
{
case LinkAction.OpenBeatmap:
// TODO: proper query params handling
if (linkArgument != null && int.TryParse(linkArgument.Contains('?') ? linkArgument.Split('?')[0] : linkArgument, out int beatmapId))
game?.ShowBeatmap(beatmapId);
2018-04-13 17:19:50 +08:00
break;
case LinkAction.OpenBeatmapSet:
if (int.TryParse(linkArgument, out int setId))
game?.ShowBeatmapSet(setId);
break;
case LinkAction.OpenChannel:
try
{
channelManager.OpenChannel(linkArgument);
}
catch (ChannelNotFoundException)
{
//channel was not found
}
2018-04-13 17:19:50 +08:00
break;
case LinkAction.OpenEditorTimestamp:
case LinkAction.JoinMultiplayerMatch:
case LinkAction.Spectate:
showNotImplementedError?.Invoke();
break;
case LinkAction.External:
2018-06-19 19:19:52 +08:00
host.OpenUrlExternally(url);
2018-04-13 17:19:50 +08:00
break;
case LinkAction.OpenUserProfile:
if (long.TryParse(linkArgument, out long userId))
game?.ShowUser(userId);
break;
default:
throw new NotImplementedException($"This {nameof(LinkAction)} ({linkType.ToString()}) is missing an associated action.");
}
},
});
}
}
}