generated from CleanroomMC/ForgeDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathTextFieldRenderer.java
More file actions
264 lines (237 loc) · 9.84 KB
/
TextFieldRenderer.java
File metadata and controls
264 lines (237 loc) · 9.84 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
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
package com.cleanroommc.modularui.widgets.textfield;
import com.cleanroommc.modularui.drawable.text.TextRenderer;
import com.cleanroommc.modularui.utils.Color;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.awt.geom.Point2D;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
public class TextFieldRenderer extends TextRenderer {
private static final DecimalFormat INTEGER_FIELD_FORMAT = new DecimalFormat("#");
private static final char groupingSeparator = INTEGER_FIELD_FORMAT.getDecimalFormatSymbols().getGroupingSeparator();
static {
INTEGER_FIELD_FORMAT.setGroupingUsed(true);
INTEGER_FIELD_FORMAT.setGroupingSize(3);
}
protected final TextFieldHandler handler;
protected int markedColor = 0x2F72A8;
protected int cursorColor = 0xFFFFFFFF;
protected boolean renderCursor = false;
private boolean formatAsInteger = false;
public TextFieldRenderer(TextFieldHandler handler) {
this.handler = handler;
}
public void toggleCursor() {
this.renderCursor = !this.renderCursor;
}
public void setCursor(boolean active) {
this.renderCursor = active;
}
public void setMarkedColor(int markedColor) {
this.markedColor = markedColor;
}
public void setCursorColor(int cursorColor) {
this.cursorColor = cursorColor;
}
@ApiStatus.Experimental
public void setFormatAsInteger(boolean formatAsInteger) {
this.formatAsInteger = formatAsInteger;
}
@Override
public void draw(List<String> lines) {
if (formatAsInteger) lines = decorateLines(lines);
super.draw(lines);
}
private static @NotNull List<String> decorateLines(List<String> lines) {
return lines.stream().map(TextFieldRenderer::tryFormatString)
.collect(Collectors.toList());
}
private static @NotNull String tryFormatString(String str) {
try {
return INTEGER_FIELD_FORMAT.format(Long.parseLong(str));
} catch (NumberFormatException e) {
return str;
}
}
@Override
protected void drawMeasuredLines(List<Line> measuredLines) {
drawMarked(measuredLines);
super.drawMeasuredLines(measuredLines);
// draw cursor
if (this.renderCursor) {
Point main = this.handler.getMainCursor();
Point2D.Float start = getPosOf(measuredLines, main);
if (this.handler.getText().get(main.y).isEmpty()) {
start.x += 0.7f;
}
drawCursor(start.x, start.y);
}
}
@Override
public List<String> wrapLine(String line) {
return Collections.singletonList(line);
}
protected void drawMarked(List<Line> measuredLines) {
if (!this.simulate && this.handler.hasTextMarked()) {
Point2D.Float start = getPosOf(measuredLines, this.handler.getStartCursor());
// render Marked
Point2D.Float end = getPosOf(measuredLines, this.handler.getEndCursor());
if (start.y == end.y) {
drawMarked(start.y, start.x, end.x);
} else {
int min = this.handler.getStartCursor().y;
int max = this.handler.getEndCursor().y;
Line line = measuredLines.get(min);
int startX = getStartX(line.getWidth());
drawMarked(start.y, start.x, startX + line.getWidth());
start.y += getFontHeight();
if (max - min > 1) {
for (int i = min + 1; i < max; i++) {
line = measuredLines.get(i);
startX = getStartX(line.getWidth());
drawMarked(start.y, startX, startX + line.getWidth());
start.y += getFontHeight();
}
}
line = measuredLines.get(max);
startX = getStartX(line.getWidth());
drawMarked(start.y, startX, end.x);
}
}
}
public Point getCursorPos(List<String> lines, int x, int y) {
if (lines.isEmpty()) {
return new Point();
}
if (formatAsInteger) lines = decorateLines(lines);
List<Line> measuredLines = measureLines(lines);
y -= getStartY(measuredLines.size());
int index = (int) (y / (getFontHeight()));
if (index < 0) return new Point();
if (index >= measuredLines.size()) {
return new Point(getRealLength(measuredLines.get(measuredLines.size() - 1).getText()), measuredLines.size() - 1);
}
Line line = measuredLines.get(index);
x -= getStartX(line.getWidth());
if (line.getWidth() <= 0) return new Point(0, index);
if (line.getWidth() < x) {
return new Point(getRealLength(line.getText()), index);
}
float currentX = 0;
int ignoredChars = 0;
for (int i = 0; i < line.getText().length(); i++) {
char c = line.getText().charAt(i);
float charWidth = getFontRenderer().getCharWidth(c) * this.scale;
currentX += charWidth;
if (isIgnoredChar(c)) ignoredChars++;
if (currentX >= x) {
// dist with current letter < dist without current letter -> next letter pos
if (Math.abs(currentX - x) < Math.abs(currentX - charWidth - x)) i++;
return new Point(i - ignoredChars, index);
}
}
return new Point();
}
/**
* Whether the given character should be ignored for cursor positioning purposes
*/
@ApiStatus.Experimental
protected boolean isIgnoredChar(int c) {
return formatAsInteger && c == groupingSeparator;
}
private int getRealLength(String text) {
int length = text.length();
if (formatAsInteger) length -= (int) text.chars().filter(this::isIgnoredChar).count();
return length;
}
public Point2D.Float getPosOf(List<Line> measuredLines, Point cursorPos) {
if (measuredLines.isEmpty()) {
return new Point2D.Float(getStartX(0), getStartYOfLines(1));
}
Line line = measuredLines.get(cursorPos.y);
String sub = getStringBeforeCursor(line, cursorPos);
return new Point2D.Float(getStartX(line.getWidth()) + getFontRenderer().getStringWidth(sub) * this.scale, getStartYOfLines(measuredLines.size()) + cursorPos.y * getFontHeight());
}
private @NotNull String getStringBeforeCursor(Line line, Point cursorPos) {
String text = line.getText();
String sub = text.substring(0, Math.min(text.length(), cursorPos.x));
if (formatAsInteger) {
int i = 0;
int ignoredChars = 0;
while (i < sub.length() && i + ignoredChars < text.length()) {
if (isIgnoredChar(text.charAt(i + ignoredChars))) {
ignoredChars++;
} else {
i++;
}
}
if (ignoredChars > 0) {
sub = sub + text.substring(sub.length(), Math.min(text.length(), cursorPos.x + ignoredChars));
}
}
return sub;
}
@SideOnly(Side.CLIENT)
public void drawMarked(float y0, float x0, float x1) {
y0 -= 1;
float y1 = y0 + getFontHeight();
float red = Color.getRedF(this.markedColor);
float green = Color.getGreenF(this.markedColor);
float blue = Color.getBlueF(this.markedColor);
float alpha = Color.getAlphaF(this.markedColor);
if (alpha == 0)
alpha = 1f;
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
GlStateManager.color(red, green, blue, alpha);
GlStateManager.disableTexture2D();
bufferbuilder.begin(7, DefaultVertexFormats.POSITION);
bufferbuilder.pos(x0, y1, 0.0D).endVertex();
bufferbuilder.pos(x1, y1, 0.0D).endVertex();
bufferbuilder.pos(x1, y0, 0.0D).endVertex();
bufferbuilder.pos(x0, y0, 0.0D).endVertex();
tessellator.draw();
GlStateManager.disableColorLogic();
GlStateManager.enableTexture2D();
GlStateManager.color(1, 1, 1, 1);
}
@SideOnly(Side.CLIENT)
private void drawCursor(float x0, float y0) {
x0 = (x0 - 0.8f) / this.scale;
y0 = (y0 - 1) / this.scale;
float x1 = x0 + 0.6f;
float y1 = y0 + 9;
float red = Color.getRedF(this.cursorColor);
float green = Color.getGreenF(this.cursorColor);
float blue = Color.getBlueF(this.cursorColor);
float alpha = Color.getAlphaF(this.cursorColor);
if (alpha == 0)
alpha = 1f;
Tessellator tessellator = Tessellator.getInstance();
BufferBuilder bufferbuilder = tessellator.getBuffer();
GlStateManager.disableBlend();
GlStateManager.pushMatrix();
GlStateManager.scale(this.scale, this.scale, 0);
GlStateManager.color(red, green, blue, alpha);
GlStateManager.disableTexture2D();
bufferbuilder.begin(7, DefaultVertexFormats.POSITION);
bufferbuilder.pos(x0, y1, 0.0D).endVertex();
bufferbuilder.pos(x1, y1, 0.0D).endVertex();
bufferbuilder.pos(x1, y0, 0.0D).endVertex();
bufferbuilder.pos(x0, y0, 0.0D).endVertex();
tessellator.draw();
GlStateManager.color(1, 1, 1, 1);
GlStateManager.enableTexture2D();
GlStateManager.popMatrix();
GlStateManager.enableBlend();
}
}