Guidebook links (#17162)

* Guidebook links

* warning!
This commit is contained in:
Nemanja
2023-06-07 03:51:11 -04:00
committed by GitHub
parent f7418588ec
commit ba9a369d0e
3 changed files with 118 additions and 2 deletions

View File

@@ -0,0 +1,70 @@
using System.Diagnostics.CodeAnalysis;
using JetBrains.Annotations;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.RichText;
using Robust.Shared.Input;
using Robust.Shared.Utility;
namespace Content.Client.Guidebook.RichText;
[UsedImplicitly]
public sealed class TextLinkTag : IMarkupTag
{
public string Name => "textlink";
public Control? Control;
/// <inheritdoc/>
public bool TryGetControl(MarkupNode node, [NotNullWhen(true)] out Control? control)
{
if (!node.Value.TryGetString(out var text)
|| !node.Attributes.TryGetValue("link", out var linkParameter)
|| !linkParameter.TryGetString(out var link))
{
control = null;
return false;
}
var label = new Label();
label.Text = text;
label.MouseFilter = Control.MouseFilterMode.Stop;
label.FontColorOverride = Color.CornflowerBlue;
label.DefaultCursorShape = Control.CursorShape.Hand;
label.OnMouseEntered += _ => label.FontColorOverride = Color.LightSkyBlue;
label.OnMouseExited += _ => label.FontColorOverride = Color.CornflowerBlue;
label.OnKeyBindDown += args => OnKeybindDown(args, link);
control = label;
Control = label;
return true;
}
private void OnKeybindDown(GUIBoundKeyEventArgs args, string link)
{
if (args.Function != EngineKeyFunctions.UIClick)
return;
if (Control == null)
return;
var current = Control;
while (current != null)
{
current = current.Parent;
if (current is not ILinkClickHandler handler)
continue;
handler.HandleClick(link);
return;
}
Logger.Warning($"Warning! No valid ILinkClickHandler found.");
}
}
public interface ILinkClickHandler
{
public void HandleClick(string link);
}