Skip to content

Commit

Permalink
Added keyboard shortcuts to NEI And More... (#271)
Browse files Browse the repository at this point in the history
* Added keyboard shortcuts to NEI

* OptionButton: Add Tooltip to long prefix

* fix formatting

* Tooltip shown when shift is held
  • Loading branch information
slprime authored Aug 19, 2022
1 parent 529f437 commit fb9d517
Show file tree
Hide file tree
Showing 30 changed files with 744 additions and 294 deletions.
59 changes: 21 additions & 38 deletions src/main/java/codechicken/nei/BookmarkPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -404,39 +404,39 @@ public String getLabelText() {
}

public void addOrRemoveItem(ItemStack stackA) {
addOrRemoveItem(stackA, "", null);
addOrRemoveItem(stackA, null, null, false, false);
}

public void addOrRemoveItem(ItemStack stackover, String handlerName, List<PositionedStack> ingredients) {
public void addOrRemoveItem(
ItemStack stackover,
final String handlerName,
final List<PositionedStack> ingredients,
boolean saveIngredients,
boolean saveStackSize) {
loadBookmarksIfNeeded();

final Point mousePos = getMousePosition();
final ItemPanelSlot slot = getSlotMouseOver(mousePos.x, mousePos.y);
final boolean addFullRecipe = NEIClientUtils.shiftKey();
final BookmarkGrid BGrid = (BookmarkGrid) grid;

if (slot != null && StackInfo.equalItemAndNBT(slot.item, stackover, true)) {
BGrid.removeRecipe(slot.slotIndex, addFullRecipe);
BGrid.removeRecipe(slot.slotIndex, saveIngredients);
} else {
final boolean saveStackSize = NEIClientUtils.controlKey();
final NBTTagCompound nbTagA = StackInfo.itemStackToNBT(stackover, saveStackSize);
final ItemStack normalizedA = StackInfo.loadFromNBT(nbTagA);
BookmarkRecipeId recipeId = null;

if (NEIClientConfig.saveCurrentRecipeInBookmarksEnabled()
&& handlerName != ""
&& ingredients != null
&& !ingredients.isEmpty()) {
if (handlerName != "" && ingredients != null && !ingredients.isEmpty()) {
recipeId = new BookmarkRecipeId(handlerName, ingredients);
}

final int idx = BGrid.indexOf(normalizedA, recipeId);

if (idx != -1) {
BGrid.removeRecipe(idx, addFullRecipe);
BGrid.removeRecipe(idx, saveIngredients);
} else {

if (addFullRecipe && handlerName != "" && ingredients != null && !ingredients.isEmpty()) {
if (saveIngredients && handlerName != "" && ingredients != null && !ingredients.isEmpty()) {
final Map<NBTTagCompound, Integer> unique = new HashMap<>();
final ArrayList<NBTTagCompound> sorted = new ArrayList<>();

Expand All @@ -448,18 +448,19 @@ public void addOrRemoveItem(ItemStack stackover, String handlerName, List<Positi
if (unique.get(nbTag) == null) {
unique.put(nbTag, 1);
sorted.add(nbTag);
} else if (saveStackSize) {
} else {
unique.put(nbTag, unique.get(nbTag) + 1);
}
}

for (NBTTagCompound nbTag : sorted) {
nbTag.setInteger("Count", nbTag.getInteger("Count") * unique.get(nbTag));
final int count = nbTag.getInteger("Count") * unique.get(nbTag);
nbTag.setInteger("Count", count);
BGrid.addItem(
StackInfo.loadFromNBT(nbTag),
new BookmarkStackMeta(
recipeId,
(saveStackSize ? 1 : -1) * nbTag.getInteger("Count"),
recipeId != null ? recipeId.copy() : null,
(saveStackSize ? 1 : -1) * count,
true,
nbTag.hasKey("gtFluidName")));
}
Expand All @@ -479,15 +480,14 @@ public void addOrRemoveItem(ItemStack stackover, String handlerName, List<Positi
saveBookmarks();
}

public BookmarkRecipeId getBookmarkRecipeId(ItemStack stackA) {

BookmarkRecipeId mouseOverRecipeId = getBookmarkMouseOverRecipeId(stackA);
if (mouseOverRecipeId != null) return mouseOverRecipeId;
public BookmarkRecipeId getBookmarkRecipeId(int slotIndex) {
return ((BookmarkGrid) grid).getRecipeId(slotIndex);
}

public BookmarkRecipeId getBookmarkRecipeId(ItemStack stackA) {
BookmarkRecipeId recipeId = ((BookmarkGrid) grid).findRecipeId(stackA);

if (recipeId == null) {

for (int idx = 0; idx < namespaces.size() && recipeId == null; idx++) {
if (idx == activeNamespaceIndex) continue;
recipeId = namespaces.get(idx).findRecipeId(stackA);
Expand All @@ -497,23 +497,6 @@ public BookmarkRecipeId getBookmarkRecipeId(ItemStack stackA) {
return recipeId;
}

public BookmarkRecipeId getBookmarkMouseOverRecipeId(ItemStack stackA) {
final Point mousePos = getMousePosition();
final ItemPanelSlot slot = getSlotMouseOver(mousePos.x, mousePos.y);

if (slot != null) {
if (stackA == null || StackInfo.equalItemAndNBT(slot.item, stackA, true)) {
return ((BookmarkGrid) grid).getRecipeId(slot.slotIndex);
}
}

return null;
}

public BookmarkRecipeId getBookmarkMouseOverRecipeId() {
return getBookmarkMouseOverRecipeId(null);
}

protected String getNamespaceLabelText(boolean shortFormat) {
String activePage = String.valueOf(activeNamespaceIndex + 1);

Expand Down Expand Up @@ -868,7 +851,7 @@ protected ItemStack getDraggedStackWithQuantity(int mouseDownSlot) {
int amount = stack.stackSize;

if (meta.factor < 0 && !meta.fluidDisplay) {
amount = NEIClientConfig.getItemQuantity();
amount = NEIClientConfig.showItemQuantityWidget() ? NEIClientConfig.getItemQuantity() : 0;

if (amount == 0) {
amount = stack.getMaxStackSize();
Expand Down
19 changes: 7 additions & 12 deletions src/main/java/codechicken/nei/HUDRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import codechicken.nei.KeyManager.IKeyStateTracker;
import codechicken.nei.api.ItemInfo;
import codechicken.nei.guihook.GuiContainerManager;
import codechicken.nei.recipe.StackInfo;
import java.awt.Dimension;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.RenderHelper;
Expand Down Expand Up @@ -39,17 +39,12 @@ public static void renderOverlay() {
&& mc.objectMouseOver != null
&& mc.objectMouseOver.typeOfHit == MovingObjectType.BLOCK) {
World world = mc.theWorld;
ArrayList<ItemStack> items = ItemInfo.getIdentifierItems(world, mc.thePlayer, mc.objectMouseOver);
if (items.isEmpty()) return;

int minDamage = Integer.MAX_VALUE;
ItemStack stack = null;
for (ItemStack astack : items) {
if (astack.getItem() != null && astack.getItemDamage() < minDamage) {
stack = astack;
minDamage = stack.getItemDamage();
}
}
ItemStack[] items = ItemInfo.getIdentifierItems(world, mc.thePlayer, mc.objectMouseOver)
.toArray(new ItemStack[0]);

if (items.length == 0) return;

ItemStack stack = StackInfo.getItemStackWithMinimumDamage(items);

renderOverlay(stack, ItemInfo.getText(stack, world, mc.thePlayer, mc.objectMouseOver), getPositioning());
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/codechicken/nei/ItemPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public ItemStack getStackMouseOver(int mousex, int mousey) {
// *looks angrily at AppleCore*
return super.getStackMouseOver(mousex, mousey);
}
;

public Button more;
public Button less;
Expand Down Expand Up @@ -155,6 +154,8 @@ public int getHeight(GuiContainer gui) {
}

protected int resizeFooter(GuiContainer gui) {
if (!NEIClientConfig.showItemQuantityWidget()) return 0;

final int BUTTON_SIZE = 16;

more.w = less.w = BUTTON_SIZE;
Expand All @@ -174,7 +175,7 @@ protected int resizeFooter(GuiContainer gui) {
public void setVisible() {
super.setVisible();

if (grid.getPerPage() > 0) {
if (grid.getPerPage() > 0 && NEIClientConfig.showItemQuantityWidget()) {
LayoutManager.addWidget(more);
LayoutManager.addWidget(less);
LayoutManager.addWidget(quantity);
Expand All @@ -185,7 +186,7 @@ protected ItemStack getDraggedStackWithQuantity(int mouseDownSlot) {
ItemStack stack = grid.getItem(mouseDownSlot);

if (stack != null) {
int amount = NEIClientConfig.getItemQuantity();
int amount = NEIClientConfig.showItemQuantityWidget() ? NEIClientConfig.getItemQuantity() : 0;

if (amount == 0) {
amount = stack.getMaxStackSize();
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/codechicken/nei/ItemQuantityField.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package codechicken.nei;

import static codechicken.nei.NEIClientUtils.translate;

public class ItemQuantityField extends TextField {
public ItemQuantityField(String ident) {
super(ident);
centered = true;
field.setDisabledTextColour(0xFF303030);
}

public int intValue() {
Expand Down Expand Up @@ -34,4 +37,18 @@ public void loseFocus() {
public void onTextChange(String oldText) {
NEIClientUtils.setItemQuantity(intValue());
}

@Override
public void draw(int mousex, int mousey) {

if (!focused() && intValue() == 0) {
field.setText(translate("itempanel.quantity.default"));
field.setEnabled(false);
super.draw(mousex, mousey);
field.setEnabled(true);
field.setText("0");
} else {
super.draw(mousex, mousey);
}
}
}
80 changes: 49 additions & 31 deletions src/main/java/codechicken/nei/ItemsGrid.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ private void checkGuiOverlap(GuiContainer gui, int start, int end, int dir) {
}
}

public Rectangle4i getItemRect(int idx) {
final List<Integer> mask = getMask();

for (int i = 0; i < mask.size(); i++) {
if (mask.get(i) != null && idx == mask.get(i)) {
return getSlotRect(i);
}
}

return null;
}

public Rectangle4i getSlotRect(int i) {
return getSlotRect(i / columns, i % columns);
}
Expand Down Expand Up @@ -222,7 +234,7 @@ private void blitExistingBuffer() {
GL11.glEnable(GL11.GL_BLEND);
OpenGlHelper.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
framebuffer.bindFramebufferTexture();
getFrameBuffer().bindFramebufferTexture();
GL11.glEnable(GL11.GL_TEXTURE_2D);
ScaledResolution res = new ScaledResolution(minecraft, minecraft.displayWidth, minecraft.displayHeight);
Tessellator tessellator = Tessellator.instance;
Expand All @@ -235,54 +247,61 @@ private void blitExistingBuffer() {
OpenGlHelper.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
}

private Framebuffer getFrameBuffer() {

if (framebuffer == null) {
framebuffer = new Framebuffer(1, 1, true);
framebuffer.setFramebufferColor(0.0F, 0.0F, 0.0F, 0.0F);
}

return framebuffer;
}

private void drawItems() {
GuiContainerManager.enableMatrixStackLogging();

final List<Integer> mask = getMask();
for (int i = 0; i < mask.size(); i++) {
if (mask.get(i) != null) {
drawItem(getSlotRect(i), mask.get(i));
}
}

GuiContainerManager.disableMatrixStackLogging();
}

public void draw(int mousex, int mousey) {
if (getPerPage() == 0) {
return;
}

boolean shouldCache = NEIClientConfig.shouldCacheItemRendering() && !PresetsWidget.inEditMode();
if (shouldCache) {
Minecraft minecraft = Minecraft.getMinecraft();
if (framebuffer == null) {
framebuffer = new Framebuffer(minecraft.displayWidth, minecraft.displayHeight, true);
framebuffer.framebufferColor[0] = 0.0F;
framebuffer.framebufferColor[1] = 0.0F;
framebuffer.framebufferColor[2] = 0.0F;
}
drawSlotOutlines(mousex, mousey);
if (NEIClientConfig.shouldCacheItemRendering()) {

if (refreshBuffer) {
Minecraft minecraft = Minecraft.getMinecraft();
Framebuffer framebuffer = getFrameBuffer();
framebuffer.createBindFramebuffer(minecraft.displayWidth, minecraft.displayHeight);
framebuffer.framebufferClear();
framebuffer.bindFramebuffer(false);

/* Set up some rendering state needed for items to work correctly */
GL11.glDisable(GL11.GL_BLEND);
GL11.glDepthMask(true);
OpenGlHelper.glBlendFunc(
GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
} else {
blitExistingBuffer();
return;
}
} else {
drawSlotOutlines(mousex, mousey);
}

GuiContainerManager.enableMatrixStackLogging();
drawItems();

final List<Integer> mask = getMask();
for (int i = 0; i < mask.size(); i++) {
if (mask.get(i) != null) {
drawItem(getSlotRect(i), mask.get(i));
Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(false);
refreshBuffer = false;
}
}

GuiContainerManager.disableMatrixStackLogging();

if (refreshBuffer && shouldCache) {
refreshBuffer = false;
Minecraft.getMinecraft().getFramebuffer().bindFramebuffer(false);
drawSlotOutlines(mousex, mousey);
blitExistingBuffer();
} else {
drawSlotOutlines(mousex, mousey);
drawItems();
}
}

Expand All @@ -307,9 +326,8 @@ public ItemPanelSlot getSlotMouseOver(int mousex, int mousey) {

if (mask.size() > slt && mask.get(slt) != null) {
final int idx = mask.get(slt);
final ItemStack stack = getItem(idx).copy();

return new ItemPanelSlot(idx, stack);
return new ItemPanelSlot(idx, getItem(idx));
}

return null;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/codechicken/nei/KeyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map.Entry;
import org.lwjgl.input.Keyboard;

/**
* Good old down/held/up keystate tracker
Expand All @@ -24,16 +23,17 @@ public static class KeyState {

public static void tickKeyStates() {
for (Entry<String, KeyState> entry : keyStates.entrySet()) {
int keyCode = NEIClientConfig.getKeyBinding(entry.getKey());
boolean down = keyCode != 0 && Keyboard.isKeyDown(keyCode);
KeyState state = entry.getValue();
final boolean down = NEIClientConfig.isKeyHashDown(entry.getKey());
final KeyState state = entry.getValue();

if (down) {
state.down = !state.held;
state.up = false;
} else {
state.up = state.held;
state.down = false;
}

state.held = down;
}

Expand Down
Loading

0 comments on commit fb9d517

Please sign in to comment.