TextContainer.cs
11.8 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
namespace TMPro
{
public enum TextContainerAnchors { TopLeft = 0, Top = 1, TopRight = 2, Left = 3, Middle = 4, Right = 5, BottomLeft = 6, Bottom = 7, BottomRight = 8, Custom = 9 };
[RequireComponent(typeof(RectTransform))]
[AddComponentMenu("Layout/Text Container")]
public class TextContainer : UIBehaviour
{
#pragma warning disable 0618 // Disabled warning related to deprecated properties. This is for backwards compatibility.
public bool hasChanged
{
get { return m_hasChanged; }
set { m_hasChanged = value; }
}
private bool m_hasChanged;
// Pivot / Transform Position
public Vector2 pivot
{
get { return m_pivot; }
set { /*Debug.Log("Pivot has changed.");*/ if (m_pivot != value) { m_pivot = value; m_anchorPosition = GetAnchorPosition(m_pivot); m_hasChanged = true; OnContainerChanged(); } }
}
[SerializeField]
private Vector2 m_pivot;
public TextContainerAnchors anchorPosition
{
get { return m_anchorPosition; }
set { /*Debug.Log("Anchor has changed.");*/ if (m_anchorPosition != value) { m_anchorPosition = value; m_pivot = GetPivot(m_anchorPosition); m_hasChanged = true; OnContainerChanged(); } }
}
[SerializeField]
private TextContainerAnchors m_anchorPosition = TextContainerAnchors.Middle;
// Rect which defines the Rectangle
public Rect rect
{
get { return m_rect; }
set { /*Debug.Log("Rectangle has changed.");*/ if (m_rect != value) { m_rect = value; /*m_size = new Vector2(m_rect.width, m_rect.height);*/ m_hasChanged = true; OnContainerChanged(); } }
}
[SerializeField]
private Rect m_rect;
public Vector2 size
{
get { return new Vector2(m_rect.width, m_rect.height); }
set { /*Debug.Log("Size has changed.");*/ if (new Vector2(m_rect.width, m_rect.height) != value) { SetRect(value); m_hasChanged = true; m_isDefaultWidth = false; m_isDefaultHeight = false; OnContainerChanged(); } }
}
// Sets the width of the Text Container.
public float width
{
get { return m_rect.width; }
set { /*Debug.Log("Width has changed.");*/ SetRect(new Vector2(value, m_rect.height)); m_hasChanged = true; m_isDefaultWidth = false; OnContainerChanged(); }
}
// Sets the height of the Text Container.
public float height
{
get { return m_rect.height; }
set { SetRect(new Vector2(m_rect.width, value)); m_hasChanged = true; m_isDefaultHeight = false; OnContainerChanged(); }
}
// Used to determine if the user has changed the width of the Text Container.
public bool isDefaultWidth
{
get { return m_isDefaultWidth; }
}
private bool m_isDefaultWidth;
// Used to determine if the user has changed the height of the Text Container.
public bool isDefaultHeight
{
get { return m_isDefaultHeight; }
}
private bool m_isDefaultHeight;
public bool isAutoFitting
{
get { return m_isAutoFitting; }
set { m_isAutoFitting = value; }
}
private bool m_isAutoFitting = false;
// Corners of the Text Container
public Vector3[] corners
{
get { return m_corners; }
}
private Vector3[] m_corners = new Vector3[4];
public Vector3[] worldCorners
{
get { return m_worldCorners; }
}
private Vector3[] m_worldCorners = new Vector3[4];
//public Vector3 normal
//{
// get { return m_normal; }
//}
//private Vector3 m_normal;
// The margin offset from the Rectangle Bounds
public Vector4 margins
{
get { return m_margins; }
set { if (m_margins != value) { /*Debug.Log("Margins have changed.");*/ m_margins = value; m_hasChanged = true; OnContainerChanged(); } }
}
[SerializeField]
private Vector4 m_margins;
/// <summary>
/// The RectTransform used by the object
/// </summary>
public RectTransform rectTransform
{
get
{
if (m_rectTransform == null) m_rectTransform = GetComponent<RectTransform>();
return m_rectTransform;
}
}
private RectTransform m_rectTransform;
//private Transform m_transform;
//private bool m_isAddingRectTransform;
private static Vector2 k_defaultSize = new Vector2(100, 100);
/// <summary>
///
/// </summary>
public TextMeshPro textMeshPro
{
get
{
if (m_textMeshPro == null) m_textMeshPro = GetComponent<TextMeshPro>();
return m_textMeshPro;
}
}
private TextMeshPro m_textMeshPro;
protected override void Awake()
{
Debug.LogWarning("The Text Container component is now Obsolete and can safely be removed from [" + gameObject.name + "].", this);
return;
}
/// <summary>
///
/// </summary>
protected override void OnEnable()
{
//Debug.Log("Text Container OnEnable() called.");
OnContainerChanged();
}
/// <summary>
///
/// </summary>
protected override void OnDisable()
{
//Debug.Log("OnDisable() called.");
}
/// <summary>
///
/// </summary>
void OnContainerChanged()
{
//Debug.Log("OnContainerChanged");
UpdateCorners();
//UpdateWorldCorners();
if (this.m_rectTransform != null)
{
m_rectTransform.sizeDelta = this.size;
m_rectTransform.hasChanged = true;
}
if (this.textMeshPro != null)
{
m_textMeshPro.SetVerticesDirty();
m_textMeshPro.margin = m_margins;
}
}
#if UNITY_EDITOR
/// <summary>
///
/// </summary>
protected override void OnValidate()
{
//Debug.Log("OnValidate() called.");
m_hasChanged = true;
OnContainerChanged();
}
#endif
/*
void LateUpdate()
{
// Used by the Run Time Text Input Component ... This will have to be changed.
if (m_transform.hasChanged)
UpdateWorldCorners();
}
*/
/// <summary>
/// Callback from Unity to handle RectTransform changes.
/// </summary>
protected override void OnRectTransformDimensionsChange()
{
// Required to add a RectTransform to objects created in previous releases.
if (this.rectTransform == null) m_rectTransform = gameObject.AddComponent<RectTransform>();
if (m_rectTransform.sizeDelta != k_defaultSize)
this.size = m_rectTransform.sizeDelta;
pivot = m_rectTransform.pivot;
m_hasChanged = true;
OnContainerChanged();
}
private void SetRect(Vector2 size)
{
m_rect = new Rect(m_rect.x, m_rect.y, size.x, size.y);
//UpdateCorners();
}
private void UpdateCorners()
{
m_corners[0] = new Vector3(-m_pivot.x * m_rect.width, (- m_pivot.y) * m_rect.height);
m_corners[1] = new Vector3(-m_pivot.x * m_rect.width, (1 - m_pivot.y) * m_rect.height);
m_corners[2] = new Vector3((1 - m_pivot.x) * m_rect.width, (1 - m_pivot.y) * m_rect.height);
m_corners[3] = new Vector3((1 - m_pivot.x) * m_rect.width, (- m_pivot.y) * m_rect.height);
//Debug.Log("Pivot " + m_pivot + " Corners 0: " + m_corners[0] + " 1: " + m_corners[1] + " 2: " + m_corners[2] + " 3: " + m_corners[3]);
if (m_rectTransform != null)
m_rectTransform.pivot = m_pivot;
}
//private void UpdateWorldCorners()
//{
// if (m_transform == null)
// return;
// Vector3 position = m_transform.position;
// m_worldCorners[0] = position + m_transform.TransformDirection(m_corners[0]);
// m_worldCorners[1] = position + m_transform.TransformDirection(m_corners[1]);
// m_worldCorners[2] = position + m_transform.TransformDirection(m_corners[2]);
// m_worldCorners[3] = position + m_transform.TransformDirection(m_corners[3]);
// m_normal = Vector3.Cross(worldCorners[1] - worldCorners[0], worldCorners[3] - worldCorners[0]);
//}
//public Vector3[] GetWorldCorners()
//{
// UpdateWorldCorners();
// return m_worldCorners;
//}
Vector2 GetPivot(TextContainerAnchors anchor)
{
Vector2 pivot = Vector2.zero;
switch (anchor)
{
case TextContainerAnchors.TopLeft:
pivot = new Vector2(0, 1);
break;
case TextContainerAnchors.Top:
pivot = new Vector2(0.5f, 1);
break;
case TextContainerAnchors.TopRight:
pivot = new Vector2(1, 1);
break;
case TextContainerAnchors.Left:
pivot = new Vector2(0, 0.5f);
break;
case TextContainerAnchors.Middle:
pivot = new Vector2(0.5f, 0.5f);
break;
case TextContainerAnchors.Right:
pivot = new Vector2(1, 0.5f);
break;
case TextContainerAnchors.BottomLeft:
pivot = new Vector2(0, 0);
break;
case TextContainerAnchors.Bottom:
pivot = new Vector2(0.5f, 0);
break;
case TextContainerAnchors.BottomRight:
pivot = new Vector2(1, 0);
break;
}
return pivot;
}
// Method which returns the Anchor position based on pivot value.
TextContainerAnchors GetAnchorPosition(Vector2 pivot)
{
if (pivot == new Vector2(0, 1))
return TextContainerAnchors.TopLeft;
else if (pivot == new Vector2(0.5f, 1))
return TextContainerAnchors.Top;
else if (pivot == new Vector2(1f, 1))
return TextContainerAnchors.TopRight;
else if (pivot == new Vector2(0, 0.5f))
return TextContainerAnchors.Left;
else if (pivot == new Vector2(0.5f, 0.5f))
return TextContainerAnchors.Middle;
else if (pivot == new Vector2(1, 0.5f))
return TextContainerAnchors.Right;
else if (pivot == new Vector2(0, 0))
return TextContainerAnchors.BottomLeft;
else if (pivot == new Vector2(0.5f, 0))
return TextContainerAnchors.Bottom;
else if (pivot == new Vector2(1, 0))
return TextContainerAnchors.BottomRight;
else
return TextContainerAnchors.Custom;
}
}
}