Skip to content

Commit ffcff41

Browse files
committed
Fix searching when selecting keybinding
1 parent 7331d03 commit ffcff41

File tree

4 files changed

+27
-19
lines changed

4 files changed

+27
-19
lines changed

common/src/main/java/io/github/notenoughupdates/moulconfig/common/IKeyboardConstants.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface IKeyboardConstants {
1212
val shiftLeft: Int
1313
val shiftRight: Int
1414
val escape: Int
15+
val none: Int
1516
val enter: Int
1617
val delete: Int
1718
val up: Int
@@ -26,4 +27,4 @@ interface IKeyboardConstants {
2627
val keyV: Int
2728
val keyN: Int
2829
val keyF: Int
29-
}
30+
}

common/src/main/java/io/github/notenoughupdates/moulconfig/gui/editors/GuiOptionEditorKeybind.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package io.github.notenoughupdates.moulconfig.gui.editors;
22

33
import io.github.notenoughupdates.moulconfig.GuiTextures;
4+
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorKeybind;
45
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
6+
import io.github.notenoughupdates.moulconfig.common.KeyboardConstants;
57
import io.github.notenoughupdates.moulconfig.common.RenderContext;
68
import io.github.notenoughupdates.moulconfig.gui.GuiComponent;
79
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext;
810
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent;
911
import io.github.notenoughupdates.moulconfig.gui.MouseEvent;
12+
import io.github.notenoughupdates.moulconfig.internal.Warnings;
1013
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
14+
import lombok.var;
1115
import org.jetbrains.annotations.NotNull;
1216

1317
import java.util.Collections;
@@ -18,6 +22,8 @@ public class GuiOptionEditorKeybind extends ComponentEditor {
1822

1923
public GuiOptionEditorKeybind(ProcessedOption option, int defaultKeyCode) {
2024
super(option);
25+
if (option.getType() != int.class && option.getType() != Integer.class)
26+
Warnings.warn(ConfigEditorKeybind.class + " can only be applied to int properties.");
2127

2228
component = wrapComponent(new GuiComponent() {
2329
@Override
@@ -37,22 +43,22 @@ public void render(@NotNull GuiImmediateContext context) {
3743
int width = getWidth();
3844

3945
renderContext.color(1, 1, 1, 1);
40-
IMinecraft.instance.bindTexture(GuiTextures.BUTTON);
46+
renderContext.bindTexture(GuiTextures.BUTTON);
4147
renderContext.drawTexturedRect(width / 6 - 24, height - 7 - 14, 48, 16);
4248

4349

4450
String keyName = IMinecraft.instance.getKeyName((int) option.get());
4551
String text = editingKeycode ? "> " + keyName + " <" : keyName;
4652
renderContext.drawStringCenteredScaledMaxWidth(text,
47-
IMinecraft.instance.getDefaultFontRenderer(),
48-
width / 6, height - 7 - 6,
49-
false, 38, 0xFF303030
53+
IMinecraft.instance.getDefaultFontRenderer(),
54+
width / 6, height - 7 - 6,
55+
false, 38, 0xFF303030
5056
);
5157

5258
int resetX = width / 6 - 24 + 48 + 3;
5359
int resetY = height - 7 - 14 + 3;
5460

55-
IMinecraft.instance.bindTexture(GuiTextures.RESET);
61+
renderContext.bindTexture(GuiTextures.RESET);
5662
renderContext.color(1, 1, 1, 1);
5763
renderContext.drawTexturedRect(resetX, resetY, 10, 11);
5864
int mouseX = context.getMouseX();
@@ -72,7 +78,7 @@ public boolean mouseEvent(@NotNull MouseEvent mouseEvent, @NotNull GuiImmediateC
7278
if (click.getMouseState() && click.getMouseButton() != -1 && editingKeycode) {
7379
editingKeycode = false;
7480
int mouseButton = click.getMouseButton();
75-
option.set(mouseButton);
81+
option.set(mouseButton); // TODO: make this distinct. This is also different from the way 1.8.9 handles those keybindings, so this class is incompatible right now. A "proper" way to do this would be to make a Keybinding class that stores both the button and whether this is a mouse or keyboard button, with some version specific helpers to test if an event matches.
7682
return true;
7783
}
7884

@@ -97,19 +103,17 @@ public boolean mouseEvent(@NotNull MouseEvent mouseEvent, @NotNull GuiImmediateC
97103
}
98104

99105
@Override
100-
public boolean keyboardEvent(KeyboardEvent keyboardEvent, GuiImmediateContext context) {
101-
boolean wasKeyPressedEvent = keyboardEvent instanceof KeyboardEvent.KeyPressed;
102-
if (wasKeyPressedEvent) {
106+
public boolean keyboardEvent(@NotNull KeyboardEvent keyboardEvent, @NotNull GuiImmediateContext context) {
107+
if (keyboardEvent instanceof KeyboardEvent.KeyPressed) {
108+
var keyPressed = (KeyboardEvent.KeyPressed) keyboardEvent;
103109
if (editingKeycode) {
104-
KeyboardEvent.KeyPressed keyPressed = (KeyboardEvent.KeyPressed) keyboardEvent;
110+
if (keyPressed.getPressed()) return true;
105111
editingKeycode = false;
106-
int keyCode = -1;
107112
int keycode = keyPressed.getKeycode();
108-
if (keycode != 256 /* GLFW_KEY_ESCAPE*/ && keycode != 0) {
109-
keyCode = keycode;
113+
if (keycode == KeyboardConstants.INSTANCE.getEscape() && keycode == 0) {
114+
keycode = KeyboardConstants.INSTANCE.getNone();
110115
}
111-
//if (keyCode > 256) keyCode = 0;
112-
option.set(keyCode);
116+
option.set(keycode);
113117
return true;
114118
} else {
115119
return false;

legacy/src/main/java/io/github/notenoughupdates/moulconfig/internal/ForgeKeyboardConstants.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ object ForgeKeyboardConstants : IKeyboardConstants {
1616
get() = Keyboard.KEY_RSHIFT
1717
override val escape: Int
1818
get() = Keyboard.KEY_ESCAPE
19-
19+
override val none: Int
20+
get() = Keyboard.KEY_NONE
2021
override val enter: Int
2122
get() = Keyboard.KEY_RETURN
2223
override val delete: Int
@@ -45,4 +46,4 @@ object ForgeKeyboardConstants : IKeyboardConstants {
4546
get() = Keyboard.KEY_N
4647
override val keyF: Int
4748
get() = Keyboard.KEY_F
48-
}
49+
}

modern/src/main/kotlin/io/github/notenoughupdates/moulconfig/platform/ModernKeyboardConstants.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ object ModernKeyboardConstants : IKeyboardConstants {
1616
get() = InputUtil.GLFW_KEY_RIGHT_SHIFT
1717
override val escape: Int
1818
get() = InputUtil.GLFW_KEY_ESCAPE
19+
override val none: Int
20+
get() = -1
1921
override val enter: Int
2022
get() = InputUtil.GLFW_KEY_ENTER
2123
override val delete: Int
@@ -44,4 +46,4 @@ object ModernKeyboardConstants : IKeyboardConstants {
4446
get() = InputUtil.GLFW_KEY_N
4547
override val keyF: Int
4648
get() = InputUtil.GLFW_KEY_F
47-
}
49+
}

0 commit comments

Comments
 (0)