From dfa656f666947f548917650ecd3073593bc75e08 Mon Sep 17 00:00:00 2001 From: warjort Date: Mon, 5 Apr 2021 18:26:49 +0100 Subject: [PATCH] Fix glitches for SlotWidgets inside ScrollableListWidgets --- .../java/gregtech/api/gui/IScissored.java | 8 ++++ src/main/java/gregtech/api/gui/Widget.java | 3 ++ .../gregtech/api/gui/impl/ModularUIGui.java | 13 +++++++ .../api/gui/widgets/AbstractWidgetGroup.java | 6 +++ .../api/gui/widgets/ScrollableListWidget.java | 9 +++++ .../gregtech/api/gui/widgets/SlotWidget.java | 37 +++++++++++++++---- 6 files changed, 68 insertions(+), 8 deletions(-) create mode 100755 src/main/java/gregtech/api/gui/IScissored.java diff --git a/src/main/java/gregtech/api/gui/IScissored.java b/src/main/java/gregtech/api/gui/IScissored.java new file mode 100755 index 0000000000..7f93a85063 --- /dev/null +++ b/src/main/java/gregtech/api/gui/IScissored.java @@ -0,0 +1,8 @@ +package gregtech.api.gui; + +import java.awt.Rectangle; + +public interface IScissored { + + Rectangle getScissor(); +} diff --git a/src/main/java/gregtech/api/gui/Widget.java b/src/main/java/gregtech/api/gui/Widget.java index d53148ddf6..4fccba4dc1 100644 --- a/src/main/java/gregtech/api/gui/Widget.java +++ b/src/main/java/gregtech/api/gui/Widget.java @@ -102,6 +102,9 @@ private void recomputePosition() { onPositionUpdate(); } + public void applyScissor(final int parentX, final int parentY, final int parentWidth, final int parentHeight) { + } + protected void onPositionUpdate() { } diff --git a/src/main/java/gregtech/api/gui/impl/ModularUIGui.java b/src/main/java/gregtech/api/gui/impl/ModularUIGui.java index 550c8ddb9c..daa792d780 100644 --- a/src/main/java/gregtech/api/gui/impl/ModularUIGui.java +++ b/src/main/java/gregtech/api/gui/impl/ModularUIGui.java @@ -1,9 +1,11 @@ package gregtech.api.gui.impl; import gregtech.api.gui.IRenderContext; +import gregtech.api.gui.IScissored; import gregtech.api.gui.ModularUI; import gregtech.api.gui.Widget; import gregtech.api.net.PacketUIWidgetUpdate; +import gregtech.api.util.RenderUtil; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.inventory.GuiContainer; import net.minecraft.client.renderer.GlStateManager; @@ -18,6 +20,7 @@ import org.lwjgl.input.Keyboard; import org.lwjgl.input.Mouse; +import java.awt.Rectangle; import java.io.IOException; public class ModularUIGui extends GuiContainer implements IRenderContext { @@ -87,6 +90,13 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) { for (int i = 0; i < this.inventorySlots.inventorySlots.size(); ++i) { Slot slot = this.inventorySlots.inventorySlots.get(i); + Rectangle scissor = null; + if (slot instanceof IScissored) { + scissor = ((IScissored) slot).getScissor(); + if (scissor != null) { + RenderUtil.pushScissorFrame(scissor.x, scissor.y, scissor.width, scissor.height); + } + } if (slot.isEnabled()) { this.drawSlotContents(slot); } @@ -94,6 +104,9 @@ public void drawScreen(int mouseX, int mouseY, float partialTicks) { renderSlotOverlay(slot); setHoveredSlot(slot); } + if (scissor != null) { + RenderUtil.popScissorFrame(); + } } RenderHelper.disableStandardItemLighting(); diff --git a/src/main/java/gregtech/api/gui/widgets/AbstractWidgetGroup.java b/src/main/java/gregtech/api/gui/widgets/AbstractWidgetGroup.java index 7ae13ec9bb..a8642a3fef 100644 --- a/src/main/java/gregtech/api/gui/widgets/AbstractWidgetGroup.java +++ b/src/main/java/gregtech/api/gui/widgets/AbstractWidgetGroup.java @@ -56,6 +56,12 @@ protected void onPositionUpdate() { recomputeSize(); } + public void applyScissor(final int parentX, final int parentY, final int parentWidth, final int parentHeight) { + for (Widget widget : getContainedWidgets(true)) { + widget.applyScissor(parentX, parentY, parentWidth, parentHeight); + } + } + protected boolean recomputeSize() { if (isDynamicSized) { Size currentSize = getSize(); diff --git a/src/main/java/gregtech/api/gui/widgets/ScrollableListWidget.java b/src/main/java/gregtech/api/gui/widgets/ScrollableListWidget.java index 9cf4e53a9e..480a43a4ac 100644 --- a/src/main/java/gregtech/api/gui/widgets/ScrollableListWidget.java +++ b/src/main/java/gregtech/api/gui/widgets/ScrollableListWidget.java @@ -62,6 +62,8 @@ private void updateElementPositions() { widget.setParentPosition(childPosition); currentPosY += widget.getSize().getHeight(); totalListHeight += widget.getSize().getHeight(); + final Size size = getSize(); + widget.applyScissor(position.x, position.y, size.width - scrollPaneWidth, size.height); } this.totalListHeight = totalListHeight; this.slotHeight = widgets.isEmpty() ? 0 : totalListHeight / widgets.size(); @@ -105,6 +107,13 @@ public void drawInBackground(int mouseX, int mouseY, IRenderContext context) { super.drawInBackground(finalMouseX, finalMouseY, context)); } + public boolean isWidgetClickable(final Widget widget) { + if (!super.isWidgetClickable(widget)) { + return false; + } + return isBoxInsideScissor(widget.toRectangleBox()); + } + private boolean isPositionInsideScissor(int mouseX, int mouseY) { return isMouseOverElement(mouseX, mouseY) && !isOnScrollPane(mouseX, mouseY); } diff --git a/src/main/java/gregtech/api/gui/widgets/SlotWidget.java b/src/main/java/gregtech/api/gui/widgets/SlotWidget.java index f7f269ced8..4222ffdb83 100644 --- a/src/main/java/gregtech/api/gui/widgets/SlotWidget.java +++ b/src/main/java/gregtech/api/gui/widgets/SlotWidget.java @@ -1,7 +1,12 @@ package gregtech.api.gui.widgets; +import java.awt.Rectangle; + +import javax.annotation.Nonnull; + import gregtech.api.gui.INativeWidget; import gregtech.api.gui.IRenderContext; +import gregtech.api.gui.IScissored; import gregtech.api.gui.Widget; import gregtech.api.gui.resources.TextureArea; import gregtech.api.util.Position; @@ -15,8 +20,6 @@ import net.minecraftforge.items.IItemHandlerModifiable; import net.minecraftforge.items.SlotItemHandler; -import javax.annotation.Nonnull; - public class SlotWidget extends Widget implements INativeWidget { protected SlotItemHandler slotReference; @@ -29,6 +32,8 @@ public class SlotWidget extends Widget implements INativeWidget { protected TextureArea[] backgroundTexture; protected Runnable changeListener; + protected Rectangle scissor; + public SlotWidget(IItemHandler itemHandler, int slotIndex, int xPosition, int yPosition, boolean canTakeItems, boolean canPutItems) { super(new Position(xPosition, yPosition), new Size(18, 18)); this.canTakeItems = canTakeItems; @@ -43,7 +48,7 @@ protected SlotItemHandler createSlot(IItemHandler itemHandler, int index) { @Override @SideOnly(Side.CLIENT) public void drawInBackground(int mouseX, int mouseY, IRenderContext context) { - if (isEnabled && backgroundTexture != null) { + if (isEnabled() && backgroundTexture != null) { Position pos = getPosition(); Size size = getSize(); for (TextureArea backgroundTexture : this.backgroundTexture) { @@ -71,6 +76,11 @@ public void setEnabled(boolean enabled) { isEnabled = enabled; } + @Override + public void applyScissor(final int parentX, final int parentY, final int parentWidth, final int parentHeight) { + this.scissor = new Rectangle(parentX, parentY, parentWidth, parentHeight); + } + @Override public void detectAndSendChanges() { } @@ -99,20 +109,26 @@ public SlotLocationInfo getSlotLocationInfo() { } public boolean canPutStack(ItemStack stack) { - return isEnabled && canPutItems; + return isEnabled() && canPutItems; } public boolean canTakeStack(EntityPlayer player) { - return isEnabled && canTakeItems; + return isEnabled() && canTakeItems; } public boolean isEnabled() { - return isEnabled; + if (!this.isEnabled) { + return false; + } + if (this.scissor == null) { + return true; + } + return scissor.intersects(toRectangleBox()); } @Override public boolean canMergeSlot(ItemStack stack) { - return isEnabled; + return isEnabled(); } public void onSlotChanged() { @@ -129,7 +145,7 @@ public final SlotItemHandler getHandle() { return slotReference; } - protected class WidgetSlotDelegate extends SlotItemHandler { + protected class WidgetSlotDelegate extends SlotItemHandler implements IScissored { public WidgetSlotDelegate(IItemHandler itemHandler, int index, int xPosition, int yPosition) { super(itemHandler, index, xPosition, yPosition); @@ -167,6 +183,11 @@ public void onSlotChanged() { public boolean isEnabled() { return SlotWidget.this.isEnabled(); } + + @Override + public Rectangle getScissor() { + return SlotWidget.this.scissor; + } } }