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