Skip to content

Commit

Permalink
Fix searching when selecting keybinding
Browse files Browse the repository at this point in the history
  • Loading branch information
nea89o committed Nov 3, 2024
1 parent 7331d03 commit ffcff41
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface IKeyboardConstants {
val shiftLeft: Int
val shiftRight: Int
val escape: Int
val none: Int
val enter: Int
val delete: Int
val up: Int
Expand All @@ -26,4 +27,4 @@ interface IKeyboardConstants {
val keyV: Int
val keyN: Int
val keyF: Int
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.github.notenoughupdates.moulconfig.gui.editors;

import io.github.notenoughupdates.moulconfig.GuiTextures;
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorKeybind;
import io.github.notenoughupdates.moulconfig.common.IMinecraft;
import io.github.notenoughupdates.moulconfig.common.KeyboardConstants;
import io.github.notenoughupdates.moulconfig.common.RenderContext;
import io.github.notenoughupdates.moulconfig.gui.GuiComponent;
import io.github.notenoughupdates.moulconfig.gui.GuiImmediateContext;
import io.github.notenoughupdates.moulconfig.gui.KeyboardEvent;
import io.github.notenoughupdates.moulconfig.gui.MouseEvent;
import io.github.notenoughupdates.moulconfig.internal.Warnings;
import io.github.notenoughupdates.moulconfig.processor.ProcessedOption;
import lombok.var;
import org.jetbrains.annotations.NotNull;

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

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

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

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


String keyName = IMinecraft.instance.getKeyName((int) option.get());
String text = editingKeycode ? "> " + keyName + " <" : keyName;
renderContext.drawStringCenteredScaledMaxWidth(text,
IMinecraft.instance.getDefaultFontRenderer(),
width / 6, height - 7 - 6,
false, 38, 0xFF303030
IMinecraft.instance.getDefaultFontRenderer(),
width / 6, height - 7 - 6,
false, 38, 0xFF303030
);

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

IMinecraft.instance.bindTexture(GuiTextures.RESET);
renderContext.bindTexture(GuiTextures.RESET);
renderContext.color(1, 1, 1, 1);
renderContext.drawTexturedRect(resetX, resetY, 10, 11);
int mouseX = context.getMouseX();
Expand All @@ -72,7 +78,7 @@ public boolean mouseEvent(@NotNull MouseEvent mouseEvent, @NotNull GuiImmediateC
if (click.getMouseState() && click.getMouseButton() != -1 && editingKeycode) {
editingKeycode = false;
int mouseButton = click.getMouseButton();
option.set(mouseButton);
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.
return true;
}

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

@Override
public boolean keyboardEvent(KeyboardEvent keyboardEvent, GuiImmediateContext context) {
boolean wasKeyPressedEvent = keyboardEvent instanceof KeyboardEvent.KeyPressed;
if (wasKeyPressedEvent) {
public boolean keyboardEvent(@NotNull KeyboardEvent keyboardEvent, @NotNull GuiImmediateContext context) {
if (keyboardEvent instanceof KeyboardEvent.KeyPressed) {
var keyPressed = (KeyboardEvent.KeyPressed) keyboardEvent;
if (editingKeycode) {
KeyboardEvent.KeyPressed keyPressed = (KeyboardEvent.KeyPressed) keyboardEvent;
if (keyPressed.getPressed()) return true;
editingKeycode = false;
int keyCode = -1;
int keycode = keyPressed.getKeycode();
if (keycode != 256 /* GLFW_KEY_ESCAPE*/ && keycode != 0) {
keyCode = keycode;
if (keycode == KeyboardConstants.INSTANCE.getEscape() && keycode == 0) {
keycode = KeyboardConstants.INSTANCE.getNone();
}
//if (keyCode > 256) keyCode = 0;
option.set(keyCode);
option.set(keycode);
return true;
} else {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ object ForgeKeyboardConstants : IKeyboardConstants {
get() = Keyboard.KEY_RSHIFT
override val escape: Int
get() = Keyboard.KEY_ESCAPE

override val none: Int
get() = Keyboard.KEY_NONE
override val enter: Int
get() = Keyboard.KEY_RETURN
override val delete: Int
Expand Down Expand Up @@ -45,4 +46,4 @@ object ForgeKeyboardConstants : IKeyboardConstants {
get() = Keyboard.KEY_N
override val keyF: Int
get() = Keyboard.KEY_F
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ object ModernKeyboardConstants : IKeyboardConstants {
get() = InputUtil.GLFW_KEY_RIGHT_SHIFT
override val escape: Int
get() = InputUtil.GLFW_KEY_ESCAPE
override val none: Int
get() = -1
override val enter: Int
get() = InputUtil.GLFW_KEY_ENTER
override val delete: Int
Expand Down Expand Up @@ -44,4 +46,4 @@ object ModernKeyboardConstants : IKeyboardConstants {
get() = InputUtil.GLFW_KEY_N
override val keyF: Int
get() = InputUtil.GLFW_KEY_F
}
}

0 comments on commit ffcff41

Please sign in to comment.