mirror of
https://github.com/GTNewHorizons/NotEnoughItems.git
synced 2026-11-12 00:17:51 +00:00
183 lines
4.3 KiB
Java
183 lines
4.3 KiB
Java
package codechicken.nei;
|
|
|
|
import net.minecraft.client.gui.GuiScreen;
|
|
import net.minecraft.util.ChatAllowedCharacters;
|
|
import org.lwjgl.input.Keyboard;
|
|
|
|
import static codechicken.lib.gui.GuiDraw.*;
|
|
|
|
public abstract class TextField extends Widget
|
|
{
|
|
public TextField(String ident)
|
|
{
|
|
identifier = ident;
|
|
}
|
|
|
|
public int getTextColour()
|
|
{
|
|
return focused() ? 0xFFE0E0E0 : 0xFF909090;
|
|
}
|
|
|
|
public void drawBox()
|
|
{
|
|
drawRect(x, y, width, height, 0xffA0A0A0);
|
|
drawRect(x + 1, y + 1, width - 2, height - 2, 0xFF000000);
|
|
}
|
|
|
|
@Override
|
|
public void draw(int mousex, int mousey)
|
|
{
|
|
drawBox();
|
|
|
|
String drawtext = text;
|
|
|
|
if(text.length() > getMaxTextLength())
|
|
{
|
|
int startOffset = drawtext.length() - getMaxTextLength();
|
|
if(startOffset < 0 || startOffset > drawtext.length())
|
|
startOffset = 0;
|
|
drawtext = drawtext.substring(startOffset);
|
|
}
|
|
|
|
if(focused() && (cursorCounter / 6) % 2 == 0)
|
|
drawtext = drawtext + '_';
|
|
|
|
int textWidth = getStringWidth(text);
|
|
int textx = centered ? x+(width-textWidth)/2 : x + 4;
|
|
int texty = y + (height + 1) / 2 - 3;
|
|
|
|
drawString(drawtext, textx, texty, getTextColour());
|
|
}
|
|
|
|
@Override
|
|
public void onGuiClick(int mousex, int mousey)
|
|
{
|
|
if(!contains(mousex, mousey))
|
|
setFocus(false);
|
|
}
|
|
|
|
@Override
|
|
public boolean handleClick(int mousex, int mousey, int button)
|
|
{
|
|
if(button == 1)
|
|
setText("");
|
|
setFocus(true);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean handleKeyPress(int keyID, char keyChar)
|
|
{
|
|
if(!focused())
|
|
return false;
|
|
|
|
if(keyID == Keyboard.KEY_BACK)
|
|
{
|
|
if(text.length() > 0)
|
|
{
|
|
setText(text.substring(0, text.length() - 1));
|
|
backdowntime = System.currentTimeMillis();
|
|
}
|
|
}
|
|
else if(keyID == Keyboard.KEY_RETURN || keyID == Keyboard.KEY_ESCAPE)
|
|
{
|
|
setFocus(false);
|
|
onExit();
|
|
}
|
|
else if(keyChar == 22)//paste
|
|
{
|
|
String pastestring = GuiScreen.getClipboardString();
|
|
if(pastestring == null)
|
|
pastestring = "";
|
|
|
|
if(isValid(text + pastestring))
|
|
setText(text + pastestring);
|
|
}
|
|
else if(isValid(text+keyChar))
|
|
setText(text+keyChar);
|
|
|
|
return true;
|
|
}
|
|
|
|
public void onExit()
|
|
{
|
|
}
|
|
|
|
public abstract void onTextChange(String oldText);
|
|
|
|
public boolean isValid(String string)
|
|
{
|
|
// Solve the problem that Minecraft can't post Chinese characters
|
|
return ChatAllowedCharacters.isAllowedCharacter(string.charAt(string.length()-1));
|
|
}
|
|
|
|
@Override
|
|
public void update()
|
|
{
|
|
cursorCounter++;
|
|
if(backdowntime > 0)
|
|
{
|
|
if(Keyboard.isKeyDown(Keyboard.KEY_BACK) && text.length() > 0)
|
|
{
|
|
if(System.currentTimeMillis() - backdowntime > 200 / (1+backs * 0.3F))
|
|
{
|
|
setText(text.substring(0, text.length() - 1));
|
|
backdowntime = System.currentTimeMillis();
|
|
backs++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
backdowntime = 0;
|
|
backs = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void setText(String s)
|
|
{
|
|
String oldText = text;
|
|
text = filterText(s);
|
|
onTextChange(oldText);
|
|
}
|
|
|
|
public String filterText(String s)
|
|
{
|
|
return s;
|
|
}
|
|
|
|
private int getMaxTextLength()
|
|
{
|
|
return width / 6 - 2;
|
|
}
|
|
|
|
public void setFocus(boolean focus)
|
|
{
|
|
if(focus)
|
|
{
|
|
LayoutManager.setInputFocused(this);
|
|
}
|
|
else if(focused())
|
|
{
|
|
LayoutManager.setInputFocused(null);
|
|
}
|
|
}
|
|
|
|
public boolean focused()
|
|
{
|
|
return LayoutManager.getInputFocused() == this;
|
|
}
|
|
|
|
public String text()
|
|
{
|
|
return text;
|
|
}
|
|
|
|
private String text = "";
|
|
public boolean centered;
|
|
public long backdowntime;
|
|
public int backs;
|
|
public String identifier;
|
|
public int cursorCounter;
|
|
}
|