-
Notifications
You must be signed in to change notification settings - Fork 152
Expand file tree
/
Copy pathAdvancedTextWidget.java
More file actions
268 lines (240 loc) · 9.77 KB
/
AdvancedTextWidget.java
File metadata and controls
268 lines (240 loc) · 9.77 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
265
266
267
268
package gregtech.api.gui.widgets;
import gregtech.api.gui.IRenderContext;
import gregtech.api.gui.Widget;
import gregtech.api.gui.translation.EnhancedTextComponentSerializer;
import gregtech.api.gui.translation.EnhancedTextComponentTranslation;
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.gui.GuiScreen;
import net.minecraft.client.gui.GuiUtilRenderComponents;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.text.*;
import net.minecraft.util.text.event.ClickEvent;
import net.minecraft.util.text.event.ClickEvent.Action;
import net.minecraft.util.text.event.HoverEvent;
import net.minecraftforge.fml.client.config.GuiUtils;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import org.lwjgl.input.Mouse;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* Represents a text-component based widget, which obtains
* text from server and automatically synchronizes it with clients
*/
public class AdvancedTextWidget extends Widget {
protected int maxWidthLimit;
@SideOnly(Side.CLIENT)
private WrapScreen wrapScreen;
protected Consumer<List<ITextComponent>> textSupplier;
protected BiConsumer<String, ClickData> clickHandler;
private List<ITextComponent> displayText = new ArrayList<>();
private int color;
public AdvancedTextWidget(int xPosition, int yPosition, Consumer<List<ITextComponent>> text, int color) {
super(new Position(xPosition, yPosition), Size.ZERO);
this.textSupplier = text;
this.color = color;
}
public static ITextComponent withButton(ITextComponent textComponent, String componentData) {
Style style = textComponent.getStyle();
style.setClickEvent(new ClickEvent(Action.OPEN_URL, "@!" + componentData));
style.setColor(TextFormatting.YELLOW);
return textComponent;
}
public static ITextComponent withHoverTextTranslate(ITextComponent textComponent, String hoverTranslation) {
Style style = textComponent.getStyle();
ITextComponent translation = new TextComponentTranslation(hoverTranslation);
translation.getStyle().setColor(TextFormatting.GRAY);
style.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, translation));
return textComponent;
}
public AdvancedTextWidget setMaxWidthLimit(int maxWidthLimit) {
this.maxWidthLimit = maxWidthLimit;
if (isClientSide()) {
updateComponentTextSize();
}
return this;
}
public AdvancedTextWidget setClickHandler(BiConsumer<String, ClickData> clickHandler) {
this.clickHandler = clickHandler;
return this;
}
@SideOnly(Side.CLIENT)
private WrapScreen getWrapScreen() {
if (wrapScreen == null)
wrapScreen = new WrapScreen();
return wrapScreen;
}
@SideOnly(Side.CLIENT)
private void resizeWrapScreen() {
if (sizes != null) {
getWrapScreen().setWorldAndResolution(Minecraft.getMinecraft(), sizes.getScreenWidth(), sizes.getScreenHeight());
}
}
@Override
public void initWidget() {
super.initWidget();
if (isClientSide()) {
resizeWrapScreen();
}
}
@Override
protected void onPositionUpdate() {
super.onPositionUpdate();
if (isClientSide()) {
resizeWrapScreen();
}
}
@Override
public void detectAndSendChanges() {
ArrayList<ITextComponent> textBuffer = new ArrayList<>();
textSupplier.accept(textBuffer);
if (!displayText.equals(textBuffer)) {
this.displayText = textBuffer;
writeUpdateInfo(1, buffer -> {
buffer.writeVarInt(displayText.size());
for (ITextComponent textComponent : displayText) {
buffer.writeString(EnhancedTextComponentSerializer.componentToJson(textComponent));
}
});
}
}
protected ITextComponent getTextUnderMouse(int mouseX, int mouseY) {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
Position position = getPosition();
int selectedLine = (mouseY - position.y) / (fontRenderer.FONT_HEIGHT + 2);
if (mouseX >= position.x && selectedLine >= 0 && selectedLine < displayText.size()) {
ITextComponent selectedComponent = displayText.get(selectedLine);
int mouseOffset = mouseX - position.x;
int currentOffset = 0;
for (ITextComponent lineComponent : selectedComponent) {
currentOffset += fontRenderer.getStringWidth(lineComponent.getUnformattedComponentText());
if (currentOffset >= mouseOffset) {
return lineComponent;
}
}
}
return null;
}
@SideOnly(Side.CLIENT)
private void updateComponentTextSize() {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
int maxStringWidth = 0;
int totalHeight = 0;
for (ITextComponent textComponent : displayText) {
maxStringWidth = Math.max(maxStringWidth, fontRenderer.getStringWidth(textComponent.getFormattedText()));
totalHeight += fontRenderer.FONT_HEIGHT + 2;
}
totalHeight -= 2;
setSize(new Size(maxStringWidth, totalHeight));
if (uiAccess != null) {
uiAccess.notifySizeChange();
}
}
@SideOnly(Side.CLIENT)
private void formatDisplayText() {
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
int maxTextWidthResult = maxWidthLimit == 0 ? Integer.MAX_VALUE : maxWidthLimit;
this.displayText = displayText.stream()
.flatMap(c -> GuiUtilRenderComponents.splitText(c, maxTextWidthResult, fontRenderer, true, true).stream())
.collect(Collectors.toList());
}
@Override
public void readUpdateInfo(int id, PacketBuffer buffer) {
if (id == 1) {
this.displayText.clear();
int count = buffer.readVarInt();
for (int i = 0; i < count; i++) {
String jsonText = buffer.readString(32767);
this.displayText.add(EnhancedTextComponentSerializer.jsonToComponent(jsonText));
}
formatDisplayText();
updateComponentTextSize();
}
}
@Override
public void handleClientAction(int id, PacketBuffer buffer) {
super.handleClientAction(id, buffer);
if (id == 1) {
ClickData clickData = ClickData.readFromBuf(buffer);
String componentData = buffer.readString(128);
if (clickHandler != null) {
clickHandler.accept(componentData, clickData);
}
}
}
@SideOnly(Side.CLIENT)
private boolean handleCustomComponentClick(ITextComponent textComponent) {
Style style = textComponent.getStyle();
if (style.getClickEvent() != null) {
ClickEvent clickEvent = style.getClickEvent();
String componentText = clickEvent.getValue();
if (clickEvent.getAction() == Action.OPEN_URL && componentText.startsWith("@!")) {
String rawText = componentText.substring(2);
ClickData clickData = new ClickData(Mouse.getEventButton(), isShiftDown(), isCtrlDown());
writeClientAction(1, buf -> {
clickData.writeToBuf(buf);
buf.writeString(rawText);
});
return true;
}
}
return false;
}
@Override
@SideOnly(Side.CLIENT)
public boolean mouseClicked(int mouseX, int mouseY, int button) {
ITextComponent textComponent = getTextUnderMouse(mouseX, mouseY);
if (textComponent != null) {
if (handleCustomComponentClick(textComponent) ||
getWrapScreen().handleComponentClick(textComponent)) {
playButtonClickSound();
return true;
}
}
return false;
}
@Override
@SideOnly(Side.CLIENT)
public void drawInBackground(int mouseX, int mouseY, IRenderContext context) {
super.drawInBackground(mouseX, mouseY, context);
FontRenderer fontRenderer = Minecraft.getMinecraft().fontRenderer;
Position position = getPosition();
for (int i = 0; i < displayText.size(); i++) {
fontRenderer.drawString(displayText.get(i).getFormattedText(), position.x, position.y + i * (fontRenderer.FONT_HEIGHT + 2), color);
}
}
@Override
@SideOnly(Side.CLIENT)
public void drawInForeground(int mouseX, int mouseY) {
super.drawInForeground(mouseX, mouseY);
ITextComponent component = getTextUnderMouse(mouseX, mouseY);
if (component != null) {
getWrapScreen().handleComponentHover(component, mouseX, mouseY);
}
}
/**
* Used to call mc-related chat component handling code,
* for example component hover rendering and default click handling
*/
@SideOnly(Side.CLIENT)
private static class WrapScreen extends GuiScreen {
@Override
public void handleComponentHover(ITextComponent component, int x, int y) {
super.handleComponentHover(component, x, y);
}
@Override
public boolean handleComponentClick(ITextComponent component) {
return super.handleComponentClick(component);
}
@Override
protected void drawHoveringText(List<String> textLines, int x, int y, FontRenderer font) {
GuiUtils.drawHoveringText(textLines, x, y, width, height, 256, font);
}
}
}