2014-04-29 10:54:20 +10:00
|
|
|
package codechicken.nei;
|
|
|
|
|
|
2019-12-24 13:37:41 -08:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
|
import net.minecraft.client.gui.GuiTextField;
|
2023-09-25 00:35:24 +02:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
import org.lwjgl.input.Keyboard;
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
public abstract class TextField extends Widget {
|
2023-01-28 17:55:51 -08:00
|
|
|
|
2020-01-02 11:30:24 -08:00
|
|
|
protected GuiTextField field;
|
2019-12-24 13:37:41 -08:00
|
|
|
|
|
|
|
|
private static final int maxSearchLength = 256;
|
|
|
|
|
|
|
|
|
|
public boolean centered;
|
|
|
|
|
public String identifier;
|
|
|
|
|
|
|
|
|
|
private boolean previousKeyboardRepeatEnabled;
|
|
|
|
|
|
2020-01-02 11:30:24 -08:00
|
|
|
protected void initInternalTextField() {
|
2019-12-24 13:37:41 -08:00
|
|
|
field = new GuiTextField(Minecraft.getMinecraft().fontRenderer, 0, 0, 0, 0);
|
|
|
|
|
field.setMaxStringLength(maxSearchLength);
|
|
|
|
|
field.setCursorPositionZero();
|
2020-01-02 11:30:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TextField(String ident) {
|
|
|
|
|
identifier = ident;
|
|
|
|
|
initInternalTextField();
|
2022-06-24 12:32:13 +01:00
|
|
|
// Keyboard.enableRepeatEvents(true);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public int getTextColour() {
|
2014-04-29 10:54:20 +10:00
|
|
|
return focused() ? 0xFFE0E0E0 : 0xFF909090;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2020-01-02 11:30:24 -08:00
|
|
|
protected void setDimensionsAndColor() {
|
2019-12-24 13:37:41 -08:00
|
|
|
field.xPosition = this.x + 2;
|
|
|
|
|
field.yPosition = this.y + 2;
|
|
|
|
|
field.width = this.w - 4;
|
|
|
|
|
field.height = this.h - 4;
|
|
|
|
|
field.setTextColor(getTextColour());
|
2020-01-02 11:30:24 -08:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2020-01-02 11:30:24 -08:00
|
|
|
@Override
|
|
|
|
|
public void draw(int mousex, int mousey) {
|
|
|
|
|
try {
|
|
|
|
|
setDimensionsAndColor();
|
|
|
|
|
field.drawTextBox();
|
2022-06-24 12:32:13 +01:00
|
|
|
} catch (NullPointerException npe) {
|
2020-01-02 11:30:24 -08:00
|
|
|
// Hack to deal with a null font renderer... recreate the field and it should work
|
|
|
|
|
GuiTextField oldField = field;
|
|
|
|
|
|
|
|
|
|
initInternalTextField();
|
|
|
|
|
setDimensionsAndColor();
|
|
|
|
|
|
|
|
|
|
field.setText(oldField.getText());
|
|
|
|
|
field.drawTextBox();
|
|
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void onGuiClick(int mousex, int mousey) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!contains(mousex, mousey)) setFocus(false);
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public boolean handleClick(int mousex, int mousey, int button) {
|
2019-12-24 13:37:41 -08:00
|
|
|
setFocus(true);
|
|
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (button == 1) setText("");
|
|
|
|
|
else field.mouseClicked(mousex, mousey, button);
|
2019-12-24 13:37:41 -08:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public boolean handleKeyPress(int keyID, char keyChar) {
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!focused()) return false;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2021-08-14 13:42:11 -06:00
|
|
|
String oldText = text();
|
2019-12-24 13:37:41 -08:00
|
|
|
boolean handled = field.textboxKeyTyped(keyChar, keyID);
|
2022-06-24 12:32:13 +01:00
|
|
|
if (!handled) {
|
2019-12-24 13:37:41 -08:00
|
|
|
if (keyID == Keyboard.KEY_RETURN || keyID == Keyboard.KEY_NUMPADENTER || keyID == Keyboard.KEY_ESCAPE) {
|
|
|
|
|
setFocus(false);
|
|
|
|
|
handled = true;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2019-12-24 13:37:41 -08:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2022-06-24 12:32:13 +01:00
|
|
|
if (handled) {
|
2021-08-14 13:42:11 -06:00
|
|
|
onTextChange(oldText);
|
2019-12-24 13:37:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handled;
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void onTextChange(String oldText);
|
|
|
|
|
|
|
|
|
|
@Override
|
2022-06-24 12:32:13 +01:00
|
|
|
public void update() {}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public void setText(String s) {
|
2019-12-24 13:37:41 -08:00
|
|
|
String oldText = text();
|
|
|
|
|
field.setText(s);
|
|
|
|
|
field.setCursorPositionZero();
|
2014-04-29 10:54:20 +10:00
|
|
|
onTextChange(oldText);
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public String filterText(String s) {
|
2014-04-29 10:54:20 +10:00
|
|
|
return s;
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public void setFocus(boolean focus) {
|
2019-12-24 13:37:41 -08:00
|
|
|
final boolean previousFocus = field.isFocused();
|
|
|
|
|
field.setFocused(focus);
|
|
|
|
|
|
|
|
|
|
if (previousFocus != focus) {
|
|
|
|
|
if (focus) {
|
|
|
|
|
previousKeyboardRepeatEnabled = Keyboard.areRepeatEventsEnabled();
|
|
|
|
|
Keyboard.enableRepeatEvents(true);
|
2021-08-22 11:15:23 -06:00
|
|
|
gainFocus();
|
2019-12-24 13:37:41 -08:00
|
|
|
} else {
|
|
|
|
|
Keyboard.enableRepeatEvents(previousKeyboardRepeatEnabled);
|
2021-08-22 11:15:23 -06:00
|
|
|
loseFocus();
|
2019-12-24 13:37:41 -08:00
|
|
|
}
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public boolean focused() {
|
2019-12-24 13:37:41 -08:00
|
|
|
return field.isFocused();
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
2014-06-29 20:31:37 +10:00
|
|
|
|
|
|
|
|
public String text() {
|
2019-12-24 13:37:41 -08:00
|
|
|
return field.getText();
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
}
|