Improvement: Bind listeners by beanbag44 · Pull Request #219 · lambda-client/lambda · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/com/lambda/mixin/input/KeyboardMixin.java
9 changes: 4 additions & 5 deletions src/main/java/com/lambda/mixin/input/MouseMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
package com.lambda.mixin.input;

import com.lambda.event.EventFlow;
import com.lambda.event.events.MouseEvent;
import com.lambda.event.events.ButtonEvent;
import com.lambda.module.modules.render.Zoom;
import com.lambda.util.math.Vec2d;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import net.minecraft.client.Mouse;
import net.minecraft.client.input.MouseInput;
import net.minecraft.client.option.SimpleOption;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -39,15 +38,15 @@ public class MouseMixin {

@WrapMethod(method = "onMouseButton")
private void onMouseButton(long window, MouseInput input, int action, Operation<Void> original) {
if (!EventFlow.post(new MouseEvent.Click(input.button(), action, input.modifiers())).isCanceled())
if (!EventFlow.post(new ButtonEvent.Mouse.Click(input.button(), action, input.modifiers())).isCanceled())
original.call(window, input, action);
}

@WrapMethod(method = "onMouseScroll(JDD)V")
private void onMouseScroll(long window, double horizontal, double vertical, Operation<Void> original) {
Vec2d delta = new Vec2d(horizontal, vertical);

if (!EventFlow.post(new MouseEvent.Scroll(delta)).isCanceled())
if (!EventFlow.post(new ButtonEvent.Mouse.Scroll(delta)).isCanceled())
original.call(window, horizontal, vertical);
}

Expand All @@ -57,7 +56,7 @@ private void onCursorPos(long window, double x, double y, Operation<Void> origin

Vec2d position = new Vec2d(x, y);

if (!EventFlow.post(new MouseEvent.Move(position)).isCanceled())
if (!EventFlow.post(new ButtonEvent.Mouse.Move(position)).isCanceled())
original.call(window, x, y);
}

Expand Down
9 changes: 7 additions & 2 deletions src/main/kotlin/com/lambda/config/Configurable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import com.lambda.config.settings.numeric.DoubleSetting
import com.lambda.config.settings.numeric.FloatSetting
import com.lambda.config.settings.numeric.IntegerSetting
import com.lambda.config.settings.numeric.LongSetting
import com.lambda.event.Muteable
import com.lambda.util.Communication.logError
import com.lambda.util.KeyCode
import com.lambda.util.Nameable
Expand Down Expand Up @@ -228,15 +229,19 @@ abstract class Configurable(
name: String,
defaultValue: Bind,
description: String = "",
alwaysListening: Boolean = false,
screenCheck: Boolean = true,
visibility: () -> Boolean = { true },
) = Setting(name, description, KeybindSetting(defaultValue), this, visibility).register()
) = Setting(name, description, KeybindSetting(defaultValue, this as? Muteable, alwaysListening, screenCheck), this, visibility).register()

fun setting(
name: String,
defaultValue: KeyCode,
description: String = "",
alwaysListening: Boolean = false,
screenCheck: Boolean = true,
visibility: () -> Boolean = { true },
) = Setting(name, description, KeybindSetting(defaultValue), this, visibility).register()
) = Setting(name, description, KeybindSetting(defaultValue, this as? Muteable, alwaysListening, screenCheck), this, visibility).register()

fun setting(
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ import com.lambda.brigadier.optional
import com.lambda.brigadier.required
import com.lambda.config.Setting
import com.lambda.config.SettingCore
import com.lambda.context.SafeContext
import com.lambda.event.Muteable
import com.lambda.event.events.ButtonEvent
import com.lambda.event.listener.SafeListener.Companion.listen
import com.lambda.gui.dsl.ImGuiBuilder
import com.lambda.util.InputUtils
import com.lambda.util.KeyCode
Expand All @@ -48,14 +52,43 @@ import org.lwjgl.glfw.GLFW.GLFW_MOD_NUM_LOCK
import org.lwjgl.glfw.GLFW.GLFW_MOD_SHIFT
import org.lwjgl.glfw.GLFW.GLFW_MOD_SUPER

class KeybindSetting(defaultValue: Bind) : SettingCore<Bind>(
class KeybindSetting(
defaultValue: Bind,
private val muteable: Muteable?,
private val alwaysListening: Boolean,
private val screenCheck: Boolean
) : SettingCore<Bind>(
defaultValue,
TypeToken.get(Bind::class.java).type
) {
constructor(defaultValue: KeyCode) : this(Bind(defaultValue.code, 0, -1))
), Muteable {
constructor(defaultValue: KeyCode, muteable: Muteable?, alwaysListen: Boolean, screenCheck: Boolean)
: this(Bind(defaultValue.code, 0, -1), muteable, alwaysListen, screenCheck)

private val pressListeners = mutableListOf<SafeContext.(ButtonEvent) -> Unit>()
private val repeatListeners = mutableListOf<SafeContext.(ButtonEvent) -> Unit>()
private val releaseListeners = mutableListOf<SafeContext.(ButtonEvent) -> Unit>()

private var listening = false

override val isMuted
get() = muteable?.isMuted == true && !alwaysListening

init {
listen<ButtonEvent.Keyboard.Press> { event -> onButtonEvent(event) }
listen<ButtonEvent.Mouse.Click> { event -> onButtonEvent(event) }
}

private fun SafeContext.onButtonEvent(event: ButtonEvent) {
if (mc.options.commandKey.isPressed ||
(screenCheck && mc.currentScreen != null) ||
!event.satisfies(value)) return

if (event.isPressed) {
if (event.isRepeated) repeatListeners.forEach { it(event) }
else pressListeners.forEach { it(event) }
} else if (event.isReleased) releaseListeners.forEach { it(event) }
}

context(setting: Setting<*, Bind>)
override fun ImGuiBuilder.buildLayout() {
text(setting.name)
Expand Down Expand Up @@ -161,6 +194,20 @@ class KeybindSetting(defaultValue: Bind) : SettingCore<Bind>(
}
}
}

companion object {
fun Setting<KeybindSetting, Bind>.onPress(block: SafeContext.(ButtonEvent) -> Unit) = apply {
core.pressListeners.add(block)
}

fun Setting<KeybindSetting, Bind>.onRepeat(block: SafeContext.(ButtonEvent) -> Unit) = apply {
core.repeatListeners.add(block)
}

fun Setting<KeybindSetting, Bind>.onRelease(block: SafeContext.(ButtonEvent) -> Unit) = apply {
core.releaseListeners.add(block)
}
}
}

data class Bind(
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/lambda/event/EventFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ object EventFlow {
* @return `true` if the listener should not be notified, `false` otherwise.
*/
private fun <T : Event> shouldNotNotify(listener: Listener<T>, event: Event) =
listener.owner is Muteable
&& (listener.owner as Muteable).isMuted
&& !listener.alwaysListen
|| event is ICancellable && event.isCanceled()
(listener.owner is Muteable &&
(listener.owner as Muteable).isMuted &&
!listener.alwaysListen) ||
(event is ICancellable && event.isCanceled())
}
110 changes: 110 additions & 0 deletions src/main/kotlin/com/lambda/event/events/ButtonEvent.kt
65 changes: 0 additions & 65 deletions src/main/kotlin/com/lambda/event/events/KeyboardEvent.kt

This file was deleted.

Loading