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

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

401 lines
12 KiB
Java
Raw Normal View History

2022-05-06 22:41:45 +03:00
package codechicken.nei;
2023-09-25 00:35:24 +02:00
import java.util.ArrayList;
import java.util.List;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.item.ItemStack;
import codechicken.lib.vec.Rectangle4i;
import codechicken.nei.ItemPanel.ItemPanelSlot;
2022-05-06 22:41:45 +03:00
import codechicken.nei.api.GuiInfo;
import codechicken.nei.api.INEIGuiHandler;
import codechicken.nei.guihook.GuiContainerManager;
2024-08-17 23:24:27 +03:00
import codechicken.nei.guihook.IContainerTooltipHandler;
2022-05-06 22:41:45 +03:00
import codechicken.nei.recipe.GuiCraftingRecipe;
import codechicken.nei.recipe.GuiRecipe;
import codechicken.nei.recipe.GuiUsageRecipe;
2024-08-17 23:24:27 +03:00
public abstract class PanelWidget extends Widget implements IContainerTooltipHandler {
2022-05-06 22:41:45 +03:00
protected static final int PADDING = 2;
2022-10-13 11:06:08 +03:00
public ItemStack draggedStack;
2022-05-06 22:41:45 +03:00
public int mouseDownSlot = -1;
public Button pagePrev;
public Button pageNext;
public Label pageLabel;
protected ItemsGrid grid;
public ArrayList<ItemStack> getItems() {
2022-05-06 22:41:45 +03:00
return grid.getItems();
}
public ItemsGrid getGrid() {
2022-05-06 22:41:45 +03:00
return grid;
}
public void init() {
pageLabel = new Label("0/0", true);
pagePrev = new Button("Prev") {
2022-05-06 22:41:45 +03:00
public boolean onButtonPress(boolean rightclick) {
2024-07-26 18:44:54 +03:00
if (rightclick) {
grid.setPage(0);
} else {
2022-05-06 22:41:45 +03:00
grid.shiftPage(-1);
}
2024-07-26 18:44:54 +03:00
return true;
2022-05-06 22:41:45 +03:00
}
@Override
public String getRenderLabel() {
return "<";
}
};
pageNext = new Button("Next") {
2022-05-06 22:41:45 +03:00
public boolean onButtonPress(boolean rightclick) {
2024-07-26 18:44:54 +03:00
if (rightclick) {
grid.setPage(grid.getNumPages() - 1);
} else {
2022-05-06 22:41:45 +03:00
grid.shiftPage(1);
}
2024-07-26 18:44:54 +03:00
return true;
2022-05-06 22:41:45 +03:00
}
@Override
public String getRenderLabel() {
return ">";
}
};
}
public abstract String getLabelText();
protected abstract String getPositioningSettingName();
public abstract int getMarginLeft(GuiContainer gui);
public abstract int getMarginTop(GuiContainer gui);
public abstract int getWidth(GuiContainer gui);
public abstract int getHeight(GuiContainer gui);
public void resize(GuiContainer gui) {
final Rectangle4i margin = new Rectangle4i(
getMarginLeft(gui),
getMarginTop(gui),
getWidth(gui),
getHeight(gui));
2022-05-06 22:41:45 +03:00
final int minWidth = 5 * ItemsGrid.SLOT_SIZE;
final int minHeight = 9 * ItemsGrid.SLOT_SIZE;
2022-05-06 22:41:45 +03:00
final String settingName = getPositioningSettingName();
int paddingLeft = (int) Math
.ceil(margin.w * NEIClientConfig.getSetting(settingName + ".left").getIntValue() / 100000.0);
int paddingTop = (int) Math
.ceil(margin.h * NEIClientConfig.getSetting(settingName + ".top").getIntValue() / 100000.0);
int paddingRight = (int) Math
.ceil(margin.w * NEIClientConfig.getSetting(settingName + ".right").getIntValue() / 100000.0);
int paddingBottom = (int) Math
.ceil(margin.h * NEIClientConfig.getSetting(settingName + ".bottom").getIntValue() / 100000.0);
2022-05-06 22:41:45 +03:00
int deltaHeight = Math.min(0, margin.h - paddingTop - paddingBottom - minHeight) / 2;
paddingLeft = Math.min(paddingLeft, Math.max(0, margin.w - paddingRight - minWidth));
paddingRight = Math.min(paddingRight, Math.max(0, margin.w - paddingLeft - minWidth));
paddingTop = Math.min(margin.h - minHeight, Math.max(0, paddingTop + deltaHeight));
paddingBottom = Math.min(margin.h - paddingTop - minHeight, Math.max(0, paddingBottom - deltaHeight));
x = margin.x + paddingLeft;
y = margin.y + paddingTop;
w = margin.w - paddingLeft - paddingRight;
h = margin.h - paddingTop - paddingBottom;
final int header = resizeHeader(gui);
final int footer = resizeFooter(gui);
grid.setGridSize(x, y + header, w, h - header - footer);
grid.refresh(gui);
}
protected int resizeHeader(GuiContainer gui) {
2022-05-06 22:41:45 +03:00
final int BUTTON_SIZE = 16;
int paddingLeft = 0;
int paddingRight = 0;
while (paddingLeft < w && GuiInfo.hideItemPanelSlot(
gui,
new Rectangle4i(x + paddingLeft, y, ItemsGrid.SLOT_SIZE, ItemsGrid.SLOT_SIZE))) {
2022-05-06 22:41:45 +03:00
paddingLeft += ItemsGrid.SLOT_SIZE;
}
while (paddingRight < w && GuiInfo.hideItemPanelSlot(
gui,
new Rectangle4i(
x + w - paddingRight - ItemsGrid.SLOT_SIZE,
y,
ItemsGrid.SLOT_SIZE,
ItemsGrid.SLOT_SIZE))) {
2022-05-06 22:41:45 +03:00
paddingRight += ItemsGrid.SLOT_SIZE;
}
if (paddingLeft + paddingRight >= w) {
return 0;
}
pagePrev.w = pageNext.w = BUTTON_SIZE;
pagePrev.h = pageNext.h = BUTTON_SIZE;
pagePrev.y = pageNext.y = y;
pagePrev.x = x + paddingLeft;
pageNext.x = x + w - pageNext.w - paddingRight;
pageLabel.x = x + paddingLeft + (w - paddingLeft - paddingRight) / 2;
pageLabel.y = y + 5;
pageLabel.text = getLabelText();
return BUTTON_SIZE + 2;
}
protected abstract int resizeFooter(GuiContainer gui);
public void setVisible() {
2022-05-06 22:41:45 +03:00
if (grid.getPerPage() > 0) {
LayoutManager.addWidget(pagePrev);
LayoutManager.addWidget(pageNext);
LayoutManager.addWidget(pageLabel);
2022-08-31 17:43:22 +09:00
grid.setVisible();
2022-05-06 22:41:45 +03:00
}
}
@Override
public void update() {
grid.update();
}
2022-05-06 22:41:45 +03:00
@Override
public void draw(int mousex, int mousey) {
2022-05-06 22:41:45 +03:00
grid.draw(mousex, mousey);
}
@Override
public void postDraw(int mousex, int mousey) {
2022-05-06 22:41:45 +03:00
if (draggedStack != null) {
GuiContainerManager.drawItems.zLevel += 100;
GuiContainerManager.drawItem(mousex - 8, mousey - 8, draggedStack);
GuiContainerManager.drawItems.zLevel -= 100;
}
}
@Override
public void mouseDragged(int mousex, int mousey, int button, long heldTime) {
2024-09-12 19:14:00 +03:00
if (mouseDownSlot >= grid.realItems.size()) {
mouseDownSlot = -1;
}
if (mouseDownSlot >= 0 && draggedStack == null
&& NEIClientUtils.getHeldItem() == null
&& NEIClientConfig.hasSMPCounterPart()) {
2022-05-06 22:41:45 +03:00
ItemPanelSlot mouseOverSlot = getSlotMouseOver(mousex, mousey);
if (mouseOverSlot == null || mouseOverSlot.slotIndex != mouseDownSlot || heldTime > 500) {
draggedStack = getDraggedStackWithQuantity(mouseDownSlot);
mouseDownSlot = -1;
}
}
}
protected abstract ItemStack getDraggedStackWithQuantity(int mouseDownSlot);
@Override
public boolean handleClick(int mousex, int mousey, int button) {
if (handleClickExt(mousex, mousey, button)) return true;
2022-05-06 22:41:45 +03:00
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
if (hoverSlot != null) {
if (button == 2) {
if (hoverSlot.item != null) {
draggedStack = getDraggedStackWithQuantity(hoverSlot.slotIndex);
}
} else {
mouseDownSlot = hoverSlot.slotIndex;
}
2022-05-06 22:41:45 +03:00
return true;
}
return false;
}
@Override
public boolean handleClickExt(int mouseX, int mouseY, int button) {
2022-05-06 22:41:45 +03:00
2022-10-13 11:06:08 +03:00
if (ItemPanels.itemPanel.draggedStack != null && ItemPanels.bookmarkPanel.contains(mouseX, mouseY)) {
ItemPanels.bookmarkPanel.addOrRemoveItem(ItemPanels.itemPanel.draggedStack, null, null, false, true);
ItemPanels.itemPanel.draggedStack = null;
return true;
}
2022-05-06 22:41:45 +03:00
2022-10-13 11:06:08 +03:00
if (ItemPanels.itemPanel.draggedStack != null) {
return ItemPanels.itemPanel.handleDraggedClick(mouseX, mouseY, button);
}
if (ItemPanels.bookmarkPanel.draggedStack != null) {
return ItemPanels.bookmarkPanel.handleDraggedClick(mouseX, mouseY, button);
}
2022-10-13 11:06:08 +03:00
if (NEIClientUtils.getHeldItem() != null) {
final ItemStack draggedStack = NEIClientUtils.getHeldItem().copy();
2022-08-23 22:15:37 +03:00
return handleGUIContainerClick(draggedStack, mouseX, mouseY, button);
}
2022-05-06 22:41:45 +03:00
return false;
}
protected boolean handleDraggedClick(int mouseX, int mouseY, int button) {
2022-10-13 11:06:08 +03:00
if (draggedStack == null) {
return false;
}
if (handleGUIContainerClick(draggedStack, mouseX, mouseY, button)) {
if (draggedStack.stackSize == 0) {
draggedStack = null;
}
return true;
}
final GuiContainer gui = NEIClientUtils.getGuiContainer();
if (mouseX < gui.guiLeft || mouseY < gui.guiTop
|| mouseX >= gui.guiLeft + gui.xSize
|| mouseY >= gui.guiTop + gui.ySize) {
draggedStack = null;
}
return true;
2022-05-06 22:41:45 +03:00
}
protected boolean handleGUIContainerClick(final ItemStack draggedStack, int mouseX, int mouseY, int button) {
2022-05-06 22:41:45 +03:00
final GuiContainer gui = NEIClientUtils.getGuiContainer();
boolean handled = false;
try {
GuiInfo.readLock.lock();
for (INEIGuiHandler handler : GuiInfo.guiHandlers) {
if (handler.handleDragNDrop(gui, mouseX, mouseY, draggedStack, button)) {
handled = true;
break;
}
}
} finally {
GuiInfo.readLock.unlock();
}
return handled;
}
@Override
public void mouseUp(int mousex, int mousey, int button) {
2022-05-06 22:41:45 +03:00
ItemPanelSlot hoverSlot = getSlotMouseOver(mousex, mousey);
if (hoverSlot != null && hoverSlot.slotIndex == mouseDownSlot && draggedStack == null) {
ItemStack item = hoverSlot.item.copy();
2022-05-06 22:41:45 +03:00
2024-08-17 23:24:27 +03:00
if (NEIController.manager.window instanceof GuiRecipe || NEIClientUtils.shiftKey()
|| !NEIClientConfig.canCheatItem(item)) {
2022-05-06 22:41:45 +03:00
if (button == 0) {
GuiCraftingRecipe.openRecipeGui("item", item);
} else if (button == 1) {
GuiUsageRecipe.openRecipeGui("item", item);
}
mouseDownSlot = -1;
return;
}
NEIClientUtils.cheatItem(getDraggedStackWithQuantity(hoverSlot.slotIndex), button, -1);
}
mouseDownSlot = -1;
}
@Override
public boolean onMouseWheel(int i, int mousex, int mousey) {
if (!contains(mousex, mousey)) return false;
2022-05-06 22:41:45 +03:00
grid.shiftPage(-i);
return true;
}
@Override
public boolean handleKeyPress(int keyID, char keyChar) {
2022-05-06 22:41:45 +03:00
if (NEIClientConfig.isKeyHashDown("gui.next")) {
2022-05-06 22:41:45 +03:00
grid.shiftPage(1);
return true;
}
if (NEIClientConfig.isKeyHashDown("gui.prev")) {
2022-05-06 22:41:45 +03:00
grid.shiftPage(-1);
return true;
}
return false;
}
@Override
public ItemStack getStackMouseOver(int mousex, int mousey) {
2022-05-06 22:41:45 +03:00
ItemPanelSlot slot = getSlotMouseOver(mousex, mousey);
return slot == null ? null : slot.item;
}
public ItemPanelSlot getSlotMouseOver(int mousex, int mousey) {
2022-05-06 22:41:45 +03:00
return grid.getSlotMouseOver(mousex, mousey);
}
public int getPage() {
2022-06-23 01:03:29 +03:00
return grid.getPage();
2022-05-06 22:41:45 +03:00
}
public int getNumPages() {
2022-06-23 01:03:29 +03:00
return grid.getNumPages();
2022-05-06 22:41:45 +03:00
}
@Override
public boolean contains(int px, int py) {
2022-05-06 22:41:45 +03:00
return grid.contains(px, py);
}
2024-08-17 23:24:27 +03:00
@Override
public List<String> handleTooltip(GuiContainer gui, int mousex, int mousey, List<String> currenttip) {
return currenttip;
}
@Override
public List<String> handleItemDisplayName(GuiContainer gui, ItemStack itemstack, List<String> currenttip) {
return currenttip;
}
@Override
public List<String> handleItemTooltip(GuiContainer gui, ItemStack itemstack, int mousex, int mousey,
List<String> currenttip) {
return currenttip;
}
2022-05-06 22:41:45 +03:00
}