package net.minecraft.src;

public class GuiTextField extends Gui {
	private final FontRenderer fontRenderer;
	private final int xPos;
	private final int yPos;
	private final int width;
	private final int height;
	private String text;
	private int maxStringLength;
	private int cursorCounter;
	public boolean isFocused = false;
	public boolean isEnabled = true;

	public GuiTextField(FontRenderer var1, int var2, int var3, int var4, int var5, String var6) {
		this.fontRenderer = var1;
		this.xPos = var2;
		this.yPos = var3;
		this.width = var4;
		this.height = var5;
		this.setText(var6);
	}

	public void setText(String var1) {
		this.text = var1;
	}

	public String getText() {
		return this.text;
	}

	public void updateCursorCounter() {
		++this.cursorCounter;
	}

	public void textboxKeyTyped(char var1, int var2) {
		if(this.isEnabled && this.isFocused) {
			if((int)var1 == 16 || (GuiScreen.isCtrlKeyDown() && var2 == 47)) {
				String var3 = GuiScreen.getClipboardString();
				if(var3 == null) {
					var3 = "";
				}

				int var4 = 32 - this.text.length();
				if(var4 > var3.length()) {
					var4 = var3.length();
				}

				if(var4 > 0) {
					this.text = this.text + var3.substring(0, var4);
				}
			}

			if(var2 == 14 && this.text.length() > 0) {
				this.text = this.text.substring(0, this.text.length() - 1);
				return;
			}

			if(FontAllowedCharacters.isAllowed(var1) >= 0 && (this.text.length() < this.maxStringLength || this.maxStringLength == 0)) {
				this.text = this.text + var1;
			}
		}
	}

	public void mouseClicked(int var1, int var2, int var3) {
		boolean var4 = this.isEnabled && var1 >= this.xPos && var1 < this.xPos + this.width && var2 >= this.yPos && var2 < this.yPos + this.height;
		if(var4 && !this.isFocused) {
			this.cursorCounter = 0;
		}

		this.isFocused = var4;
	}

	public void drawTextBox() {
		this.drawRect(this.xPos - 1, this.yPos - 1, this.xPos + this.width + 1, this.yPos + this.height + 1, -6250336);
		this.drawRect(this.xPos, this.yPos, this.xPos + this.width, this.yPos + this.height, -16777216);
		if(this.isEnabled) {
			boolean var1 = this.isFocused && this.cursorCounter / 6 % 2 == 0;
			this.drawString(this.fontRenderer, this.text + (var1 ? "_" : ""), this.xPos + 4, this.yPos + (this.height - 8) / 2, 14737632);
		} else {
			this.drawString(this.fontRenderer, this.text, this.xPos + 4, this.yPos + (this.height - 8) / 2, 7368816);
		}

	}

	public void setMaxStringLength(int var1) {
		this.maxStringLength = var1;
	}
}
