-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextItem.java
More file actions
112 lines (92 loc) · 3.36 KB
/
Copy pathTextItem.java
File metadata and controls
112 lines (92 loc) · 3.36 KB
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
/*
ahbejarano
Game item wrapper class.
*/
package geetransit.minecraft05.engine;
import java.util.*;
import java.nio.charset.Charset;
import org.joml.Vector4f;
public class TextItem extends Item {
private static final float ZPOS = 0f;
private static final int VERTICES_PER_QUAD = 4;
private String text;
private final int fontCols;
private final int fontRows;
public TextItem(String text, String fontFile, int fontCols, int fontRows) throws Exception {
super();
this.text = text;
this.fontCols = fontCols;
this.fontRows = fontRows;
this.mesh = this.buildMesh(new Texture(fontFile));
}
public String getText() { return this.text; }
public int getFontCols() { return this.fontCols; }
public int getFontRows() { return this.fontRows; }
public Item setText(String text) {
this.text = text;
Vector4f color = this.mesh.getColor();
this.mesh.cleanup(false);
this.mesh = this.buildMesh(this.mesh.getTexture());
this.mesh.setColor(color);
return this;
}
private Mesh buildMesh(Texture texture) {
byte[] charArray = this.text.getBytes(Charset.forName("ISO-8859-1"));
List<Float> posList = new ArrayList<>();
List<Float> coordList = new ArrayList<>();
List<Integer> indexList = new ArrayList<>();
float width = (float) texture.getWidth() / this.fontCols;
float length = (float) texture.getLength() / this.fontRows;
int currentCol = 0;
int currentRow = 0;
int currentIndex = 0;
for (int i = 0; i < charArray.length; i++) {
byte currentChar = charArray[i];
if (currentChar == '\n') {
currentRow++;
currentCol = 0;
continue;
}
// Build a character tile composed by two triangles
int fontCol = currentChar % this.fontCols;
int fontRow = currentChar / this.fontCols;
// Left Top vertex
posList.add(currentCol*width); // x
posList.add(currentRow*length); // y
posList.add(ZPOS); // z
coordList.add((float) fontCol / this.fontCols);
coordList.add((float) fontRow / this.fontRows);
indexList.add(currentIndex*VERTICES_PER_QUAD + 0);
// Left Bottom vertex
posList.add(currentCol*width); // x
posList.add(currentRow*length + length); // y
posList.add(ZPOS); // z
coordList.add((float) fontCol / this.fontCols);
coordList.add((float) (fontRow + 1) / this.fontRows);
indexList.add(currentIndex*VERTICES_PER_QUAD + 1);
// Right Bottom vertex
posList.add(currentCol*width + width); // x
posList.add(currentRow*length + length); // y
posList.add(ZPOS); // z
coordList.add((float) (fontCol + 1) / this.fontCols);
coordList.add((float) (fontRow + 1) / this.fontRows);
indexList.add(currentIndex*VERTICES_PER_QUAD + 2);
// Right Top vertex
posList.add(currentCol*width + width); // x
posList.add(currentRow*length); // y
posList.add(ZPOS); // z
coordList.add((float) (fontCol + 1) / this.fontCols);
coordList.add((float) fontRow / this.fontRows);
indexList.add(currentIndex*VERTICES_PER_QUAD + 3);
// Add indices for left top and bottom right vertices
indexList.add(currentIndex*VERTICES_PER_QUAD + 0);
indexList.add(currentIndex*VERTICES_PER_QUAD + 2);
currentCol++;
currentIndex++;
}
float[] posArray = Utils.floatListToArray(posList);
float[] coordArray = Utils.floatListToArray(coordList);
int[] indexArray = Utils.intListToArray(indexList);
return new Mesh(posArray, indexArray, coordArray).setTexture(texture);
}
}