aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gtPlusPlus/core/gui/widget/GuiValueField.java
blob: 7976cf0bf635eca54a747e849b0285704857c823 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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.");
        }
    }
}