aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/polyfrost/oneconfig/gui/elements/TextInputField.java
blob: e2c760295cd7f8ff0f46d8cc8e4765eab4716b03 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package io.polyfrost.oneconfig.gui.elements;

import com.google.common.base.Strings;
import io.polyfrost.oneconfig.config.OneConfigConfig;
import io.polyfrost.oneconfig.lwjgl.RenderManager;
import io.polyfrost.oneconfig.lwjgl.font.Fonts;
import net.minecraft.client.gui.GuiScreen;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.input.Keyboard;

import static org.lwjgl.nanovg.NanoVG.*;

public class TextInputField extends BasicElement {

    protected final String defaultText;
    protected String input;
    protected final boolean multiLine;
    protected boolean password;

    protected int caretPos;

    public TextInputField(int width, int height, String defaultText, boolean multiLine, boolean password) {
        super(width, height, false);
        this.multiLine = multiLine;
        this.defaultText = defaultText;
        this.password = password;
        this.input = "";
    }

    public void setInput(String input) {
        this.input = input;
    }

    public String getInput() {
        return input;
    }

    public void setPassword(boolean password) {
        this.password = password;
    }

    @Override
    public void draw(long vg, int x, int y) {
        RenderManager.drawHollowRoundRect(vg, x, y, width, height, OneConfigConfig.GRAY_700, 12f, 2f);
        super.update(x, y);
        int color = toggled ? OneConfigConfig.WHITE : OneConfigConfig.WHITE_60;
        float width;
        StringBuilder s = new StringBuilder();
        int offset = 12;
        if(!password) {
            width = RenderManager.getTextWidth(vg, input.substring(0, caretPos), 14f);
        } else {
            for(int i = 0; i < input.length(); i++) {
                s.append("*");
            }
            width = RenderManager.getTextWidth(vg, s.substring(0, caretPos), 14f);
        }

        if(toggled) {
            RenderManager.drawLine(vg, x + width + 12, (float) y + 7, x + width + 13, (float) y + height - 7, 1, OneConfigConfig.WHITE);
        }

        if(input.equals("")){
            RenderManager.drawString(vg, defaultText, x + 12, y + 17, color, 14f, Fonts.INTER_REGULAR);
        }
        nvgScissor(vg, x, y, this.width, height);
        if(!password) {
            RenderManager.drawString(vg, input, x + offset, y + 17, color, 14f, Fonts.INTER_REGULAR);
            nvgResetScissor(vg);
        } else {

            RenderManager.drawString(vg, s.toString(), x + offset, y + 17, color, 14f, Fonts.INTER_REGULAR);
        }
    }

    public void keyTyped(char c, int key) {
        if (toggled) {
            if(GuiScreen.isCtrlKeyDown()) {
                if(key == Keyboard.KEY_BACK) {
                    try {
                        input = input.substring(0, input.lastIndexOf(" "));
                        caretPos = input.length();
                    } catch (Exception e) {
                        input = "";
                        caretPos = 0;
                    }
                }
                return;
            }
            if (key == Keyboard.KEY_BACK) {
                if (input.length() > 0) {
                    if(caretPos == input.length()) {
                        input = input.substring(0, input.length() - 1);
                    } else {
                        input = input.substring(0, caretPos - 1) + input.substring(caretPos);
                    }
                    caretPos--;
                }
                return;
            }
            if(key == Keyboard.KEY_TAB) {
                input += "    ";
                caretPos += 4;
                return;
            }

            if (key == Keyboard.KEY_RIGHT) {
                caretPos++;
                if(caretPos > input.length()) {
                    caretPos = input.length();
                }
                return;
            }
            if (key == Keyboard.KEY_LEFT) {
                caretPos--;
                if(caretPos < 0) {
                    caretPos = 0;
                }
                return;
            }



            if(key == Keyboard.KEY_RETURN) {
                toggled = false;
            }

            if(key == Keyboard.KEY_LCONTROL || key == Keyboard.KEY_RCONTROL || key == Keyboard.KEY_LMENU || key == Keyboard.KEY_RMENU || key == Keyboard.KEY_LMETA || key == Keyboard.KEY_RMETA || key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT || key == Keyboard.KEY_RETURN || key == Keyboard.KEY_CAPITAL || key == 221) {
                return;
            }
            input = addCharAtPoint(caretPos, c);
            caretPos++;
        }
    }

    private @NotNull String addCharAtPoint(int index, char c) {
        return input.substring(0, index) + c + input.substring(index);
    }
}