Added keyboard shortcuts to NEI And More... (#271)

* Added keyboard shortcuts to NEI

* OptionButton: Add Tooltip to long prefix

* fix formatting

* Tooltip shown when shift is held
This commit is contained in:
slprime authored and GitHub committed 2022-08-19 14:03:42 +02:00
1 parent 529f4374d6
commit fb9d517f78
30 files changed
+756 -306

No files matched your search

@@ -1,46 +1,94 @@
package codechicken.nei.config;
import static codechicken.lib.gui.GuiDraw.drawRect;
import static codechicken.lib.gui.GuiDraw.drawStringC;
import codechicken.nei.NEIClientUtils;
import java.awt.Rectangle;
import net.minecraft.client.Minecraft;
import net.minecraft.util.EnumChatFormatting;
import org.lwjgl.input.Keyboard;
public class OptionKeyBind extends OptionButton {
private boolean hasFocus = false;
private boolean editing = false;
private boolean useHash = false;
private int keyboard = Keyboard.CHAR_NONE;
public OptionKeyBind(String name) {
this(name, false);
}
public OptionKeyBind(final String name, final boolean useHash) {
super("keys." + name);
this.useHash = useHash;
}
@Override
public void onMouseClicked(int mousex, int mousey, int button) {
hasFocus = false;
if (editing) {
resetChanges();
}
}
@Override
public void keyTyped(char c, int keycode) {
if (hasFocus) {
setValue(keycode);
hasFocus = false;
if (!editing) {
return;
}
if (keycode == Keyboard.KEY_RETURN) {
applyChanges();
return;
}
keyboard = useHash ? NEIClientUtils.getKeyHash() : keycode;
if (keyboard != Keyboard.CHAR_NONE) {
applyChanges();
}
}
@Override
public boolean onClick(int button) {
if (button == 0) {
hasFocus = true;
return true;
}
if (button == 1 && getValue() != 0) {
setValue(0);
public boolean onClick(int btn) {
if (editing) {
return true;
} else if (btn == 0) {
editing = true;
return true;
} else if (btn == 1) {
keyboard = Keyboard.CHAR_NONE;
applyChanges();
return true;
}
return false;
}
public boolean conflicted() {
if (getValue() == 0) return false;
protected void applyChanges() {
setValue(keyboard);
resetChanges();
}
for (Option o : slot.options)
if (o instanceof OptionKeyBind && o != this && ((OptionKeyBind) o).getValue() == getValue()) return true;
protected void resetChanges() {
editing = false;
keyboard = Keyboard.CHAR_NONE;
}
public boolean conflicted() {
final int keyboard = getValue();
if (keyboard <= 0) {
return false;
}
for (Option o : slot.options) {
if (o instanceof OptionKeyBind && o != this && ((OptionKeyBind) o).getValue() == keyboard) {
return true;
}
}
return false;
}
@@ -58,19 +106,50 @@ public class OptionKeyBind extends OptionButton {
return translateN(name);
}
@Override
public void drawButton(int mx, int my) {
if (editing) {
Rectangle b = buttonSize();
drawRect(b.x, b.y, b.width, b.height, -6250336);
drawRect(b.x + 1, b.y + 1, b.width - 2, b.height - 2, -16777216);
drawStringC(getButtonText(), b.x, b.y, b.width, b.height, getTextColour(mx, my));
} else {
super.drawButton(mx, my);
}
}
@Override
public String getButtonText() {
if (hasFocus)
return EnumChatFormatting.WHITE + "> " + EnumChatFormatting.YELLOW + "??? " + EnumChatFormatting.WHITE
+ "<";
String keyText = "";
if (conflicted()) return EnumChatFormatting.RED + Keyboard.getKeyName(getValue());
if (useHash) {
final int metahash = Math.max(editing ? NEIClientUtils.getMetaHash() : getValue(), 0);
final String DELIMITER = EnumChatFormatting.GRAY + " + " + EnumChatFormatting.RESET;
return Keyboard.getKeyName(getValue());
if ((metahash & NEIClientUtils.CTRL_HASH) == NEIClientUtils.CTRL_HASH) {
keyText += NEIClientUtils.translate(Minecraft.isRunningOnMac ? "key.ctrl.mac" : "key.ctrl") + DELIMITER;
}
if ((metahash & NEIClientUtils.SHIFT_HASH) == NEIClientUtils.SHIFT_HASH) {
keyText += "SHIFT" + DELIMITER;
}
if ((metahash & NEIClientUtils.ALT_HASH) == NEIClientUtils.ALT_HASH) {
keyText += "ALT" + DELIMITER;
}
}
if (!editing) {
keyText += Keyboard.getKeyName(
getValue() & ~(NEIClientUtils.CTRL_HASH | NEIClientUtils.SHIFT_HASH | NEIClientUtils.ALT_HASH));
}
return keyText;
}
@Override
public int getTextColour(int mousex, int mousey) {
return -1;
return !editing && conflicted() ? 0xFFFF6666 : -1;
}
}