forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIMGUITextHandle.cs
More file actions
292 lines (250 loc) · 11.1 KB
/
Copy pathIMGUITextHandle.cs
File metadata and controls
292 lines (250 loc) · 11.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.TextCore.Text;
namespace UnityEngine
{
internal class IMGUITextHandle : TextHandle
{
internal LinkedListNode<TextHandleTuple> tuple;
const float sFallbackFontSize = 13;
const float sTimeToFlush = 1.0f;
const string kDefaultFontName = "LegacyRuntime.ttf";
internal static Func<Object> GetEditorTextSettings;
private static TextSettings s_EditorTextSettings;
private static Dictionary<int, IMGUITextHandle> textHandles = new Dictionary<int, IMGUITextHandle>();
private static LinkedList<TextHandleTuple> textHandlesTuple = new LinkedList<TextHandleTuple>();
private static float lastCleanupTime;
internal bool isCachedOnNative = false;
internal class TextHandleTuple
{
public TextHandleTuple(float lastTimeUsed, int hashCode)
{
this.hashCode = hashCode;
this.lastTimeUsed = lastTimeUsed;
}
public float lastTimeUsed;
public int hashCode;
}
// This cleans both the managed and the native cache
internal static void EmptyCache()
{
GUIStyle.Internal_CleanupAllTextGenerator();
textHandles.Clear();
textHandlesTuple.Clear();
}
// This only cleans up the cache on the managed side. We assume it is already cleaned on the native side to avoid calls.
internal static void EmptyManagedCache()
{
textHandles.Clear();
textHandlesTuple.Clear();
}
internal static IMGUITextHandle GetTextHandle(GUIStyle style, Rect position, string content, Color32 textColor)
{
bool isCached = false;
ConvertGUIStyleToGenerationSettings(settings, style, textColor, content, position);
return GetTextHandle(settings, false, ref isCached);
}
internal static IMGUITextHandle GetTextHandle(GUIStyle style, Rect position, string content, Color32 textColor, ref bool isCached)
{
ConvertGUIStyleToGenerationSettings(settings, style, textColor, content, position);
return GetTextHandle(settings, true, ref isCached);
}
private static bool ShouldCleanup(float currentTime, float lastTime)
{
// timeSinceLastCleanup can end up negative if lastCleanupTime is from a previous run.
// Clean up if this happens.
float timeSinceLastCleanup = currentTime - lastTime;
return timeSinceLastCleanup > sTimeToFlush || timeSinceLastCleanup < 0;
}
private static void ClearUnusedTextHandles()
{
var currentTime = Time.realtimeSinceStartup;
while (textHandlesTuple.Count > 0)
{
var tuple = textHandlesTuple.First();
if (ShouldCleanup(currentTime, tuple.lastTimeUsed))
{
GUIStyle.Internal_DestroyTextGenerator(tuple.hashCode);
textHandles.Remove(tuple.hashCode);
textHandlesTuple.RemoveFirst();
}
else
break;
}
}
private static IMGUITextHandle GetTextHandle(TextCore.Text.TextGenerationSettings settings, bool isCalledFromNative, ref bool isCached)
{
isCached = false;
var currentTime = Time.realtimeSinceStartup;
if (ShouldCleanup(currentTime, lastCleanupTime))
{
ClearUnusedTextHandles();
lastCleanupTime = currentTime;
}
int hash = settings.GetHashCode();
if (textHandles.TryGetValue(hash, out IMGUITextHandle textHandleCached))
{
textHandlesTuple.Remove(textHandleCached.tuple);
textHandlesTuple.AddLast(textHandleCached.tuple);
isCached = isCalledFromNative ? textHandleCached.isCachedOnNative : true;
if (!textHandleCached.isCachedOnNative && isCalledFromNative)
{
textHandleCached.Update();
textHandleCached.UpdatePreferredSize();
textHandleCached.isCachedOnNative = true;
}
return textHandleCached;
}
var handle = new IMGUITextHandle();
var tuple = new TextHandleTuple(currentTime, hash);
var listNode = new LinkedListNode<TextHandleTuple>(tuple);
handle.tuple = listNode;
textHandles[hash] = handle;
handle.Update();
handle.UpdatePreferredSize();
textHandlesTuple.AddLast(listNode);
handle.isCachedOnNative = isCalledFromNative;
return handle;
}
internal static float GetLineHeight(GUIStyle style)
{
ConvertGUIStyleToGenerationSettings(settings, style, Color.white, "", Rect.zero);
return GetLineHeightDefault(settings);
}
internal Vector2 GetPreferredSize()
{
return preferredSize;
}
internal int GetNumCharactersThatFitWithinWidth(float width)
{
AddTextInfoToCache();
int characterCount = textInfo.lineInfo[0].characterCount;
int charCount;
float currentSize = 0;
for (charCount = 0; charCount < characterCount; charCount++)
{
currentSize += textInfo.textElementInfo[charCount].xAdvance - textInfo.textElementInfo[charCount].origin;
if (currentSize > width)
{
break;
}
}
return charCount;
}
public Rect[] GetHyperlinkRects(Rect content)
{
AddTextInfoToCache();
List<Rect> rects = new List<Rect>();
for (int i = 0; i < textInfo.linkCount; i++)
{
var minPos = GetCursorPositionFromStringIndexUsingLineHeight(textInfo.linkInfo[i].linkTextfirstCharacterIndex) + new Vector2(content.x, content.y);
var maxPos = GetCursorPositionFromStringIndexUsingLineHeight(textInfo.linkInfo[i].linkTextLength + textInfo.linkInfo[i].linkTextfirstCharacterIndex) + new Vector2(content.x, content.y);
var lineHeight = textInfo.lineInfo[0].lineHeight;
if (minPos.y == maxPos.y)
{
rects.Add(new Rect(minPos.x, minPos.y - lineHeight, maxPos.x - minPos.x, lineHeight));
}
else
{
// Rect for the first line - including end part
rects.Add(new Rect(minPos.x, minPos.y - lineHeight, textInfo.lineInfo[0].width - minPos.x, lineHeight));
// Rect for the middle part
rects.Add(new Rect(content.x, minPos.y, textInfo.lineInfo[0].width, maxPos.y - minPos.y - lineHeight));
// Rect for the bottom line - up to selection
if (maxPos.x != 0f)
rects.Add(new Rect(content.x, maxPos.y - lineHeight, maxPos.x, lineHeight));
}
}
return rects.ToArray();
}
private static void ConvertGUIStyleToGenerationSettings(UnityEngine.TextCore.Text.TextGenerationSettings settings, GUIStyle style, Color textColor, string text, Rect rect)
{
if (s_EditorTextSettings == null)
{
s_EditorTextSettings = (TextSettings)GetEditorTextSettings();
}
settings.textSettings = s_EditorTextSettings;
if (settings.textSettings == null)
return;
Font font = style.font;
if (!font)
{
font = GUIStyle.GetDefaultFont();
}
settings.fontAsset = settings.textSettings.GetCachedFontAsset(font, TextShaderUtilities.ShaderRef_MobileSDF_IMGUI);
if (settings.fontAsset == null)
return;
settings.material = settings.fontAsset.material;
// We only want to update the sharpness of the text in the editor with those preferences
settings.fontAsset.material.SetFloat("_Sharpness", settings.textSettings.GetEditorTextSharpness());
settings.screenRect = new Rect(0, 0, rect.width, rect.height);
settings.text = text;
var tempAlignment = style.alignment;
if (style.imagePosition == ImagePosition.ImageAbove)
{
switch (style.alignment)
{
case TextAnchor.MiddleRight:
case TextAnchor.LowerRight:
tempAlignment = TextAnchor.UpperRight;
break;
case TextAnchor.MiddleCenter:
case TextAnchor.LowerCenter:
tempAlignment = TextAnchor.UpperCenter;
break;
case TextAnchor.MiddleLeft:
case TextAnchor.LowerLeft:
tempAlignment = TextAnchor.UpperLeft;
break;
}
}
settings.fontStyle = TextGeneratorUtilities.LegacyStyleToNewStyle(style.fontStyle);
settings.textAlignment = TextGeneratorUtilities.LegacyAlignmentToNewAlignment(tempAlignment);
settings.overflowMode = LegacyClippingToNewOverflow(style.clipping);
settings.wordWrappingRatio = 0.4f;
if (rect.width > 0 && style.wordWrap)
{
settings.textWrappingMode = TextWrappingMode.PreserveWhitespace;
}
else
{
settings.textWrappingMode = TextWrappingMode.PreserveWhitespaceNoWrap;
}
settings.richText = style.richText;
settings.parseControlCharacters = false;
settings.isPlaceholder = false;
settings.isRightToLeft = false;
if (style.fontSize > 0)
settings.fontSize = style.fontSize;
else if (font)
settings.fontSize = font.fontSize;
else
settings.fontSize = sFallbackFontSize;
settings.characterSpacing = 0;
settings.wordSpacing = 0;
settings.paragraphSpacing = 0;
settings.color = textColor;
settings.inverseYAxis = true;
settings.isIMGUI = true;
settings.shouldConvertToLinearSpace = false;
settings.fontFeatures = m_ActiveFontFeatures;
}
static TextOverflowMode LegacyClippingToNewOverflow(TextClipping clipping)
{
switch (clipping)
{
case TextClipping.Clip:
return TextOverflowMode.Masking;
case TextClipping.Ellipsis:
return TextOverflowMode.Ellipsis;
case TextClipping.Overflow:
default:
return TextOverflowMode.Overflow;
}
}
}
}