A game about forced loneliness, made by TACStudios
1using UnityEngine;
2using UnityEngine.EventSystems;
3using System.Collections;
4
5
6namespace TMPro
7{
8
9 public enum TextContainerAnchors { TopLeft = 0, Top = 1, TopRight = 2, Left = 3, Middle = 4, Right = 5, BottomLeft = 6, Bottom = 7, BottomRight = 8, Custom = 9 };
10
11
12 [RequireComponent(typeof(RectTransform))]
13 //[AddComponentMenu("Layout/Text Container")]
14 public class TextContainer : UIBehaviour
15 {
16
17 #pragma warning disable 0618 // Disabled warning related to deprecated properties. This is for backwards compatibility.
18
19 public bool hasChanged
20 {
21 get { return m_hasChanged; }
22 set { m_hasChanged = value; }
23 }
24 private bool m_hasChanged;
25
26
27 // Pivot / Transform Position
28 public Vector2 pivot
29 {
30 get { return m_pivot; }
31 set { /*Debug.Log("Pivot has changed.");*/ if (m_pivot != value) { m_pivot = value; m_anchorPosition = GetAnchorPosition(m_pivot); m_hasChanged = true; OnContainerChanged(); } }
32 }
33 [SerializeField]
34 private Vector2 m_pivot;
35
36
37 public TextContainerAnchors anchorPosition
38 {
39 get { return m_anchorPosition; }
40 set { /*Debug.Log("Anchor has changed.");*/ if (m_anchorPosition != value) { m_anchorPosition = value; m_pivot = GetPivot(m_anchorPosition); m_hasChanged = true; OnContainerChanged(); } }
41 }
42 [SerializeField]
43 private TextContainerAnchors m_anchorPosition = TextContainerAnchors.Middle;
44
45
46 // Rect which defines the Rectangle
47 public Rect rect
48 {
49 get { return m_rect; }
50 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(); } }
51 }
52 [SerializeField]
53 private Rect m_rect;
54
55
56 public Vector2 size
57 {
58 get { return new Vector2(m_rect.width, m_rect.height); }
59 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(); } }
60 }
61
62
63 // Sets the width of the Text Container.
64 public float width
65 {
66 get { return m_rect.width; }
67 set { /*Debug.Log("Width has changed.");*/ SetRect(new Vector2(value, m_rect.height)); m_hasChanged = true; m_isDefaultWidth = false; OnContainerChanged(); }
68 }
69
70
71 // Sets the height of the Text Container.
72 public float height
73 {
74 get { return m_rect.height; }
75 set { SetRect(new Vector2(m_rect.width, value)); m_hasChanged = true; m_isDefaultHeight = false; OnContainerChanged(); }
76 }
77
78
79 // Used to determine if the user has changed the width of the Text Container.
80 public bool isDefaultWidth
81 {
82 get { return m_isDefaultWidth; }
83 }
84 private bool m_isDefaultWidth;
85
86 // Used to determine if the user has changed the height of the Text Container.
87 public bool isDefaultHeight
88 {
89 get { return m_isDefaultHeight; }
90 }
91 private bool m_isDefaultHeight;
92
93
94 public bool isAutoFitting
95 {
96 get { return m_isAutoFitting; }
97 set { m_isAutoFitting = value; }
98 }
99 private bool m_isAutoFitting = false;
100
101
102 // Corners of the Text Container
103 public Vector3[] corners
104 {
105 get { return m_corners; }
106 }
107 private Vector3[] m_corners = new Vector3[4];
108
109
110 public Vector3[] worldCorners
111 {
112 get { return m_worldCorners; }
113 }
114 private Vector3[] m_worldCorners = new Vector3[4];
115
116
117 //public Vector3 normal
118 //{
119 // get { return m_normal; }
120 //}
121 //private Vector3 m_normal;
122
123
124 // The margin offset from the Rectangle Bounds
125 public Vector4 margins
126 {
127 get { return m_margins; }
128 set { if (m_margins != value) { /*Debug.Log("Margins have changed.");*/ m_margins = value; m_hasChanged = true; OnContainerChanged(); } }
129 }
130 [SerializeField]
131 private Vector4 m_margins;
132
133
134 /// <summary>
135 /// The RectTransform used by the object
136 /// </summary>
137 public RectTransform rectTransform
138 {
139 get
140 {
141 if (m_rectTransform == null) m_rectTransform = GetComponent<RectTransform>();
142
143 return m_rectTransform;
144 }
145 }
146 private RectTransform m_rectTransform;
147
148
149 //private Transform m_transform;
150 //private bool m_isAddingRectTransform;
151 private static Vector2 k_defaultSize = new Vector2(100, 100);
152
153
154 /// <summary>
155 ///
156 /// </summary>
157 public TextMeshPro textMeshPro
158 {
159 get
160 {
161 if (m_textMeshPro == null) m_textMeshPro = GetComponent<TextMeshPro>();
162 return m_textMeshPro;
163 }
164 }
165 private TextMeshPro m_textMeshPro;
166
167
168 protected override void Awake()
169 {
170 Debug.LogWarning("The Text Container component is now Obsolete and can safely be removed from [" + gameObject.name + "].", this);
171
172 return;
173 }
174
175
176 /// <summary>
177 ///
178 /// </summary>
179 protected override void OnEnable()
180 {
181 //Debug.Log("Text Container OnEnable() called.");
182
183 OnContainerChanged();
184 }
185
186
187 /// <summary>
188 ///
189 /// </summary>
190 protected override void OnDisable()
191 {
192 //Debug.Log("OnDisable() called.");
193 }
194
195
196 /// <summary>
197 ///
198 /// </summary>
199 void OnContainerChanged()
200 {
201 //Debug.Log("OnContainerChanged");
202
203 UpdateCorners();
204 //UpdateWorldCorners();
205
206 if (this.m_rectTransform != null)
207 {
208 m_rectTransform.sizeDelta = this.size;
209 m_rectTransform.hasChanged = true;
210 }
211
212 if (this.textMeshPro != null)
213 {
214 m_textMeshPro.SetVerticesDirty();
215 m_textMeshPro.margin = m_margins;
216 }
217 }
218
219
220#if UNITY_EDITOR
221 /// <summary>
222 ///
223 /// </summary>
224 protected override void OnValidate()
225 {
226 //Debug.Log("OnValidate() called.");
227 m_hasChanged = true;
228 OnContainerChanged();
229 }
230#endif
231
232
233 /*
234 void LateUpdate()
235 {
236 // Used by the Run Time Text Input Component ... This will have to be changed.
237 if (m_transform.hasChanged)
238 UpdateWorldCorners();
239 }
240 */
241
242
243
244 /// <summary>
245 /// Callback from Unity to handle RectTransform changes.
246 /// </summary>
247 protected override void OnRectTransformDimensionsChange()
248 {
249 // Required to add a RectTransform to objects created in previous releases.
250 if (this.rectTransform == null) m_rectTransform = gameObject.AddComponent<RectTransform>();
251
252 if (m_rectTransform.sizeDelta != k_defaultSize)
253 this.size = m_rectTransform.sizeDelta;
254
255 pivot = m_rectTransform.pivot;
256
257 m_hasChanged = true;
258 OnContainerChanged();
259 }
260
261
262 private void SetRect(Vector2 size)
263 {
264 m_rect = new Rect(m_rect.x, m_rect.y, size.x, size.y);
265 //UpdateCorners();
266 }
267
268 private void UpdateCorners()
269 {
270 m_corners[0] = new Vector3(-m_pivot.x * m_rect.width, (- m_pivot.y) * m_rect.height);
271 m_corners[1] = new Vector3(-m_pivot.x * m_rect.width, (1 - m_pivot.y) * m_rect.height);
272 m_corners[2] = new Vector3((1 - m_pivot.x) * m_rect.width, (1 - m_pivot.y) * m_rect.height);
273 m_corners[3] = new Vector3((1 - m_pivot.x) * m_rect.width, (- m_pivot.y) * m_rect.height);
274 //Debug.Log("Pivot " + m_pivot + " Corners 0: " + m_corners[0] + " 1: " + m_corners[1] + " 2: " + m_corners[2] + " 3: " + m_corners[3]);
275
276 if (m_rectTransform != null)
277 m_rectTransform.pivot = m_pivot;
278 }
279
280
281 //private void UpdateWorldCorners()
282 //{
283 // if (m_transform == null)
284 // return;
285
286 // Vector3 position = m_transform.position;
287 // m_worldCorners[0] = position + m_transform.TransformDirection(m_corners[0]);
288 // m_worldCorners[1] = position + m_transform.TransformDirection(m_corners[1]);
289 // m_worldCorners[2] = position + m_transform.TransformDirection(m_corners[2]);
290 // m_worldCorners[3] = position + m_transform.TransformDirection(m_corners[3]);
291
292 // m_normal = Vector3.Cross(worldCorners[1] - worldCorners[0], worldCorners[3] - worldCorners[0]);
293 //}
294
295
296 //public Vector3[] GetWorldCorners()
297 //{
298 // UpdateWorldCorners();
299
300 // return m_worldCorners;
301 //}
302
303
304 Vector2 GetPivot(TextContainerAnchors anchor)
305 {
306 Vector2 pivot = Vector2.zero;
307
308 switch (anchor)
309 {
310 case TextContainerAnchors.TopLeft:
311 pivot = new Vector2(0, 1);
312 break;
313 case TextContainerAnchors.Top:
314 pivot = new Vector2(0.5f, 1);
315 break;
316 case TextContainerAnchors.TopRight:
317 pivot = new Vector2(1, 1);
318 break;
319 case TextContainerAnchors.Left:
320 pivot = new Vector2(0, 0.5f);
321 break;
322 case TextContainerAnchors.Middle:
323 pivot = new Vector2(0.5f, 0.5f);
324 break;
325 case TextContainerAnchors.Right:
326 pivot = new Vector2(1, 0.5f);
327 break;
328 case TextContainerAnchors.BottomLeft:
329 pivot = new Vector2(0, 0);
330 break;
331 case TextContainerAnchors.Bottom:
332 pivot = new Vector2(0.5f, 0);
333 break;
334 case TextContainerAnchors.BottomRight:
335 pivot = new Vector2(1, 0);
336 break;
337 }
338
339 return pivot;
340 }
341
342
343 // Method which returns the Anchor position based on pivot value.
344 TextContainerAnchors GetAnchorPosition(Vector2 pivot)
345 {
346
347 if (pivot == new Vector2(0, 1))
348 return TextContainerAnchors.TopLeft;
349 else if (pivot == new Vector2(0.5f, 1))
350 return TextContainerAnchors.Top;
351 else if (pivot == new Vector2(1f, 1))
352 return TextContainerAnchors.TopRight;
353 else if (pivot == new Vector2(0, 0.5f))
354 return TextContainerAnchors.Left;
355 else if (pivot == new Vector2(0.5f, 0.5f))
356 return TextContainerAnchors.Middle;
357 else if (pivot == new Vector2(1, 0.5f))
358 return TextContainerAnchors.Right;
359 else if (pivot == new Vector2(0, 0))
360 return TextContainerAnchors.BottomLeft;
361 else if (pivot == new Vector2(0.5f, 0))
362 return TextContainerAnchors.Bottom;
363 else if (pivot == new Vector2(1, 0))
364 return TextContainerAnchors.BottomRight;
365 else
366 return TextContainerAnchors.Custom;
367
368 }
369 }
370}