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.

278 lines
9.4 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei;
2023-09-25 00:34:42 +02:00
import java.util.ArrayList;
2024-07-26 18:44:54 +03:00
import java.util.List;
import java.util.Map;
2023-09-25 00:35:24 +02:00
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
2024-08-17 23:24:27 +03:00
import codechicken.lib.gui.GuiDraw;
2025-08-22 15:03:05 +03:00
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.ItemsPanelGrid.ItemsPanelGridSlot;
import codechicken.nei.util.NEIMouseUtils;
public class ItemPanel extends PanelWidget<ItemsPanelGrid> {
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);
}
public ItemHistoryPanel historyPanel = new ItemHistoryPanel();
2025-08-22 14:58:41 +03:00
public ItemCraftablesPanel craftablesPanel = new ItemCraftablesPanel();
protected Button toggleGroups;
@Deprecated
public static class ItemPanelSlot extends ItemsPanelGridSlot {
2014-04-29 10:54:20 +10:00
@Deprecated
public ItemPanelSlot(int idx, ItemStack item) {
this(idx, 0, item);
2022-01-02 23:30:54 +02:00
}
@Deprecated
public ItemPanelSlot(int idx) {
this(idx, 0, ItemPanels.itemPanel.getGrid().getItem(idx));
2024-07-26 18:44:54 +03:00
}
private ItemPanelSlot(int slotIndex, int itemIndex, ItemStack item) {
super(slotIndex, itemIndex, item);
2024-07-26 18:44:54 +03:00
}
protected static ItemPanelSlot of(ItemsPanelGridSlot slot) {
2024-07-26 18:44:54 +03:00
if (slot != null) {
ItemPanelSlot customSlot = new ItemPanelSlot(slot.slotIndex, slot.itemIndex, slot.item);
customSlot.groupIndex = slot.groupIndex;
customSlot.displayName = slot.displayName;
customSlot.bgItemStack = slot.bgItemStack;
customSlot.groupSize = slot.groupSize;
customSlot.extended = slot.extended;
customSlot.bgColor = slot.bgColor;
return customSlot;
2024-07-26 18:44:54 +03:00
}
return null;
2022-08-31 17:43:22 +09:00
}
2022-01-02 23:30:54 +02:00
}
public ItemPanel() {
this.grid = new ItemsPanelGrid();
2022-01-02 23:30:54 +02:00
}
public static void updateItemList(ArrayList<ItemStack> newItems) {
ItemPanels.itemPanel.getGrid().setItems(newItems);
2022-01-02 23:30:54 +02:00
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") {
2025-08-22 14:58:41 +03:00
@Override
public String getRenderLabel() {
return NEIClientUtils.translate("itempanel.collapsed.button");
}
2024-07-26 18:44:54 +03:00
@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;
}
};
2020-02-04 20:11:17 -08:00
}
@Override
public ItemPanelSlot getSlotMouseOver(int mousex, int mousey) {
// use ItemPanelSlot for Backwards compat
return ItemPanelSlot.of(this.grid.getSlotMouseOver(mousex, mousey));
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
}
2026-02-09 23:11:10 +02:00
@Override
2025-08-22 15:03:05 +03:00
public Rectangle4i calculateBounds() {
final GuiContainer gui = NEIClientUtils.getGuiContainer();
2026-02-09 23:11:10 +02:00
final int maxWidth = (gui.width - 176) / 2 - PADDING * 2;
final int maxHeight = gui.height - PADDING * 2;
final int freeSpace = (gui.width - gui.xSize) / 2 - PADDING * 2;
final int paddingLeft = (int) Math
.ceil(maxWidth * NEIClientConfig.getSetting("world.panels.items.left").getIntValue() / 100000.0);
final int paddingTop = (int) Math
.ceil(maxHeight * NEIClientConfig.getSetting("world.panels.items.top").getIntValue() / 100000.0);
final int paddingRight = (int) Math
.ceil(maxWidth * NEIClientConfig.getSetting("world.panels.items.right").getIntValue() / 100000.0);
final int paddingBottom = (int) Math
.ceil(maxHeight * NEIClientConfig.getSetting("world.panels.items.bottom").getIntValue() / 100000.0);
final int width = Math.max(
ItemsGrid.SLOT_SIZE,
snapDownToMultiple(
Math.min(freeSpace - paddingRight, maxWidth - paddingLeft - paddingRight),
ItemsGrid.SLOT_SIZE));
final int height = Math.max(ItemsGrid.SLOT_SIZE, maxHeight - paddingTop - paddingBottom);
return new Rectangle4i(gui.width - width - PADDING - paddingRight, paddingTop + PADDING, width, height);
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) {
2025-08-22 14:58:41 +03:00
int footerY = y + h;
int footerHeight = 0;
if (grid.getPerPage() == 0) {
return 0;
}
2025-08-22 14:58:41 +03:00
if (NEIClientConfig.showItemQuantityWidget() || !NEIClientConfig.isSearchWidgetCentered()) {
footerY = LayoutManager.quantity.y;
footerHeight = LayoutManager.quantity.h + PanelWidget.PADDING;
}
2024-02-21 18:12:28 +02:00
if (NEIClientConfig.showHistoryPanelWidget()) {
historyPanel.x = x;
2025-08-22 14:58:41 +03:00
if (historyPanel.setPanelWidth(w) != 0) {
footerY = historyPanel.y = footerY - historyPanel.h;
footerHeight += historyPanel.h;
}
}
if (NEIClientConfig.showCraftablesPanelWidget()) {
craftablesPanel.x = x;
if (craftablesPanel.setPanelWidth(w) != 0) {
footerY = craftablesPanel.y = footerY - craftablesPanel.h;
footerHeight += craftablesPanel.h;
}
2024-02-21 18:12:28 +02:00
}
2025-08-22 14:58:41 +03:00
return footerHeight;
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) {
2025-08-22 14:58:41 +03:00
if (!CollapsibleItems.isEmpty() && !grid.isEmpty()) {
2024-07-26 18:44:54 +03:00
LayoutManager.addWidget(toggleGroups);
}
2025-08-22 14:58:41 +03:00
if (NEIClientConfig.showCraftablesPanelWidget()) {
LayoutManager.addWidget(craftablesPanel);
}
if (NEIClientConfig.showHistoryPanelWidget()) {
2024-02-21 18:12:28 +02:00
LayoutManager.addWidget(historyPanel);
}
2025-08-22 14:58:41 +03:00
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);
2025-08-22 14:58:41 +03:00
this.historyPanel.resize(gui);
this.craftablesPanel.resize(gui);
2024-02-21 18:12:28 +02:00
}
protected ItemStack getDraggedStackWithQuantity(ItemStack itemStack) {
return ItemQuantityField.prepareStackWithQuantity(itemStack, 0);
2022-01-02 23:30:54 +02:00
}
2024-07-26 18:44:54 +03:00
@Override
public List<String> handleItemTooltip(ItemStack itemstack, int mousex, int mousey, List<String> currenttip) {
if (!this.grid.forceExpand && !currenttip.isEmpty()) {
final ItemsPanelGridSlot hoverSlot = this.grid.getSlotMouseOver(mousex, mousey);
if (hoverSlot != null && hoverSlot.groupIndex >= 0
&& NEIClientConfig.getBooleanSetting("inventory.collapsibleItems.customName")
&& !hoverSlot.extended
&& !"".equals(hoverSlot.displayName)) {
currenttip.clear();
currenttip.add(hoverSlot.displayName + GuiDraw.TOOLTIP_LINESPACE);
2024-07-26 18:44:54 +03:00
}
}
return super.handleItemTooltip(itemstack, mousex, mousey, currenttip);
2024-07-26 18:44:54 +03:00
}
@Override
public boolean handleClick(int mousex, int mousey, int button) {
if (button == 0 && !this.grid.forceExpand && NEIClientUtils.altKey()) {
final ItemsPanelGridSlot hoverSlot = this.grid.getSlotMouseOver(mousex, mousey);
2024-07-26 18:44:54 +03:00
if (hoverSlot != null && hoverSlot.groupIndex >= 0) {
CollapsibleItems.setExpanded(hoverSlot.groupIndex, !hoverSlot.extended);
this.grid.setItems(this.grid.rawItems);
NEIClientUtils.playClickSound();
return true;
2024-07-26 18:44:54 +03:00
}
}
return super.handleClick(mousex, mousey, button);
}
@Override
public Map<String, String> handleHotkeys(int mousex, int mousey, Map<String, String> hotkeys) {
final ItemsPanelGridSlot hoverSlot = this.grid.getSlotMouseOver(mousex, mousey);
if (!this.grid.forceExpand && hoverSlot != null && hoverSlot.groupIndex >= 0 && hoverSlot.groupSize > 1) {
final String message = hoverSlot.extended ? "itempanel.collapsed.hint.collapse"
: "itempanel.collapsed.hint.expand";
hotkeys.put(
NEIClientUtils.getKeyName(NEIClientUtils.ALT_HASH, NEIMouseUtils.MOUSE_BTN_LMB),
NEIClientUtils.translate(message, hoverSlot.groupSize));
}
return super.handleHotkeys(mousex, mousey, hotkeys);
}
public boolean containsWithSubpanels(int px, int py) {
return contains(px, py) || NEIClientConfig.showHistoryPanelWidget() && this.historyPanel.contains(px, py)
|| NEIClientConfig.showCraftablesPanelWidget() && this.craftablesPanel.contains(px, py);
}
2014-04-29 10:54:20 +10:00
}