From 38b79b5f976556099944062c42f483d9de2cfc69 Mon Sep 17 00:00:00 2001 From: deirn Date: Wed, 30 Aug 2023 21:42:22 +0700 Subject: [PATCH] make `KeyBinding#isPressed` and `wasPressed` work on screen --- .../client/keybinding/KeyBindingsTest.java | 1 - fabric-screen-api-v1/build.gradle | 2 +- .../fabric/mixin/screen/ScreenMixin.java | 18 +++++++++++++++++- .../src/client/resources/fabric.mod.json | 3 ++- .../fabric/test/screen/ScreenTests.java | 13 +++++++++++++ .../lang/en_us.json | 3 +++ 6 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 fabric-screen-api-v1/src/testmodClient/resources/assets/fabric-screen-api-v1-testmod/lang/en_us.json diff --git a/fabric-key-binding-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/client/keybinding/KeyBindingsTest.java b/fabric-key-binding-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/client/keybinding/KeyBindingsTest.java index 589ce48854..1da1ba7bd6 100644 --- a/fabric-key-binding-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/client/keybinding/KeyBindingsTest.java +++ b/fabric-key-binding-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/client/keybinding/KeyBindingsTest.java @@ -39,7 +39,6 @@ public void onInitializeClient() { KeyBinding duplicateBinding = KeyBindingHelper.registerKeyBinding(new KeyBinding("key.fabric-key-binding-api-v1-testmod.test_keybinding_duplicate", GLFW.GLFW_KEY_RIGHT_SHIFT, "key.category.first.test")); KeyBinding inGameBinding = register("in_game_keybinding", GLFW.GLFW_KEY_EQUAL, "context", KeyBindingContext.IN_GAME); - KeyBinding screenBinding = register("screen_keybinding", GLFW.GLFW_KEY_EQUAL, "context", KeyBindingContext.IN_SCREEN); KeyBinding allBinding = register("all_keybinding", GLFW.GLFW_KEY_BACKSLASH, "context", KeyBindingContext.ALL); // context1 won't conflict with context2 diff --git a/fabric-screen-api-v1/build.gradle b/fabric-screen-api-v1/build.gradle index b1e70a3f58..c5d8aeb4e6 100644 --- a/fabric-screen-api-v1/build.gradle +++ b/fabric-screen-api-v1/build.gradle @@ -1,4 +1,4 @@ archivesBaseName = "fabric-screen-api-v1" version = getSubprojectVersion(project) -moduleDependencies(project, ['fabric-api-base']) +moduleDependencies(project, ['fabric-api-base', 'fabric-key-binding-api-v1']) diff --git a/fabric-screen-api-v1/src/client/java/net/fabricmc/fabric/mixin/screen/ScreenMixin.java b/fabric-screen-api-v1/src/client/java/net/fabricmc/fabric/mixin/screen/ScreenMixin.java index 92483af80d..fb685192ba 100644 --- a/fabric-screen-api-v1/src/client/java/net/fabricmc/fabric/mixin/screen/ScreenMixin.java +++ b/fabric-screen-api-v1/src/client/java/net/fabricmc/fabric/mixin/screen/ScreenMixin.java @@ -26,12 +26,14 @@ import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import net.minecraft.client.gui.Selectable; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.Drawable; import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.option.KeyBinding; +import net.minecraft.client.util.InputUtil; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents; @@ -149,9 +151,23 @@ private void beforeInit(MinecraftClient client, int width, int height) { this.beforeMouseScrollEvent = ScreenEventFactory.createBeforeMouseScrollEvent(); this.afterMouseScrollEvent = ScreenEventFactory.createAfterMouseScrollEvent(); + // Make KeyBinding.isPressed and wasPressed work on screen + // Registered here to make sure it is called first + beforeKeyPressEvent.register((screen, keycode, scancode, modifiers) -> onKeyBind(InputUtil.fromKeyCode(keycode, scancode), true)); + beforeKeyReleaseEvent.register((screen, keycode, scancode, modifiers) -> onKeyBind(InputUtil.fromKeyCode(keycode, scancode), false)); + beforeMouseClickEvent.register((screen, mouseX, mouseY, button) -> onKeyBind(InputUtil.Type.MOUSE.createFromCode(button), true)); + beforeMouseReleaseEvent.register((screen, mouseX, mouseY, button) -> onKeyBind(InputUtil.Type.MOUSE.createFromCode(button), false)); + removeEvent.register(screen -> KeyBinding.unpressAll()); + ScreenEvents.BEFORE_INIT.invoker().beforeInit(client, (Screen) (Object) this, width, height); } + @Unique + private static void onKeyBind(InputUtil.Key key, boolean pressed) { + KeyBinding.setKeyPressed(key, pressed); + if (pressed) KeyBinding.onKeyPressed(key); + } + @Unique private void afterInit(MinecraftClient client, int width, int height) { ScreenEvents.AFTER_INIT.invoker().afterInit(client, (Screen) (Object) this, width, height); diff --git a/fabric-screen-api-v1/src/client/resources/fabric.mod.json b/fabric-screen-api-v1/src/client/resources/fabric.mod.json index c22ea4cfd2..99aca01cf8 100644 --- a/fabric-screen-api-v1/src/client/resources/fabric.mod.json +++ b/fabric-screen-api-v1/src/client/resources/fabric.mod.json @@ -17,7 +17,8 @@ ], "depends": { "fabricloader": ">=0.8.2", - "fabric-api-base": "*" + "fabric-api-base": "*", + "fabric-key-binding-api-v1": "*" }, "description": "Adds screen related hooks.", "mixins": [ diff --git a/fabric-screen-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/screen/ScreenTests.java b/fabric-screen-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/screen/ScreenTests.java index f3a867a156..9bc2e1bbb0 100644 --- a/fabric-screen-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/screen/ScreenTests.java +++ b/fabric-screen-api-v1/src/testmodClient/java/net/fabricmc/fabric/test/screen/ScreenTests.java @@ -18,6 +18,7 @@ import java.util.List; +import org.lwjgl.glfw.GLFW; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -25,9 +26,12 @@ import net.minecraft.client.gui.screen.Screen; import net.minecraft.client.gui.screen.TitleScreen; import net.minecraft.client.gui.widget.ClickableWidget; +import net.minecraft.client.option.KeyBinding; import net.minecraft.util.Identifier; import net.fabricmc.api.ClientModInitializer; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingContext; +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper; import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents; import net.fabricmc.fabric.api.client.screen.v1.Screens; @@ -36,6 +40,8 @@ public final class ScreenTests implements ClientModInitializer { public static final Identifier GUI_ICONS_TEXTURE = new Identifier("textures/gui/icons.png"); private static final Logger LOGGER = LoggerFactory.getLogger("FabricScreenApiTests"); + private static final KeyBinding SCREEN_BINDING = new KeyBinding("key.fabric-screen-api-v1-testmod.screen_keybinding", GLFW.GLFW_KEY_EQUAL, "key.category.context"); + @Override public void onInitializeClient() { LOGGER.info("Started Screen Testmod"); @@ -44,6 +50,7 @@ public void onInitializeClient() { }); ScreenEvents.AFTER_INIT.register(this::afterInitScreen); + KeyBindingHelper.registerKeyBinding(SCREEN_BINDING, KeyBindingContext.IN_SCREEN); } private void afterInitScreen(MinecraftClient client, Screen screen, int windowWidth, int windowHeight) { @@ -88,5 +95,11 @@ private void afterInitScreen(MinecraftClient client, Screen screen, int windowWi LOGGER.warn("Pressed, Code: {}, Scancode: {}, Modifiers: {}", key, scancode, modifiers); }); } + + ScreenKeyboardEvents.beforeKeyPress(screen).register((screen1, key, scancode, modifiers) -> { + if (SCREEN_BINDING.isPressed()) { + LOGGER.info("Screen key binding was pressed on screen {}!", screen.getClass().getSimpleName()); + } + }); } } diff --git a/fabric-screen-api-v1/src/testmodClient/resources/assets/fabric-screen-api-v1-testmod/lang/en_us.json b/fabric-screen-api-v1/src/testmodClient/resources/assets/fabric-screen-api-v1-testmod/lang/en_us.json new file mode 100644 index 0000000000..94dfbe6916 --- /dev/null +++ b/fabric-screen-api-v1/src/testmodClient/resources/assets/fabric-screen-api-v1-testmod/lang/en_us.json @@ -0,0 +1,3 @@ +{ + "key.fabric-screen-api-v1-testmod.screen_keybinding": "In Screen" +}