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
137 lines (113 loc) · 4.81 KB
/
Copy pathTextElement.cs
File metadata and controls
137 lines (113 loc) · 4.81 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
// 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 : VisualElement, ITextElement
{
public new class UxmlFactory : UxmlFactory<TextElement, UxmlTraits> {}
public new class UxmlTraits : VisualElement.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);
}
[SerializeField]
private string m_Text;
public virtual string text
{
get { return m_Text ?? String.Empty; }
set
{
if (m_Text == value)
return;
m_Text = value;
IncrementVersion(VersionChangeType.Layout | VersionChangeType.Repaint);
if (!string.IsNullOrEmpty(viewDataKey))
SaveViewData();
}
}
internal override void DoRepaint(IStylePainter painter)
{
var stylePainter = (IStylePainterInternal)painter;
stylePainter.DrawText(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 textParams = TextStylePainterParameters.GetDefault(ve, textToMeasure);
textParams.text = textToMeasure;
textParams.font = usedFont;
textParams.wordWrapWidth = 0.0f;
textParams.wordWrap = false;
textParams.richText = true;
//we make sure to round up as yoga could decide to round down and text would start wrapping
measuredWidth = Mathf.Ceil(TextNative.ComputeTextWidth(textParams.GetTextNativeSettings(scaling)));
if (widthMode == MeasureMode.AtMost)
{
measuredWidth = Mathf.Min(measuredWidth, width);
}
}
if (heightMode == MeasureMode.Exactly)
{
measuredHeight = height;
}
else
{
var textParams = TextStylePainterParameters.GetDefault(ve, textToMeasure);
textParams.text = textToMeasure;
textParams.font = usedFont;
textParams.wordWrapWidth = measuredWidth;
textParams.richText = true;
measuredHeight = Mathf.Ceil(TextNative.ComputeTextHeight(textParams.GetTextNativeSettings(scaling)));
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);
}
}
}