Skip to content

Commit 899260f

Browse files
committed
Merge remote-tracking branch 'origin/feature/renderer' into feature/renderer
# Conflicts: # common/src/main/kotlin/com/lambda/gui/api/component/button/InputBarOverlay.kt
2 parents a165a3b + 7205551 commit 899260f

File tree

11 files changed

+93
-77
lines changed

11 files changed

+93
-77
lines changed

common/src/main/java/com/lambda/mixin/input/KeyboardMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public class KeyboardMixin {
1414
void onKey(long window, int key, int scancode, int action, int modifiers, CallbackInfo ci) {
1515
if (key <= 0) return;
1616
if (action != 1) return;
17-
EventFlow.post(new KeyPressEvent(key));
17+
EventFlow.post(new KeyPressEvent(key, scancode, action, modifiers));
1818
}
1919
}

common/src/main/kotlin/com/lambda/config/serializer/KeyCodeSerializer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ object KeyCodeSerializer : JsonSerializer<KeyCode>, JsonDeserializer<KeyCode> {
1919
typeOfT: Type?,
2020
context: JsonDeserializationContext?,
2121
): KeyCode =
22-
json?.asString?.let(KeyCode::fromNameOrNull) ?: throw JsonParseException("Invalid key code format")
22+
json?.asString?.let(KeyCode::fromKeyName) ?: throw JsonParseException("Invalid key code format")
2323
}

common/src/main/kotlin/com/lambda/event/events/KeyPressEvent.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,25 @@ package com.lambda.event.events
33
import com.lambda.event.EventFlow
44
import com.lambda.event.callback.Cancellable
55
import com.lambda.event.callback.ICancellable
6+
import com.lambda.util.KeyCode
67

78
/**
89
* A class representing a [KeyPressEvent] in the event system ([EventFlow]).
910
*
1011
* A [KeyPressEvent] is a type of event that is triggered when a key is pressed.
1112
* It implements [ICancellable] interface, which means the event can be cancelled.
1213
*
13-
* @property key The key code of the key that was pressed.
14+
* @property keyCode The key code of the key that was pressed.
15+
* @property scanCode The scan code of the key that was pressed.
16+
* @property action The action that was performed on the key.
17+
* @property modifiers The modifiers that were active when the key was pressed.
1418
*/
15-
class KeyPressEvent(val key: Int) : ICancellable by Cancellable()
19+
data class KeyPressEvent(
20+
val keyCode: Int,
21+
val scanCode: Int,
22+
val action: Int,
23+
val modifiers: Int,
24+
) : ICancellable by Cancellable() {
25+
val translated: KeyCode
26+
get() = KeyCode.fromUS(keyCode, scanCode)
27+
}

common/src/main/kotlin/com/lambda/gui/api/LambdaGui.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,17 @@ abstract class LambdaGui(
8080
}
8181

