Global Game Jam 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
9.1 KiB

  1. using UnityEngine;
  2. using UnityEngine.Events;
  3. using UnityEngine.EventSystems;
  4. using System;
  5. namespace TMPro
  6. {
  7. public class TMP_TextEventHandler : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
  8. {
  9. [Serializable]
  10. public class CharacterSelectionEvent : UnityEvent<char, int> { }
  11. [Serializable]
  12. public class SpriteSelectionEvent : UnityEvent<char, int> { }
  13. [Serializable]
  14. public class WordSelectionEvent : UnityEvent<string, int, int> { }
  15. [Serializable]
  16. public class LineSelectionEvent : UnityEvent<string, int, int> { }
  17. [Serializable]
  18. public class LinkSelectionEvent : UnityEvent<string, string, int> { }
  19. /// <summary>
  20. /// Event delegate triggered when pointer is over a character.
  21. /// </summary>
  22. public CharacterSelectionEvent onCharacterSelection
  23. {
  24. get { return m_OnCharacterSelection; }
  25. set { m_OnCharacterSelection = value; }
  26. }
  27. [SerializeField]
  28. private CharacterSelectionEvent m_OnCharacterSelection = new CharacterSelectionEvent();
  29. /// <summary>
  30. /// Event delegate triggered when pointer is over a sprite.
  31. /// </summary>
  32. public SpriteSelectionEvent onSpriteSelection
  33. {
  34. get { return m_OnSpriteSelection; }
  35. set { m_OnSpriteSelection = value; }
  36. }
  37. [SerializeField]
  38. private SpriteSelectionEvent m_OnSpriteSelection = new SpriteSelectionEvent();
  39. /// <summary>
  40. /// Event delegate triggered when pointer is over a word.
  41. /// </summary>
  42. public WordSelectionEvent onWordSelection
  43. {
  44. get { return m_OnWordSelection; }
  45. set { m_OnWordSelection = value; }
  46. }
  47. [SerializeField]
  48. private WordSelectionEvent m_OnWordSelection = new WordSelectionEvent();
  49. /// <summary>
  50. /// Event delegate triggered when pointer is over a line.
  51. /// </summary>
  52. public LineSelectionEvent onLineSelection
  53. {
  54. get { return m_OnLineSelection; }
  55. set { m_OnLineSelection = value; }
  56. }
  57. [SerializeField]
  58. private LineSelectionEvent m_OnLineSelection = new LineSelectionEvent();
  59. /// <summary>
  60. /// Event delegate triggered when pointer is over a link.
  61. /// </summary>
  62. public LinkSelectionEvent onLinkSelection
  63. {
  64. get { return m_OnLinkSelection; }
  65. set { m_OnLinkSelection = value; }
  66. }
  67. [SerializeField]
  68. private LinkSelectionEvent m_OnLinkSelection = new LinkSelectionEvent();
  69. private TMP_Text m_TextComponent;
  70. private Camera m_Camera;
  71. private Canvas m_Canvas;
  72. private int m_selectedLink = -1;
  73. private int m_lastCharIndex = -1;
  74. private int m_lastWordIndex = -1;
  75. private int m_lastLineIndex = -1;
  76. void Awake()
  77. {
  78. // Get a reference to the text component.
  79. m_TextComponent = gameObject.GetComponent<TMP_Text>();
  80. // Get a reference to the camera rendering the text taking into consideration the text component type.
  81. if (m_TextComponent.GetType() == typeof(TextMeshProUGUI))
  82. {
  83. m_Canvas = gameObject.GetComponentInParent<Canvas>();
  84. if (m_Canvas != null)
  85. {
  86. if (m_Canvas.renderMode == RenderMode.ScreenSpaceOverlay)
  87. m_Camera = null;
  88. else
  89. m_Camera = m_Canvas.worldCamera;
  90. }
  91. }
  92. else
  93. {
  94. m_Camera = Camera.main;
  95. }
  96. }
  97. void LateUpdate()
  98. {
  99. if (TMP_TextUtilities.IsIntersectingRectTransform(m_TextComponent.rectTransform, Input.mousePosition, m_Camera))
  100. {
  101. #region Example of Character or Sprite Selection
  102. int charIndex = TMP_TextUtilities.FindIntersectingCharacter(m_TextComponent, Input.mousePosition, m_Camera, true);
  103. if (charIndex != -1 && charIndex != m_lastCharIndex)
  104. {
  105. m_lastCharIndex = charIndex;
  106. TMP_TextElementType elementType = m_TextComponent.textInfo.characterInfo[charIndex].elementType;
  107. // Send event to any event listeners depending on whether it is a character or sprite.
  108. if (elementType == TMP_TextElementType.Character)
  109. SendOnCharacterSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
  110. else if (elementType == TMP_TextElementType.Sprite)
  111. SendOnSpriteSelection(m_TextComponent.textInfo.characterInfo[charIndex].character, charIndex);
  112. }
  113. #endregion
  114. #region Example of Word Selection
  115. // Check if Mouse intersects any words and if so assign a random color to that word.
  116. int wordIndex = TMP_TextUtilities.FindIntersectingWord(m_TextComponent, Input.mousePosition, m_Camera);
  117. if (wordIndex != -1 && wordIndex != m_lastWordIndex)
  118. {
  119. m_lastWordIndex = wordIndex;
  120. // Get the information about the selected word.
  121. TMP_WordInfo wInfo = m_TextComponent.textInfo.wordInfo[wordIndex];
  122. // Send the event to any listeners.
  123. SendOnWordSelection(wInfo.GetWord(), wInfo.firstCharacterIndex, wInfo.characterCount);
  124. }
  125. #endregion
  126. #region Example of Line Selection
  127. // Check if Mouse intersects any words and if so assign a random color to that word.
  128. int lineIndex = TMP_TextUtilities.FindIntersectingLine(m_TextComponent, Input.mousePosition, m_Camera);
  129. if (lineIndex != -1 && lineIndex != m_lastLineIndex)
  130. {
  131. m_lastLineIndex = lineIndex;
  132. // Get the information about the selected word.
  133. TMP_LineInfo lineInfo = m_TextComponent.textInfo.lineInfo[lineIndex];
  134. // Send the event to any listeners.
  135. char[] buffer = new char[lineInfo.characterCount];
  136. for (int i = 0; i < lineInfo.characterCount && i < m_TextComponent.textInfo.characterInfo.Length; i++)
  137. {
  138. buffer[i] = m_TextComponent.textInfo.characterInfo[i + lineInfo.firstCharacterIndex].character;
  139. }
  140. string lineText = new string(buffer);
  141. SendOnLineSelection(lineText, lineInfo.firstCharacterIndex, lineInfo.characterCount);
  142. }
  143. #endregion
  144. #region Example of Link Handling
  145. // Check if mouse intersects with any links.
  146. int linkIndex = TMP_TextUtilities.FindIntersectingLink(m_TextComponent, Input.mousePosition, m_Camera);
  147. // Handle new Link selection.
  148. if (linkIndex != -1 && linkIndex != m_selectedLink)
  149. {
  150. m_selectedLink = linkIndex;
  151. // Get information about the link.
  152. TMP_LinkInfo linkInfo = m_TextComponent.textInfo.linkInfo[linkIndex];
  153. // Send the event to any listeners.
  154. SendOnLinkSelection(linkInfo.GetLinkID(), linkInfo.GetLinkText(), linkIndex);
  155. }
  156. #endregion
  157. }
  158. else
  159. {
  160. // Reset all selections given we are hovering outside the text container bounds.
  161. m_selectedLink = -1;
  162. m_lastCharIndex = -1;
  163. m_lastWordIndex = -1;
  164. m_lastLineIndex = -1;
  165. }
  166. }
  167. public void OnPointerEnter(PointerEventData eventData)
  168. {
  169. //Debug.Log("OnPointerEnter()");
  170. }
  171. public void OnPointerExit(PointerEventData eventData)
  172. {
  173. //Debug.Log("OnPointerExit()");
  174. }
  175. private void SendOnCharacterSelection(char character, int characterIndex)
  176. {
  177. if (onCharacterSelection != null)
  178. onCharacterSelection.Invoke(character, characterIndex);
  179. }
  180. private void SendOnSpriteSelection(char character, int characterIndex)
  181. {
  182. if (onSpriteSelection != null)
  183. onSpriteSelection.Invoke(character, characterIndex);
  184. }
  185. private void SendOnWordSelection(string word, int charIndex, int length)
  186. {
  187. if (onWordSelection != null)
  188. onWordSelection.Invoke(word, charIndex, length);
  189. }
  190. private void SendOnLineSelection(string line, int charIndex, int length)
  191. {
  192. if (onLineSelection != null)
  193. onLineSelection.Invoke(line, charIndex, length);
  194. }
  195. private void SendOnLinkSelection(string linkID, string linkText, int linkIndex)
  196. {
  197. if (onLinkSelection != null)
  198. onLinkSelection.Invoke(linkID, linkText, linkIndex);
  199. }
  200. }
  201. }