From 7339ad031399cb5db82676956a437cee4750430d Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Wed, 13 Sep 2023 23:04:54 +0200 Subject: [PATCH] feat: Add context-aware keybindings support for Fabric https://github.com/TwelveIterationMods/CraftingTweaks/issues/191 --- .../keymappings/FabricBalmKeyMappings.java | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/client/keymappings/FabricBalmKeyMappings.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/client/keymappings/FabricBalmKeyMappings.java index d90e35bd..fb520bd6 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/client/keymappings/FabricBalmKeyMappings.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/client/keymappings/FabricBalmKeyMappings.java @@ -10,8 +10,13 @@ import net.minecraft.client.Minecraft; import org.jetbrains.annotations.Nullable; +import java.util.HashMap; +import java.util.Map; + public class FabricBalmKeyMappings implements BalmKeyMappings { + private final Map contextAwareKeyMappings = new HashMap<>(); + @Override public KeyMapping registerKeyMapping(String name, int keyCode, String category) { return KeyBindingHelper.registerKeyBinding(new KeyMapping(name, InputConstants.Type.KEYSYM, keyCode, category)); @@ -24,12 +29,16 @@ public KeyMapping registerKeyMapping(String name, InputConstants.Type type, int @Override public KeyMapping registerKeyMapping(String name, KeyConflictContext conflictContext, KeyModifier modifier, int keyCode, String category) { - return KeyBindingHelper.registerKeyBinding(new KeyMapping(name, InputConstants.Type.KEYSYM, keyCode, category)); + KeyMapping keyMapping = KeyBindingHelper.registerKeyBinding(new KeyMapping(name, InputConstants.Type.KEYSYM, keyCode, category)); + contextAwareKeyMappings.put(keyMapping, conflictContext); + return keyMapping; } @Override public KeyMapping registerKeyMapping(String name, KeyConflictContext conflictContext, KeyModifier modifier, InputConstants.Type type, int keyCode, String category) { - return KeyBindingHelper.registerKeyBinding(new KeyMapping(name, type, keyCode, category)); + KeyMapping keyBinding = new KeyMapping(name, type, keyCode, category); + contextAwareKeyMappings.put(keyBinding, conflictContext); + return KeyBindingHelper.registerKeyBinding(keyBinding); } @Override @@ -38,6 +47,11 @@ public boolean isActiveAndMatches(@Nullable KeyMapping keyMapping, InputConstant return false; } + KeyConflictContext conflictContext = contextAwareKeyMappings.getOrDefault(keyMapping, KeyConflictContext.UNIVERSAL); + if (!isContextActive(conflictContext)) { + return false; + } + return input.getType() == InputConstants.Type.MOUSE ? keyMapping.matchesMouse(input.getValue()) : keyMapping.matches(input.getType() == InputConstants.Type.KEYSYM ? input.getValue() : InputConstants.UNKNOWN.getValue(), @@ -46,7 +60,16 @@ public boolean isActiveAndMatches(@Nullable KeyMapping keyMapping, InputConstant @Override public boolean isActiveAndMatches(@Nullable KeyMapping keyMapping, int keyCode, int scanCode) { - return keyMapping != null && keyMapping.matches(keyCode, scanCode); + if (keyMapping == null) { + return false; + } + + KeyConflictContext conflictContext = contextAwareKeyMappings.getOrDefault(keyMapping, KeyConflictContext.UNIVERSAL); + if (!isContextActive(conflictContext)) { + return false; + } + + return keyMapping.matches(keyCode, scanCode); } @Override @@ -55,12 +78,26 @@ public boolean isActiveAndMatches(@Nullable KeyMapping keyMapping, InputConstant return false; } + KeyConflictContext conflictContext = contextAwareKeyMappings.getOrDefault(keyMapping, KeyConflictContext.UNIVERSAL); + if (!isContextActive(conflictContext)) { + return false; + } + return type == InputConstants.Type.MOUSE ? keyMapping.matchesMouse(keyCode) : keyMapping.matches(keyCode, scanCode); } @Override public boolean isActiveAndWasPressed(@Nullable KeyMapping keyMapping) { - return keyMapping != null && keyMapping.consumeClick(); + if (keyMapping == null) { + return false; + } + + KeyConflictContext conflictContext = contextAwareKeyMappings.getOrDefault(keyMapping, KeyConflictContext.UNIVERSAL); + if (!isContextActive(conflictContext)) { + return false; + } + + return keyMapping.consumeClick(); } private boolean isActiveAndMatchesStrictModifier(@Nullable KeyMapping keyMapping, int keyCode, int scanCode) { @@ -70,6 +107,11 @@ private boolean isActiveAndMatchesStrictModifier(@Nullable KeyMapping keyMapping } }*/ + KeyConflictContext conflictContext = contextAwareKeyMappings.getOrDefault(keyMapping, KeyConflictContext.UNIVERSAL); + if (!isContextActive(conflictContext)) { + return false; + } + return keyMapping != null && keyMapping.matches(keyCode, scanCode); } @@ -91,10 +133,22 @@ public boolean isActiveAndKeyDown(@Nullable KeyMapping keyMapping) { return false; } + KeyConflictContext conflictContext = contextAwareKeyMappings.getOrDefault(keyMapping, KeyConflictContext.UNIVERSAL); + if (!isContextActive(conflictContext)) { + return false; + } + InputConstants.Key key = ((KeyMappingAccessor) keyMapping).getKey(); return keyMapping.isDown() || (key.getValue() != -1 && key.getType() == InputConstants.Type.KEYSYM && InputConstants.isKeyDown(Minecraft.getInstance() .getWindow() .getWindow(), key.getValue())); } + private boolean isContextActive(KeyConflictContext conflictContext) { + return switch (conflictContext) { + case GUI -> Minecraft.getInstance().screen != null; + case INGAME -> Minecraft.getInstance().screen == null; + default -> true; + }; + } }