UITooltip.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using UnityEngine;
  3. [AddComponentMenu("NGUI/UI/Tooltip")]
  4. public class UITooltip : MonoBehaviour
  5. {
  6. public static bool isVisible
  7. {
  8. get
  9. {
  10. return UITooltip.mInstance != null && UITooltip.mInstance.mTarget == 1f;
  11. }
  12. }
  13. private void Awake()
  14. {
  15. UITooltip.mInstance = this;
  16. }
  17. private void OnDestroy()
  18. {
  19. UITooltip.mInstance = null;
  20. }
  21. protected virtual void Start()
  22. {
  23. this.mTrans = base.transform;
  24. this.mWidgets = base.GetComponentsInChildren<UIWidget>();
  25. this.mPos = this.mTrans.localPosition;
  26. if (this.uiCamera == null)
  27. {
  28. this.uiCamera = NGUITools.FindCameraForLayer(base.gameObject.layer);
  29. }
  30. this.SetAlpha(0f);
  31. }
  32. protected virtual void Update()
  33. {
  34. if (this.mHover != UICamera.hoveredObject)
  35. {
  36. this.mHover = null;
  37. this.mTarget = 0f;
  38. }
  39. if (this.mCurrent != this.mTarget)
  40. {
  41. this.mCurrent = Mathf.Lerp(this.mCurrent, this.mTarget, RealTime.deltaTime * this.appearSpeed);
  42. if (Mathf.Abs(this.mCurrent - this.mTarget) < 0.001f)
  43. {
  44. this.mCurrent = this.mTarget;
  45. }
  46. this.SetAlpha(this.mCurrent * this.mCurrent);
  47. if (this.scalingTransitions)
  48. {
  49. Vector3 b = this.mSize * 0.25f;
  50. b.y = -b.y;
  51. Vector3 localScale = Vector3.one * (1.5f - this.mCurrent * 0.5f);
  52. Vector3 localPosition = Vector3.Lerp(this.mPos - b, this.mPos, this.mCurrent);
  53. this.mTrans.localPosition = localPosition;
  54. this.mTrans.localScale = localScale;
  55. }
  56. }
  57. }
  58. protected virtual void SetAlpha(float val)
  59. {
  60. int i = 0;
  61. int num = this.mWidgets.Length;
  62. while (i < num)
  63. {
  64. UIWidget uiwidget = this.mWidgets[i];
  65. Color color = uiwidget.color;
  66. color.a = val;
  67. uiwidget.color = color;
  68. i++;
  69. }
  70. }
  71. protected virtual void SetText(string tooltipText)
  72. {
  73. if (this.text != null && !string.IsNullOrEmpty(tooltipText))
  74. {
  75. this.mTarget = 1f;
  76. this.mHover = UICamera.hoveredObject;
  77. this.text.text = tooltipText;
  78. this.mPos = Input.mousePosition;
  79. Transform transform = this.text.transform;
  80. Vector3 localPosition = transform.localPosition;
  81. Vector3 localScale = transform.localScale;
  82. this.mSize = this.text.printedSize;
  83. this.mSize.x = this.mSize.x * localScale.x;
  84. this.mSize.y = this.mSize.y * localScale.y;
  85. if (this.background != null)
  86. {
  87. Vector4 border = this.background.border;
  88. this.mSize.x = this.mSize.x + (border.x + border.z + (localPosition.x - border.x) * 2f);
  89. this.mSize.y = this.mSize.y + (border.y + border.w + (-localPosition.y - border.y) * 2f);
  90. this.background.width = Mathf.RoundToInt(this.mSize.x);
  91. this.background.height = Mathf.RoundToInt(this.mSize.y);
  92. }
  93. if (this.uiCamera != null)
  94. {
  95. this.mPos.x = Mathf.Clamp01(this.mPos.x / (float)UICamera.ScreenWidth);
  96. this.mPos.y = Mathf.Clamp01(this.mPos.y / (float)UICamera.ScreenHeight);
  97. float num = this.uiCamera.orthographicSize / this.mTrans.parent.lossyScale.y;
  98. float num2 = (float)UICamera.ScreenHeight * 0.5f / num;
  99. Vector2 vector = new Vector2(num2 * this.mSize.x / (float)UICamera.ScreenWidth, num2 * this.mSize.y / (float)UICamera.ScreenHeight);
  100. this.mPos.x = Mathf.Min(this.mPos.x, 1f - vector.x);
  101. this.mPos.y = Mathf.Max(this.mPos.y, vector.y);
  102. this.mTrans.position = this.uiCamera.ViewportToWorldPoint(this.mPos);
  103. this.mPos = this.mTrans.localPosition;
  104. this.mPos.x = Mathf.Round(this.mPos.x);
  105. this.mPos.y = Mathf.Round(this.mPos.y);
  106. this.mTrans.localPosition = this.mPos;
  107. }
  108. else
  109. {
  110. if (this.mPos.x + this.mSize.x > (float)UICamera.ScreenWidth)
  111. {
  112. this.mPos.x = (float)UICamera.ScreenWidth - this.mSize.x;
  113. }
  114. if (this.mPos.y - this.mSize.y < 0f)
  115. {
  116. this.mPos.y = this.mSize.y;
  117. }
  118. this.mPos.x = this.mPos.x - (float)UICamera.ScreenWidth * 0.5f;
  119. this.mPos.y = this.mPos.y - (float)UICamera.ScreenHeight * 0.5f;
  120. }
  121. }
  122. else
  123. {
  124. this.mHover = null;
  125. this.mTarget = 0f;
  126. }
  127. }
  128. [Obsolete("Use UITooltip.Show instead")]
  129. public static void ShowText(string text)
  130. {
  131. if (UITooltip.mInstance != null)
  132. {
  133. UITooltip.mInstance.SetText(text);
  134. }
  135. }
  136. public static void Show(string text)
  137. {
  138. if (UITooltip.mInstance != null)
  139. {
  140. UITooltip.mInstance.SetText(text);
  141. }
  142. }
  143. public static void Hide()
  144. {
  145. if (UITooltip.mInstance != null)
  146. {
  147. UITooltip.mInstance.mHover = null;
  148. UITooltip.mInstance.mTarget = 0f;
  149. }
  150. }
  151. protected static UITooltip mInstance;
  152. public Camera uiCamera;
  153. public UILabel text;
  154. public UISprite background;
  155. public float appearSpeed = 10f;
  156. public bool scalingTransitions = true;
  157. protected GameObject mHover;
  158. protected Transform mTrans;
  159. protected float mTarget;
  160. protected float mCurrent;
  161. protected Vector3 mPos;
  162. protected Vector3 mSize = Vector3.zero;
  163. protected UIWidget[] mWidgets;
  164. }