Skip to content

Commit

Permalink
Added hotkey utils methods (#599)
Browse files Browse the repository at this point in the history
Co-authored-by: slprime <[email protected]>
  • Loading branch information
slprime and slprime authored Jan 26, 2025
1 parent 48934d6 commit ac50d1b
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 68 deletions.
27 changes: 24 additions & 3 deletions src/main/java/codechicken/nei/NEIClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import codechicken.nei.recipe.IRecipeHandler;
import codechicken.nei.recipe.RecipeCatalysts;
import codechicken.nei.recipe.RecipeInfo;
import codechicken.nei.util.NEIKeyboardUtils;
import codechicken.obfuscator.ObfuscationRun;

public class NEIClientConfig {
Expand Down Expand Up @@ -789,7 +790,7 @@ public static void unloadWorld() {
private static final Map<String, String> keySettings = new HashMap<>();

public static int getKeyBinding(String string) {
final String key = keySettings.computeIfAbsent(string, (s) -> "keys." + s);
final String key = keySettings.computeIfAbsent(string, s -> "keys." + s);
return getSetting(key).getIntValue(Keyboard.KEY_NONE);
}

Expand All @@ -799,17 +800,37 @@ public static void setDefaultKeyBinding(String string, int key) {

public static boolean isKeyHashDown(String string) {
final int hash = getKeyBinding(string);
return hash != Keyboard.CHAR_NONE && hash == NEIClientUtils.getKeyHash();

if (hash != Keyboard.CHAR_NONE && Keyboard.getEventKeyState()) {
return KeyManager.keyStates.containsKey(string) ? hash == Keyboard.getEventKey()
: hash == NEIClientUtils.getKeyHash();
}

return false;
}

public static String getKeyName(String keyBind) {
return getKeyName(keyBind, 0);
}

public static String getKeyName(String keyBind, int meta, int mouseBind) {
final int hash = getKeyBinding(keyBind);

if (hash == Keyboard.CHAR_NONE) {
return null;
}

return NEIClientUtils.getKeyName(hash + meta, mouseBind);
}

public static String getKeyName(String keyBind, int meta) {
final int hash = getKeyBinding(keyBind);

if (hash == Keyboard.CHAR_NONE) {
return null;
}

return NEIClientUtils.getKeyName(hash);
return NEIKeyboardUtils.getKeyName(hash + meta);
}

public static void bootNEI(World world) {
Expand Down
62 changes: 13 additions & 49 deletions src/main/java/codechicken/nei/NEIClientUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,32 +50,19 @@
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.IInfiniteItemHandler;
import codechicken.nei.api.ItemInfo;
import codechicken.nei.util.NEIKeyboardUtils;
import codechicken.nei.util.NEIMouseUtils;

public class NEIClientUtils extends NEIServerUtils {

public static LangProxy lang = new LangProxy("nei");

/** Formats a number with group separator and at most 2 fraction digits. */
private static final Map<Locale, DecimalFormat> decimalFormatters = new HashMap<>();
private static final Map<Integer, Integer> keyHashMap = new HashMap<>();

public static final int ALT_HASH = 1 << 27;
public static final int SHIFT_HASH = 1 << 26;
public static final int CTRL_HASH = 1 << 25;

static {
keyHashMap.put(Keyboard.KEY_LSHIFT, SHIFT_HASH);
keyHashMap.put(Keyboard.KEY_RSHIFT, SHIFT_HASH);

keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);

keyHashMap.put(Keyboard.KEY_LMETA, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_RMETA, CTRL_HASH);

keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
}
public static final int ALT_HASH = NEIKeyboardUtils.ALT_HASH;
public static final int SHIFT_HASH = NEIKeyboardUtils.SHIFT_HASH;
public static final int CTRL_HASH = NEIKeyboardUtils.CTRL_HASH;

public static Minecraft mc() {
return Minecraft.getMinecraft();
Expand Down Expand Up @@ -452,53 +439,30 @@ public static int getKeyHash() {
if (Keyboard.getEventKeyState()) {
final int keycode = Keyboard.getEventKey();

if (!keyHashMap.containsKey(keycode)) {
if (!NEIKeyboardUtils.isHashKey(keycode)) {
return getMetaHash() + keycode;
}
}

return Keyboard.CHAR_NONE;
}

public static String getKeyHashName(int keyBind) {
StringJoiner keyText = new StringJoiner(" + ");

if ((keyBind & CTRL_HASH) != 0) {
keyText.add(translate(Minecraft.isRunningOnMac ? "key.ctrl.mac" : "key.ctrl"));
}

if ((keyBind & SHIFT_HASH) != 0) {
keyText.add(translate("key.shift"));
}

if ((keyBind & ALT_HASH) != 0) {
keyText.add(translate("key.alt"));
}

return keyText.toString();
}

public static String getKeyName(int keyBind) {
keyBind = keyHashMap.getOrDefault(keyBind, keyBind);
public static String getKeyName(int keyBind, int mouseBind) {
StringJoiner keyText = new StringJoiner(" + ");
String hashText = getKeyHashName(keyBind);
int keyID = unHashKey(keyBind);
String keyHash = keyBind == Keyboard.KEY_NONE ? "" : NEIKeyboardUtils.getKeyName(keyBind);
String mouseHash = mouseBind == NEIMouseUtils.MOUSE_BTN_NONE ? "" : NEIMouseUtils.getKeyName(mouseBind);

if (!hashText.isEmpty()) {
keyText.add(hashText);
if (!keyHash.isEmpty()) {
keyText.add(keyHash);
}

if (keyID != Keyboard.CHAR_NONE || hashText.isEmpty()) {
keyText.add(Keyboard.getKeyName(keyID));
if (!mouseHash.isEmpty()) {
keyText.add(mouseHash);
}

return keyText.toString();
}

public static int unHashKey(int keyBind) {
return keyBind & ~(CTRL_HASH | SHIFT_HASH | ALT_HASH);
}

public static void playClickSound() {
mc().getSoundHandler()
.playSound(PositionedSoundRecord.func_147674_a(new ResourceLocation("gui.button.press"), 1.0F));
Expand Down
19 changes: 12 additions & 7 deletions src/main/java/codechicken/nei/SubsetWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import codechicken.nei.api.ItemFilter.ItemFilterProvider;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.guihook.IContainerTooltipHandler;
import codechicken.nei.util.NEIMouseUtils;

public class SubsetWidget extends Button implements ItemFilterProvider, IContainerTooltipHandler {

Expand Down Expand Up @@ -841,21 +842,21 @@ public Map<String, String> handleHotkeys(GuiContainer gui, int mousex, int mouse

if (NEIClientConfig.canCheatItem(SubsetWidget.hoverStack)) {
hotkeys.put(
NEIClientUtils.translate("subsets.item.cheat.key"),
NEIClientUtils.getKeyName(NEIClientUtils.CTRL_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.item.cheat"));
}

if (SubsetWidget.enableSearchBySubsets) {
hotkeys.put(
NEIClientUtils.translate("subsets.item.search.key"),
NEIClientUtils.getKeyName(NEIClientUtils.SHIFT_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.item.search"));
}

hotkeys.put(
NEIClientUtils.translate("subsets.item.show.key"),
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.item.show"));
hotkeys.put(
NEIClientUtils.translate("subsets.item.hide.key"),
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_RMB),
NEIClientUtils.translate("subsets.item.hide"));
} else if (SubsetWidget.hoverTag != null) {
hotkeys.put(
Expand All @@ -864,12 +865,16 @@ public Map<String, String> handleHotkeys(GuiContainer gui, int mousex, int mouse

if (SubsetWidget.enableSearchBySubsets) {
hotkeys.put(
NEIClientUtils.translate("subsets.tag.search.key"),
NEIClientUtils.getKeyName(NEIClientUtils.SHIFT_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.tag.search"));
}

hotkeys.put(NEIClientUtils.translate("subsets.tag.show.key"), NEIClientUtils.translate("subsets.tag.show"));
hotkeys.put(NEIClientUtils.translate("subsets.tag.hide.key"), NEIClientUtils.translate("subsets.tag.hide"));
hotkeys.put(
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate("subsets.tag.show"));
hotkeys.put(
NEIMouseUtils.getKeyName(NEIMouseUtils.MOUSE_BTN_RMB),
NEIClientUtils.translate("subsets.tag.hide"));
}

return hotkeys;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/codechicken/nei/config/OptionKeyBind.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.lwjgl.input.Keyboard;

import codechicken.nei.NEIClientUtils;
import codechicken.nei.util.NEIKeyboardUtils;

public class OptionKeyBind extends OptionButton {

Expand Down Expand Up @@ -123,10 +124,10 @@ public void drawButton(int mx, int my) {
public String getButtonText() {

if (editing) {
return NEIClientUtils.getKeyHashName(NEIClientUtils.getMetaHash());
return NEIKeyboardUtils.getHashName(NEIClientUtils.getMetaHash());
}

return NEIClientUtils.getKeyName(Math.max(getValue(), 0));
return NEIKeyboardUtils.getKeyName(Math.max(getValue(), 0));
}

@Override
Expand Down
80 changes: 80 additions & 0 deletions src/main/java/codechicken/nei/util/NEIKeyboardUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package codechicken.nei.util;

import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;

import net.minecraft.client.Minecraft;

import org.lwjgl.input.Keyboard;

import codechicken.nei.NEIClientUtils;

public class NEIKeyboardUtils {

public static final int ALT_HASH = 1 << 27;
public static final int SHIFT_HASH = 1 << 26;
public static final int CTRL_HASH = 1 << 25;

private static final Map<Integer, Integer> keyHashMap = new HashMap<>();

static {
keyHashMap.put(Keyboard.KEY_LSHIFT, SHIFT_HASH);
keyHashMap.put(Keyboard.KEY_RSHIFT, SHIFT_HASH);

keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_LCONTROL, CTRL_HASH);

keyHashMap.put(Keyboard.KEY_LMETA, CTRL_HASH);
keyHashMap.put(Keyboard.KEY_RMETA, CTRL_HASH);

keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
keyHashMap.put(Keyboard.KEY_LMENU, ALT_HASH);
}

private NEIKeyboardUtils() {}

public static boolean isHashKey(int keycode) {
return keyHashMap.containsKey(keycode);
}

public static String getHashName(int keyBind) {
StringJoiner keyText = new StringJoiner(" + ");

if ((keyBind & CTRL_HASH) != 0) {
keyText.add(NEIClientUtils.translate(Minecraft.isRunningOnMac ? "key.ctrl.mac" : "key.ctrl"));
}

if ((keyBind & SHIFT_HASH) != 0) {
keyText.add(NEIClientUtils.translate("key.shift"));
}

if ((keyBind & ALT_HASH) != 0) {
keyText.add(NEIClientUtils.translate("key.alt"));
}

return keyText.toString();
}

public static String getKeyName(int keyBind) {
keyBind = keyHashMap.getOrDefault(keyBind, keyBind);
StringJoiner keyText = new StringJoiner(" + ");
String hashText = getHashName(keyBind);
int keyID = unhash(keyBind);

if (!hashText.isEmpty()) {
keyText.add(hashText);
}

if (keyID != Keyboard.CHAR_NONE || hashText.isEmpty()) {
keyText.add(Keyboard.getKeyName(keyID));
}

return keyText.toString();
}

public static int unhash(int keyBind) {
return keyBind & ~(CTRL_HASH | SHIFT_HASH | ALT_HASH);
}

}
67 changes: 67 additions & 0 deletions src/main/java/codechicken/nei/util/NEIMouseUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package codechicken.nei.util;

import java.util.StringJoiner;

import codechicken.nei.NEIClientUtils;

public class NEIMouseUtils {

public static final int MOUSE_BTN_NONE = Integer.MIN_VALUE;

public static final int MOUSE_BTN_LMB = 0;
public static final int MOUSE_BTN_RMB = 1;
public static final int MOUSE_BTN_MMB = 2;

public static final int MOUSE_SCROLL = 1 << 27;
public static final int MOUSE_DRAG = 1 << 26;

private NEIMouseUtils() {}

public static String getHashName(int mouseBind) {
StringJoiner mouseText = new StringJoiner(" + ");

if ((mouseBind & MOUSE_SCROLL) != 0) {
mouseText.add(NEIClientUtils.translate("mouse.scroll"));
}

if ((mouseBind & MOUSE_DRAG) != 0) {
mouseText.add(NEIClientUtils.translate("mouse.drag"));
}

return mouseText.toString();
}

public static String getKeyName(int mouseBind) {
StringJoiner mouseText = new StringJoiner(" + ");
String hashText = getHashName(mouseBind < 0 ? mouseBind - MOUSE_BTN_NONE : mouseBind);

if (mouseBind >= 0) {
int button = unhash(mouseBind);
switch (button) {
case 0:
mouseText.add(NEIClientUtils.translate("mouse.left"));
break;
case 1:
mouseText.add(NEIClientUtils.translate("mouse.right"));
break;
case 2:
mouseText.add(NEIClientUtils.translate("mouse.middle"));
break;
default:
mouseText.add(NEIClientUtils.translate("mouse.other", button));
break;
}
}

if (!hashText.isEmpty()) {
mouseText.add(hashText);
}

return mouseText.toString();
}

public static int unhash(int mouseBind) {
return mouseBind & ~(MOUSE_SCROLL | MOUSE_DRAG);
}

}
Loading

0 comments on commit ac50d1b

Please sign in to comment.