11package io .github .notenoughupdates .moulconfig .gui .editors ;
22
33import io .github .notenoughupdates .moulconfig .GuiTextures ;
4+ import io .github .notenoughupdates .moulconfig .annotations .ConfigEditorKeybind ;
45import io .github .notenoughupdates .moulconfig .common .IMinecraft ;
6+ import io .github .notenoughupdates .moulconfig .common .KeyboardConstants ;
57import io .github .notenoughupdates .moulconfig .common .RenderContext ;
68import io .github .notenoughupdates .moulconfig .gui .GuiComponent ;
79import io .github .notenoughupdates .moulconfig .gui .GuiImmediateContext ;
810import io .github .notenoughupdates .moulconfig .gui .KeyboardEvent ;
911import io .github .notenoughupdates .moulconfig .gui .MouseEvent ;
12+ import io .github .notenoughupdates .moulconfig .internal .Warnings ;
1013import io .github .notenoughupdates .moulconfig .processor .ProcessedOption ;
14+ import lombok .var ;
1115import org .jetbrains .annotations .NotNull ;
1216
1317import 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 ;
0 commit comments