forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextElement.cs
More file actions
181 lines (155 loc) · 6.39 KB
/
TextElement.cs
File metadata and controls
181 lines (155 loc) · 6.39 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
// 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;
namespace UnityEngine.UIElements
{
internal interface ITextElement
{
string text { get; set; }
}
public class TextElement : BindableElement, ITextElement, INotifyValueChanged<string>
{
public new class UxmlFactory : UxmlFactory<TextElement, UxmlTraits> {}
public new class UxmlTraits : BindableElement.UxmlTraits
{
UxmlStringAttributeDescription m_Text = new UxmlStringAttributeDescription { name = "text" };
public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}
public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);
((ITextElement)ve).text = m_Text.GetValueFromBag(bag, cc);
}
}
public static readonly string ussClassName = "unity-text-element";
public TextElement()
{
requireMeasureFunction = true;
AddToClassList(ussClassName);
generateVisualContent += OnGenerateVisualContent;
}
[SerializeField]
private string m_Text;
public virtual string text
{
get { return ((INotifyValueChanged<string>) this).value; }
set
{
((INotifyValueChanged<string>) this).value = value;
}
}
private void OnGenerateVisualContent(MeshGenerationContext mgc)
{
mgc.Text(MeshGenerationContextUtils.TextParams.MakeStyleBased(this, this.text));
}
public Vector2 MeasureTextSize(string textToMeasure, float width, MeasureMode widthMode, float height,
MeasureMode heightMode)
{
return MeasureVisualElementTextSize(this, textToMeasure, width, widthMode, height, heightMode);
}
internal static Vector2 MeasureVisualElementTextSize(VisualElement ve, string textToMeasure, float width, MeasureMode widthMode, float height, MeasureMode heightMode)
{
float measuredWidth = float.NaN;
float measuredHeight = float.NaN;
Font usedFont = ve.computedStyle.unityFont.value;
if (textToMeasure == null || usedFont == null)
return new Vector2(measuredWidth, measuredHeight);
var elementScaling = ve.ComputeGlobalScale();
float pixelsPerPoint = (ve.elementPanel != null) ? ve.elementPanel.currentPixelsPerPoint : GUIUtility.pixelsPerPoint;
float scaling = (elementScaling.x + elementScaling.y) * 0.5f * pixelsPerPoint;
if (widthMode == MeasureMode.Exactly)
{
measuredWidth = width;
}
else
{
var textSettings = GetTextNativeSettings(ve, textToMeasure, scaling);
textSettings.wordWrapWidth = 0.0f;
textSettings.wordWrap = false;
//we make sure to round up as yoga could decide to round down and text would start wrapping
measuredWidth = Mathf.Ceil(TextNative.ComputeTextWidth(textSettings));
if (widthMode == MeasureMode.AtMost)
{
measuredWidth = Mathf.Min(measuredWidth, width);
}
}
if (heightMode == MeasureMode.Exactly)
{
measuredHeight = height;
}
else
{
var textSettings = GetTextNativeSettings(ve, textToMeasure, scaling);
textSettings.wordWrapWidth = measuredWidth;
measuredHeight = Mathf.Ceil(TextNative.ComputeTextHeight(textSettings));
if (heightMode == MeasureMode.AtMost)
{
measuredHeight = Mathf.Min(measuredHeight, height);
}
}
return new Vector2(measuredWidth, measuredHeight);
}
protected internal override Vector2 DoMeasure(float desiredWidth, MeasureMode widthMode, float desiredHeight, MeasureMode heightMode)
{
return MeasureTextSize(text, desiredWidth, widthMode, desiredHeight, heightMode);
}
private static TextNativeSettings GetTextNativeSettings(VisualElement ve, string text, float scaling)
{
ComputedStyle style = ve.computedStyle;
return new TextNativeSettings
{
text = text,
font = style.unityFont.value,
size = (int)style.fontSize.value.value,
scaling = scaling,
style = style.unityFontStyleAndWeight.value,
color = style.color.value,
anchor = style.unityTextAlign.value,
wordWrap = style.whiteSpace.value == WhiteSpace.Normal,
wordWrapWidth = style.whiteSpace.value == WhiteSpace.Normal ? ve.contentRect.width : 0.0f,
richText = true
};
}
//INotifyValueChange
string INotifyValueChanged<string>.value
{
get
{
return m_Text ?? String.Empty;
}
set
{
if (m_Text != value)
{
if (panel != null)
{
using (ChangeEvent<string> evt = ChangeEvent<string>.GetPooled(this.text, value))
{
evt.target = this;
((INotifyValueChanged<string>) this).SetValueWithoutNotify(value);
SendEvent(evt);
}
}
else
{
((INotifyValueChanged<string>) this).SetValueWithoutNotify(value);
}
}
}
}
void INotifyValueChanged<string>.SetValueWithoutNotify(string newValue)
{
if (m_Text != newValue)
{
m_Text = newValue;
IncrementVersion(VersionChangeType.Layout | VersionChangeType.Repaint);
if (!string.IsNullOrEmpty(viewDataKey))
SaveViewData();
}
}
}
}