8282
final override fun keyPressed(keyCode: Int, scanCode: Int, modifiers: Int): Boolean {
83-
KeyCode.fromKeyCodeOrNull(keyCode)?.let {
84-
onEvent(GuiEvent.KeyPress(it))
85-
}
83+
val translated = KeyCode.fromUS(keyCode, scanCode)
84+
onEvent(GuiEvent.KeyPress(translated))
8685

87-
if (keyCode == KeyCode.Escape.keyCode) {
86+
if (keyCode == KeyCode.ESCAPE.keyCode) {
8887
close()
8988
}
9089

9190
return true
9291
}
9392

93+
9494
final override fun charTyped(chr: Char, modifiers: Int): Boolean {
9595
onEvent(GuiEvent.CharTyped(chr))
9696
return true

common/src/main/kotlin/com/lambda/gui/impl/clickgui/buttons/setting/BindButton.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.lambda.gui.impl.clickgui.buttons.ModuleButton
99
import com.lambda.gui.impl.clickgui.buttons.SettingButton
1010
import com.lambda.util.KeyCode
1111
import com.lambda.util.math.ColorUtils.multAlpha
12+
import com.lambda.util.primitives.extension.displayValue
1213

1314
class BindButton(
1415
setting: KeyBindSetting,
@@ -22,7 +23,7 @@ class BindButton(
2223
override val showAnimation get() = this@BindButton.showAnimation
2324
override val isKeyBind = true
2425

25-
override fun getText() = value.localizedName
26+
override fun getText() = value.displayValue
2627
override fun setKeyValue(key: KeyCode) { value = key }
2728
}.apply(layer.children::add)
2829

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import com.lambda.event.listener.SafeListener.Companion.listener
1515
import com.lambda.event.listener.UnsafeListener
1616
import com.lambda.gui.impl.clickgui.LambdaClickGui
1717
import com.lambda.module.tag.ModuleTag
18+
import com.lambda.util.Communication.info
1819
import com.lambda.util.KeyCode
1920
import com.lambda.util.Nameable
21+
import org.lwjgl.glfw.GLFW
2022

2123
/**
2224
* A [Module] is a feature or tool for the utility mod.
@@ -33,7 +35,7 @@ import com.lambda.util.Nameable
3335
* The default [keybind] is the key on which
3436
* the module will be activated by default.
3537
* If a module does not need to be activated by a key (like [ClickGUI]),
36-
* the default [keybind] should not be set (using [KeyCode.Unbound]).
38+
* the default [keybind] should not be set (using [KeyCode.UNBOUND]).
3739
*
3840
* [Module]s are [Configurable]s with [settings] (see [AbstractSetting] for all setting types).
3941
* For example, a [BooleanSetting] and a [DoubleSetting] can be defined like this:
@@ -93,7 +95,7 @@ abstract class Module(
9395
val defaultTags: Set<ModuleTag> = setOf(),
9496
private val alwaysListening: Boolean = false,
9597
enabledByDefault: Boolean = false,
96-
defaultKeybind: KeyCode = KeyCode.Unbound,
98+
defaultKeybind: KeyCode = KeyCode.UNBOUND,
9799
) : Nameable, Muteable, Configurable(ModuleConfig) {
98100
private val isEnabledSetting = setting("Enabled", enabledByDefault, visibility = { false })
99101
private val keybindSetting = setting("Keybind", defaultKeybind)
@@ -109,7 +111,7 @@ abstract class Module(
109111
init {
110112
listener<KeyPressEvent>(alwaysListen = true) { event ->
111113
val screen = mc.currentScreen
112-
if (event.key == keybind.keyCode
114+
if (event.translated == keybind
113115
&& !mc.options.commandKey.isPressed
114116
&& (screen == null
115117
|| screen is LambdaClickGui)

common/src/main/kotlin/com/lambda/module/modules/player/Nuker.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ import net.minecraft.util.math.Vec3i
1212

1313
object Nuker : Module(
1414
name = "Nuker",
15-
description = "Breaks blocks around you",
16-
defaultKeybind = KeyCode.Comma
15+
description = "Breaks blocks around you"
1716
) {
1817
private val flatten by setting("Flatten", true)
1918

common/src/main/kotlin/com/lambda/module/modules/player/Replay.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ object Replay : Module(
9898
listener<KeyPressEvent> {
9999
if (mc.currentScreen != null && !mc.options.commandKey.isPressed) return@listener
100100

101-
when (it.key) {
102-
record.keyCode -> handleRecord()
103-
play.keyCode -> handlePlay()
104-
cycle.keyCode -> handlePlayModeCycle()
105-
check.keyCode -> handleCheckpoint()
101+
when (it.translated) {
102+
record -> handleRecord()
103+
play -> handlePlay()
104+
cycle -> handlePlayModeCycle()
105+
check -> handleCheckpoint()
106106
else -> {}
107107
}
108108
}

common/src/main/kotlin/com/lambda/task/tasks/TaskTester.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import org.lwjgl.glfw.GLFW
99
object TaskTester {
1010
init {
1111
listener<KeyPressEvent> {
12-
if (it.key != GLFW.GLFW_KEY_Z) {
12+
if (it.keyCode != GLFW.GLFW_KEY_Z) {
1313
return@listener
1414
}
1515

common/src/main/kotlin/com/lambda/util/KeyCode.kt

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
package com.lambda.util
22

3-
import com.lambda.util.primitives.extension.displayValue
43
import org.lwjgl.glfw.GLFW
54

65
enum class KeyCode(val keyCode: Int) {
7-
Unbound(GLFW.GLFW_KEY_UNKNOWN),
8-
Space(GLFW.GLFW_KEY_SPACE),
9-
Apostrophe(GLFW.GLFW_KEY_APOSTROPHE),
10-
Comma(GLFW.GLFW_KEY_COMMA),
11-
Minus(GLFW.GLFW_KEY_MINUS),
12-
Period(GLFW.GLFW_KEY_PERIOD),
13-
Slash(GLFW.GLFW_KEY_SLASH),
14-
Num0(GLFW.GLFW_KEY_0),
15-
Num1(GLFW.GLFW_KEY_1),
16-
Num2(GLFW.GLFW_KEY_2),
17-
Num3(GLFW.GLFW_KEY_3),
18-
Num4(GLFW.GLFW_KEY_4),
19-
Num5(GLFW.GLFW_KEY_5),
20-
Num6(GLFW.GLFW_KEY_6),
21-
Num7(GLFW.GLFW_KEY_7),
22-
Num8(GLFW.GLFW_KEY_8),
23-
Num9(GLFW.GLFW_KEY_9),
24-
Semicolon(GLFW.GLFW_KEY_SEMICOLON),
25-
Equal(GLFW.GLFW_KEY_EQUAL),
6+
UNBOUND(GLFW.GLFW_KEY_UNKNOWN),
7+
SPACE(GLFW.GLFW_KEY_SPACE),
8+
APOSTROPHE(GLFW.GLFW_KEY_APOSTROPHE),
9+
COMMA(GLFW.GLFW_KEY_COMMA),
10+
MINUS(GLFW.GLFW_KEY_MINUS),
11+
PERIOD(GLFW.GLFW_KEY_PERIOD),
12+
SLASH(GLFW.GLFW_KEY_SLASH),
13+
NUM_0(GLFW.GLFW_KEY_0),
14+
NUM_1(GLFW.GLFW_KEY_1),
15+
NUM_2(GLFW.GLFW_KEY_2),
16+
NUM_3(GLFW.GLFW_KEY_3),
17+
NUM_4(GLFW.GLFW_KEY_4),
18+
NUM_5(GLFW.GLFW_KEY_5),
19+
NUM_6(GLFW.GLFW_KEY_6),
20+
NUM_7(GLFW.GLFW_KEY_7),
21+
NUM_8(GLFW.GLFW_KEY_8),
22+
NUM_9(GLFW.GLFW_KEY_9),
23+
SEMICOLON(GLFW.GLFW_KEY_SEMICOLON),
24+
EQUAL(GLFW.GLFW_KEY_EQUAL),
2625
A(GLFW.GLFW_KEY_A),
2726
B(GLFW.GLFW_KEY_B),
2827
C(GLFW.GLFW_KEY_C),
@@ -49,31 +48,31 @@ enum class KeyCode(val keyCode: Int) {
4948
X(GLFW.GLFW_KEY_X),
5049
Y(GLFW.GLFW_KEY_Y),
5150
Z(GLFW.GLFW_KEY_Z),
52-
LeftBracket(GLFW.GLFW_KEY_LEFT_BRACKET),
53-
Backslash(GLFW.GLFW_KEY_BACKSLASH),
54-
RightBracket(GLFW.GLFW_KEY_RIGHT_BRACKET),
55-
GraveAccent(GLFW.GLFW_KEY_GRAVE_ACCENT),
56-
World1(GLFW.GLFW_KEY_WORLD_1),
57-
World2(GLFW.GLFW_KEY_WORLD_2),
58-
Escape(GLFW.GLFW_KEY_ESCAPE),
59-
Enter(GLFW.GLFW_KEY_ENTER),
60-
Tab(GLFW.GLFW_KEY_TAB),
61-
Backspace(GLFW.GLFW_KEY_BACKSPACE),
62-
Insert(GLFW.GLFW_KEY_INSERT),
63-
Delete(GLFW.GLFW_KEY_DELETE),
64-
Right(GLFW.GLFW_KEY_RIGHT),
65-
Left(GLFW.GLFW_KEY_LEFT),
66-
Down(GLFW.GLFW_KEY_DOWN),
67-
Up(GLFW.GLFW_KEY_UP),
68-
PageUp(GLFW.GLFW_KEY_PAGE_UP),
69-
PageDown(GLFW.GLFW_KEY_PAGE_DOWN),
70-
Home(GLFW.GLFW_KEY_HOME),
71-
End(GLFW.GLFW_KEY_END),
72-
CapsLock(GLFW.GLFW_KEY_CAPS_LOCK),
73-
ScrollLock(GLFW.GLFW_KEY_SCROLL_LOCK),
74-
NumLock(GLFW.GLFW_KEY_NUM_LOCK),
75-
PrintScreen(GLFW.GLFW_KEY_PRINT_SCREEN),
76-
Pause(GLFW.GLFW_KEY_PAUSE),
51+
LEFT_BRACKET(GLFW.GLFW_KEY_LEFT_BRACKET),
52+
BACKSLASH(GLFW.GLFW_KEY_BACKSLASH),
53+
RIGHT_BRACKET(GLFW.GLFW_KEY_RIGHT_BRACKET),
54+
GRAVE_ACCENT(GLFW.GLFW_KEY_GRAVE_ACCENT),
55+
WORLD_1(GLFW.GLFW_KEY_WORLD_1),
56+
WORLD_2(GLFW.GLFW_KEY_WORLD_2),
57+
ESCAPE(GLFW.GLFW_KEY_ESCAPE),
58+
ENTER(GLFW.GLFW_KEY_ENTER),
59+
TAB(GLFW.GLFW_KEY_TAB),
60+
BACKSPACE(GLFW.GLFW_KEY_BACKSPACE),
61+
INSERT(GLFW.GLFW_KEY_INSERT),
62+
DELETE(GLFW.GLFW_KEY_DELETE),
63+
RIGHT(GLFW.GLFW_KEY_RIGHT),
64+
LEFT(GLFW.GLFW_KEY_LEFT),
65+
DOWN(GLFW.GLFW_KEY_DOWN),
66+
UP(GLFW.GLFW_KEY_UP),
67+
PAGE_UP(GLFW.GLFW_KEY_PAGE_UP),
68+
PAGE_DOWN(GLFW.GLFW_KEY_PAGE_DOWN),
69+
HOME(GLFW.GLFW_KEY_HOME),
70+
END(GLFW.GLFW_KEY_END),
71+
CAPS_LOCK(GLFW.GLFW_KEY_CAPS_LOCK),
72+
SCROLL_LOCK(GLFW.GLFW_KEY_SCROLL_LOCK),
73+
NUM_LOCK(GLFW.GLFW_KEY_NUM_LOCK),
74+
PRINT_SCREEN(GLFW.GLFW_KEY_PRINT_SCREEN),
75+
PAUSE(GLFW.GLFW_KEY_PAUSE),
7776
F1(GLFW.GLFW_KEY_F1),
7877
F2(GLFW.GLFW_KEY_F2),
7978
F3(GLFW.GLFW_KEY_F3),
@@ -127,13 +126,15 @@ enum class KeyCode(val keyCode: Int) {
127126
MENU(GLFW.GLFW_KEY_MENU),
128127
LAST(GLFW.GLFW_KEY_LAST);
129128

130-
val localizedName by lazy { GLFW.glfwGetKeyName(keyCode, 0)?.uppercase() ?: displayValue }
131-
132129
companion object {
133-
fun fromKeyCodeOrNull(keyCode: Int) = entries.firstOrNull { it.keyCode == keyCode }
134-
fun fromKeyCode(keyCode: Int) = fromKeyCodeOrNull(keyCode) ?: Unbound
130+
private val keyCodeMap: Map<Int, KeyCode> = entries.associateBy { it.keyCode }
131+
private val nameMap: Map<String, KeyCode> = entries.associateBy { it.name.lowercase() }
132+
133+
fun fromKeyCodeOrNull(keyCode: Int) = keyCodeMap[keyCode]
134+
fun fromKeyCode(keyCode: Int) = fromKeyCodeOrNull(keyCode) ?: UNBOUND
135+
fun fromKeyName(name: String) = nameMap[name.lowercase()] ?: UNBOUND
135136

136-
fun fromNameOrNull(name: String) = entries.firstOrNull { it.name.equals(name, true) }
137-
fun fromName(name: String) = fromNameOrNull(name) ?: Unbound
137+
fun fromUS(keyCode: Int, scanCode: Int) =
138+
GLFW.glfwGetKeyName(keyCode, scanCode)?.let { fromKeyName(it) } ?: fromKeyCode(keyCode)
138139
}
139-
}
140+
}

0 commit comments

Comments
 (0)