123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- using System;
- using UnityEngine;
- [AddComponentMenu("NGUI/UI/Tooltip")]
- public class UITooltip : MonoBehaviour
- {
- public static bool isVisible
- {
- get
- {
- return UITooltip.mInstance != null && UITooltip.mInstance.mTarget == 1f;
- }
- }
- private void Awake()
- {
- UITooltip.mInstance = this;
- }
- private void OnDestroy()
- {
- UITooltip.mInstance = null;
- }
- protected virtual void Start()
- {
- this.mTrans = base.transform;
- this.mWidgets = base.GetComponentsInChildren<UIWidget>();
- this.mPos = this.mTrans.localPosition;
- if (this.uiCamera == null)
- {
- this.uiCamera = NGUITools.FindCameraForLayer(base.gameObject.layer);
- }
- this.SetAlpha(0f);
- }
- protected virtual void Update()
- {
- if (this.mHover != UICamera.hoveredObject)
- {
- this.mHover = null;
- this.mTarget = 0f;
- }
- if (this.mCurrent != this.mTarget)
- {
- this.mCurrent = Mathf.Lerp(this.mCurrent, this.mTarget, RealTime.deltaTime * this.appearSpeed);
- if (Mathf.Abs(this.mCurrent - this.mTarget) < 0.001f)
- {
- this.mCurrent = this.mTarget;
- }
- this.SetAlpha(this.mCurrent * this.mCurrent);
- if (this.scalingTransitions)
- {
- Vector3 b = this.mSize * 0.25f;
- b.y = -b.y;
- Vector3 localScale = Vector3.one * (1.5f - this.mCurrent * 0.5f);
- Vector3 localPosition = Vector3.Lerp(this.mPos - b, this.mPos, this.mCurrent);
- this.mTrans.localPosition = localPosition;
- this.mTrans.localScale = localScale;
- }
- }
- }
- protected virtual void SetAlpha(float val)
- {
- int i = 0;
- int num = this.mWidgets.Length;
- while (i < num)
- {
- UIWidget uiwidget = this.mWidgets[i];
- Color color = uiwidget.color;
- color.a = val;
- uiwidget.color = color;
- i++;
- }
- }
- protected virtual void SetText(string tooltipText)
- {
- if (this.text != null && !string.IsNullOrEmpty(tooltipText))
- {
- this.mTarget = 1f;
- this.mHover = UICamera.hoveredObject;
- this.text.text = tooltipText;
- this.mPos = Input.mousePosition;
- Transform transform = this.text.transform;
- Vector3 localPosition = transform.localPosition;
- Vector3 localScale = transform.localScale;
- this.mSize = this.text.printedSize;
- this.mSize.x = this.mSize.x * localScale.x;
- this.mSize.y = this.mSize.y * localScale.y;
- if (this.background != null)
- {
- Vector4 border = this.background.border;
- this.mSize.x = this.mSize.x + (border.x + border.z + (localPosition.x - border.x) * 2f);
- this.mSize.y = this.mSize.y + (border.y + border.w + (-localPosition.y - border.y) * 2f);
- this.background.width = Mathf.RoundToInt(this.mSize.x);
- this.background.height = Mathf.RoundToInt(this.mSize.y);
- }
- if (this.uiCamera != null)
- {
- this.mPos.x = Mathf.Clamp01(this.mPos.x / (float)UICamera.ScreenWidth);
- this.mPos.y = Mathf.Clamp01(this.mPos.y / (float)UICamera.ScreenHeight);
- float num = this.uiCamera.orthographicSize / this.mTrans.parent.lossyScale.y;
- float num2 = (float)UICamera.ScreenHeight * 0.5f / num;
- Vector2 vector = new Vector2(num2 * this.mSize.x / (float)UICamera.ScreenWidth, num2 * this.mSize.y / (float)UICamera.ScreenHeight);
- this.mPos.x = Mathf.Min(this.mPos.x, 1f - vector.x);
- this.mPos.y = Mathf.Max(this.mPos.y, vector.y);
- this.mTrans.position = this.uiCamera.ViewportToWorldPoint(this.mPos);
- this.mPos = this.mTrans.localPosition;
- this.mPos.x = Mathf.Round(this.mPos.x);
- this.mPos.y = Mathf.Round(this.mPos.y);
- this.mTrans.localPosition = this.mPos;
- }
- else
- {
- if (this.mPos.x + this.mSize.x > (float)UICamera.ScreenWidth)
- {
- this.mPos.x = (float)UICamera.ScreenWidth - this.mSize.x;
- }
- if (this.mPos.y - this.mSize.y < 0f)
- {
- this.mPos.y = this.mSize.y;
- }
- this.mPos.x = this.mPos.x - (float)UICamera.ScreenWidth * 0.5f;
- this.mPos.y = this.mPos.y - (float)UICamera.ScreenHeight * 0.5f;
- }
- }
- else
- {
- this.mHover = null;
- this.mTarget = 0f;
- }
- }
- [Obsolete("Use UITooltip.Show instead")]
- public static void ShowText(string text)
- {
- if (UITooltip.mInstance != null)
- {
- UITooltip.mInstance.SetText(text);
- }
- }
- public static void Show(string text)
- {
- if (UITooltip.mInstance != null)
- {
- UITooltip.mInstance.SetText(text);
- }
- }
- public static void Hide()
- {
- if (UITooltip.mInstance != null)
- {
- UITooltip.mInstance.mHover = null;
- UITooltip.mInstance.mTarget = 0f;
- }
- }
- protected static UITooltip mInstance;
- public Camera uiCamera;
- public UILabel text;
- public UISprite background;
- public float appearSpeed = 10f;
- public bool scalingTransitions = true;
- protected GameObject mHover;
- protected Transform mTrans;
- protected float mTarget;
- protected float mCurrent;
- protected Vector3 mPos;
- protected Vector3 mSize = Vector3.zero;
- protected UIWidget[] mWidgets;
- }
|