Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix glitches for SlotWidgets inside ScrollableListWidgets #1558

Merged
merged 3 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/java/gregtech/api/gui/IScissored.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package gregtech.api.gui;

import java.awt.Rectangle;

public interface IScissored {

Rectangle getScissor();
}
3 changes: 3 additions & 0 deletions src/main/java/gregtech/api/gui/Widget.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/gregtech/api/gui/impl/ModularUIGui.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -87,13 +90,23 @@ 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);
}
if (isPointInRegion(slot.xPos, slot.yPos, 16, 16, mouseX, mouseY) && slot.isEnabled()) {
renderSlotOverlay(slot);
setHoveredSlot(slot);
}
if (scissor != null) {
RenderUtil.popScissorFrame();
}
}

RenderHelper.disableStandardItemLighting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
37 changes: 29 additions & 8 deletions src/main/java/gregtech/api/gui/widgets/SlotWidget.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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() {
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down Expand Up @@ -167,6 +183,11 @@ public void onSlotChanged() {
public boolean isEnabled() {
return SlotWidget.this.isEnabled();
}

@Override
public Rectangle getScissor() {
return SlotWidget.this.scissor;
}
}

}