package gtPlusPlus.core.gui.widget; import java.lang.reflect.Field; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiTextField; import gregtech.asm.GTCorePlugin; import gtPlusPlus.core.gui.machine.GUIVolumetricFlaskSetter; import gtPlusPlus.core.util.reflect.ReflectionUtils; public class GuiValueField extends GuiTextField { private final FontRenderer mFontRenderer; private final int mScreenLocationX; private final int mScreenLocationY; private final GUIVolumetricFlaskSetter mGUI; public GuiValueField(FontRenderer aFontRenderer, int aX, int aY, int aScreenLocationX, int aScreenLocationY, int aWidth, int aHeight, GUIVolumetricFlaskSetter aGUI) { super(aFontRenderer, aX, aY, aWidth, aHeight); mFontRenderer = aFontRenderer; mScreenLocationX = aScreenLocationX; mScreenLocationY = aScreenLocationY; mGUI = aGUI; } @Override public boolean isFocused() { return super.isFocused(); } public boolean isBackgroundDrawingEnabled() { return this.getEnableBackgroundDrawing(); } public int getLineScrollOffset() { Field lineScrollOffset = ReflectionUtils .getField(GuiTextField.class, GTCorePlugin.isDevEnv() ? "lineScrollOffset" : "field_146225_q"); if (lineScrollOffset != null) { return ReflectionUtils.getFieldValue(lineScrollOffset, this); } return 0; } public boolean didClickInTextField(int aX, int aY) { mGUI.log("Clicked at X:" + aX + ", Y:" + aY); boolean aDidClick = aX >= this.mScreenLocationX && aX < this.mScreenLocationX + this.width && aY >= this.mScreenLocationY && aY < this.mScreenLocationY + this.height; mGUI.log("Did click in textbox? " + aDidClick); mGUI.log("Expected Region: X:" + mScreenLocationX + "-" + (this.mScreenLocationX + this.width)); mGUI.log("Expected Region: Y:" + mScreenLocationY + "-" + (this.mScreenLocationY + this.height)); return aDidClick; } /** * Args: x, y, buttonClicked */ @Override public void mouseClicked(int aX, int aY, int aButton) { boolean aDidClick = didClickInTextField(aX, aY); mGUI.log("Did click inside text box? " + aDidClick); mGUI.log("Focus 1: " + this.isFocused()); this.setFocused(aDidClick); mGUI.log("Focus 2: " + this.isFocused()); if (isFocused()) { int l = aX - this.mScreenLocationX; if (isBackgroundDrawingEnabled()) { l -= 4; } if (aButton == 0) { mGUI.log("Left clicked in text box."); String s = this.mFontRenderer.trimStringToWidth( this.getText() .substring(getLineScrollOffset()), this.getWidth()); this.setCursorPosition( this.mFontRenderer.trimStringToWidth(s, l) .length() + getLineScrollOffset()); } else if (aButton == 1) { mGUI.log("Right clicked in text box."); mGUI.setText(0); mGUI.sendUpdateToServer(); String s = this.mFontRenderer.trimStringToWidth( this.getText() .substring(getLineScrollOffset()), this.getWidth()); this.setCursorPosition( this.mFontRenderer.trimStringToWidth(s, l) .length() + getLineScrollOffset()); } } else { mGUI.log("Clicked, but no focus."); } } }