Files
NotEnoughItems/src/main/java/codechicken/nei/ItemPanel.java
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

573 lines
20 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei;
2024-07-26 18:44:54 +03:00
import java.awt.Color;
2023-09-25 00:34:42 +02:00
import java.util.ArrayList;
2024-07-26 18:44:54 +03:00
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
2023-09-25 00:34:42 +02:00
2023-09-25 00:35:24 +02:00
import javax.annotation.Nullable;
import net.minecraft.client.gui.inventory.GuiContainer;
2024-07-26 18:44:54 +03:00
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.Tessellator;
2023-09-25 00:35:24 +02:00
import net.minecraft.item.ItemStack;
2024-07-26 18:44:54 +03:00
import net.minecraft.util.EnumChatFormatting;
import org.lwjgl.opengl.GL11;
2023-09-25 00:35:24 +02:00
2024-08-17 23:24:27 +03:00
import codechicken.lib.gui.GuiDraw;
2023-09-25 00:35:24 +02:00
import codechicken.lib.vec.Rectangle4i;
2024-07-26 18:44:54 +03:00
import codechicken.nei.guihook.GuiContainerManager;
2024-08-17 23:24:27 +03:00
public class ItemPanel extends PanelWidget {
2014-04-29 10:54:20 +10:00
2020-01-20 09:42:39 -08:00
/**
* Backwards compat :-/
*/
2022-05-16 19:03:09 +02:00
@SuppressWarnings("unused")
2022-01-02 23:30:54 +02:00
@Deprecated
private int itemsPerPage = 0;
@Deprecated
2020-01-20 09:42:39 -08:00
public static ArrayList<ItemStack> items = new ArrayList<>();
2022-01-02 23:30:54 +02:00
@Deprecated
2020-01-20 09:42:39 -08:00
public ArrayList<ItemStack> realItems = new ArrayList<>();
2022-05-16 19:03:09 +02:00
public ItemStack getStackMouseOver(int mousex, int mousey) {
// *looks angrily at AppleCore*
return super.getStackMouseOver(mousex, mousey);
}
2022-05-06 22:41:45 +03:00
public Button more;
public Button less;
public ItemQuantityField quantity;
2024-02-21 18:12:28 +02:00
public ItemHistoryPanel historyPanel;
2024-07-26 18:44:54 +03:00
public Button toggleGroups;
public static class ItemPanelSlot {
public ItemStack item;
2014-04-29 10:54:20 +10:00
public int slotIndex;
public ItemPanelSlot(int idx, ItemStack stack) {
2022-01-02 23:30:54 +02:00
slotIndex = idx;
item = stack;
}
@Deprecated
public ItemPanelSlot(int idx) {
2022-01-02 23:30:54 +02:00
this(idx, ItemPanels.itemPanel.getGrid().getItem(idx));
2014-04-29 10:54:20 +10:00
}
}
2024-07-26 18:44:54 +03:00
protected static class MaskMetadata {
public int groupIndex = -1;
2024-08-17 23:24:27 +03:00
public String displayName = "";
2024-07-26 18:44:54 +03:00
public boolean extended = false;
public Color bgColor = null;
public Color color = null;
public boolean left = false;
public boolean top = false;
public boolean right = false;
public boolean bottom = false;
}
protected static class ItemPanelGrid extends ItemsGrid {
2022-01-02 23:30:54 +02:00
public ArrayList<ItemStack> newItems;
2024-07-26 18:44:54 +03:00
public ArrayList<ItemStack> rawItems;
protected final HashMap<Integer, List<ItemStack>> groupItems = new HashMap<>();
protected final HashMap<Integer, MaskMetadata> maskMetadata = new HashMap<>();
protected boolean forceExpand = false;
public void setItems(ArrayList<ItemStack> items) {
2022-01-02 23:30:54 +02:00
newItems = items;
}
2019-12-24 13:37:41 -08:00
public void refresh(GuiContainer gui) {
2019-12-24 13:37:41 -08:00
2024-07-26 18:44:54 +03:00
if (this.newItems != null) {
this.groupItems.clear();
this.forceExpand = false;
if (!CollapsibleItems.isEmpty() && !this.newItems.isEmpty()) {
2024-07-26 18:44:54 +03:00
final Set<Integer> groups = new HashSet<>();
boolean outsideGroup = false;
this.realItems = new ArrayList<>();
this.rawItems = new ArrayList<>(this.newItems);
for (ItemStack stack : this.newItems) {
final int groupIndex = CollapsibleItems.getGroupIndex(stack);
2024-07-26 18:44:54 +03:00
if (groupIndex == -1) {
this.realItems.add(stack);
outsideGroup = true;
} else {
if (!groups.contains(groupIndex) || CollapsibleItems.isExpanded(groupIndex)) {
2024-07-26 18:44:54 +03:00
this.realItems.add(stack);
groups.add(groupIndex);
}
if (!this.groupItems.containsKey(groupIndex)) {
this.groupItems.put(groupIndex, new ArrayList<>());
}
this.groupItems.get(groupIndex).add(stack);
}
}
// automatically opens if there are elements from only one group
if (!outsideGroup && groups.size() == 1) {
this.realItems = new ArrayList<>(this.newItems);
this.forceExpand = true;
}
} else {
this.realItems = new ArrayList<>(this.newItems);
}
this.newItems = null;
onItemsChanged();
2022-01-02 23:30:54 +02:00
}
2022-01-02 23:30:54 +02:00
super.refresh(gui);
}
2022-02-06 17:42:11 +02:00
@Override
2024-07-26 18:44:54 +03:00
protected List<Integer> getMask() {
final boolean updateBorders = this.gridMask == null;
final List<Integer> mask = super.getMask();
if (updateBorders) {
calculateGroupBorders(mask);
}
return mask;
}
protected void calculateGroupBorders(List<Integer> mask) {
this.maskMetadata.clear();
if (mask.isEmpty() || this.groupItems.isEmpty()) {
return;
}
final HashMap<Integer, Integer> maskGroup = new HashMap<>();
final Color collapsedColor = new Color(
NEIClientConfig.getSetting("inventory.collapsibleItems.collapsedColor").getHexValue(),
true);
final Color expandedColor = new Color(
NEIClientConfig.getSetting("inventory.collapsibleItems.expandedColor").getHexValue(),
true);
for (int slotIndex = 0; slotIndex < mask.size(); slotIndex++) {
if (mask.get(slotIndex) == null) {
continue;
}
int idx = mask.get(slotIndex);
maskGroup.put(idx, CollapsibleItems.getGroupIndex(getItem(idx)));
2024-07-26 18:44:54 +03:00
}
for (int slotIndex = 0; slotIndex < mask.size(); slotIndex++) {
if (mask.get(slotIndex) == null) {
continue;
}
int idx = mask.get(slotIndex);
int groupIndex = maskGroup.get(idx);
if (groupIndex == -1 || this.groupItems.get(groupIndex).size() == 1) {
continue;
}
int column = slotIndex % this.columns;
int row = slotIndex / this.columns;
int prevSlotIndex = (row - 1) * this.columns + column;
int nextSlotIndex = (row + 1) * this.columns + column;
MaskMetadata metadata = new MaskMetadata();
metadata.groupIndex = groupIndex;
metadata.displayName = CollapsibleItems.getDisplayName(groupIndex);
metadata.extended = this.forceExpand || CollapsibleItems.isExpanded(groupIndex);
2024-07-26 18:44:54 +03:00
metadata.bgColor = metadata.extended ? expandedColor : collapsedColor;
metadata.color = darkerColor(metadata.bgColor);
metadata.left = column == 0 || idx == 0
|| mask.get(slotIndex - 1) == null
|| maskGroup.getOrDefault(idx - 1, -1) != groupIndex;
metadata.right = column == this.columns - 1 || maskGroup.getOrDefault(idx + 1, -1) != groupIndex;
if (prevSlotIndex >= 0) {
Integer previdx = mask.get(prevSlotIndex);
metadata.top = previdx == null || maskGroup.getOrDefault(previdx, -1) != groupIndex;
} else {
metadata.top = true;
}
if (nextSlotIndex < mask.size()) {
Integer nextidx = mask.get(nextSlotIndex);
metadata.bottom = nextidx == null || maskGroup.getOrDefault(nextidx, -1) != groupIndex;
} else {
metadata.bottom = true;
}
this.maskMetadata.put(idx, metadata);
}
}
@Override
protected void beforeDrawItems(int mousex, int mousey, @Nullable ItemPanelSlot focused) {
final List<Integer> mask = getMask();
super.beforeDrawItems(mousex, mousey, focused);
for (int i = 0; i < mask.size(); i++) {
if (mask.get(i) != null) {
drawBorder(mask.get(i), getSlotRect(i));
}
}
}
protected void drawBorder(int slotIdx, Rectangle4i rect) {
final MaskMetadata metadata = this.maskMetadata.get(slotIdx);
if (metadata != null) {
final float LINE_WIDTH = 0.75f;
drawRect(rect.x, rect.y, rect.w, rect.h, metadata.bgColor);
if (metadata.left) {
float leftBottom = metadata.bottom ? -LINE_WIDTH : 0;
drawRect(rect.x, rect.y, LINE_WIDTH, rect.h + leftBottom, metadata.color);
}
if (metadata.right) {
float rightTop = metadata.top ? LINE_WIDTH : 0;
drawRect(rect.x + rect.w, rect.y - rightTop, LINE_WIDTH, rect.h + rightTop, metadata.color);
}
if (metadata.top) {
drawRect(rect.x, rect.y - LINE_WIDTH, rect.w, LINE_WIDTH, metadata.color);
}
if (metadata.bottom) {
drawRect(rect.x, rect.y + rect.h - LINE_WIDTH, rect.w, LINE_WIDTH, metadata.color);
}
}
}
private static Color darkerColor(Color color) {
return new Color(
color.getRed(),
color.getGreen(),
color.getBlue(),
Math.min((int) (color.getAlpha() + (255f / 5) * 2), 255));
}
private static void drawRect(double left, double top, double width, double height, Color color) {
Tessellator tessellator = Tessellator.instance;
GL11.glEnable(GL11.GL_BLEND);
GL11.glDisable(GL11.GL_TEXTURE_2D);
OpenGlHelper.glBlendFunc(770, 771, 1, 0);
GL11.glColor4f(
color.getRed() / 255f,
color.getGreen() / 255f,
color.getBlue() / 255f,
color.getAlpha() / 255f);
tessellator.startDrawingQuads();
tessellator.addVertex(left, top + height, 0.0D);
tessellator.addVertex(left + width, top + height, 0.0D);
tessellator.addVertex(left + width, top, 0.0D);
tessellator.addVertex(left, top, 0.0D);
tessellator.draw();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glDisable(GL11.GL_BLEND);
}
@Override
protected void drawItem(Rectangle4i rect, int slotIdx) {
final MaskMetadata metadata = this.maskMetadata.get(slotIdx);
if (metadata != null && !metadata.extended) {
final List<ItemStack> groupItems = this.groupItems.get(metadata.groupIndex);
GuiContainerManager.drawItems.zLevel -= 10F;
GuiContainerManager.drawItem(rect.x + 1, rect.y - 1, groupItems.get(groupItems.size() - 1), true, "");
GuiContainerManager.drawItems.zLevel += 10F;
GuiContainerManager.drawItem(rect.x - 1, rect.y + 1, getItem(slotIdx), true, "");
} else {
2024-07-26 18:44:54 +03:00
super.drawItem(rect, slotIdx);
2022-02-06 17:42:11 +02:00
}
}
2022-08-31 17:43:22 +09:00
@Override
public String getMessageOnEmpty() {
return ItemList.loadFinished ? null : NEIClientUtils.translate("itempanel.loading");
}
2022-01-02 23:30:54 +02:00
}
public ItemPanel() {
2022-05-06 22:41:45 +03:00
grid = new ItemPanelGrid();
2022-01-02 23:30:54 +02:00
}
public static void updateItemList(ArrayList<ItemStack> newItems) {
2022-01-02 23:30:54 +02:00
((ItemPanelGrid) ItemPanels.itemPanel.getGrid()).setItems(newItems);
ItemPanels.itemPanel.realItems = newItems;
}
2022-05-06 22:41:45 +03:00
@Override
public void init() {
2022-05-06 22:41:45 +03:00
super.init();
2019-12-24 13:37:41 -08:00
2024-07-26 18:44:54 +03:00
toggleGroups = new Button("G") {
@Override
public String getButtonTip() {
return NEIClientUtils.translate("itempanel.collapsed.button.tip");
}
@Override
public boolean onButtonPress(boolean rightclick) {
CollapsibleItems.toggleGroups(rightclick ? false : null);
2024-07-26 18:44:54 +03:00
ItemList.updateFilter.restart();
return true;
}
};
more = new Button("+") {
2022-05-06 22:41:45 +03:00
@Override
2019-12-24 13:37:41 -08:00
public boolean onButtonPress(boolean rightclick) {
if (rightclick) return false;
2022-05-06 22:41:45 +03:00
int modifier = NEIClientUtils.controlKey() ? 64 : NEIClientUtils.shiftKey() ? 10 : 1;
int quantity = NEIClientConfig.getItemQuantity() + modifier;
if (quantity < 0) {
quantity = 0;
2019-12-24 13:37:41 -08:00
}
2022-05-06 22:41:45 +03:00
ItemPanels.itemPanel.quantity.setText(Integer.toString(quantity));
return true;
2019-12-24 13:37:41 -08:00
}
};
less = new Button("-") {
2022-05-06 22:41:45 +03:00
@Override
2019-12-24 13:37:41 -08:00
public boolean onButtonPress(boolean rightclick) {
if (rightclick) return false;
2022-05-06 22:41:45 +03:00
int modifier = NEIClientUtils.controlKey() ? -64 : NEIClientUtils.shiftKey() ? -10 : -1;
int quantity = NEIClientConfig.getItemQuantity() + modifier;
if (quantity < 0) {
quantity = 0;
2019-12-24 13:37:41 -08:00
}
2022-05-06 22:41:45 +03:00
ItemPanels.itemPanel.quantity.setText(Integer.toString(quantity));
return true;
2019-12-24 13:37:41 -08:00
}
};
2022-05-06 22:41:45 +03:00
quantity = new ItemQuantityField("quantity");
2024-02-21 18:12:28 +02:00
historyPanel = new ItemHistoryPanel();
2020-02-04 20:11:17 -08:00
}
2022-05-06 22:41:45 +03:00
@Deprecated
public void scroll(int i) {
2022-05-06 22:41:45 +03:00
grid.shiftPage(i);
2019-12-24 13:37:41 -08:00
}
public String getLabelText() {
return String.format("%d/%d", getPage(), Math.max(1, getNumPages()));
2019-12-24 13:37:41 -08:00
}
protected String getPositioningSettingName() {
2022-05-06 22:41:45 +03:00
return "world.panels.items";
2019-12-24 13:37:41 -08:00
}
public int getMarginLeft(GuiContainer gui) {
2022-05-06 22:41:45 +03:00
return (gui.width + gui.xSize) / 2 + PADDING;
2019-12-24 13:37:41 -08:00
}
public int getMarginTop(GuiContainer gui) {
2022-05-06 22:41:45 +03:00
return PADDING;
2020-01-20 09:42:39 -08:00
}
public int getWidth(GuiContainer gui) {
2022-05-06 22:41:45 +03:00
return gui.width - (gui.xSize + gui.width) / 2 - PADDING * 2;
2014-04-29 10:54:20 +10:00
}
public int getHeight(GuiContainer gui) {
2022-05-06 22:41:45 +03:00
return gui.height - getMarginTop(gui) - PADDING;
2014-04-29 10:54:20 +10:00
}
2024-07-26 18:44:54 +03:00
@Override
protected int resizeHeader(GuiContainer gui) {
int marginTop = super.resizeHeader(gui);
toggleGroups.w = pageNext.w;
toggleGroups.h = pageNext.h;
toggleGroups.y = pageNext.y;
toggleGroups.x = pageNext.x - toggleGroups.w - 2;
return marginTop;
}
protected int resizeFooter(GuiContainer gui) {
if (!NEIClientConfig.showItemQuantityWidget() && NEIClientConfig.isSearchWidgetCentered()
&& !NEIClientConfig.showHistoryPanelWidget()) {
return 0;
}
final int BUTTON_SIZE = 20;
2022-05-06 22:41:45 +03:00
more.w = less.w = BUTTON_SIZE;
quantity.h = BUTTON_SIZE;
if (NEIClientConfig.isSearchWidgetCentered()) {
more.h = less.h = BUTTON_SIZE;
more.x = x + w - BUTTON_SIZE;
more.y = less.y = quantity.y = y + h - BUTTON_SIZE;
less.x = x;
quantity.x = x + BUTTON_SIZE + 2;
quantity.w = more.x - quantity.x - 2;
} else {
quantity.x = (int) (x + (w * 0.7)) + 3;
quantity.y = y + h - BUTTON_SIZE;
quantity.w = (int) ((w * 0.3) - BUTTON_SIZE - 1);
more.h = less.h = BUTTON_SIZE / 2;
more.y = y + h - (more.h * 2);
less.x = more.x = quantity.x + quantity.w;
less.y = more.y + more.h;
}
2022-05-06 22:41:45 +03:00
2024-02-21 18:12:28 +02:00
if (NEIClientConfig.showHistoryPanelWidget()) {
historyPanel.x = x;
historyPanel.w = w;
historyPanel.h = ItemsGrid.SLOT_SIZE * NEIClientConfig.getIntSetting("inventory.history.useRows");
if (NEIClientConfig.showItemQuantityWidget() || !NEIClientConfig.isSearchWidgetCentered()) {
historyPanel.y = quantity.y - historyPanel.h - PanelWidget.PADDING;
return quantity.h + historyPanel.h + PanelWidget.PADDING * 2;
} else {
historyPanel.y = y + h - historyPanel.h;
return historyPanel.h + PanelWidget.PADDING;
}
2024-02-21 18:12:28 +02:00
}
return quantity.h + PanelWidget.PADDING;
2014-04-29 10:54:20 +10:00
}
2014-04-29 10:54:20 +10:00
@Override
public void setVisible() {
2022-05-06 22:41:45 +03:00
super.setVisible();
2024-02-21 18:12:28 +02:00
if (grid.getPerPage() > 0) {
if (NEIClientConfig.showItemQuantityWidget()) {
LayoutManager.addWidget(more);
LayoutManager.addWidget(less);
LayoutManager.addWidget(quantity);
}
if (!CollapsibleItems.isEmpty()) {
2024-07-26 18:44:54 +03:00
LayoutManager.addWidget(toggleGroups);
}
2024-02-21 18:12:28 +02:00
if (NEIClientConfig.showHistoryPanelWidget()) {
LayoutManager.addWidget(historyPanel);
}
2014-04-29 10:54:20 +10:00
}
}
2024-02-21 18:12:28 +02:00
@Override
public void resize(GuiContainer gui) {
super.resize(gui);
historyPanel.resize(gui);
}
protected ItemStack getDraggedStackWithQuantity(int mouseDownSlot) {
2022-01-02 23:30:54 +02:00
ItemStack stack = grid.getItem(mouseDownSlot);
if (stack != null) {
int amount = NEIClientConfig.showItemQuantityWidget() ? NEIClientConfig.getItemQuantity() : 0;
2022-01-02 23:30:54 +02:00
if (amount == 0) {
amount = stack.getMaxStackSize();
}
return NEIServerUtils.copyStack(stack, amount);
}
return null;
}
2024-07-26 18:44:54 +03:00
@Override
public List<String> handleItemTooltip(GuiContainer gui, ItemStack itemstack, int mousex, int mousey,
List<String> currenttip) {
final ItemPanelGrid panelGrid = ((ItemPanelGrid) this.grid);
if (!panelGrid.forceExpand && !NEIClientConfig.isHidden() && !currenttip.isEmpty()) {
final ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
if (hoverSlot != null) {
final MaskMetadata metadata = panelGrid.maskMetadata.get(hoverSlot.slotIndex);
if (metadata != null) {
final List<ItemStack> items = panelGrid.groupItems.get(metadata.groupIndex);
2024-08-17 23:24:27 +03:00
if (NEIClientConfig.getBooleanSetting("inventory.collapsibleItems.customName") && !metadata.extended
&& !"".equals(metadata.displayName)) {
currenttip.clear();
currenttip.add(metadata.displayName + GuiDraw.TOOLTIP_LINESPACE);
}
2024-07-26 18:44:54 +03:00
if (items != null && items.size() > 1) {
String message = metadata.extended ? "itempanel.collapsed.hint.collapse"
: "itempanel.collapsed.hint.expand";
currenttip.add(
1,
EnumChatFormatting.GRAY + NEIClientUtils.translate(message, items.size())
+ EnumChatFormatting.RESET);
}
}
}
}
2024-08-17 23:24:27 +03:00
return super.handleItemTooltip(gui, itemstack, mousex, mousey, currenttip);
2024-07-26 18:44:54 +03:00
}
@Override
public boolean handleClick(int mousex, int mousey, int button) {
final ItemPanelGrid panelGrid = ((ItemPanelGrid) this.grid);
if (NEIClientUtils.altKey() && button == 0 && !panelGrid.forceExpand) {
final ItemPanelSlot hoverSlot = grid.getSlotMouseOver(mousex, mousey);
if (hoverSlot != null) {
final MaskMetadata metadata = panelGrid.maskMetadata.get(hoverSlot.slotIndex);
if (metadata != null) {
CollapsibleItems.setExpanded(metadata.groupIndex, !metadata.extended);
2024-07-26 18:44:54 +03:00
panelGrid.setItems(panelGrid.rawItems);
return true;
}
}
}
return super.handleClick(mousex, mousey, button);
}
2014-04-29 10:54:20 +10:00
}