-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathSimpleTextWidget.java
More file actions
72 lines (62 loc) · 2.57 KB
/
SimpleTextWidget.java
File metadata and controls
72 lines (62 loc) · 2.57 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
package gregtech.api.gui.widgets;
import gregtech.api.gui.IRenderContext;
import gregtech.api.gui.Widget;
import gregtech.api.util.LocalisationUtils;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.network.PacketBuffer;
import java.util.function.Supplier;
/**
* Simple one-line text widget with text synced and displayed
* as the raw string from the server
*/
public class SimpleTextWidget extends Widget {
protected String formatLocale;
protected int color;
protected Supplier<String> textSupplier;
protected String lastText = "";
public SimpleTextWidget(int xPosition, int yPosition, String formatLocale, int color, Supplier<String> textSupplier) {
super(new Position(xPosition, yPosition), Size.ZERO);
this.color = color;
this.formatLocale = formatLocale;
this.textSupplier = textSupplier;
}
public SimpleTextWidget(int xPosition, int yPosition, String formatLocale, Supplier<String> textSupplier) {
this(xPosition, yPosition, formatLocale, 0x404040, textSupplier);
}
private void updateSize() {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
int stringWidth = fontRenderer.getStringWidth(lastText);
setSize(new Size(stringWidth, fontRenderer.FONT_HEIGHT));
if (uiAccess != null) {
uiAccess.notifySizeChange();
}
}
@Override
public void drawInBackground(int mouseX, int mouseY, IRenderContext context) {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
String text = formatLocale.isEmpty() ? (LocalisationUtils.hasKey(lastText) ? LocalisationUtils.format(lastText) : lastText) : LocalisationUtils.format(formatLocale, lastText);
Position position = getPosition();
fontRenderer.drawString(text,
position.x - fontRenderer.getStringWidth(text) / 2,
position.y - fontRenderer.FONT_HEIGHT / 2, color);
GlStateManager.color(1.0f, 1.0f, 1.0f);
}
@Override
public void detectAndSendChanges() {
if (!textSupplier.get().equals(lastText)) {
this.lastText = textSupplier.get();
writeUpdateInfo(1, buffer -> buffer.writeString(lastText));
}
}
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
if (id == 1) {
this.lastText = buffer.readString(Short.MAX_VALUE);
updateSize();
}
}
}