/
TextFieldLayout.ts
282 lines (262 loc) · 8.36 KB
/
TextFieldLayout.ts
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
/// <summary>
/// <para>Where the anchor of the text is placed.</para>
/// </summary>
import { fonts } from "../../../assets/Fonts";
import { FontInfo } from "../../../loader/parser/FontParser";
import { GUIQuad } from "../core/GUIQuad";
import { UITextField } from "./UITextField";
import { UITransform } from "./UITransform";
export enum TextAnchor {
/// <summary>
/// <para>Text is anchored in upper left corner.</para>
/// </summary>
UpperLeft,
/// <summary>
/// <para>Text is anchored in upper side, centered horizontally.</para>
/// </summary>
UpperCenter,
/// <summary>
/// <para>Text is anchored in upper right corner.</para>
/// </summary>
UpperRight,
/// <summary>
/// <para>Text is anchored in left side, centered vertically.</para>
/// </summary>
MiddleLeft,
/// <summary>
/// <para>Text is centered both horizontally and vertically.</para>
/// </summary>
MiddleCenter,
/// <summary>
/// <para>Text is anchored in right side, centered vertically.</para>
/// </summary>
MiddleRight,
/// <summary>
/// <para>Text is anchored in lower left corner.</para>
/// </summary>
LowerLeft,
/// <summary>
/// <para>Text is anchored in lower side, centered horizontally.</para>
/// </summary>
LowerCenter,
/// <summary>
/// <para>Text is anchored in lower right corner.</para>
/// </summary>
LowerRight,
}
export enum Vertical {
Upper,
Middle,
Lower,
}
export enum Horizontal {
Left,
Center,
Right,
}
export class TextFieldLine {
public charList: string[] = [];
public quadList: GUIQuad[] = [];
public width: number = 0;
public index: number = 0;
}
export class TextFieldLayout {
public layout(target: UITextField): TextFieldLine[] {
let lineList: TextFieldLine[] = [];
let originSize = target.originSize;
let fontData = fonts.getFontData(target.font, originSize);
let realSize = target.fontSize / originSize;
this.makeTextLine(target.uiTransform, target.alignment, lineList, target.font, fontData, target.text, realSize, originSize, target.lineSpacing);
return lineList;
}
private makeTextLine(
transform: UITransform,
alignment: TextAnchor,
lineList: TextFieldLine[],
fontName: string,
fontData: FontInfo,
text: string,
realSize: number,
originSize: number,
lineSpacing: number,
): void {
let curLineIndex: number = -1;
let offsetX = 0;
let unitSize = originSize * realSize;
let halfUnitSize = unitSize * 0.5;
let maxTextWidthReal = transform.width / realSize;
let maxTextHeightReal = transform.height / realSize;
let transformOffsetX = 0;
let transformOffsetY = transform.height;
//new line
let makeLine = (): TextFieldLine => {
offsetX = 0;
curLineIndex++;
let line: TextFieldLine = new TextFieldLine();
line.index = curLineIndex;
lineList.push(line);
return line;
};
//make quad by char code
let makeQuad = (char: string, line: TextFieldLine): GUIQuad => {
const code = char.charCodeAt(0).toString();
let charSprite = fonts.getFnt(fontName, originSize, code);
let quad: GUIQuad = null;
if (charSprite) {
quad = GUIQuad.spawnQuad();
quad.sprite = charSprite;
quad.x = (offsetX + charSprite.xoffset) * realSize - transformOffsetX;
quad.y = (fontData.base - charSprite.height - charSprite.yoffset - fontData.base) * realSize + transformOffsetY;
quad.width = charSprite.offsetSize.width * realSize;
quad.height = charSprite.offsetSize.height * realSize;
offsetX += charSprite.xadvance;
} else {
if (char == '\n') {
} else if (char == '\t') {
offsetX += unitSize;
} else {
offsetX += halfUnitSize;
}
}
line.width = offsetX;
line.quadList.push(quad);
line.charList.push(char);
return quad;
};
//alignment
let alignTextLine = (): void => {
let tuple = this.getAlignment(alignment);
switch (tuple.v) {
case Vertical.Upper:
for (let i: number = 0, countI = lineList.length; i < countI; i++) {
let line = lineList[i];
if (i > 0) {
let lineOffsetY = i * unitSize * lineSpacing;
for (let j: number = 0, countJ = line.quadList.length; j < countJ; j++) {
let quad = line.quadList[j];
if (quad) {
quad.y -= lineOffsetY;
}
}
}
}
break;
case Vertical.Middle:
for (let i: number = 0, countI = lineList.length; i < countI; i++) {
let line = lineList[i];
let lineOffsetY = (maxTextHeightReal - countI * originSize * lineSpacing) * 0.5 * realSize + i * unitSize * lineSpacing;
for (let j: number = 0, countJ = line.quadList.length; j < countJ; j++) {
let quad = line.quadList[j];
if (quad) {
quad.y -= lineOffsetY;
}
}
}
break;
case Vertical.Lower:
for (let i: number = 0, countI = lineList.length; i < countI; i++) {
let line = lineList[i];
let lineOffsetY = (maxTextHeightReal - countI * originSize * lineSpacing) * realSize + i * unitSize * lineSpacing;
for (let j: number = 0, countJ = line.quadList.length; j < countJ; j++) {
let quad = line.quadList[j];
if (quad) {
quad.y -= lineOffsetY;
}
}
}
break;
}
switch (tuple.h) {
case Horizontal.Left:
break;
case Horizontal.Center:
for (let i: number = 0, countI = lineList.length; i < countI; i++) {
let line = lineList[i];
let lineOffsetX = (maxTextWidthReal - line.width) * 0.5 * realSize;
for (let j: number = 0, countJ = line.quadList.length; j < countJ; j++) {
let quad = line.quadList[j];
if (quad) {
quad.x += lineOffsetX;
}
}
}
break;
case Horizontal.Right:
for (let i: number = 0, countI = lineList.length; i < countI; i++) {
let line = lineList[i];
let lineOffsetX = (maxTextWidthReal - line.width) * realSize;
for (let j: number = 0, countJ = line.quadList.length; j < countJ; j++) {
let quad = line.quadList[j];
if (quad) {
quad.x += lineOffsetX;
}
}
}
break;
}
};
//Parse text
let parseText = (): void => {
let curLine: TextFieldLine = null;
let totalLength: number = text.length;
for (let i = 0; i < totalLength; i++) {
curLine ||= makeLine();
let char = text.charAt(i);
if (char == '\n' || char == '\t') {
//wrap symbol
curLine = null;
} else {
makeQuad(char, curLine);
let autoWrap = curLine.width + halfUnitSize >= maxTextWidthReal;
if (autoWrap) {
curLine = makeLine();
}
}
}
};
parseText();
alignTextLine();
}
private getAlignment(alignment: TextAnchor): { v: Vertical; h: Horizontal } {
let ret: { v: Vertical; h: Horizontal } = { v: Vertical.Upper, h: Horizontal.Left };
switch (alignment) {
case TextAnchor.UpperCenter:
ret.v = Vertical.Upper;
ret.h = Horizontal.Center;
break;
case TextAnchor.UpperLeft:
ret.v = Vertical.Upper;
ret.h = Horizontal.Left;
break;
case TextAnchor.UpperRight:
ret.v = Vertical.Upper;
ret.h = Horizontal.Right;
break;
case TextAnchor.MiddleCenter:
ret.v = Vertical.Middle;
ret.h = Horizontal.Center;
break;
case TextAnchor.MiddleLeft:
ret.v = Vertical.Middle;
ret.h = Horizontal.Left;
break;
case TextAnchor.MiddleRight:
ret.v = Vertical.Middle;
ret.h = Horizontal.Right;
break;
case TextAnchor.LowerCenter:
ret.v = Vertical.Lower;
ret.h = Horizontal.Center;
break;
case TextAnchor.LowerLeft:
ret.v = Vertical.Lower;
ret.h = Horizontal.Left;
break;
case TextAnchor.LowerRight:
ret.v = Vertical.Lower;
ret.h = Horizontal.Right;
break;
}
return ret;
}
}