|
17 | 17 |
|
18 | 18 | package com.lambda.util |
19 | 19 |
|
| 20 | +import com.lambda.Lambda.mc |
20 | 21 | import com.lambda.context.SafeContext |
21 | 22 | import com.lambda.core.Loadable |
22 | 23 | import com.lambda.event.events.KeyboardEvent |
23 | 24 | import com.lambda.event.events.MouseEvent |
24 | | -import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe |
25 | | -import com.lambda.util.math.Vec2d |
26 | | -import net.minecraft.client.util.InputUtil |
| 25 | +import org.lwjgl.glfw.GLFW |
| 26 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_ALT |
| 27 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_CONTROL |
| 28 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SHIFT |
| 29 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SUPER |
| 30 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_ALT |
| 31 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_CONTROL |
| 32 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_SHIFT |
| 33 | +import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_SUPER |
| 34 | +import org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_1 |
| 35 | +import org.lwjgl.glfw.GLFW.GLFW_MOUSE_BUTTON_8 |
| 36 | +import org.lwjgl.glfw.GLFW.GLFW_PRESS |
| 37 | +import org.lwjgl.glfw.GLFW.glfwGetKey |
| 38 | +import org.lwjgl.glfw.GLFW.glfwGetMouseButton |
27 | 39 |
|
28 | 40 | object InputUtils : Loadable { |
29 | | - var lastKeyboardEvent: KeyboardEvent.Press? = null; private set |
30 | | - var lastMouseEvent: MouseEvent.Click? = null; private set |
| 41 | + private val keys = KeyCode.entries.map { it.code } |
| 42 | + private val scancodes = keys.associateWith { GLFW.glfwGetKeyScancode(it) } |
| 43 | + |
| 44 | + private val mouses = GLFW_MOUSE_BUTTON_1..GLFW_MOUSE_BUTTON_8 |
| 45 | + |
| 46 | + private val modMap = mapOf( |
| 47 | + GLFW_KEY_LEFT_SHIFT to 0x1, |
| 48 | + GLFW_KEY_RIGHT_SHIFT to 0x1, |
| 49 | + GLFW_KEY_LEFT_CONTROL to 0x2, |
| 50 | + GLFW_KEY_RIGHT_CONTROL to 0x2, |
| 51 | + GLFW_KEY_LEFT_ALT to 0x4, |
| 52 | + GLFW_KEY_RIGHT_ALT to 0x4, |
| 53 | + GLFW_KEY_LEFT_SUPER to 0x8, |
| 54 | + GLFW_KEY_RIGHT_SUPER to 0x8, |
| 55 | + ) |
31 | 56 |
|
32 | 57 | /** |
33 | 58 | * Returns whether any of the key-codes (not scan-codes) are being pressed |
34 | 59 | */ |
35 | 60 | fun SafeContext.isKeyPressed(vararg keys: Int) = |
36 | | - keys.any { InputUtil.isKeyPressed(mc.window.handle, it) } |
| 61 | + keys.any { glfwGetKey(mc.window.handle, it) >= GLFW_PRESS } |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a new event from the currently pressed keys |
| 65 | + */ |
| 66 | + fun newKeyboardEvent(): KeyboardEvent.Press? { |
| 67 | + val pressedKeys = keys |
| 68 | + .associateWith { glfwGetKey(mc.window.handle, it) } |
| 69 | + .filter { (_, state) -> state >= GLFW_PRESS } |
| 70 | + |
| 71 | + val mods = pressedKeys.keys |
| 72 | + .filter { it in GLFW_KEY_LEFT_SHIFT..GLFW_KEY_RIGHT_SUPER } |
| 73 | + .foldRight(0) { v, acc -> acc or modMap.getValue(v) } |
| 74 | + |
| 75 | + val key = pressedKeys |
| 76 | + .firstNotNullOfOrNull { (key, state) -> key to state } ?: return null |
| 77 | + |
| 78 | + val scancode = scancodes.getValue(key.first) |
| 79 | + |
| 80 | + return KeyboardEvent.Press(key.first, scancode, key.second, mods) |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Creates a new mouse event from the current glfw states. |
| 85 | + */ |
| 86 | + fun newMouseEvent(): MouseEvent.Click? { |
| 87 | + val mods = (GLFW_KEY_LEFT_SHIFT..GLFW_KEY_RIGHT_SUPER) |
| 88 | + .filter { glfwGetKey(mc.window.handle, it) >= GLFW_PRESS } |
| 89 | + .foldRight(0) { v, acc -> acc or modMap.getValue(v) } |
| 90 | + |
| 91 | + val mouse = mouses |
| 92 | + .firstOrNull { glfwGetMouseButton(mc.window.handle, it) == GLFW_PRESS } |
| 93 | + ?: return null |
37 | 94 |
|
38 | | - init { |
39 | | - // hacking imgui jni lib rn because it's missing a lot of native functions including i/o stuff |
40 | | - listenUnsafe<KeyboardEvent.Press> { lastKeyboardEvent = it } |
41 | | - listenUnsafe<MouseEvent.Click> { lastMouseEvent = it } |
| 95 | + return MouseEvent.Click(mouse, GLFW_PRESS, mods) |
42 | 96 | } |
43 | 97 | } |
0 commit comments