BasicTooltipReceiver.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //ToolTip is written by Emiliano Pastorelli, H&R Tallinn (Estonia), http://www.hammerandravens.com
  2. //Copyright (c) 2015 Emiliano Pastorelli, H&R - Hammer&Ravens, Tallinn, Estonia.
  3. //All rights reserved.
  4. //Redistribution and use in source and binary forms are permitted
  5. //provided that the above copyright notice and this paragraph are
  6. //duplicated in all such forms and that any documentation,
  7. //advertising materials, and other materials related to such
  8. //distribution and use acknowledge that the software was developed
  9. //by H&R, Hammer&Ravens. The name of the
  10. //H&R, Hammer&Ravens may not be used to endorse or promote products derived
  11. //from this software without specific prior written permission.
  12. //THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  13. //IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  14. //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  15. namespace UnityEngine.UI.Extensions
  16. {
  17. [RequireComponent(typeof(RectTransform))]
  18. public class BasicTooltipReceiver : MonoBehaviour
  19. {
  20. private RectTransform _rectTransform, canvasRectTransform;
  21. [Tooltip("The canvas used by the tooltip as positioning and scaling reference. Should usually be the root Canvas of the hierarchy this component is in")]
  22. private Canvas canvas;
  23. [Tooltip("Sets if tooltip triggers will run ForceUpdateCanvases and refresh the tooltip's layout group " +
  24. "(if any) when hovered, in order to prevent momentousness misplacement sometimes caused by ContentSizeFitters")]
  25. public bool tooltipTriggersCanForceCanvasUpdate = false;
  26. /// <summary>
  27. /// the tooltip's Layout Group, if any
  28. /// </summary>
  29. private LayoutGroup _layoutGroup;
  30. //if the tooltip is inside a UI element
  31. private bool _inside;
  32. private float width, height;//, canvasWidth, canvasHeight;
  33. public float YShift = 4,xShift = -20;
  34. [HideInInspector]
  35. public RenderMode guiMode;
  36. private Camera _guiCamera;
  37. public Camera GuiCamera
  38. {
  39. get
  40. {
  41. if (!_guiCamera) {
  42. _guiCamera = Camera.main;
  43. }
  44. return _guiCamera;
  45. }
  46. }
  47. private Vector3 screenLowerLeft, screenUpperRight, shiftingVector;
  48. /// <summary>
  49. /// a screen-space point where the tooltip would be placed before applying X and Y shifts and border checks
  50. /// </summary>
  51. private Vector3 baseTooltipPos;
  52. private Vector3 newTTPos;
  53. private Vector3 adjustedNewTTPos;
  54. private Vector3 adjustedTTLocalPos;
  55. private Vector3 shifterForBorders;
  56. private float borderTest;
  57. void Reset() {
  58. canvas = GetComponentInParent<Canvas>();
  59. canvas = canvas.rootCanvas;
  60. }
  61. // Use this for initialization
  62. public void Awake()
  63. {
  64. if (!canvas) {
  65. canvas = GetComponentInParent<Canvas>();
  66. canvas = canvas.rootCanvas;
  67. }
  68. _guiCamera = canvas.worldCamera;
  69. guiMode = canvas.renderMode;
  70. _rectTransform = GetComponent<RectTransform>();
  71. canvasRectTransform = canvas.GetComponent<RectTransform>();
  72. _layoutGroup = GetComponentInChildren<LayoutGroup>();
  73. _inside = false;
  74. gameObject.SetActive(false);
  75. // Ç¿ÖÆÖö¥
  76. transform.SetParent(canvasRectTransform, false);
  77. transform.SetAsLastSibling();
  78. }
  79. //Call this function externally to set the text of the template and activate the tooltip
  80. public void SetTooltip(Vector3 basePos, bool refreshCanvasesBeforeGetSize = false)
  81. {
  82. baseTooltipPos = basePos;
  83. ContextualTooltipUpdate(refreshCanvasesBeforeGetSize);
  84. }
  85. //call this function on mouse exit to deactivate the template
  86. public void HideTooltip()
  87. {
  88. gameObject.SetActive(false);
  89. _inside = false;
  90. }
  91. // Update is called once per frame
  92. void Update()
  93. {
  94. if (_inside)
  95. {
  96. ContextualTooltipUpdate();
  97. }
  98. }
  99. /// <summary>
  100. /// forces rebuilding of Canvases in order to update the tooltip's content size fitting.
  101. /// Can prevent the tooltip from being visibly misplaced for one frame when being resized.
  102. /// Only runs if tooltipTriggersCanForceCanvasUpdate is true
  103. /// </summary>
  104. public void RefreshTooltipSize() {
  105. if (tooltipTriggersCanForceCanvasUpdate) {
  106. Canvas.ForceUpdateCanvases();
  107. if (_layoutGroup) {
  108. _layoutGroup.enabled = false;
  109. _layoutGroup.enabled = true;
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// Runs the appropriate tooltip placement method, according to the parent canvas's render mode
  115. /// </summary>
  116. /// <param name="refreshCanvasesBeforeGettingSize"></param>
  117. public void ContextualTooltipUpdate(bool refreshCanvasesBeforeGettingSize = false) {
  118. switch (guiMode) {
  119. case RenderMode.ScreenSpaceCamera:
  120. OnScreenSpaceCamera(refreshCanvasesBeforeGettingSize);
  121. break;
  122. case RenderMode.ScreenSpaceOverlay:
  123. OnScreenSpaceOverlay(refreshCanvasesBeforeGettingSize);
  124. break;
  125. }
  126. }
  127. //main tooltip edge of screen guard and movement - camera
  128. public void OnScreenSpaceCamera(bool refreshCanvasesBeforeGettingSize = false)
  129. {
  130. shiftingVector.x = xShift;
  131. shiftingVector.y = YShift;
  132. baseTooltipPos.z = canvas.planeDistance;
  133. newTTPos = GuiCamera.ScreenToViewportPoint(baseTooltipPos - shiftingVector);
  134. adjustedNewTTPos = GuiCamera.ViewportToWorldPoint(newTTPos);
  135. gameObject.SetActive(true);
  136. if (refreshCanvasesBeforeGettingSize) RefreshTooltipSize();
  137. //consider scaled dimensions when comparing against the edges
  138. width = transform.lossyScale.x * _rectTransform.sizeDelta[0];
  139. height = transform.lossyScale.y * _rectTransform.sizeDelta[1];
  140. // check and solve problems for the tooltip that goes out of the screen on the horizontal axis
  141. RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, Vector2.zero, GuiCamera, out screenLowerLeft);
  142. RectTransformUtility.ScreenPointToWorldPointInRectangle(canvasRectTransform, new Vector2(Screen.width, Screen.height), GuiCamera, out screenUpperRight);
  143. //check for right edge of screen
  144. borderTest = (adjustedNewTTPos.x + width);
  145. if (borderTest > screenUpperRight.x)
  146. {
  147. shifterForBorders.x = borderTest - screenUpperRight.x;
  148. adjustedNewTTPos.x -= shifterForBorders.x;
  149. }
  150. //check for left edge of screen
  151. borderTest = (adjustedNewTTPos.x);
  152. if (borderTest < screenLowerLeft.x)
  153. {
  154. shifterForBorders.x = screenLowerLeft.x - borderTest;
  155. adjustedNewTTPos.x += shifterForBorders.x;
  156. }
  157. // check and solve problems for the tooltip that goes out of the screen on the vertical axis
  158. //check for lower edge of the screen
  159. borderTest = (adjustedNewTTPos.y - height);
  160. if (borderTest < screenLowerLeft.y) {
  161. shifterForBorders.y = screenLowerLeft.y - borderTest;
  162. adjustedNewTTPos.y += shifterForBorders.y;
  163. }
  164. //check for upper edge of the screen
  165. borderTest = (adjustedNewTTPos.y);
  166. if (borderTest > screenUpperRight.y)
  167. {
  168. shifterForBorders.y = borderTest - screenUpperRight.y;
  169. adjustedNewTTPos.y -= shifterForBorders.y;
  170. }
  171. //failed attempt to circumvent issues caused when rotating the camera
  172. adjustedNewTTPos = transform.rotation * adjustedNewTTPos;
  173. transform.position = adjustedNewTTPos;
  174. adjustedTTLocalPos = transform.localPosition;
  175. adjustedTTLocalPos.z = 0;
  176. transform.localPosition = adjustedTTLocalPos;
  177. _inside = true;
  178. }
  179. //main tooltip edge of screen guard and movement - overlay
  180. public void OnScreenSpaceOverlay(bool refreshCanvasesBeforeGettingSize = false) {
  181. shiftingVector.x = xShift;
  182. shiftingVector.y = YShift;
  183. newTTPos = (baseTooltipPos - shiftingVector) / canvas.scaleFactor;
  184. adjustedNewTTPos = newTTPos;
  185. gameObject.SetActive(true);
  186. if (refreshCanvasesBeforeGettingSize) RefreshTooltipSize();
  187. width = _rectTransform.sizeDelta[0];
  188. height = _rectTransform.sizeDelta[1];
  189. // check and solve problems for the tooltip that goes out of the screen on the horizontal axis
  190. //screen's 0 = overlay canvas's 0 (always?)
  191. screenLowerLeft = Vector3.zero;
  192. screenUpperRight = canvasRectTransform.sizeDelta;
  193. //check for right edge of screen
  194. borderTest = (newTTPos.x + width);
  195. if (borderTest > screenUpperRight.x) {
  196. shifterForBorders.x = borderTest - screenUpperRight.x;
  197. adjustedNewTTPos.x -= shifterForBorders.x;
  198. }
  199. //check for left edge of screen
  200. borderTest = (adjustedNewTTPos.x);
  201. if (borderTest < screenLowerLeft.x) {
  202. shifterForBorders.x = screenLowerLeft.x - borderTest;
  203. adjustedNewTTPos.x += shifterForBorders.x;
  204. }
  205. // check and solve problems for the tooltip that goes out of the screen on the vertical axis
  206. //check for lower edge of the screen
  207. borderTest = (adjustedNewTTPos.y - height);
  208. if (borderTest < screenLowerLeft.y) {
  209. shifterForBorders.y = screenLowerLeft.y - borderTest;
  210. adjustedNewTTPos.y += shifterForBorders.y;
  211. }
  212. //check for upper edge of the screen
  213. borderTest = (adjustedNewTTPos.y);
  214. if (borderTest > screenUpperRight.y) {
  215. shifterForBorders.y = borderTest - screenUpperRight.y;
  216. adjustedNewTTPos.y -= shifterForBorders.y;
  217. }
  218. //remove scale factor for the actual positioning of the TT
  219. adjustedNewTTPos *= canvas.scaleFactor;
  220. transform.position = adjustedNewTTPos;
  221. _inside = true;
  222. }
  223. }
  224. }