summaryrefslogtreecommitdiff
path: root/src/main/java/com/arslaancodes/arslaansmagichax/screens/clickgui/setting/KeybindingSelector.java
blob: 699ec0dc40a6acc68c3d1259279c26e74f473294 (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
package com.arslaancodes.arslaansmagichax.screens.clickgui.setting;

import com.arslaancodes.arslaansmagichax.screens.clickgui.ModuleButton;
import org.lwjgl.glfw.GLFW;

import java.awt.*;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import com.arslaancodes.arslaansmagichax.screens.clickgui.setting.SettingComponent;

public class KeybindingSelector extends SettingComponent {

    private boolean listening = false;

    public KeybindingSelector(ModuleButton parent, int offset) {
        super(null, parent, offset);
    }

    @Override
    public void render(GuiGraphicsExtractor context, int mouseX, int mouseY, float deltaTicks) {
        int alpha = isHovered(mouseX, mouseY) ? 230 : 160;
        context.fill(parent.parent.x, parent.parent.y + parent.offset + offset, parent.parent.x + parent.parent.width, parent.parent.y + parent.offset + offset + parent.parent.height, new Color(30, 30, 47, alpha).getRGB());

        if (listening) {
            context.fill(parent.parent.x, parent.parent.y + parent.offset + offset, parent.parent.x + parent.parent.width, parent.parent.y + parent.offset + offset + parent.parent.height, new Color(252, 210, 77, 255).getRGB());
        }

        String displayText = listening ? "Press a key..." : "Keybind: " + getKeyName(parent.module.getGlfwKeybinding());
        context.text(mc.font, displayText, parent.parent.x + 5, parent.parent.y + parent.offset + offset + (parent.parent.height - mc.font.lineHeight) / 2, Color.white.getRGB());
    }

    @Override
    public void mouseClicked(double mouseX, double mouseY, int button) {
        if (isHovered(mouseX, mouseY) && button == 0) {
            listening = !listening;
        }
    }

    public void onKeyPressed(int keyCode) {
        if (!listening) return;

        listening = false;

        if (keyCode == GLFW.GLFW_KEY_ESCAPE || keyCode == GLFW.GLFW_KEY_DELETE || keyCode == GLFW.GLFW_KEY_BACKSPACE) {
            // remove bingdingd AHH I CANT SPELL
            parent.module.setGlfwKeybinding(-1);
            return;
        }

        parent.module.setGlfwKeybinding(keyCode);
    }

    private String getKeyName(int key) {
        if (key == -1) return "NONE";
        String name = GLFW.glfwGetKeyName(key, 0);

        if (name != null) return name.toUpperCase();

        return switch (key) {
            case GLFW.GLFW_KEY_LEFT_SHIFT -> "LSHIFT";
            case GLFW.GLFW_KEY_RIGHT_SHIFT -> "RSHIFT";
            case GLFW.GLFW_KEY_LEFT_CONTROL -> "LCTRL";
            case GLFW.GLFW_KEY_RIGHT_CONTROL -> "RCTRL";
            case GLFW.GLFW_KEY_LEFT_ALT -> "LALT";
            case GLFW.GLFW_KEY_RIGHT_ALT -> "RALT";
            case GLFW.GLFW_KEY_SPACE -> "SPACE";
            case GLFW.GLFW_KEY_TAB -> "TAB";
            case GLFW.GLFW_KEY_ENTER -> "ENTER";
            case GLFW.GLFW_KEY_UP -> "UP";
            case GLFW.GLFW_KEY_DOWN -> "DOWN";
            case GLFW.GLFW_KEY_LEFT -> "LEFT";
            case GLFW.GLFW_KEY_RIGHT -> "RIGHT";
            default -> "KEY_" + key;
        };
    }
}