HyperText.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. [ExecuteInEditMode]
  8. public class HyperText : HyperTextBase
  9. {
  10. protected override void OnEnable()
  11. {
  12. base.OnEnable();
  13. string regexPattern = "<a\\s+[^>]*href\\s*=\\s*[\"'](?<href>[^\"']*)[\"'][^>]*>(?<text>.*?)</a>";
  14. this.SetClickableByRegex(regexPattern, new Action<string>(this.OnClickHyperLink), new Action<HyperTextBase.ClickableEntry>(this.OnEnterHyperLink), new Action<HyperTextBase.ClickableEntry>(this.OnExitHyperLink));
  15. }
  16. public void SetClickableByRegex(string regexPattern, Action<string> onClick, Action<HyperTextBase.ClickableEntry> onEnter, Action<HyperTextBase.ClickableEntry> onExit)
  17. {
  18. this.SetClickableByRegex(regexPattern, Color.cyan, onClick, onEnter, onExit);
  19. }
  20. public void SetClickableByRegex(string regexPattern, Color color, Action<string> onClick, Action<HyperTextBase.ClickableEntry> onEnter, Action<HyperTextBase.ClickableEntry> onExit)
  21. {
  22. if (string.IsNullOrEmpty(regexPattern) || onClick == null)
  23. {
  24. return;
  25. }
  26. this._entryTable[regexPattern] = new HyperText.Entry(regexPattern, color, onClick, onEnter, onExit);
  27. }
  28. public override void RemoveClickable()
  29. {
  30. base.RemoveClickable();
  31. this._entryTable.Clear();
  32. }
  33. protected override void RegisterClickable()
  34. {
  35. foreach (HyperText.Entry entry in this._entryTable.Values)
  36. {
  37. Regex regex = new Regex(entry.RegexPattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
  38. IEnumerator enumerator2 = regex.Matches(base.text.text).GetEnumerator();
  39. try
  40. {
  41. while (enumerator2.MoveNext())
  42. {
  43. object obj = enumerator2.Current;
  44. Match match = (Match)obj;
  45. Group group = match.Groups["text"];
  46. Group group2 = match.Groups["href"];
  47. base.RegisterClickable(group.Index, group.Value.Length, group2.Value, entry.Color, entry.OnClick, entry.OnEnter, entry.OnExit);
  48. }
  49. }
  50. finally
  51. {
  52. IDisposable disposable;
  53. if ((disposable = (enumerator2 as IDisposable)) != null)
  54. {
  55. disposable.Dispose();
  56. }
  57. }
  58. }
  59. }
  60. private void OnClickHyperLink(string href)
  61. {
  62. if (GameMain.Instance.CMSystem.NetUse)
  63. {
  64. Application.OpenURL(href);
  65. }
  66. else
  67. {
  68. Debug.Log("リンク先:" + href);
  69. }
  70. }
  71. private void OnEnterHyperLink(HyperTextBase.ClickableEntry entry)
  72. {
  73. }
  74. private void OnExitHyperLink(HyperTextBase.ClickableEntry entry)
  75. {
  76. }
  77. private readonly Dictionary<string, HyperText.Entry> _entryTable = new Dictionary<string, HyperText.Entry>();
  78. private struct Entry
  79. {
  80. public Entry(string regexPattern, Color color, Action<string> onClick, Action<HyperTextBase.ClickableEntry> onEnter, Action<HyperTextBase.ClickableEntry> onExit)
  81. {
  82. this.RegexPattern = regexPattern;
  83. this.Color = color;
  84. this.OnClick = onClick;
  85. this.OnEnter = onEnter;
  86. this.OnExit = onExit;
  87. }
  88. public string RegexPattern;
  89. public Color Color;
  90. public Action<string> OnClick;
  91. public Action<HyperTextBase.ClickableEntry> OnEnter;
  92. public Action<HyperTextBase.ClickableEntry> OnExit;
  93. }
  94. }