Skip to content

Commit

Permalink
feat: Add context-aware keybindings support for Fabric TwelveIteratio…
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Sep 13, 2023
1 parent dbe234c commit 7339ad0
Showing 1 changed file with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyMapping, KeyConflictContext> contextAwareKeyMappings = new HashMap<>();

@Override
public KeyMapping registerKeyMapping(String name, int keyCode, String category) {
return KeyBindingHelper.registerKeyBinding(new KeyMapping(name, InputConstants.Type.KEYSYM, keyCode, category));
Expand All @@ -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
Expand All @@ -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(),
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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;
};
}
}

0 comments on commit 7339ad0

Please sign in to comment.