TextLinkTag.cs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Diagnostics.CodeAnalysis;
  2. using JetBrains.Annotations;
  3. using Robust.Client.UserInterface;
  4. using Robust.Client.UserInterface.Controls;
  5. using Robust.Client.UserInterface.RichText;
  6. using Robust.Shared.Input;
  7. using Robust.Shared.Utility;
  8. namespace Content.Client.Guidebook.RichText;
  9. [UsedImplicitly]
  10. public sealed class TextLinkTag : IMarkupTag
  11. {
  12. public string Name => "textlink";
  13. public Control? Control;
  14. /// <inheritdoc/>
  15. public bool TryGetControl(MarkupNode node, [NotNullWhen(true)] out Control? control)
  16. {
  17. if (!node.Value.TryGetString(out var text)
  18. || !node.Attributes.TryGetValue("link", out var linkParameter)
  19. || !linkParameter.TryGetString(out var link))
  20. {
  21. control = null;
  22. return false;
  23. }
  24. var label = new Label();
  25. label.Text = text;
  26. label.MouseFilter = Control.MouseFilterMode.Stop;
  27. label.FontColorOverride = Color.CornflowerBlue;
  28. label.DefaultCursorShape = Control.CursorShape.Hand;
  29. label.OnMouseEntered += _ => label.FontColorOverride = Color.LightSkyBlue;
  30. label.OnMouseExited += _ => label.FontColorOverride = Color.CornflowerBlue;
  31. label.OnKeyBindDown += args => OnKeybindDown(args, link);
  32. control = label;
  33. Control = label;
  34. return true;
  35. }
  36. private void OnKeybindDown(GUIBoundKeyEventArgs args, string link)
  37. {
  38. if (args.Function != EngineKeyFunctions.UIClick)
  39. return;
  40. if (Control == null)
  41. return;
  42. var current = Control;
  43. while (current != null)
  44. {
  45. current = current.Parent;
  46. if (current is not ILinkClickHandler handler)
  47. continue;
  48. handler.HandleClick(link);
  49. return;
  50. }
  51. Logger.Warning($"Warning! No valid ILinkClickHandler found.");
  52. }
  53. }
  54. public interface ILinkClickHandler
  55. {
  56. public void HandleClick(string link);
  57. }