Skip to content

Commit e724b48

Browse files
committed
changes
1 parent 7a896dd commit e724b48

File tree

8 files changed

+106
-42
lines changed

8 files changed

+106
-42
lines changed

src/main/kotlin/com/lambda/config/Configurable.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import com.lambda.config.settings.collections.MapSetting
3030
import com.lambda.config.settings.collections.SetSetting
3131
import com.lambda.config.settings.comparable.BooleanSetting
3232
import com.lambda.config.settings.comparable.EnumSetting
33+
import com.lambda.config.settings.complex.Bind
3334
import com.lambda.config.settings.complex.BlockPosSetting
3435
import com.lambda.config.settings.complex.BlockSetting
3536
import com.lambda.config.settings.complex.ColorSetting
@@ -381,7 +382,7 @@ abstract class Configurable(
381382
*/
382383
fun setting(
383384
name: String,
384-
defaultValue: KeyCode,
385+
defaultValue: Bind,
385386
description: String = "",
386387
visibility: () -> Boolean = { true },
387388
) = KeybindSetting(name, defaultValue, description, visibility).register()

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

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,20 @@
1818
package com.lambda.config.settings.complex
1919

2020
import com.google.gson.reflect.TypeToken
21+
import com.lambda.brigadier.CommandResult.Companion.failure
22+
import com.lambda.brigadier.CommandResult.Companion.success
23+
import com.lambda.brigadier.argument.boolean
2124
import com.lambda.brigadier.argument.value
2225
import com.lambda.brigadier.argument.word
23-
import com.lambda.brigadier.execute
26+
import com.lambda.brigadier.executeWithResult
27+
import com.lambda.brigadier.optional
2428
import com.lambda.brigadier.required
2529
import com.lambda.config.AbstractSetting
30+
import com.lambda.config.settings.complex.Bind.Companion.mouseBind
2631
import com.lambda.gui.dsl.ImGuiBuilder
32+
import com.lambda.util.InputUtils
2733
import com.lambda.util.KeyCode
28-
import com.lambda.util.KeyboardUtils
34+
import com.lambda.util.Mouse
2935
import com.lambda.util.StringUtils.capitalize
3036
import com.lambda.util.extension.CommandBuilder
3137
import imgui.ImGui.isMouseClicked
@@ -37,22 +43,23 @@ import org.lwjgl.glfw.GLFW
3743

3844
class KeybindSetting(
3945
override val name: String,
40-
defaultValue: KeyCode,
46+
defaultValue: Bind,
4147
description: String,
4248
visibility: () -> Boolean,
43-
) : AbstractSetting<KeyCode>(
49+
) : AbstractSetting<Bind>(
4450
defaultValue,
45-
TypeToken.get(KeyCode::class.java).type,
51+
TypeToken.get(Bind::class.java).type,
4652
description,
4753
visibility
4854
) {
4955
private var listening = false
5056

5157
override fun ImGuiBuilder.buildLayout() {
52-
val key = value
53-
val scancode = if (key == KeyCode.UNBOUND) -69 else GLFW.glfwGetKeyScancode(key.code)
54-
val translated = if (key == KeyCode.UNBOUND) key else KeyCode.virtualMapUS(key.code, scancode)
55-
val preview = if (listening) "$name: Press any key…" else "$name: $translated"
58+
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"
5663

5764
if (listening) {
5865
withStyleColor(ImGuiCol.Button, 0.20f, 0.50f, 1.00f, 1.00f) {
@@ -75,7 +82,7 @@ class KeybindSetting(
7582
}
7683

7784
onItemClick(ImGuiMouseButton.Right) {
78-
value = KeyCode.UNBOUND
85+
value = Bind.EMPTY
7986
listening = false
8087
}
8188

@@ -85,38 +92,78 @@ class KeybindSetting(
8592

8693
sameLine()
8794
smallButton("Unbind") {
88-
value = KeyCode.UNBOUND
95+
value = Bind.EMPTY
8996
listening = false
9097
}
9198
onItemHover(ImGuiHoveredFlags.Stationary) {
9299
lambdaTooltip("Clear binding")
93100
}
94101

95-
val poll = KeyboardUtils.lastEvent
96-
if (listening && poll.isPressed) {
97-
when (val key = poll.translated) {
98-
KeyCode.ESCAPE -> listening = false
99-
KeyCode.BACKSPACE, KeyCode.DELETE -> {
100-
value = KeyCode.UNBOUND
101-
listening = false
102-
}
103-
else -> {
104-
value = key
105-
listening = false
102+
val keyboardPoll = InputUtils.lastKeyboardEvent
103+
val mousePoll = InputUtils.lastMouseEvent
104+
if (listening) {
105+
if (keyboardPoll.isPressed) {
106+
when (val key = keyboardPoll.translated) {
107+
KeyCode.ESCAPE -> {}
108+
KeyCode.BACKSPACE, KeyCode.DELETE -> value = Bind.EMPTY
109+
else -> value = Bind(key)
106110
}
111+
listening = false
112+
} else if (mousePoll.action == Mouse.Action.Click.ordinal) {
113+
value = mouseBind(mousePoll.button)
114+
listening = false
107115
}
108116
}
109117
}
110118

111119
override fun CommandBuilder.buildCommand(registry: CommandRegistryAccess) {
112-
required(word(name)) { parameter ->
120+
required(word(name)) { name ->
113121
suggests { _, builder ->
114122
KeyCode.entries.forEach { builder.suggest(it.name.capitalize()) }
123+
(1..10).forEach { builder.suggest(it) }
115124
builder.buildFuture()
116125
}
117-
execute {
118-
trySetValue(KeyCode.valueOf(parameter().value()))
126+
optional(boolean("mouse button")) { isMouseButton ->
127+
executeWithResult {
128+
val isMouse = if (isMouseButton != null) isMouseButton().value() else false
129+
var bind = Bind.EMPTY
130+
if (isMouse) {
131+
val num = try {
132+
name().value().toInt()
133+
} catch(_: NumberFormatException) {
134+
return@executeWithResult failure("${name().value()} doesn't match with a mouse button")
135+
}
136+
bind = mouseBind(num)
137+
} else {
138+
bind = try {
139+
Bind(KeyCode.valueOf(name().value()))
140+
} catch(_: IllegalArgumentException) {
141+
return@executeWithResult failure("${name().value()} doesn't match with a bind")
142+
}
143+
}
144+
145+
trySetValue(bind)
146+
return@executeWithResult success()
147+
}
119148
}
120149
}
121150
}
122151
}
152+
153+
data class Bind(
154+
val keyCode: KeyCode = KeyCode.UNBOUND,
155+
val code: Int = keyCode.code,
156+
val isMouseButton: Boolean = false
157+
) {
158+
val name: String
159+
get() = if (isMouseButton) "Mouse $code" else keyCode.name
160+
161+
override fun toString() =
162+
"Key Code: $keyCode, Code: $code, Mouse Button: $isMouseButton"
163+
164+
companion object {
165+
val EMPTY = Bind()
166+
167+
fun mouseBind(code: Int) = Bind(code = code, isMouseButton = true)
168+
}
169+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
package com.lambda.module
1919

20+
import com.lambda.config.settings.complex.Bind
2021
import com.lambda.gui.Layout
2122
import com.lambda.module.tag.ModuleTag
22-
import com.lambda.util.KeyCode
2323
import java.awt.Color
2424

2525
abstract class HudModule(
@@ -29,7 +29,7 @@ abstract class HudModule(
2929
val customWindow: Boolean = false,
3030
alwaysListening: Boolean = false,
3131
enabledByDefault: Boolean = false,
32-
defaultKeybind: KeyCode = KeyCode.UNBOUND,
32+
defaultKeybind: Bind = Bind.EMPTY,
3333
) : Module(name, description, tag, alwaysListening, enabledByDefault, defaultKeybind), Layout {
3434
val backgroundColor by setting("Background Color", Color(0, 0, 0, 0))
3535
val outline by setting("Show Outline", false)

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717

1818
package com.lambda.module
1919

20+
import com.lambda.Lambda.mc
2021
import com.lambda.command.LambdaCommand
2122
import com.lambda.config.AbstractSetting
2223
import com.lambda.config.Configurable
2324
import com.lambda.config.Configuration
2425
import com.lambda.config.configurations.ModuleConfig
26+
import com.lambda.config.settings.complex.Bind
2527
import com.lambda.context.SafeContext
2628
import com.lambda.event.Muteable
2729
import com.lambda.event.events.ClientEvent
2830
import com.lambda.event.events.ConnectionEvent
2931
import com.lambda.event.events.KeyboardEvent
32+
import com.lambda.event.events.MouseEvent
3033
import com.lambda.event.listener.Listener
3134
import com.lambda.event.listener.SafeListener
3235
import com.lambda.event.listener.SafeListener.Companion.listen
@@ -35,6 +38,7 @@ import com.lambda.module.tag.ModuleTag
3538
import com.lambda.sound.LambdaSound
3639
import com.lambda.sound.SoundManager.play
3740
import com.lambda.util.KeyCode
41+
import com.lambda.util.Mouse
3842
import com.lambda.util.Nameable
3943

4044
/**
@@ -111,7 +115,7 @@ abstract class Module(
111115
val tag: ModuleTag,
112116
private val alwaysListening: Boolean = false,
113117
enabledByDefault: Boolean = false,
114-
defaultKeybind: KeyCode = KeyCode.UNBOUND,
118+
defaultKeybind: Bind = Bind.EMPTY,
115119
autoDisable: Boolean = false
116120
) : Nameable, Muteable, Configurable(ModuleConfig) {
117121
private val isEnabledSetting = setting("Enabled", enabledByDefault) { false }
@@ -122,20 +126,20 @@ abstract class Module(
122126
var isEnabled by isEnabledSetting
123127
val isDisabled get() = !isEnabled
124128

125-
val keybind by keybindSetting
129+
var keybind by keybindSetting
126130

127131
override val isMuted: Boolean
128132
get() = !isEnabled && !alwaysListening
129133

130134
init {
131135
listen<KeyboardEvent.Press>(alwaysListen = true) { event ->
132-
if (mc.options.commandKey.isPressed) return@listen
133-
if (!event.isPressed) return@listen
134-
if (keybind == KeyCode.UNBOUND) return@listen
135-
if (event.translated != keybind) return@listen
136-
if (mc.currentScreen != null) return@listen
136+
if (event.isPressed)
137+
onButtonPress(event.keyCode)
138+
}
137139

138-
toggle()
140+
listen<MouseEvent.Click>(alwaysListen = true) { event ->
141+
if (event.action == Mouse.Action.Click.ordinal)
142+
onButtonPress(event.button)
139143
}
140144

141145
onEnable { LambdaSound.MODULE_ON.play() }
@@ -149,6 +153,14 @@ abstract class Module(
149153
listen<ConnectionEvent.Disconnect> { if (autoDisable) disable() }
150154
}
151155

156+
private fun onButtonPress(code: Int, mouseButton: Boolean = false) {
157+
if (mc.options.commandKey.isPressed) return
158+
if (mc.currentScreen != null) return
159+
if (keybind.code == KeyCode.UNBOUND.code) return
160+
if (code != keybind.code) return
161+
toggle()
162+
}
163+
152164
fun enable() {
153165
isEnabled = true
154166
}

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 == KeyCode.UNBOUND) Color.RED else Color.GREEN
42+
val color = if (it.keybind.code == KeyCode.UNBOUND.code) Color.RED else Color.GREEN
4343

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

src/main/kotlin/com/lambda/module/modules/player/InventoryMove.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import com.lambda.interaction.request.rotating.RotationMode
2727
import com.lambda.interaction.request.rotating.visibilty.lookAt
2828
import com.lambda.module.Module
2929
import com.lambda.module.tag.ModuleTag
30-
import com.lambda.util.KeyboardUtils.isKeyPressed
30+
import com.lambda.util.InputUtils.isKeyPressed
3131
import com.lambda.util.math.MathUtils.toFloatSign
3232
import net.minecraft.client.gui.screen.ChatScreen
3333
import net.minecraft.client.gui.screen.Screen

src/main/kotlin/com/lambda/module/modules/player/Scaffold.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import com.lambda.module.tag.ModuleTag
3838
import com.lambda.util.BlockUtils.blockPos
3939
import com.lambda.util.BlockUtils.blockState
4040
import com.lambda.util.KeyCode
41-
import com.lambda.util.KeyboardUtils.isKeyPressed
41+
import com.lambda.util.InputUtils.isKeyPressed
4242
import com.lambda.util.NamedEnum
4343
import com.lambda.util.math.distSq
4444
import com.lambda.util.world.raycast.InteractionMask

src/main/kotlin/com/lambda/util/KeyboardUtils.kt renamed to src/main/kotlin/com/lambda/util/InputUtils.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ package com.lambda.util
2020
import com.lambda.context.SafeContext
2121
import com.lambda.core.Loadable
2222
import com.lambda.event.events.KeyboardEvent
23+
import com.lambda.event.events.MouseEvent
2324
import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe
25+
import com.lambda.util.math.Vec2d
2426
import net.minecraft.client.util.InputUtil
2527

26-
object KeyboardUtils : Loadable {
27-
var lastEvent: KeyboardEvent.Press = KeyboardEvent.Press(0, 0, -1, 0)
28+
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
2831

2932
/**
3033
* Returns whether any of the key-codes (not scan-codes) are being pressed
@@ -34,6 +37,7 @@ object KeyboardUtils : Loadable {
3437

3538
init {
3639
// hacking imgui jni lib rn because it's missing a lot of native functions including i/o stuff
37-
listenUnsafe<KeyboardEvent.Press> { lastEvent = it }
40+
listenUnsafe<KeyboardEvent.Press> { lastKeyboardEvent = it }
41+
listenUnsafe<MouseEvent.Click> { lastMouseEvent = it }
3842
}
3943
}

0 commit comments

Comments
 (0)