Skip to content

Commit

Permalink
keyboard mash
Browse files Browse the repository at this point in the history
  • Loading branch information
EnnuiL committed Aug 27, 2022
1 parent e53ae27 commit ffd84e5
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,36 @@
import org.quiltmc.qsl.base.api.util.InjectedInterface;
import org.quiltmc.qsl.key.binds.impl.chords.KeyChord;

// TODO - Add Javadocs
// TODO - Explain what are key chords.
/**
* An interface that adds key chord support to key binds.
*/
@Environment(EnvType.CLIENT)
@InjectedInterface(KeyBind.class)
public interface ChordedKeyBind {
/**
* Gets the bound key chord of the key bind.
*
* @return the key bind's bound key chord.
*/
default KeyChord getBoundChord() {
throw new UnsupportedOperationException();
}

/**
* Sets the bound key chord of the key bind.
*/
default void setBoundChord(KeyChord chord) { }

// TODO - This is a temporary measure until CHASM comes. Replace it with a proper constructor or builder
// TODO - This is a temporary measure until Chasm arrives. Replace it with a proper constructor or builder
/**
* Specifies the default key chord for the key bind.
*
* <p>This method is to be used only on creating a key bind instance.
*
* @param keys the keys of the default key chord
* @return the original key bind instance
*/
default KeyBind withChord(InputUtil.Key... keys) {
throw new UnsupportedOperationException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

import com.mojang.blaze3d.platform.InputUtil;

/**
* An injected interface for KeyBind that adds few Quilt-relevant utility methods.
*/
public interface QuiltKeyBind {
/**
* Gets whenever the key bind is from Vanilla or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.quiltmc.qsl.base.api.util.InjectedInterface;

// TODO - Add Javadocs; You can nab the ones from KeyBindRegistry
/**
* An interface for adding toggling capabilities to key binds.
*/
@Environment(EnvType.CLIENT)
@InjectedInterface(KeyBind.class)
public interface ToggleableKeyBind {
Expand All @@ -45,6 +48,15 @@ default boolean isDisabled() {
return false;
}

/**
* Gets whenever the key bind can be disabled or not.
*
* @return {@code true} if the key bind can be disabled, {@code false} otherwise
*/
default boolean canDisable() {
return false;
}

/**
* Enables the key bind.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package org.quiltmc.qsl.key.binds.impl;

import org.jetbrains.annotations.ApiStatus;

@ApiStatus.Internal
public interface InternalQuiltKeyBind {
void markAsVanilla();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@

import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
import org.jetbrains.annotations.ApiStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

Expand All @@ -30,8 +28,6 @@
@Environment(EnvType.CLIENT)
@ApiStatus.Internal
public class KeyBindRegistryImpl {
public static final Logger LOGGER = LoggerFactory.getLogger("KeyBindRegistry");

private static final List<KeyBind> ALL_KEY_BINDS = new ReferenceArrayList<>();
private static final List<KeyBind> ENABLED_KEYS = new ReferenceArrayList<>();
private static KeyBindManager keyBindManager = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@

import java.util.SortedMap;

import com.mojang.blaze3d.platform.InputUtil;
import it.unimi.dsi.fastutil.objects.Object2BooleanAVLTreeMap;

import com.mojang.blaze3d.platform.InputUtil;

public class KeyChord {
// TODO - Private this, add methods for getting/modifying it
public SortedMap<InputUtil.Key, Boolean> keys = new Object2BooleanAVLTreeMap<>();
Expand All @@ -44,6 +45,6 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
return keys.keySet().hashCode();
return this.keys.keySet().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ public abstract class KeyBindEntryMixin extends KeyBindListWidget.Entry implemen

@Inject(method = "<init>", at = @At("TAIL"))
private void initPreviousBoundKey(KeyBindListWidget list, KeyBind key, Text text, CallbackInfo ci) {
quilt$previousProtoChord = null;
this.quilt$previousProtoChord = null;
quilt$changedProtoChord = null;
quilt$addKeyNameToTooltip = false;
this.quilt$addKeyNameToTooltip = false;
}

@Inject(
Expand Down Expand Up @@ -108,7 +108,7 @@ private void collectConflictTooltips(MatrixStack matrices, int index, int y, int
quilt$changedProtoChord = boundProtoChord;
}

quilt$addKeyNameToTooltip = true;
this.quilt$addKeyNameToTooltip = true;

if (!this.key.isUnbound()) {
for (KeyBind otherKey : KeyBindRegistryImpl.getKeyBinds()) {
Expand Down Expand Up @@ -167,9 +167,9 @@ private void shortenText(MatrixStack matrices, int index, int y, int x, int entr
}

if (client.textRenderer.getWidth(protoText) > targetWidth) {
if (quilt$addKeyNameToTooltip) {
if (this.quilt$addKeyNameToTooltip) {
this.quilt$conflictTooltips.add(0, this.key.getKeyName());
quilt$addKeyNameToTooltip = false;
this.quilt$addKeyNameToTooltip = false;
}

protoText = client.textRenderer.trimToWidth(protoText, targetWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.Map;

import com.mojang.blaze3d.platform.InputUtil;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
Expand All @@ -30,6 +29,8 @@
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;

import com.mojang.blaze3d.platform.InputUtil;

import net.minecraft.client.option.KeyBind;

import org.quiltmc.qsl.key.binds.api.QuiltKeyBind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package org.quiltmc.qsl.key.binds.mixin.client.chords;

import com.mojang.blaze3d.platform.InputUtil;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import com.mojang.blaze3d.platform.InputUtil;

@Mixin(InputUtil.Key.class)
public abstract class InputUtilKeyMixin implements Comparable<InputUtil.Key> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,36 +67,44 @@ public class KeyBindMixin implements ChordedKeyBind {
method = "<init>(Ljava/lang/String;Lcom/mojang/blaze3d/platform/InputUtil$Type;ILjava/lang/String;)V"
)
private void initializeChordFields(String string, InputUtil.Type type, int i, String string2, CallbackInfo ci) {
quilt$defaultChord = null;
quilt$boundChord = null;
this.quilt$defaultChord = null;
this.quilt$boundChord = null;
}

@Inject(at = @At("HEAD"), method = "onKeyPressed")
@Inject(at = @At("HEAD"), method = "onKeyPressed", cancellable = true)
private static void detectChordsOnIncrement(InputUtil.Key startingKey, CallbackInfo ci) {
boolean cancel = false;
for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) {
if (chord.keys.containsKey(startingKey) && !chord.keys.containsValue(false)) {
// This ensures that the chord will only be incremented once instead of N times
if (startingKey.equals(chord.keys.keySet().toArray()[0])) {
KeyBind keyBind = KEY_BINDS_BY_CHORD.get(chord);
((KeyBindAccessor) keyBind).setTimesPressed(((KeyBindAccessor) keyBind).getTimesPressed() + 1);
cancel = true;
}
}
}

if (cancel) ci.cancel();
}

@Inject(at = @At("HEAD"), method = "setKeyPressed")
@Inject(at = @At("HEAD"), method = "setKeyPressed", cancellable = true)
private static void detectChordsOnSet(InputUtil.Key startingKey, boolean pressed, CallbackInfo ci) {
boolean cancel = false;
for (KeyChord chord : KEY_BINDS_BY_CHORD.keySet()) {
if (chord.keys.containsKey(startingKey)) {
chord.keys.put(startingKey, pressed);

if (!chord.keys.containsValue(false)) {
KEY_BINDS_BY_CHORD.get(chord).setPressed(true);
cancel = true;
} else {
KEY_BINDS_BY_CHORD.get(chord).setPressed(false);
}
}
}

if (cancel) ci.cancel();
}

@Inject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ private void initializeProtoChord(CallbackInfo ci) {
)
private void modifyMouseClicked(double mouseX, double mouseY, int button, CallbackInfoReturnable<Boolean> cir) {
InputUtil.Key key = InputUtil.Type.MOUSE.createFromKeyCode(button);
if (!quilt$focusedProtoChord.contains(key)) {
quilt$focusedProtoChord.add(key);
if (!this.quilt$focusedProtoChord.contains(key)) {
this.quilt$focusedProtoChord.add(key);
}

cir.setReturnValue(true);
Expand All @@ -101,8 +101,8 @@ private void excludeFirstMouseClick(double mouseX, double mouseY, int button, Ca
)
private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
InputUtil.Key key = InputUtil.fromKeyCode(keyCode, scanCode);
if (!quilt$focusedProtoChord.contains(key)) {
quilt$focusedProtoChord.add(key);
if (!this.quilt$focusedProtoChord.contains(key)) {
this.quilt$focusedProtoChord.add(key);
}

cir.setReturnValue(true);
Expand All @@ -111,19 +111,19 @@ private void modifyKeyPressed(int keyCode, int scanCode, int modifiers, Callback
@Override
public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
if (this.focusedKey != null) {
if (quilt$focusedProtoChord.size() == 1) {
this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0));
} else if (quilt$focusedProtoChord.size() > 1) {
if (this.quilt$focusedProtoChord.size() == 1) {
this.gameOptions.setKeyCode(this.focusedKey, this.quilt$focusedProtoChord.get(0));
} else if (this.quilt$focusedProtoChord.size() > 1) {
SortedMap<InputUtil.Key, Boolean> map = new Object2BooleanAVLTreeMap<>();
for (int i = 0; i < quilt$focusedProtoChord.size(); i++) {
map.put(quilt$focusedProtoChord.get(i), false);
for (int i = 0; i < this.quilt$focusedProtoChord.size(); i++) {
map.put(this.quilt$focusedProtoChord.get(i), false);
}

this.focusedKey.setBoundChord(new KeyChord(map));
QuiltKeyBindsConfigManager.updateConfig(false);
}

quilt$focusedProtoChord.clear();
this.quilt$focusedProtoChord.clear();
this.focusedKey = null;
this.time = Util.getMeasuringTimeMs();
KeyBind.updateBoundKeys();
Expand All @@ -137,19 +137,19 @@ public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
public boolean mouseReleased(double mouseX, double mouseY, int button) {
// TODO - Don't duplicate code, have a common method
if (this.focusedKey != null && !this.quilt$initialMouseRelease) {
if (quilt$focusedProtoChord.size() == 1) {
this.gameOptions.setKeyCode(this.focusedKey, quilt$focusedProtoChord.get(0));
} else if (quilt$focusedProtoChord.size() > 1) {
if (this.quilt$focusedProtoChord.size() == 1) {
this.gameOptions.setKeyCode(this.focusedKey, this.quilt$focusedProtoChord.get(0));
} else if (this.quilt$focusedProtoChord.size() > 1) {
SortedMap<InputUtil.Key, Boolean> map = new Object2BooleanAVLTreeMap<>();
for (int i = 0; i < quilt$focusedProtoChord.size(); i++) {
map.put(quilt$focusedProtoChord.get(i), false);
for (int i = 0; i < this.quilt$focusedProtoChord.size(); i++) {
map.put(this.quilt$focusedProtoChord.get(i), false);
}

this.focusedKey.setBoundChord(new KeyChord(map));
QuiltKeyBindsConfigManager.updateConfig(false);
}

quilt$focusedProtoChord.clear();
this.quilt$focusedProtoChord.clear();
this.focusedKey = null;
this.time = Util.getMeasuringTimeMs();
KeyBind.updateBoundKeys();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;
import java.util.Map;

import com.mojang.blaze3d.platform.InputUtil;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -29,6 +28,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.mojang.blaze3d.platform.InputUtil;

import net.minecraft.client.option.KeyBind;

import org.quiltmc.qsl.key.binds.api.ToggleableKeyBind;
Expand Down Expand Up @@ -63,26 +64,31 @@ private void initializeToggleFields(String string, InputUtil.Type type, int i, S
}

KeyBindRegistryImpl.registerKeyBind((KeyBind) (Object) this);
quilt$disableCounter = 0;
this.quilt$disableCounter = 0;
}

@Override
public boolean isEnabled() {
return quilt$disableCounter == 0;
return this.quilt$disableCounter == 0;
}

@Override
public boolean isDisabled() {
return quilt$disableCounter > 0;
return this.quilt$disableCounter > 0;
}

@Override
public boolean canDisable() {
return true;
}

@Override
public void enable() {
// Hahahahaha no.
if (quilt$disableCounter <= 0) return;
if (this.quilt$disableCounter <= 0) return;

quilt$disableCounter--;
if (quilt$disableCounter == 0) {
this.quilt$disableCounter--;
if (this.quilt$disableCounter == 0) {
KeyBindRegistryImpl.updateKeyBindState((KeyBind) (Object) this);
KEY_BINDS.put(this.getTranslationKey(), (KeyBind) (Object) this);
KeyBind.updateBoundKeys();
Expand All @@ -92,8 +98,8 @@ public void enable() {

@Override
public void disable() {
quilt$disableCounter++;
if (quilt$disableCounter == 1) {
this.quilt$disableCounter++;
if (this.quilt$disableCounter == 1) {
KeyBindRegistryImpl.updateKeyBindState((KeyBind) (Object) this);
KEY_BINDS.remove(this.getTranslationKey(), (KeyBind) (Object) this);
KeyBind.updateBoundKeys();
Expand Down

0 comments on commit ffd84e5

Please sign in to comment.