From 0185cd64b10ed30af47d09e17d5e6062b09226f5 Mon Sep 17 00:00:00 2001 From: slprime <31038811+slprime@users.noreply.github.com> Date: Thu, 12 Nov 2026 01:35:53 +0200 Subject: [PATCH] Optimize GUI overlap scan for item panels (#1118) --- .../codechicken/nei/AbstractSubpanel.java | 9 +- .../codechicken/nei/GridOverlapScanner.java | 188 ++++++++++++++++++ src/main/java/codechicken/nei/ItemsGrid.java | 62 +++--- .../java/codechicken/nei/LayoutManager.java | 3 + .../java/codechicken/nei/NEIClientConfig.java | 8 - .../java/codechicken/nei/PanelWidget.java | 51 +++-- src/main/resources/assets/nei/lang/en_US.lang | 3 - src/main/resources/assets/nei/lang/ru_RU.lang | 3 - src/main/resources/assets/nei/lang/uk_UA.lang | 3 - .../java/codechicken/nei/ItemsGridTest.java | 43 +++- 10 files changed, 288 insertions(+), 85 deletions(-) create mode 100644 src/main/java/codechicken/nei/GridOverlapScanner.java diff --git a/src/main/java/codechicken/nei/AbstractSubpanel.java b/src/main/java/codechicken/nei/AbstractSubpanel.java index abc90957..d51b3c4c 100644 --- a/src/main/java/codechicken/nei/AbstractSubpanel.java +++ b/src/main/java/codechicken/nei/AbstractSubpanel.java @@ -8,6 +8,7 @@ import org.lwjgl.opengl.GL11; import codechicken.lib.vec.Rectangle4i; import codechicken.nei.ItemsGrid.ItemsGridSlot; import codechicken.nei.api.GuiInfo; +import codechicken.nei.api.INEIGuiHandler; import codechicken.nei.api.ShortcutInputHandler; import codechicken.nei.drawable.DrawableBuilder; import codechicken.nei.drawable.DrawableResource; @@ -55,12 +56,14 @@ public abstract class AbstractSubpanel lastGui = new WeakReference<>(null); + + private boolean invalidated = true; + + private final Rectangle4i rect = new Rectangle4i(); + private boolean[] checked = new boolean[0]; + private int[] queue = new int[0]; + private int tail; + + private Predicate isHidden; + private boolean[] hidden; + private int hiddenCount; + private int left; + private int top; + private int slotSize; + private int columns; + + public static void requestScan() { + scanRequested = true; + } + + public static void beginFrame(GuiContainer gui, boolean shouldShowTooltip) { + + if (wasShowTooltip != shouldShowTooltip) { + scanRequested = true; + } + + wasShowTooltip = shouldShowTooltip; + + if (lastGui.get() != gui) { + lastGui = new WeakReference<>(gui); + scanRequested = true; + } + + scanFrame = scanRequested; + scanRequested = false; + } + + public static boolean isScanFrame() { + return scanFrame; + } + + public void invalidate() { + this.invalidated = true; + } + + public boolean shouldRefresh() { + final boolean refresh = this.invalidated || scanFrame; + this.invalidated = false; + return refresh; + } + + public int scan(Predicate isHidden, int left, int top, int slotSize, int rows, int columns, + boolean seedLeft, boolean seedRight, boolean[] hidden) { + final int size = rows * columns; + + if (this.checked.length < size) { + this.checked = new boolean[size]; + this.queue = new int[size]; + } else { + Arrays.fill(this.checked, 0, size, false); + } + + this.isHidden = isHidden; + this.hidden = hidden; + this.hiddenCount = 0; + this.tail = 0; + this.left = left; + this.top = top; + this.slotSize = slotSize; + this.columns = columns; + + for (int r = 0; r < rows; r++) { + if (seedLeft) { + probe(r, 0); + } + if (seedRight) { + probe(r, columns - 1); + } + } + + for (int head = 0; head < this.tail; head++) { + final int row = this.queue[head] / columns; + final int column = this.queue[head] % columns; + + for (int r = Math.max(0, row - 1); r <= Math.min(rows - 1, row + 1); r++) { + for (int c = Math.max(0, column - 1); c <= Math.min(columns - 1, column + 1); c++) { + probe(r, c); + } + } + } + + for (int r = 0; r < rows; r++) { + for (int c = 0; c < columns; c++) { + if (this.checked[r * columns + c]) { + continue; + } + + int c1 = c; + while (c1 + 1 < columns && !this.checked[r * columns + c1 + 1]) { + c1++; + } + + int r1 = r; + while (r1 + 1 < rows && isUnchecked(r1 + 1, c, c1)) { + r1++; + } + + scanArea(r, r1, c, c1); + c = c1; + } + } + + this.isHidden = null; + this.hidden = null; + + return this.hiddenCount; + } + + private void probe(int row, int column) { + final int idx = row * this.columns + column; + + if (!this.checked[idx]) { + this.checked[idx] = true; + + if (test(row, row, column, column)) { + this.hidden[idx] = true; + this.queue[this.tail++] = idx; + this.hiddenCount++; + } + } + } + + private void scanArea(int r0, int r1, int c0, int c1) { + + if (!test(r0, r1, c0, c1)) { + for (int r = r0; r <= r1; r++) { + Arrays.fill(this.checked, r * this.columns + c0, r * this.columns + c1 + 1, true); + } + } else if (r0 == r1 && c0 == c1) { + this.checked[r0 * this.columns + c0] = true; + this.hidden[r0 * this.columns + c0] = true; + this.hiddenCount++; + } else if (r1 - r0 >= c1 - c0) { + final int mid = (r0 + r1) / 2; + scanArea(r0, mid, c0, c1); + scanArea(mid + 1, r1, c0, c1); + } else { + final int mid = (c0 + c1) / 2; + scanArea(r0, r1, c0, mid); + scanArea(r0, r1, mid + 1, c1); + } + } + + private boolean isUnchecked(int row, int c0, int c1) { + for (int c = c0; c <= c1; c++) { + if (this.checked[row * this.columns + c]) { + return false; + } + } + return true; + } + + private boolean test(int r0, int r1, int c0, int c1) { + this.rect.set( + this.left + c0 * this.slotSize, + this.top + r0 * this.slotSize, + (c1 - c0 + 1) * this.slotSize, + (r1 - r0 + 1) * this.slotSize); + return this.isHidden.test(this.rect); + } + +} diff --git a/src/main/java/codechicken/nei/ItemsGrid.java b/src/main/java/codechicken/nei/ItemsGrid.java index e8595585..481b32e0 100644 --- a/src/main/java/codechicken/nei/ItemsGrid.java +++ b/src/main/java/codechicken/nei/ItemsGrid.java @@ -282,6 +282,8 @@ public abstract class ItemsGrid getItems() { @@ -359,12 +361,13 @@ public abstract class ItemsGrid 0) { final INEIGuiHandler[] handlers = GuiInfo.getGuiHandlersSnapshot(); - if (NEIClientConfig.optimizeGuiOverlapComputation()) { - checkGuiOverlap(gui, handlers, 0, columns - 2, 1); - checkGuiOverlap(gui, handlers, columns - 1, 1, -1); - } else { - checkGuiOverlap(gui, handlers, 0, columns, 1); - } + final Rectangle4i origin = getSlotRect(0, 0); + + this.perPage -= this.overlapScanner.scan( + rect -> GuiInfo.hideItemPanelSlot(gui, rect, handlers), + origin.x, + origin.y, + SLOT_SIZE, + this.rows, + this.columns, + origin.x + this.columns * SLOT_SIZE > gui.guiLeft, + origin.x < gui.guiLeft + gui.xSize, + slotMap); } - if (oldRows != rows || oldColumns != columns || !Arrays.equals(oldSlotMap, invalidSlotMap)) { + this.invalidSlotMap = slotMap; + + if (!Arrays.equals(oldSlotMap, slotMap)) { onGridChanged(); } } - private void checkGuiOverlap(GuiContainer gui, INEIGuiHandler[] handlers, int start, int end, int dir) { - boolean validColumn = false; - - for (int c = start; c != end && (!NEIClientConfig.optimizeGuiOverlapComputation() || !validColumn); c += dir) { - validColumn = true; - - for (int r = 0; r < rows; r++) { - final int idx = columns * r + c; - if (idx >= 0 && idx < invalidSlotMap.length - && !invalidSlotMap[idx] - && GuiInfo.hideItemPanelSlot(gui, getSlotRect(r, c), handlers)) { - invalidSlotMap[idx] = true; - validColumn = false; - perPage--; - } - } - } - } - public T getSlotMouseOver(int mousex, int mousey) { if (!contains(mousex, mousey)) { return null; diff --git a/src/main/java/codechicken/nei/LayoutManager.java b/src/main/java/codechicken/nei/LayoutManager.java index 0b5c0dea..7caabfa6 100644 --- a/src/main/java/codechicken/nei/LayoutManager.java +++ b/src/main/java/codechicken/nei/LayoutManager.java @@ -388,6 +388,8 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH } public static void layout(GuiContainer gui) { + GridOverlapScanner.beginFrame(gui, GuiContainerManager.shouldShowTooltip(gui)); + VisiblityData visiblity = new VisiblityData(); visiblity.showNEI = !isHidden(); visiblity.showBookmarkPanel = !isBookmarkPanelHidden(); @@ -821,6 +823,7 @@ public class LayoutManager implements IContainerInputHandler, IContainerTooltipH if (!isEnabled()) return; + GridOverlapScanner.requestScan(); AutoFocusWidget.instance.guiTick(); for (Widget widget : drawWidgets) widget.update(); diff --git a/src/main/java/codechicken/nei/NEIClientConfig.java b/src/main/java/codechicken/nei/NEIClientConfig.java index 1a532500..1af5bbc3 100644 --- a/src/main/java/codechicken/nei/NEIClientConfig.java +++ b/src/main/java/codechicken/nei/NEIClientConfig.java @@ -498,10 +498,6 @@ public class NEIClientConfig { tag.getTag("inventory.showItemQuantityWidget").setComment("Show Item Quantity Widget").getBooleanValue(true); API.addOption(new OptionToggleButton("inventory.showItemQuantityWidget", true)); - tag.getTag("inventory.optimize_gui_overlap_computation").setComment("Optimize computation for GUI overlap") - .getBooleanValue(false); - API.addOption(new OptionToggleButton("inventory.optimize_gui_overlap_computation", true)); - tag.getTag("inventory.autocrafting").setComment("Autocrafting").getBooleanValue(true); API.addOption(new OptionToggleButton("inventory.autocrafting", true)); @@ -1160,10 +1156,6 @@ public class NEIClientConfig { return getBooleanSetting("inventory.bookmarks.craftingTreeZoomWithoutModifier"); } - public static boolean optimizeGuiOverlapComputation() { - return getBooleanSetting("inventory.optimize_gui_overlap_computation"); - } - public static boolean showCycledIngredientsTooltip() { return getBooleanSetting("inventory.guirecipe.cycledIngredientsTooltip"); } diff --git a/src/main/java/codechicken/nei/PanelWidget.java b/src/main/java/codechicken/nei/PanelWidget.java index 11d5041f..8fca99bf 100644 --- a/src/main/java/codechicken/nei/PanelWidget.java +++ b/src/main/java/codechicken/nei/PanelWidget.java @@ -29,6 +29,9 @@ public abstract class PanelWidget getItems() { return this.grid.getItems(); } @@ -105,26 +108,15 @@ public abstract class PanelWidget= w) { + if (paddingLeft + paddingRight >= this.w) { return 0; } @@ -132,16 +124,35 @@ public abstract class PanelWidget { private int overlapRefreshes; - @Override - protected int getGridRenderingCacheMode() { - return 1; - } - @Override protected void updateGuiOverlapSlots(GuiContainer gui) { overlapRefreshes++;