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
|
package kr.syeyoung.dungeonsguide.gui.elements;
import kr.syeyoung.dungeonsguide.gui.MPanel;
import lombok.Getter;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.gui.ScaledResolution;
import org.lwjgl.input.Keyboard;
import java.awt.*;
import java.awt.datatransfer.*;
import java.awt.event.KeyEvent;
import java.io.IOException;
@Getter
public class MTextField extends MPanel {
private Color foreground = Color.white;
private String text = "asdasdasd";
private int cursorBlickTicker = 0;
private int selectionStart = 0;
private int selectionEnd = 0;
private int cursor = 0;
private int xOffset = 0;
public void edit(String str) {
}
public void setText(String text) {
this.text = text;
}
private void setText0(String text) {
this.text = text;
edit(text);
}
@Override
public void render(int absMousex, int absMousey, int relMousex0, int relMousey0, float partialTicks, Rectangle clip) {
Gui.drawRect(0,0,getBounds().width, getBounds().height, isFocused ? Color.white.getRGB() : Color.gray.getRGB());
Gui.drawRect(1,1,getBounds().width - 1, getBounds().height - 1, Color.black.getRGB());
Minecraft mc = Minecraft.getMinecraft();
clip(new ScaledResolution(mc), clip.x + 1, clip.y + 1, clip.width - 2, clip.height - 2);
FontRenderer fr = mc.fontRendererObj;
int y = (getBounds().height - fr.FONT_HEIGHT) / 2;
fr.drawString(text, 3 - xOffset, y, foreground.getRGB());
// draw selection
if (isFocused) {
if (selectionStart != -1) {
int startX = fr.getStringWidth(text.substring(0, selectionStart)) - xOffset;
int endX = fr.getStringWidth(text.substring(0, selectionEnd)) - xOffset;
Gui.drawRect(3 + startX, y, 3 + endX, y + fr.FONT_HEIGHT, 0xFF00FF00);
fr.drawString(text.substring(selectionStart, selectionEnd), 3 + startX, y, foreground.getRGB());
}
// draw cursor
if (cursor != -1) {
if (cursor > text.length()) cursor = text.length();
int x = fr.getStringWidth(text.substring(0, cursor)) - xOffset;
cursorBlickTicker++;
if (cursorBlickTicker < 10)
Gui.drawRect(3 + x, y, 4 + x, y + fr.FONT_HEIGHT, 0xFFFFFFFF);
if (cursorBlickTicker == 20) cursorBlickTicker = 0;
}
}
}
@Override
public void mouseClicked(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int mouseButton) {
Rectangle actualField = new Rectangle(1, 3,getBounds().width - 2, getBounds().height - 6);
if (!actualField.contains(relMouseX, relMouseY)) return;
if (!lastAbsClip.contains(absMouseX, absMouseY)) return;
int relStartT = relMouseX-3;
int offseted = relStartT + xOffset;
selectionStart = -1;
FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
for (int i = 0; i < text.length(); i++) {
int totalWidth = fr.getStringWidth(text.substring(0, i));
if (offseted < totalWidth) {
cursor = i;
return;
}
}
cursor = text.length();
}
@Override
public void mouseClickMove(int absMouseX, int absMouseY, int relMouseX, int relMouseY, int clickedMouseButton, long timeSinceLastClick) {
if (!isFocused) return;
selectionStart = cursor;
selectionEnd = cursor;
int relStartT = relMouseX-3;
int offseted = relStartT + xOffset;
FontRenderer fr = Minecraft.getMinecraft().fontRendererObj;
for (int i = 0; i < text.length(); i++) {
int totalWidth = fr.getStringWidth(text.substring(0, i));
if (offseted < totalWidth) {
if (i < cursor) {
selectionStart = i;
selectionEnd = cursor;
} else {
selectionStart = cursor;
selectionEnd = i;
}
return;
}
}
selectionEnd = text.length();
if (selectionStart == selectionEnd) {
selectionStart = -1;
}
}
@Override
public void mouseScrolled(int absMouseX, int absMouseY, int relMouseX0, int relMouseY0, int scrollAmount) {
if (!isFocused) return;
if (scrollAmount > 0) {
xOffset += 5;
} else if (scrollAmount < 0){
xOffset -= 5;
}
if (xOffset < 0) {
xOffset = 0;
}
int width = Minecraft.getMinecraft().fontRendererObj.getStringWidth(text);
int overflow = getBounds().width - 3 - width;
if (overflow >= 0) {
xOffset = 0;
} else if (width - xOffset + 10 < getBounds().width) {
xOffset = width - getBounds().width+10;
}
}
@Override
public void keyTyped(char typedChar, int keycode) {
if (!isFocused) return;
if (selectionStart == -1) {
if (keycode == 199) { // home
cursor = 0;
return;
}
if (keycode == 207) { // end
cursor = text.length();
return;
}
if (keycode == 203) { // left
cursor--;
if (cursor < 0) cursor = 0;
return;
}
if (keycode == 205) { // right
cursor ++;
if (cursor > text.length()) cursor = text.length();
return;
}
// backspace
if (keycode == 14 && cursor > 0) {
setText0(this.text.substring(0, cursor - 1) + this.text.substring(cursor));
cursor--;
return;
}
//del
if (keycode == 211 && cursor < text.length()) {
setText0(this.text.substring(0, cursor) + this.text.substring(cursor+1));
return;
}
// paste
boolean shouldPaste = false;
if (keycode == 47) {
if (Minecraft.isRunningOnMac) { // mac
if (Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220)) {
shouldPaste = true;
}
} else { // literally everything else
if (Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157)) {
shouldPaste = true;
}
}
}
if (shouldPaste) {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
Object theText = transferable.getTransferData(DataFlavor.stringFlavor);
setText0(
this.text.substring(0, this.cursor)
+ theText
+ this.text.substring(this.cursor));
cursor += theText.toString().length();
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return;
}
// text
if (isPrintableChar(typedChar)) {
setText0(
this.text.substring(0, this.cursor)
+ typedChar
+ this.text.substring(this.cursor));
this.cursor++;
return;
}
} else {
if (keycode == 199) { // home
cursor = 0;
selectionStart = -1;
return;
}
if (keycode == 207) { // end
selectionStart = -1;
cursor = text.length();
return;
}
if (keycode == 203) { // left
cursor = selectionStart;
selectionStart = -1;
return;
}
if (keycode == 205) { // right
cursor = selectionEnd;
selectionStart = -1;
return;
}
// backspace
if (keycode == 14 && cursor > 0) {
setText0(this.text.substring(0, selectionStart) + this.text.substring(selectionEnd));
cursor = selectionStart;
selectionStart = -1;
return;
}
//del
if (keycode == 211 && cursor < text.length()) {
setText0(this.text.substring(0, selectionStart) + this.text.substring(selectionEnd));
cursor = selectionStart;
selectionStart = -1;
return;
}
// paste
boolean shouldPaste = false;
if (keycode == 47) {
if (Minecraft.isRunningOnMac) { // mac
if (Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220)) {
shouldPaste = true;
}
} else { // literally everything else
if (Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157)) {
shouldPaste = true;
}
}
}
if (shouldPaste) {
Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
Object theText = transferable.getTransferData(DataFlavor.stringFlavor);
setText0(
this.text.substring(0, this.selectionStart)
+ theText
+ this.text.substring(this.selectionEnd));
cursor = this.selectionStart + theText.toString().length();
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
selectionStart = -1;
}
return;
}
boolean shouldCopy = false;
if (keycode == 46) {
if (Minecraft.isRunningOnMac) { // mac
if (Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220)) {
shouldCopy = true;
}
} else { // literally everything else
if (Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157)) {
shouldCopy = true;
}
}
}
if (shouldCopy) {
StringSelection selection = new StringSelection(text.substring(selectionStart, selectionEnd));
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(selection, selection);
return;
}
// text
if (isPrintableChar(typedChar)) {
setText0(
this.text.substring(0, this.selectionStart)
+ typedChar
+ this.text.substring(this.selectionEnd));
this.cursor = this.selectionStart + 1;
selectionStart = -1;
return;
}
}
}
public boolean isPrintableChar( char c ) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
c != KeyEvent.CHAR_UNDEFINED &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
}
|