Skip to content

Commit

Permalink
make KeyBinding#isPressed and wasPressed work on screen
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Aug 30, 2023
1 parent 901c458 commit 38b79b5
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion fabric-screen-api-v1/build.gradle
Original file line number Diff line number Diff line change
@@ -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'])
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion fabric-screen-api-v1/src/client/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,20 @@

import java.util.List;

import org.lwjgl.glfw.GLFW;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.minecraft.client.MinecraftClient;
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;
Expand All @@ -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");
Expand All @@ -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) {
Expand Down Expand Up @@ -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());
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key.fabric-screen-api-v1-testmod.screen_keybinding": "In Screen"
}

0 comments on commit 38b79b5

Please sign in to comment.