-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
TextTask.java
422 lines (368 loc) · 13 KB
/
TextTask.java
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package org.figuramc.figura.model.rendertasks;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import org.figuramc.figura.FiguraMod;
import org.figuramc.figura.avatar.Avatar;
import org.figuramc.figura.avatar.Badges;
import org.figuramc.figura.font.Emojis;
import org.figuramc.figura.lua.LuaNotNil;
import org.figuramc.figura.lua.LuaWhitelist;
import org.figuramc.figura.lua.docs.LuaMethodDoc;
import org.figuramc.figura.lua.docs.LuaMethodOverload;
import org.figuramc.figura.lua.docs.LuaTypeDoc;
import org.figuramc.figura.math.vector.FiguraVec3;
import org.figuramc.figura.math.vector.FiguraVec4;
import org.figuramc.figura.model.FiguraModelPart;
import org.figuramc.figura.utils.ColorUtils;
import org.figuramc.figura.utils.LuaUtils;
import org.figuramc.figura.utils.TextUtils;
import org.joml.Matrix4f;
import org.luaj.vm2.LuaError;
import java.util.List;
@LuaWhitelist
@LuaTypeDoc(
name = "TextTask",
value = "text_task"
)
public class TextTask extends RenderTask {
private String textCached;
private List<Component> text;
private TextUtils.Alignment alignment = TextUtils.Alignment.LEFT;
private boolean shadow = false, outline = false;
private boolean background = false, seeThrough = false;
private Integer outlineColor, backgroundColor;
private int opacity = 0xFF;
private int width = 0;
private boolean wrap = true;
private int cachedComplexity, cacheWidth, cacheHeight;
public TextTask(String name, Avatar owner, FiguraModelPart parent) {
super(name, owner, parent);
}
@Override
public void render(PoseStack poseStack, MultiBufferSource buffer, int light, int overlay) {
if (opacity == 0) return; // lol
// prepare matrices
Matrix4f matrix = poseStack.last().pose();
matrix.scale(-1, -1, -1);
// prepare variables
Font font = Minecraft.getInstance().font;
int l = this.customization.light != null ? this.customization.light : light;
int bg = backgroundColor != null ? ColorUtils.intRGBAToIntARGB(backgroundColor) : background ? (int) (Minecraft.getInstance().options.getBackgroundOpacity(0.25f) * 0xFF) << 24 : 0;
int out = outlineColor != null ? outlineColor : 0x202020;
int op = opacity << 24 | 0xFFFFFF;
Font.DisplayMode displayMode = seeThrough ? Font.DisplayMode.SEE_THROUGH : Font.DisplayMode.POLYGON_OFFSET;
float vertexOffset = outline ? FiguraMod.VERTEX_OFFSET : 0f;
// background
if (bg != 0) {
int offset = alignment.apply(cacheWidth);
float x1 = -1 - offset;
float x2 = cacheWidth - offset;
VertexConsumer vertexConsumer = buffer.getBuffer(seeThrough ? RenderType.textBackgroundSeeThrough() : RenderType.textBackground());
vertexConsumer.vertex(matrix, x1, -1f, vertexOffset).color(bg).uv2(l).endVertex();
vertexConsumer.vertex(matrix, x1, cacheHeight, vertexOffset).color(bg).uv2(l).endVertex();
vertexConsumer.vertex(matrix, x2, cacheHeight, vertexOffset).color(bg).uv2(l).endVertex();
vertexConsumer.vertex(matrix, x2, -1f, vertexOffset).color(bg).uv2(l).endVertex();
}
// text
for (int i = 0, j = 0; i < text.size(); i++, j += (font.lineHeight + 1)) {
Component text = this.text.get(i);
int x = -alignment.apply(font, text);
if (outline) {
font.drawInBatch8xOutline(text.getVisualOrderText(), x, j, -1, out, matrix, buffer, l);
if (seeThrough)
font.drawInBatch(text, x, j, op, shadow, matrix, buffer, displayMode, 0, l);
} else {
font.drawInBatch(text, x, j, op, shadow, matrix, buffer, displayMode, 0, l);
}
}
}
@Override
public int getComplexity() {
return cachedComplexity;
}
@Override
public boolean shouldRender() {
return super.shouldRender() && text != null && text.size() != 0;
}
private void updateText() {
if (this.textCached == null) {
this.text = null;
return;
}
Component component = TextUtils.tryParseJson(this.textCached);
component = Badges.noBadges4U(component);
component = Emojis.applyEmojis(component);
component = Emojis.removeBlacklistedEmojis(component);
this.text = TextUtils.formatInBounds(component, Minecraft.getInstance().font, width, wrap);
Font font = Minecraft.getInstance().font;
cacheWidth = TextUtils.getWidth(this.text, font);
cacheHeight = TextUtils.getHeight(this.text, font);
}
// -- lua -- //
@LuaWhitelist
@LuaMethodDoc("text_task.get_text")
public String getText() {
return textCached;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = String.class,
argumentNames = "text"
),
aliases = "text",
value = "text_task.set_text"
)
public TextTask setText(String text) {
this.textCached = text;
updateText();
if (text != null) this.cachedComplexity = text.length() + 1;
return this;
}
@LuaWhitelist
public TextTask text(String text) {
return setText(text);
}
@LuaWhitelist
@LuaMethodDoc("text_task.get_alignment")
public String getAlignment() {
return this.alignment.name();
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = String.class,
argumentNames = "alignment"
),
aliases = "alignment",
value = "text_task.set_alignment"
)
public TextTask setAlignment(@LuaNotNil String alignment) {
try {
this.alignment = TextUtils.Alignment.valueOf(alignment.toUpperCase());
} catch (Exception ignored) {
throw new LuaError("Invalid alignment type \"" + alignment + "\"");
}
return this;
}
@LuaWhitelist
public TextTask alignment(@LuaNotNil String alignment) {
return setAlignment(alignment);
}
@LuaWhitelist
@LuaMethodDoc("text_task.has_shadow")
public boolean hasShadow() {
return this.shadow;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = Boolean.class,
argumentNames = "shadow"
),
aliases = "shadow",
value = "text_task.set_shadow"
)
public TextTask setShadow(boolean shadow) {
this.shadow = shadow;
return this;
}
@LuaWhitelist
public TextTask shadow(boolean shadow) {
return setShadow(shadow);
}
@LuaWhitelist
@LuaMethodDoc("text_task.has_outline")
public boolean hasOutline() {
return this.outline;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = Boolean.class,
argumentNames = "outline"
),
aliases = "outline",
value = "text_task.set_outline"
)
public TextTask setOutline(boolean outline) {
this.outline = outline;
return this;
}
@LuaWhitelist
public TextTask outline(boolean outline) {
return setOutline(outline);
}
@LuaWhitelist
@LuaMethodDoc("text_task.get_outline_color")
public FiguraVec3 getOutlineColor() {
return this.outlineColor != null ? ColorUtils.intToRGB(this.outlineColor) : FiguraVec3.of();
}
@LuaWhitelist
@LuaMethodDoc(
overloads = {
@LuaMethodOverload(
argumentTypes = FiguraVec3.class,
argumentNames = "color"
),
@LuaMethodOverload(
argumentTypes = {Double.class, Double.class, Double.class},
argumentNames = {"r", "g", "b"}
)
},
aliases = "outlineColor",
value = "text_task.set_outline_color"
)
public TextTask setOutlineColor(Object x, Double y, Double z) {
FiguraVec3 vec = LuaUtils.parseVec3("setOutlineColor", x, y, z);
this.outlineColor = ColorUtils.rgbToInt(vec);
return this;
}
@LuaWhitelist
public TextTask outlineColor(Object x, Double y, Double z) {
return setOutlineColor(x, y, z);
}
@LuaWhitelist
@LuaMethodDoc("text_task.get_width")
public int getWidth() {
return width;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = Integer.class,
argumentNames = "width"
),
aliases = "width",
value = "text_task.set_width"
)
public TextTask setWidth(int width) {
this.width = width;
updateText();
return this;
}
@LuaWhitelist
public TextTask width(int width) {
return setWidth(width);
}
@LuaWhitelist
@LuaMethodDoc("text_task.has_wrap")
public boolean hasWrap() {
return wrap;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = Boolean.class,
argumentNames = "wrap"
),
aliases = "wrap",
value = "text_task.set_wrap"
)
public TextTask setWrap(boolean wrap) {
this.wrap = wrap;
updateText();
return this;
}
@LuaWhitelist
public TextTask wrap(boolean wrap) {
return setWrap(wrap);
}
@LuaWhitelist
@LuaMethodDoc("text_task.is_see_through")
public boolean isSeeThrough() {
return this.seeThrough;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = Boolean.class,
argumentNames = "seeThrough"
),
aliases = "seeThrough",
value = "text_task.set_see_through"
)
public TextTask setSeeThrough(boolean seeThrough) {
this.seeThrough = seeThrough;
return this;
}
@LuaWhitelist
public TextTask seeThrough(boolean seeThrough) {
return setSeeThrough(seeThrough);
}
@LuaWhitelist
@LuaMethodDoc("text_task.has_background")
public boolean hasBackground() {
return this.background;
}
@LuaWhitelist
@LuaMethodDoc(
overloads = @LuaMethodOverload(
argumentTypes = Boolean.class,
argumentNames = "background"
),
aliases = "background",
value = "text_task.set_background"
)
public TextTask setBackground(boolean background) {
this.background = background;
return this;
}
@LuaWhitelist
public TextTask background(boolean background) {
return setBackground(background);
}
@LuaWhitelist
@LuaMethodDoc("text_task.get_background_color")
public FiguraVec4 getBackgroundColor() {
return this.backgroundColor == null ? null : ColorUtils.intToRGBA(this.backgroundColor);
}
@LuaWhitelist
@LuaMethodDoc(
overloads = {
@LuaMethodOverload(
argumentTypes = FiguraVec4.class,
argumentNames = "rgba"
),
@LuaMethodOverload(
argumentTypes = {Double.class, Double.class, Double.class, Double.class},
argumentNames = {"r", "g", "b", "a"}
)
},
aliases = "backgroundColor",
value = "text_task.set_background_color"
)
public TextTask setBackgroundColor(Object r, Double g, Double b, Double a) {
FiguraVec4 vec = LuaUtils.parseVec4("setBackgroundColor", r, g, b, a, 0, 0, 0, Minecraft.getInstance().options.getBackgroundOpacity(0.25f));
this.backgroundColor = ColorUtils.rgbaToInt(vec);
return this;
}
@LuaWhitelist
public TextTask backgroundColor(Object r, Double g, Double b, Double a) {
return setBackgroundColor(r, g, b, a);
}
@LuaWhitelist
@LuaMethodDoc("text_task.get_opacity")
public float getOpacity() {
return opacity / (float) 0xFF;
}
@LuaWhitelist
@LuaMethodDoc(overloads = @LuaMethodOverload(argumentTypes = Float.class, argumentNames = "opacity"), aliases = "opacity", value = "text_task.set_opacity")
public TextTask setOpacity(float opacity) {
this.opacity = (int) (opacity * 0xFF);
return this;
}
@LuaWhitelist
public TextTask opacity(float opacity) {
return setOpacity(opacity);
}
@Override
public String toString() {
return name + " (Text Render Task)";
}
}