-
Notifications
You must be signed in to change notification settings - Fork 12
/
TextRenderer.java
429 lines (368 loc) · 13.2 KB
/
TextRenderer.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
423
424
425
426
427
428
429
package top.hendrixshen.magiclib.impl.render;
import com.google.common.collect.Lists;
import com.mojang.blaze3d.platform.GlStateManager;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.api.render.context.RenderContext;
import top.hendrixshen.magiclib.impl.render.context.RenderGlobal;
import top.hendrixshen.magiclib.util.minecraft.PositionUtil;
import top.hendrixshen.magiclib.util.minecraft.render.RenderUtil;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
//#if MC > 12005 || MC < 11700 && MC > 11502
import com.mojang.blaze3d.vertex.PoseStack;
//#endif
//#if MC > 11903
//$$ import net.minecraft.client.gui.Font;
//#endif
//#if MC > 11605
//$$ import com.mojang.blaze3d.systems.RenderSystem;
//#endif
//#if MC > 11502
import net.minecraft.util.FormattedCharSequence;
import top.hendrixshen.magiclib.util.minecraft.render.TextRenderUtil;
//#endif
//#if MC > 11404
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.math.Matrix4f;
import com.mojang.math.Transformation;
import net.minecraft.client.renderer.MultiBufferSource;
//#endif
/**
* Reference to <a href="https://github.com/Fallen-Breath/tweakermore">TweakerMore</a>
*/
public class TextRenderer {
public static final double DEFAULT_FONT_SCALE = 0.025;
private final List<TextHolder> lines;
@Getter
private Vec3 pos;
private double shiftX;
private double shiftY;
private double fontScale;
private double lineHeightRatio;
private int color;
private int backgroundColor;
private boolean shadow;
private boolean seeThrough;
private HorizontalAlignment horizontalAlignment;
private VerticalAlignment verticalAlignment;
public static @NotNull TextRenderer create() {
return new TextRenderer();
}
private TextRenderer() {
this.lines = Lists.newArrayList();
this.shiftX = this.shiftY = 0.0;
this.fontScale = TextRenderer.DEFAULT_FONT_SCALE;
this.lineHeightRatio = 1.0 * RenderUtil.TEXT_LINE_HEIGHT / RenderUtil.TEXT_HEIGHT;
this.color = 0xFFFFFFFF;
this.backgroundColor = 0x00000000;
this.shadow = false;
this.seeThrough = false;
this.horizontalAlignment = HorizontalAlignment.DEFAULT;
this.verticalAlignment = VerticalAlignment.DEFAULT;
}
/**
* Draw given lines with centered format.
* <p>
* Reference: {@link net.minecraft.client.renderer.debug.DebugRenderer#renderFloatingText(String, double,
* double, double, int, float, boolean, float, boolean)}
* <p>
* Note:
* - shadow=true + seeThrough=false might result in weird rendering.
*/
@SuppressWarnings("UnnecessaryLocalVariable")
public void render() {
if (this.lines.isEmpty()) {
return;
}
Minecraft mc = Minecraft.getInstance();
RenderContext context = RenderContext.of(
//#if MC > 12004
//$$ new PoseStack()
//#elseif MC > 11605
//$$ RenderSystem.getModelViewStack()
//#elseif MC > 11502
new PoseStack()
//#endif
);
CameraPositionTransformer positionTransformer = CameraPositionTransformer.create(this.pos);
positionTransformer.apply(context);
context.scale(-this.fontScale, -this.fontScale, this.fontScale);
//#if MC < 11700
RenderGlobal.disableLighting();
//#endif
if (this.seeThrough) {
RenderGlobal.disableDepthTest();
} else {
RenderGlobal.enableDepthTest();
}
//#if MC < 11904
RenderGlobal.enableTexture();
//#endif
RenderGlobal.depthMask(true);
int lineNum = this.lines.size();
double maxTextWidth = this.lines.stream().mapToInt(TextHolder::getWidth).max().orElse(0);
double totalTextWidth = maxTextWidth;
double totalTextHeight = RenderUtil.TEXT_HEIGHT * lineNum + (this.lineHeightRatio - 1) * (lineNum - 1);
context.translate(this.horizontalAlignment.getTranslateX(totalTextWidth),
this.verticalAlignment.getTranslateY(totalTextHeight), 0);
context.translate(this.shiftX, this.shiftY, 0);
//#if MC > 11605
//$$ RenderSystem.applyModelViewMatrix();
//#else
RenderGlobal.enableAlphaTest();
//#endif
// Enable transparent-able text rendering.
RenderGlobal.enableBlend();
RenderGlobal.blendFunc(
//#if MC > 11404
GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA
//#else
//$$ GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA
//#endif
);
for (int i = 0; i < lineNum; i++) {
TextHolder holder = this.lines.get(i);
float textX = (float) this.horizontalAlignment.getTextX(maxTextWidth, holder.getWidth());
float textY = (float) (getLineHeight() * i);
//#if MC > 11404
int backgroundColor = this.backgroundColor;
while (true) {
MultiBufferSource.BufferSource immediate = MultiBufferSource.immediate(
Tesselator.getInstance().getBuilder());
//#if MC > 12004
//$$ Matrix4f matrix4f = context.getPoseStack().last().pose();
//#else
Matrix4f matrix4f = Transformation.identity().getMatrix();
//#endif
mc.font.drawInBatch(
holder.text,
textX,
textY,
this.color,
this.shadow,
matrix4f,
immediate,
//#if MC > 11903
//$$ this.seeThrough ? Font.DisplayMode.SEE_THROUGH : Font.DisplayMode.NORMAL,
//#else
this.seeThrough,
//#endif
backgroundColor,
0xF000F0
);
immediate.endBatch();
// Draw twice when having background.
if (backgroundColor == 0) {
break;
} else {
backgroundColor = 0;
}
}
//#else
//$$ if (this.shadow) {
//$$ mc.font.drawShadow(holder.text, textX, textY, this.color);
//$$ } else {
//$$ mc.font.draw(holder.text, textX, textY, this.color);
//$$ }
//#endif
}
//#if MC < 11600
//$$ RenderGlobal.color4f(1.0F, 1.0F, 1.0F, 1.0F);
//#endif
//TODO check color4f, see if it can replace blendFunc
//#if MC < 11904
RenderGlobal.enableDepthTest();
//#endif
positionTransformer.restore();
//#if MC > 11605
//$$ RenderSystem.applyModelViewMatrix();
//#endif
}
private TextRenderer addLines(TextHolder... lines) {
Collections.addAll(this.lines, lines);
return this;
}
private TextRenderer setLines(TextHolder... lines) {
this.lines.clear();
this.addLines(lines);
return this;
}
//#if MC > 11600
public TextRenderer text(FormattedCharSequence text) {
return this.setLines(TextHolder.of(text));
}
//#endif
public TextRenderer text(String text) {
return this.setLines(TextHolder.of(text));
}
public TextRenderer text(Component text) {
return this.setLines(TextHolder.of(text));
}
//#if MC > 11502
public TextRenderer addLine(FormattedCharSequence text) {
return this.addLines(TextHolder.of(text));
}
//#endif
public TextRenderer addLine(String text) {
return this.addLines(TextHolder.of(text));
}
public TextRenderer addLine(Component text) {
return this.addLines(TextHolder.of(text));
}
public TextRenderer lineHeightRatio(double lineHeightRatio) {
this.lineHeightRatio = lineHeightRatio;
return this;
}
public TextRenderer at(Vec3 vec3) {
this.pos = vec3;
return this;
}
public TextRenderer at(double x, double y, double z) {
return this.at(new Vec3(x, y, z));
}
public TextRenderer atCenter(BlockPos blockPos) {
return this.at(PositionUtil.centerOf(blockPos));
}
/**
* Shift the text in the render length unit.
*/
public TextRenderer shift(double x, double y) {
this.shiftX = x;
this.shiftY = y;
return this;
}
public TextRenderer fontScale(double fontScale) {
this.fontScale = fontScale;
return this;
}
/**
* @param color the text color in the 0xAARRGGBB format.
*/
public TextRenderer color(int color) {
this.color = color;
return this;
}
/**
* @param backgroundColor the background color in the 0xAARRGGBB format.
*/
public TextRenderer bgColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
return this;
}
/**
* @param color the text color in the 0xAARRGGBB format.
* @param backgroundColor the background color in the 0xAARRGGBB format.
*/
public TextRenderer color(int color, int backgroundColor) {
this.color(color);
this.bgColor(backgroundColor);
return this;
}
public TextRenderer shadow(boolean shadow) {
this.shadow = shadow;
return this;
}
public TextRenderer shadow() {
return this.shadow(true);
}
public TextRenderer seeThrough(boolean seeThrough) {
this.seeThrough = seeThrough;
return this;
}
public TextRenderer seeThrough() {
return this.seeThrough(true);
}
public TextRenderer align(HorizontalAlignment horizontalAlignment) {
this.horizontalAlignment = horizontalAlignment;
return this;
}
public TextRenderer align(VerticalAlignment verticalAlignment) {
this.verticalAlignment = verticalAlignment;
return this;
}
public double getLineHeight() {
return RenderUtil.TEXT_HEIGHT * this.lineHeightRatio;
}
private static class TextHolder {
//#if MC > 11502
public final FormattedCharSequence text;
//#else
//$$ public final String text;
//#endif
private TextHolder(
//#if MC > 11502
FormattedCharSequence text
//#else
//$$ String text
//#endif
) {
this.text = text;
}
public static @NotNull TextHolder of(
//#if MC > 11600
FormattedCharSequence text
//#else
//$$ String text
//#endif
) {
return new TextHolder(text);
}
//#if MC > 11502
@Contract("_ -> new")
public static @NotNull TextHolder of(String text) {
return TextHolder.of(TextRenderUtil.string2formattedCharSequence(text));
}
//#endif
public static @NotNull TextHolder of(@NotNull Component text) {
return new TextHolder(
//#if MC > 11502
text.getVisualOrderText()
//#else
//$$ text.getColoredString()
//#endif
);
}
public int getWidth() {
return RenderUtil.getRenderWidth(this.text);
}
}
public enum HorizontalAlignment {
LEFT(w -> 0.0, (w, tw) -> 0.0), // [-x] ^Text [+x]
RIGHT(w -> -w, (w, tw) -> w - tw), // [-x] Text^ [+x]
CENTER(w -> -0.5 * w, (w, tw) -> 0.5 * (w - tw)); // [-x] Te^xt [+x]
public static final HorizontalAlignment DEFAULT = HorizontalAlignment.CENTER;
private final Function<Double, Double> trMapper;
private final BiFunction<Double, Double, Double> posMapper;
HorizontalAlignment(Function<Double, Double> trMapper, BiFunction<Double, Double, Double> posMapper) {
this.trMapper = trMapper;
this.posMapper = posMapper;
}
public double getTranslateX(double width) {
return this.trMapper.apply(width);
}
public double getTextX(double width, double textWidth) {
return this.posMapper.apply(width, textWidth);
}
}
public enum VerticalAlignment {
TOP(h -> 0.0), // [-y] ^Text [+y]
BOTTOM(h -> -h), // [-y] Text^ [+y]
CENTER(h -> -0.5 * h); // [-y] Te^xt [+y]
private final Function<Double, Double> trMapper;
public static final VerticalAlignment DEFAULT = VerticalAlignment.CENTER;
VerticalAlignment(Function<Double, Double> trMapper) {
this.trMapper = trMapper;
}
public double getTranslateY(double height) {
return this.trMapper.apply(height);
}
}
}