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

@@ -20,6 +20,7 @@ import java.util.LinkedList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.GL11;
public class GuiOptionList extends GuiScreenWidget {
@@ -119,12 +120,16 @@ public class GuiOptionList extends GuiScreenWidget {
o.copyGlobals();
Option.playClickSound();
}
} else if (slotBounds(slot).contains(mx, my)) options.get(slot).mouseClicked(mx - 24, my - 2, button);
} else if (slotBounds(slot).contains(mx, my)) {
options.get(slot).mouseClicked(mx - 24, my - 2, button);
}
}
@Override
public void mouseClicked(int mx, int my, int button) {
for (Option o : options) o.onMouseClicked(mx, my, button);
for (Option o : options) {
o.onMouseClicked(mx, my, button);
}
super.mouseClicked(mx, my, button);
}
@@ -247,8 +252,7 @@ public class GuiOptionList extends GuiScreenWidget {
@Override
public void keyTyped(char c, int keycode) {
if (keycode == 1) // esc
{
if (keycode == Keyboard.KEY_ESCAPE) {
GuiScreen p = parent;
while (p instanceof GuiOptionList) p = ((GuiOptionList) p).parent;
@@ -6,8 +6,10 @@ import static codechicken.lib.gui.GuiDraw.drawStringC;
import static codechicken.lib.gui.GuiDraw.getStringWidth;
import codechicken.nei.LayoutManager;
import codechicken.nei.NEIClientUtils;
import java.awt.Rectangle;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
@@ -53,7 +55,7 @@ public abstract class OptionButton extends Option {
public Rectangle buttonSize() {
if (getPrefix() == null) return new Rectangle(0, 0, slot.slotWidth(), 20);
int width = Math.max(60, getStringWidth(getButtonText()));
int width = Math.max(60, getStringWidth(getButtonText()) + 8);
return new Rectangle(slot.slotWidth() - width, 0, width, 20);
}
@@ -69,14 +71,34 @@ public abstract class OptionButton extends Option {
}
public String getTooltip() {
if (tooltip == null) return null;
String s = translateN(tooltip);
if (s.equals(namespaced(tooltip))) return null;
return s;
String tip = null;
if (tooltip != null) {
String s = translateN(tooltip);
if (!s.equals(namespaced(tooltip))) {
tip = s;
}
}
if (tip == null && getPrefix() != null) {
final int width = getStringWidth(getPrefix());
final Rectangle b = buttonSize();
if (width >= b.x) {
tip = translateN(name);
}
}
return tip;
}
public void drawPrefix() {
if (getPrefix() != null) drawString(getPrefix(), 10, 6, -1);
final String prefix = getPrefix();
if (prefix != null) {
final Rectangle b = buttonSize();
drawString(NEIClientUtils.cropText(Minecraft.getMinecraft().fontRenderer, prefix, b.x - 10), 10, 6, -1);
}
}
public void drawButton(int mx, int my) {
@@ -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;
}
}