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.

254 lines
7.3 KiB
Java
Raw Normal View History

2014-04-29 10:54:20 +10:00
package codechicken.nei;
2023-09-25 00:35:24 +02:00
import static codechicken.lib.gui.GuiDraw.drawRect;
2014-04-29 10:54:20 +10:00
2023-09-25 00:34:42 +02:00
import java.util.ArrayList;
2023-09-25 00:35:24 +02:00
import javax.annotation.Nullable;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import codechicken.lib.vec.Rectangle4i;
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;
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
}
}
protected static class ItemPanelGrid extends ItemsGrid {
2022-01-02 23:30:54 +02:00
public ArrayList<ItemStack> newItems;
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
2022-01-02 23:30:54 +02:00
if (newItems != null) {
2022-10-13 11:06:08 +03:00
realItems = newItems;
2022-01-02 23:30:54 +02:00
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
protected void beforeDrawSlot(@Nullable ItemPanelSlot focused, int slotIdx, Rectangle4i rect) {
if (PresetsWidget.inEditMode()) {
if (!PresetsWidget.isHidden(getItem(slotIdx))) drawRect(rect.x, rect.y, rect.w, rect.h, 0xee555555);
} else {
super.beforeDrawSlot(focused, slotIdx, rect);
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
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
}
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 (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;
}
2014-04-29 10:54:20 +10:00
}