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;
|
2014-04-29 10:54:20 +10:00
|
|
|
import org.lwjgl.input.Keyboard;
|
|
|
|
|
|
|
|
|
|
public abstract class TextField extends Widget
|
|
|
|
|
{
|
2019-12-30 20:19:16 -08:00
|
|
|
protected final 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;
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
public TextField(String ident) {
|
2014-04-29 10:54:20 +10:00
|
|
|
identifier = ident;
|
2019-12-24 13:37:41 -08:00
|
|
|
field = new GuiTextField(Minecraft.getMinecraft().fontRenderer, 0, 0, 0, 0);
|
|
|
|
|
field.setMaxStringLength(maxSearchLength);
|
|
|
|
|
field.setCursorPositionZero();
|
|
|
|
|
// 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
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void draw(int mousex, int mousey) {
|
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());
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2019-12-24 13:37:41 -08:00
|
|
|
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) {
|
|
|
|
|
if (!contains(mousex, mousey))
|
2014-04-29 10:54:20 +10:00
|
|
|
setFocus(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
2014-06-29 20:31:37 +10:00
|
|
|
if (button == 1)
|
2014-04-29 10:54:20 +10:00
|
|
|
setText("");
|
2019-12-24 13:37:41 -08:00
|
|
|
else
|
|
|
|
|
field.mouseClicked(mousex, mousey, button);
|
|
|
|
|
|
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) {
|
|
|
|
|
if (!focused())
|
2014-04-29 10:54:20 +10:00
|
|
|
return false;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2019-12-24 13:37:41 -08:00
|
|
|
boolean handled = field.textboxKeyTyped(keyChar, keyID);
|
|
|
|
|
if(!handled) {
|
|
|
|
|
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
|
|
|
|
2019-12-24 13:37:41 -08:00
|
|
|
if(handled) {
|
|
|
|
|
onTextChange(text());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return handled;
|
2014-06-29 20:31:37 +10:00
|
|
|
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract void onTextChange(String oldText);
|
|
|
|
|
|
|
|
|
|
@Override
|
2014-06-29 20:31:37 +10:00
|
|
|
public void update() {
|
2014-04-29 10:54:20 +10:00
|
|
|
}
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
Keyboard.enableRepeatEvents(previousKeyboardRepeatEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|