-
Notifications
You must be signed in to change notification settings - Fork 1
/
EleText.cs
138 lines (114 loc) · 4.99 KB
/
EleText.cs
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
// <copyright file="EleText.cs" company="Pixel Precision LLC">
// Copyright (c) 2020 All Rights Reserved
// </copyright>
// <author>William Leu</author>
// <date>09/25/2020</date>
// <summary></summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PxPre.UIL
{
public class EleText : EleBaseRect
{
public UnityEngine.UI.Text text;
bool wrap = false;
public EleText(EleBaseRect parent, string text, bool wrap, Font font, Color color, int fontSize, Vector2 size, string name = "")
: base(parent, size, name)
{
this._Create(parent, text, wrap, font, color, fontSize, size, name);
}
public EleText(EleBaseRect parent, string text, bool wrap, Font font, Color color, int fontSize)
: base(parent, new Vector2(-1.0f, -1.0f), "")
{
this._Create(parent, text, wrap, font, color, fontSize, new Vector2(-1.0f, -1.0f), "");
}
protected void _Create(EleBaseRect parent, string text, bool wrap, Font font, Color color, int fontSize, Vector2 size, string name)
{
GameObject go = new GameObject("Text_" + name);
go.transform.SetParent(parent.GetContentRect());
this.text = go.AddComponent<UnityEngine.UI.Text>();
this.text.color = color;
this.text.font = font;
this.text.fontSize = fontSize;
this.text.alignment = TextAnchor.UpperLeft;
this.text.text = text;
this.text.verticalOverflow = VerticalWrapMode.Overflow;
this.text.horizontalOverflow =
wrap ?
HorizontalWrapMode.Wrap :
HorizontalWrapMode.Overflow;
SetRTTopLeft(this.text.rectTransform);
this.wrap = wrap;
if (wrap == true)
{
this.text.horizontalOverflow = HorizontalWrapMode.Wrap;
}
else
{
this.text.horizontalOverflow = HorizontalWrapMode.Overflow;
}
this.text.rectTransform.RTQ().Identity();
}
public override Vector2 Layout(
Dictionary<Ele, Vector2> cached,
Dictionary<Ele, float> widths,
Vector2 rectOffset,
Vector2 offset,
Vector2 size,
bool collapsable = true)
{
Vector2 ret = base.Layout(cached, widths, rectOffset, offset, size);
return ret;
}
protected override float ImplCalcMinSizeWidth(Dictionary<Ele, float> cache)
{
if (this.text.font == null)
return 0.0f;
float minWrapWidth = 50.0f;
TextGenerationSettings tgs =
(this.wrap == true) ?
this.text.GetGenerationSettings(new Vector2(minWrapWidth, float.PositiveInfinity)) :
this.text.GetGenerationSettings(new Vector2(float.PositiveInfinity, float.PositiveInfinity));
//tgs.scaleFactor = 1.0f;
TextGenerator tg = this.text.cachedTextGenerator;
float ret = tg.GetPreferredWidth(this.text.text, tgs) / this.text.rectTransform.lossyScale.x;
// There's an odd issue with wrapping not being honored.
if(this.wrap == false)
return Mathf.Ceil(ret) + 1.0f;
else
return Mathf.Min(minWrapWidth, ret);
}
protected override Vector2 ImplCalcMinSize(
Dictionary<Ele, Vector2> cache,
Dictionary<Ele, float> widths,
float width,
bool collapsable = true)
{
if(this.text.font == null)
return new Vector2(0.0f, 0.0f);
if(this.wrap == false)
width = float.PositiveInfinity;
TextGenerationSettings tgs = this.text.GetGenerationSettings(new Vector2(width, Mathf.Infinity));
// This is freaking bizzare. We need to account for scale, but we can't just do ...
//tgs.scaleFactor = 1.0f;
// ... because that can screw up the calculation of how many lines are in the
// output when GetPreferredHeight is called.
//
TextGenerator tg = this.text.cachedTextGenerator;
// So we end up accounting for it by looking at the world scale.
float y = Mathf.Ceil(tg.GetPreferredHeight(this.text.text, tgs)) / this.text.rectTransform.lossyScale.y + 1.0f;
if(this.wrap == true)
{
float entireLineX = Mathf.Ceil(tg.GetPreferredWidth(this.text.text, tgs)) / this.text.rectTransform.lossyScale.x + 1.0f;
return new Vector2(Mathf.Min(width, entireLineX), y);
}
float x = Mathf.Ceil(tg.GetPreferredWidth(this.text.text, tgs)) / this.text.rectTransform.lossyScale.x + 1.0f;
return new Vector2(x,y);
}
public override RectTransform RT
{
get{ return this.text.rectTransform; }
}
}
}