-
Notifications
You must be signed in to change notification settings - Fork 16
/
KeyboardHandler.java
131 lines (114 loc) · 5 KB
/
KeyboardHandler.java
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
package dmillerw.menu.handler;
import dmillerw.menu.MineMenu;
import dmillerw.menu.data.menu.RadialMenu;
import dmillerw.menu.gui.RadialMenuScreen;
import dmillerw.menu.helper.KeyReflectionHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.client.util.InputMappings;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@EventBusSubscriber(modid = MineMenu.MOD_ID, value = Dist.CLIENT)
public class KeyboardHandler {
public static final KeyboardHandler INSTANCE = new KeyboardHandler();
private static final KeyBinding WHEEL = new KeyBinding("key.open_menu", GLFW.GLFW_KEY_R, "key.categories.misc");
public static void register() {
ClientRegistry.registerKeyBinding(WHEEL);
}
private static boolean lastWheelState = false;
private static final List<KeyBinding> FIRED_KEYS = new ArrayList<>();
private static final List<KeyBinding> TOGGLED_KEYS = new ArrayList<>();
private static boolean ignoreNextTick = false;
private KeyboardHandler() {
}
public void fireKey(KeyBinding key) {
FIRED_KEYS.add(key);
KeyBinding.setKeyBindState(key.getKey(), true);
KeyReflectionHelper.setPressTime(key, 1);
ignoreNextTick = true;
}
public void toggleKey(KeyBinding key) {
if (!TOGGLED_KEYS.contains(key)) {
TOGGLED_KEYS.add(key);
KeyBinding.setKeyBindState(key.getKey(), true);
KeyReflectionHelper.setPressTime(key, 1);
} else {
TOGGLED_KEYS.remove(key);
KeyBinding.setKeyBindState(key.getKey(), false);
}
ignoreNextTick = true;
}
@SubscribeEvent
public static void onClientTick(TickEvent.ClientTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
return;
}
Minecraft mc = Minecraft.getInstance();
if (mc.world == null) {
return;
}
long handle = Minecraft.getInstance().getMainWindow().getHandle();
boolean wheelKeyPressed = WHEEL.getKey().getKeyCode() >= 0 ? InputMappings.isKeyDown(handle, WHEEL.getKey().getKeyCode()) : InputMappings.isKeyDown(handle, WHEEL.getKey().getKeyCode() + 100);
if (wheelKeyPressed != lastWheelState) {
if (ConfigHandler.GENERAL.toggle.get()) {
if (wheelKeyPressed) {
if (RadialMenuScreen.active) {
if (ConfigHandler.GENERAL.releaseToSelect.get()) {
RadialMenuScreen.INSTANCE.mouseClicked(mc.mouseHelper.getMouseX(), mc.mouseHelper.getMouseY(), 0);
}
RadialMenuScreen.deactivate();
} else {
if (mc.currentScreen == null || mc.currentScreen instanceof RadialMenuScreen) {
RadialMenu.resetCategory();
RadialMenu.resetTimer();
RadialMenuScreen.activate();
}
}
}
} else {
if (wheelKeyPressed != RadialMenuScreen.active) {
if (wheelKeyPressed) {
if (mc.currentScreen == null || mc.currentScreen instanceof RadialMenuScreen) {
RadialMenu.resetCategory();
RadialMenu.resetTimer();
RadialMenuScreen.activate();
}
} else {
if (ConfigHandler.GENERAL.releaseToSelect.get()) {
RadialMenuScreen.INSTANCE.mouseClicked(mc.mouseHelper.getMouseX(), mc.mouseHelper.getMouseY(), 0);
}
RadialMenuScreen.deactivate();
}
}
}
}
lastWheelState = wheelKeyPressed;
if (ignoreNextTick) {
ignoreNextTick = false;
return;
}
Iterator<KeyBinding> iterator = FIRED_KEYS.iterator();
while (iterator.hasNext()) {
KeyBinding keyBinding = iterator.next();
KeyBinding.setKeyBindState(keyBinding.getKey(), false);
iterator.remove();
}
iterator = TOGGLED_KEYS.iterator();
while (iterator.hasNext()) {
KeyBinding keyBinding = iterator.next();
if ((keyBinding.getKey().getKeyCode() >= 0 ? keyBinding.isPressed() : InputMappings.isKeyDown(handle, keyBinding.getKey().getKeyCode() + 100)) || mc.currentScreen != null) {
iterator.remove();
}
}
for (KeyBinding keyBinding : TOGGLED_KEYS) {
KeyReflectionHelper.setPressTime(keyBinding, 1);
}
}
}