Skip to content

Commit cc19dbf

Browse files
committed
Added multi key and mouse binds
1 parent 668b540 commit cc19dbf

File tree

7 files changed

+71
-36
lines changed

7 files changed

+71
-36
lines changed

src/main/kotlin/com/lambda/config/settings/complex/KeybindSetting.kt

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,14 @@ import imgui.flag.ImGuiCol
3939
import imgui.flag.ImGuiHoveredFlags
4040
import imgui.flag.ImGuiMouseButton
4141
import net.minecraft.command.CommandRegistryAccess
42-
import org.lwjgl.glfw.GLFW
42+
import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT_SHIFT
43+
import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT_SUPER
44+
import org.lwjgl.glfw.GLFW.GLFW_MOD_ALT
45+
import org.lwjgl.glfw.GLFW.GLFW_MOD_CAPS_LOCK
46+
import org.lwjgl.glfw.GLFW.GLFW_MOD_CONTROL
47+
import org.lwjgl.glfw.GLFW.GLFW_MOD_NUM_LOCK
48+
import org.lwjgl.glfw.GLFW.GLFW_MOD_SHIFT
49+
import org.lwjgl.glfw.GLFW.GLFW_MOD_SUPER
4350

4451
class KeybindSetting(
4552
override val name: String,
@@ -56,10 +63,9 @@ class KeybindSetting(
5663

5764
override fun ImGuiBuilder.buildLayout() {
5865
val bind = value
59-
val scancode = if (bind.code == KeyCode.UNBOUND.code) -69 else GLFW.glfwGetKeyScancode(bind.code)
60-
val translated = if (bind.keyCode == KeyCode.UNBOUND) bind.keyCode else KeyCode.virtualMapUS(bind.code, scancode)
61-
val preview = if (listening) "$name: Press any key…"
62-
else if (bind.isMouseButton) "Mouse ${bind.code}" else "$name: $translated"
66+
val preview =
67+
if (listening) "$name: Press any key…"
68+
else bind.name
6369

6470
if (listening) {
6571
withStyleColor(ImGuiCol.Button, 0.20f, 0.50f, 1.00f, 1.00f) {
@@ -99,18 +105,23 @@ class KeybindSetting(
99105
lambdaTooltip("Clear binding")
100106
}
101107

102-
val keyboardPoll = InputUtils.lastKeyboardEvent
103-
val mousePoll = InputUtils.lastMouseEvent
108+
val keypoll = InputUtils.lastKeyboardEvent ?: return
109+
val mousepoll = InputUtils.lastMouseEvent ?: return
110+
104111
if (listening) {
105-
if (keyboardPoll.isPressed) {
106-
when (val key = keyboardPoll.translated) {
112+
val isModKey = keypoll.keyCode in GLFW_KEY_LEFT_SHIFT..GLFW_KEY_RIGHT_SUPER
113+
114+
if ((keypoll.isPressed && !isModKey)
115+
|| (keypoll.isReleased && isModKey)
116+
) {
117+
when (keypoll.translated) {
107118
KeyCode.ESCAPE -> {}
108119
KeyCode.BACKSPACE, KeyCode.DELETE -> value = Bind.EMPTY
109-
else -> value = Bind(key)
120+
else -> value = Bind(keypoll.keyCode, keypoll.modifiers, -1)
110121
}
111122
listening = false
112-
} else if (mousePoll.action == Mouse.Action.Click.ordinal) {
113-
value = mouseBind(mousePoll.button)
123+
} else if (mousepoll.action == Mouse.Action.Click.ordinal) {
124+
value = Bind(0, mousepoll.modifiers, mousepoll.button)
114125
listening = false
115126
}
116127
}
@@ -136,7 +147,7 @@ class KeybindSetting(
136147
bind = mouseBind(num)
137148
} else {
138149
bind = try {
139-
Bind(KeyCode.valueOf(name().value()))
150+
Bind(KeyCode.valueOf(name().value()).code, 0)
140151
} catch(_: IllegalArgumentException) {
141152
return@executeWithResult failure("${name().value()} doesn't match with a bind")
142153
}
@@ -151,19 +162,34 @@ class KeybindSetting(
151162
}
152163

153164
data class Bind(
154-
val keyCode: KeyCode = KeyCode.UNBOUND,
155-
val code: Int = keyCode.code,
156-
val isMouseButton: Boolean = false
165+
val key: Int,
166+
val modifiers: Int,
167+
val mouse: Int = -1,
157168
) {
169+
val truemods = buildList {
170+
if (modifiers and GLFW_MOD_SHIFT != 0) add(KeyCode.LEFT_SHIFT)
171+
if (modifiers and GLFW_MOD_CONTROL != 0) add(KeyCode.LEFT_CONTROL)
172+
if (modifiers and GLFW_MOD_ALT != 0) add(KeyCode.LEFT_ALT)
173+
if (modifiers and GLFW_MOD_SUPER != 0) add(KeyCode.LEFT_SUPER)
174+
if (modifiers and GLFW_MOD_CAPS_LOCK != 0) add(KeyCode.CAPS_LOCK)
175+
if (modifiers and GLFW_MOD_NUM_LOCK != 0) add(KeyCode.NUM_LOCK)
176+
}
177+
158178
val name: String
159-
get() = if (isMouseButton) "Mouse $code" else keyCode.name
179+
get() = buildString {
180+
if (mouse >= 0) append("${Mouse.Button.fromMouseCode(mouse)} + ")
181+
if (modifiers > 0) append("${truemods.joinToString(separator = "+") { it.name }} +")
182+
if (key > 0) append(KeyCode.fromKeyCode(key))
183+
184+
if (mouse < 0 && modifiers <= 0 && key <= 0) append("Unbound")
185+
}
160186

161187
override fun toString() =
162-
"Key Code: $keyCode, Code: $code, Mouse Button: $isMouseButton"
188+
"Key Code: $key"
163189

164190
companion object {
165-
val EMPTY = Bind()
191+
val EMPTY = Bind(0, 0)
166192

167-
fun mouseBind(code: Int) = Bind(code = code, isMouseButton = true)
193+
fun mouseBind(code: Int) = Bind(0, 0, code)
168194
}
169195
}

src/main/kotlin/com/lambda/event/events/KeyboardEvent.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.lambda.event.events
1919

20+
import com.lambda.config.settings.complex.Bind
2021
import com.lambda.event.Event
2122
import com.lambda.util.KeyCode
2223
import org.lwjgl.glfw.GLFW.GLFW_MOD_ALT
@@ -54,6 +55,8 @@ sealed class KeyboardEvent {
5455
val isPressed = action == GLFW_PRESS
5556
val isReleased = action == GLFW_RELEASE
5657

58+
fun satisfies(bind: Bind) = bind.key == keyCode && bind.modifiers == modifiers
59+
5760
val hasShift = modifiers and GLFW_MOD_SHIFT != 0
5861
val hasControl = modifiers and GLFW_MOD_CONTROL != 0
5962
val hasAlt = modifiers and GLFW_MOD_ALT != 0

src/main/kotlin/com/lambda/event/events/MouseEvent.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package com.lambda.event.events
1919

20+
import com.lambda.config.settings.complex.Bind
2021
import com.lambda.event.callback.Cancellable
2122
import com.lambda.event.callback.ICancellable
2223
import com.lambda.util.Mouse
@@ -50,6 +51,8 @@ sealed class MouseEvent {
5051
position
5152
)
5253

54+
fun satisfies(bind: Bind) = bind.modifiers == modifiers && bind.mouse == button
55+
5356
val isMainButton = button <= 2
5457
val isSideButton = button > 2
5558
val isLeftButton = button == 0

src/main/kotlin/com/lambda/module/Module.kt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.lambda.module
1919

20-
import com.lambda.Lambda.mc
20+
import com.lambda.Lambda
2121
import com.lambda.command.LambdaCommand
2222
import com.lambda.config.AbstractSetting
2323
import com.lambda.config.Configurable
@@ -135,13 +135,24 @@ abstract class Module(
135135

136136
init {
137137
listen<KeyboardEvent.Press>(alwaysListen = true) { event ->
138-
onButtonPress(event.keyCode, event.isPressed, event.isReleased)
138+
if (Lambda.mc.options.commandKey.isPressed
139+
|| Lambda.mc.currentScreen != null
140+
|| !event.satisfies(keybind)) return@listen
141+
142+
if (event.isPressed) toggle()
143+
else if (event.isReleased && disableOnRelease) disable()
139144
}
140145

141146
listen<MouseEvent.Click>(alwaysListen = true) { event ->
142147
val pressed = event.action == Mouse.Action.Click.ordinal
143148
val released = event.action == Mouse.Action.Release.ordinal
144-
onButtonPress(event.button, pressed, released)
149+
150+
if (mc.options.commandKey.isPressed
151+
|| mc.currentScreen != null
152+
|| !event.satisfies(keybind)) return@listen
153+
154+
if (pressed) toggle()
155+
else if (released && disableOnRelease) disable()
145156
}
146157

147158
onEnable { LambdaSound.MODULE_ON.play() }
@@ -155,14 +166,6 @@ abstract class Module(
155166
listen<ConnectionEvent.Disconnect> { if (autoDisable) disable() }
156167
}
157168

158-
private fun onButtonPress(code: Int, pressed: Boolean, released: Boolean) {
159-
if (mc.options.commandKey.isPressed) return
160-
if (mc.currentScreen != null) return
161-
if (code != keybind.code) return
162-
if (pressed) toggle()
163-
else if (released && disableOnRelease) disable()
164-
}
165-
166169
fun enable() {
167170
isEnabled = true
168171
}

src/main/kotlin/com/lambda/module/hud/ModuleList.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ object ModuleList : HudModule(
3939

4040
enabled.forEach {
4141
text(it.name); sameLine()
42-
val color = if (it.keybind.code == KeyCode.UNBOUND.code) Color.RED else Color.GREEN
42+
val color = if (it.keybind.key == 0 && it.keybind.mouse == -1) Color.RED else Color.GREEN
4343

4444
withStyleColor(ImGuiCol.Text, color) { text(" [${it.keybind.name}]") }
4545
}

src/main/kotlin/com/lambda/util/Communication.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,8 @@ object Communication {
179179
}
180180
literal("Keybind: ")
181181
color(ClickGuiLayout.primaryColor) {
182-
if (module.keybind.code != -1) {
183-
literal(module.keybind.code.toString())
182+
if (module.keybind.key != 0 || module.keybind.mouse > -1) {
183+
literal(module.keybind.name)
184184
} else {
185185
literal("Unbound")
186186
}

src/main/kotlin/com/lambda/util/InputUtils.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ import com.lambda.util.math.Vec2d
2626
import net.minecraft.client.util.InputUtil
2727

2828
object InputUtils : Loadable {
29-
var lastKeyboardEvent: KeyboardEvent.Press = KeyboardEvent.Press(0, 0, -1, 0); private set
30-
var lastMouseEvent: MouseEvent.Click = MouseEvent.Click(Mouse.Button.Left, Mouse.Action.Click, 0, Vec2d(0, 0)); private set
29+
var lastKeyboardEvent: KeyboardEvent.Press? = null; private set
30+
var lastMouseEvent: MouseEvent.Click? = null; private set
3131

3232
/**
3333
* Returns whether any of the key-codes (not scan-codes) are being pressed

0 commit comments

Comments
 (0)