Skip to content

Commit 9b36ff4

Browse files
rfresh2Avanatiker
andauthored
Mouse bindings (#379)
* Mouse bindings * Allow mid click bindings and use button name numbers instead of index Human-readable mouse names start with 1. Using this scheme aligns with what you'd see in RusherHack for example. To accomplish this, any raw mouse input that comes as an index we simply increment by 1. I'm planning on PR'ing a hud module that shows all bindings, again like in Rusherhack. So this would trigger my OCD having those bind names not align between clients. * fix comment and comparisons for clarity * clarify what min index means * Cleanup and cast safety * More null safety (!! can throw an exception) also mousekey is concurrent and may change after first null check. Co-authored-by: Constructor <[email protected]>
1 parent 3dcbc8a commit 9b36ff4

File tree

6 files changed

+67
-19
lines changed

6 files changed

+67
-19
lines changed

src/main/kotlin/com/lambda/client/event/ForgeEventProcessor.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import net.minecraftforge.fml.common.gameevent.InputEvent
2222
import net.minecraftforge.fml.common.gameevent.TickEvent
2323
import net.minecraftforge.fml.common.network.FMLNetworkEvent
2424
import org.lwjgl.input.Keyboard
25+
import org.lwjgl.input.Mouse
2526

2627
internal object ForgeEventProcessor {
2728
private val mc = Wrapper.minecraft
@@ -82,6 +83,13 @@ internal object ForgeEventProcessor {
8283
ModuleManager.onBind(Keyboard.getEventKey())
8384
}
8485

86+
@SubscribeEvent
87+
fun onEventMouse(event: InputEvent.MouseInputEvent) {
88+
LambdaEventBus.post(event)
89+
if (!Mouse.getEventButtonState()) return
90+
ModuleManager.onMouseBind(Mouse.getEventButton() + 1)
91+
}
92+
8593
@SubscribeEvent(priority = EventPriority.HIGHEST)
8694
fun onChatSent(event: ClientChatEvent) {
8795
MessageDetection.Command.BARITONE.removedOrNull(event.message)?.let {
@@ -94,11 +102,6 @@ internal object ForgeEventProcessor {
94102
}
95103
}
96104

97-
@SubscribeEvent
98-
fun onEventMouse(event: InputEvent.MouseInputEvent) {
99-
LambdaEventBus.post(event)
100-
}
101-
102105
/**
103106
* Includes events of subclasses like ChunkEvent and GetCollisionBoxesEvent
104107
*/

src/main/kotlin/com/lambda/client/gui/rgui/component/BindButton.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.lambda.client.module.modules.client.ClickGUI
44
import com.lambda.client.module.modules.client.CustomFont
55
import com.lambda.client.module.modules.client.GuiColors
66
import com.lambda.client.setting.settings.impl.other.BindSetting
7+
import com.lambda.client.util.Bind.Companion.minMouseIndex
78
import com.lambda.client.util.graphics.VertexHelper
89
import com.lambda.client.util.graphics.font.FontRenderAdapter
910
import com.lambda.client.util.math.Vec2f
@@ -20,6 +21,11 @@ class BindButton(
2021

2122
override fun onRelease(mousePos: Vec2f, buttonId: Int) {
2223
super.onRelease(mousePos, buttonId)
24+
if (listening && buttonId >= minMouseIndex) {
25+
setting.value.apply {
26+
setMouseBind(buttonId + 1)
27+
}
28+
}
2329
listening = !listening
2430
}
2531

src/main/kotlin/com/lambda/client/module/ModuleManager.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,15 @@ object ModuleManager : AsyncLoader<List<Class<out AbstractModule>>> {
6464

6565
internal fun onBind(eventKey: Int) {
6666
if (Keyboard.isKeyDown(Keyboard.KEY_F3)) return // if key is the 'none' key (stuff like mod key in i3 might return 0)
67-
for (module in modules) {
68-
if (module.bind.value.isDown(eventKey)) module.toggle()
69-
}
67+
modules.filter {
68+
it.bind.value.isDown(eventKey)
69+
}.forEach { it.toggle() }
70+
}
71+
72+
internal fun onMouseBind(eventMouse: Int) {
73+
modules.filter {
74+
it.bind.value.isMouseDown(eventMouse)
75+
}.forEach { it.toggle() }
7076
}
7177

7278
fun getModuleOrNull(moduleName: String?) = moduleName?.let { moduleSet[it] }

src/main/kotlin/com/lambda/client/setting/settings/impl/other/BindSetting.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class BindSetting(
1414
description: String = ""
1515
) : ImmutableSetting<Bind>(name, value, visibility, { _, input -> input }, description, unit = "") {
1616

17-
override val defaultValue: Bind = Bind(TreeSet(value.modifierKeys), value.key)
17+
override val defaultValue: Bind = Bind(TreeSet(value.modifierKeys), value.key, null)
1818

1919
override fun resetValue() {
2020
value.setBind(defaultValue.modifierKeys, defaultValue.key)
@@ -25,6 +25,12 @@ class BindSetting(
2525
value.clear()
2626
return
2727
}
28+
if (valueIn.startsWith("Mouse", ignoreCase = true)) {
29+
valueIn.split("Mouse").lastOrNull()?.toIntOrNull()?.let {
30+
value.setMouseBind(it)
31+
}
32+
return
33+
}
2834

2935
val splitNames = valueIn.split('+')
3036
val lastKey = KeyboardUtils.getKey(splitNames.last())

src/main/kotlin/com/lambda/client/util/Bind.kt

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@ import java.util.*
55

66
class Bind(
77
modifierKeysIn: TreeSet<Int>,
8-
keyIn: Int
8+
keyIn: Int,
9+
mouseIn: Int?
910
) {
1011

1112
constructor() : this(0)
1213

13-
constructor(key: Int) : this(TreeSet(keyComparator), key)
14+
constructor(key: Int) : this(TreeSet(keyComparator), key, null)
1415

15-
constructor(vararg modifierKeys: Int, key: Int) : this(TreeSet(keyComparator).apply { modifierKeys.forEach { add(it) } }, key)
16+
constructor(vararg modifierKeys: Int, key: Int) : this(TreeSet(keyComparator).apply { modifierKeys.forEach { add(it) } }, key, null)
1617

1718
val modifierKeys = modifierKeysIn
1819
var key = keyIn; private set
20+
var mouseKey = mouseIn; private set
21+
1922
private var cachedName = getName()
2023

21-
val isEmpty get() = key !in 1..255
24+
val isEmpty get() = key !in 1..255 && compareValues(mouseKey, minMouseIndex) < 0
2225

2326
fun isDown(eventKey: Int): Boolean {
2427
return eventKey != 0
@@ -27,6 +30,12 @@ class Bind(
2730
&& synchronized(this) { modifierKeys.all { isModifierKeyDown(eventKey, it) } }
2831
}
2932

33+
fun isMouseDown(eventKey: Int): Boolean {
34+
return eventKey > minMouseIndex
35+
&& !isEmpty
36+
&& mouseKey == (eventKey)
37+
}
38+
3039
private fun isModifierKeyDown(eventKey: Int, modifierKey: Int) =
3140
eventKey != modifierKey
3241
&& when (modifierKey) {
@@ -62,11 +71,21 @@ class Bind(
6271
setBind(cache, keyIn)
6372
}
6473

74+
fun setMouseBind(mouseIn: Int) {
75+
synchronized(this) {
76+
modifierKeys.clear()
77+
key = 0
78+
mouseKey = mouseIn
79+
cachedName = getName()
80+
}
81+
}
82+
6583
fun setBind(modifierKeysIn: Collection<Int>, keyIn: Int) {
6684
synchronized(this) {
6785
modifierKeys.clear()
6886
modifierKeys.addAll(modifierKeysIn)
6987
key = keyIn
88+
mouseKey = null
7089
cachedName = getName()
7190
}
7291
}
@@ -75,6 +94,7 @@ class Bind(
7594
synchronized(this) {
7695
modifierKeys.clear()
7796
key = 0
97+
mouseKey = null
7898
cachedName = getName()
7999
}
80100
}
@@ -88,18 +108,25 @@ class Bind(
88108
"None"
89109
} else {
90110
StringBuilder().run {
91-
for (key in modifierKeys) {
92-
val name = modifierName[key] ?: KeyboardUtils.getDisplayName(key) ?: continue
93-
append(name)
94-
append('+')
111+
mouseKey?.let {
112+
if (it > minMouseIndex) append("Mouse$mouseKey")
113+
} ?: run {
114+
for (key in modifierKeys) {
115+
val name = modifierName[key] ?: KeyboardUtils.getDisplayName(key) ?: continue
116+
append(name)
117+
append('+')
118+
}
119+
append(KeyboardUtils.getDisplayName(key))
95120
}
96-
append(KeyboardUtils.getDisplayName(key))
121+
97122
toString()
98123
}
99124
}
100125
}
101126

102127
companion object {
128+
const val minMouseIndex: Int = 2 // middle click button index. Button number = index + 1.
129+
103130
private val modifierName: Map<Int, String> = hashMapOf(
104131
Keyboard.KEY_LCONTROL to "Ctrl",
105132
Keyboard.KEY_RCONTROL to "Ctrl",

src/main/kotlin/com/lambda/client/util/KeyboardUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ object KeyboardUtils {
3636
fun sendUnknownKeyError(bind: String) {
3737
MessageSendHelper.sendErrorMessage("Unknown key [${formatValue(bind)}]! " +
3838
"Right shift is ${formatValue("rshift")}, " +
39-
"left Control is ${formatValue("lcontrol")}, " +
39+
"left control is ${formatValue("lcontrol")}, " +
4040
"and ` is ${formatValue("grave")}. " +
4141
"You cannot bind the ${formatValue("meta")} key."
4242
)

0 commit comments

Comments
 (0)