Skip to content

Commit

Permalink
slot fixes (#366)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yefancy authored Dec 19, 2021
1 parent cb12a95 commit b446ee1
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 47 deletions.
6 changes: 1 addition & 5 deletions src/main/java/gregtech/api/gui/INativeWidget.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package gregtech.api.gui;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
Expand All @@ -12,9 +11,6 @@
* Rendering is still handled by widget via helpers in {@link gregtech.api.gui.IRenderContext}
*/
public interface INativeWidget {

ItemStack VANILLA_LOGIC = new ItemStack(Items.AIR);

/**
* You should return MC slot handle instance you created earlier
*
Expand Down Expand Up @@ -43,7 +39,7 @@ default ItemStack onItemTake(EntityPlayer player, ItemStack stack, boolean simul

/**
* Called when slot is clicked in Container
* Return {@link INativeWidget#VANILLA_LOGIC} to fallback to vanilla logic
* Return null to fallback to vanilla logic
*/
ItemStack slotClick(int dragType, ClickType clickTypeIn, EntityPlayer player);

Expand Down
1 change: 0 additions & 1 deletion src/main/java/gregtech/api/gui/ModularUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public final class ModularUI implements ISizeProvider {
private ModularUIGui modularUIGui;

public boolean isJEIHandled;
public boolean needNativeClick;

/**
* UIHolder of this modular UI
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/gregtech/api/gui/impl/ModularUIContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,14 @@ public ItemStack slotClick(int slotId, int dragType, @Nonnull ClickType clickTyp
if (slotId >= 0 && slotId < inventorySlots.size()) {
Slot slot = getSlot(slotId);
ItemStack result = slotMap.get(slot).slotClick(dragType, clickTypeIn, player);
if (result == INativeWidget.VANILLA_LOGIC) {
if (result == null) {
return super.slotClick(slotId, dragType, clickTypeIn, player);
}
return result;
}
if (slotId == -999) {
super.slotClick(slotId, dragType, clickTypeIn, player);
}
return ItemStack.EMPTY;
}

Expand Down
40 changes: 25 additions & 15 deletions src/main/java/gregtech/api/gui/impl/ModularUIGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.lwjgl.input.Mouse;

import java.io.IOException;
import java.util.Set;

public class ModularUIGui extends GuiContainer implements IRenderContext {

Expand All @@ -28,6 +29,8 @@ public class ModularUIGui extends GuiContainer implements IRenderContext {
public static final float gColorForOverlay = 1;
public static final float bColorForOverlay = 1;
private float lastUpdate;
public int dragSplittingLimit;
public int dragSplittingButton;

public ModularUI getModularUI() {
return modularUI;
Expand Down Expand Up @@ -234,33 +237,41 @@ protected void mouseWheelMove(int mouseX, int mouseY, int wheelDelta) {
}
}

public Set<Slot> getDragSplittingSlots() {
return dragSplittingSlots;
}

public boolean getDragSplitting() {
return dragSplitting;
}

@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) {
for (int i = modularUI.guiWidgets.size() - 1; i >= 0; i--) {
Widget widget = modularUI.guiWidgets.get(i);
if(widget.isVisible() && widget.isActive() && widget.mouseClicked(mouseX, mouseY, mouseButton)) {
if (getModularUI().needNativeClick) {
super.mouseClicked(mouseX, mouseY, mouseButton);
getModularUI().needNativeClick = false;
}
return;
}
}
// super.mouseClicked(mouseX, mouseY, mouseButton);
}

public void superMouseClicked(int mouseX, int mouseY, int mouseButton) {
try {
super.mouseClicked(mouseX, mouseY, mouseButton);
} catch (Exception ignored) { }
}

@Override
protected void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) {
for (int i = modularUI.guiWidgets.size() - 1; i >= 0; i--) {
Widget widget = modularUI.guiWidgets.get(i);
if(widget.isVisible() && widget.isActive() && widget.mouseDragged(mouseX, mouseY, clickedMouseButton, timeSinceLastClick)) {
if (getModularUI().needNativeClick) {
super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick);
getModularUI().needNativeClick = false;
}
return;
}
}
}

public void superMouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) {
super.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick);
}

Expand All @@ -269,14 +280,13 @@ protected void mouseReleased(int mouseX, int mouseY, int state) {
for (int i = modularUI.guiWidgets.size() - 1; i >= 0; i--) {
Widget widget = modularUI.guiWidgets.get(i);
if(widget.isVisible() && widget.isActive() && widget.mouseReleased(mouseX, mouseY, state)) {
if (getModularUI().needNativeClick) {
super.mouseReleased(mouseX, mouseY, state);
getModularUI().needNativeClick = false;
}
return;
}
}
// super.mouseReleased(mouseX, mouseY, state);
}

public void superMouseReleased(int mouseX, int mouseY, int state) {
super.mouseReleased(mouseX, mouseY, state);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public PhantomSlotWidget(IItemHandlerModifiable itemHandler, int slotIndex, int
@Override
public boolean mouseClicked(int mouseX, int mouseY, int button) {
if (isMouseOverElement(mouseX, mouseY) && gui != null) {
gui.needNativeClick = true;
gui.getModularUIGui().superMouseClicked(mouseX, mouseY, button);
return true;
}
return false;
Expand Down
80 changes: 57 additions & 23 deletions src/main/java/gregtech/api/gui/widgets/SlotWidget.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gregtech.api.gui.IRenderContext;
import gregtech.api.gui.ISizeProvider;
import gregtech.api.gui.Widget;
import gregtech.api.gui.impl.ModularUIGui;
import gregtech.api.gui.resources.IGuiTexture;
import gregtech.api.util.Position;
import gregtech.api.util.Size;
Expand All @@ -13,6 +14,7 @@
import net.minecraft.client.renderer.RenderItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -64,11 +66,7 @@ protected Slot createSlot(IItemHandler itemHandler, int index) {

@Override
public void drawInForeground(int mouseX, int mouseY) {
if (isMouseOverElement(mouseX, mouseY) && isActive()) {
((ISlotWidget) slotReference).setHover(true);
} else {
((ISlotWidget) slotReference).setHover(false);
}
((ISlotWidget) slotReference).setHover(isMouseOverElement(mouseX, mouseY) && isActive());
}

@Override
Expand All @@ -81,18 +79,36 @@ public void drawInBackground(int mouseX, int mouseY, float partialTicks, IRender
backgroundTexture.draw(pos.x, pos.y, size.width, size.height);
}
}
GlStateManager.disableRescaleNormal();
GlStateManager.disableLighting();
RenderHelper.disableStandardItemLighting();
RenderHelper.enableStandardItemLighting();
RenderHelper.enableGUIStandardItemLighting();
GlStateManager.pushMatrix();
RenderItem itemRender = Minecraft.getMinecraft().getRenderItem();
itemRender.renderItemAndEffectIntoGUI(slotReference.getStack(), pos.x + 1, pos.y + 1);
itemRender.renderItemOverlayIntoGUI(Minecraft.getMinecraft().fontRenderer, slotReference.getStack(), pos.x + 1, pos.y + 1, null);
GlStateManager.enableAlpha();
GlStateManager.popMatrix();
RenderHelper.disableStandardItemLighting();
ItemStack itemStack = slotReference.getStack();
ModularUIGui modularUIGui = gui == null ? null : gui.getModularUIGui();
if (itemStack.isEmpty() && modularUIGui!= null && modularUIGui.getDragSplitting() && modularUIGui.getDragSplittingSlots().contains(slotReference)) { // draw split
int splitSize = modularUIGui.getDragSplittingSlots().size();
itemStack = gui.entityPlayer.inventory.getItemStack();
if (!itemStack.isEmpty() && splitSize > 1 && Container.canAddItemToSlot(slotReference, itemStack, true)) {
itemStack = itemStack.copy();
Container.computeStackSize(modularUIGui.getDragSplittingSlots(), modularUIGui.dragSplittingLimit, itemStack, slotReference.getStack().isEmpty() ? 0 : slotReference.getStack().getCount());
int k = Math.min(itemStack.getMaxStackSize(), slotReference.getItemStackLimit(itemStack));
if (itemStack.getCount() > k) {
itemStack.setCount(k);
}
}
}
if (!itemStack.isEmpty()) {
GlStateManager.enableBlend();
GlStateManager.enableDepth();
GlStateManager.disableRescaleNormal();
GlStateManager.disableLighting();
RenderHelper.disableStandardItemLighting();
RenderHelper.enableStandardItemLighting();
RenderHelper.enableGUIStandardItemLighting();
GlStateManager.pushMatrix();
RenderItem itemRender = Minecraft.getMinecraft().getRenderItem();
itemRender.renderItemAndEffectIntoGUI(itemStack, pos.x + 1, pos.y + 1);
itemRender.renderItemOverlayIntoGUI(Minecraft.getMinecraft().fontRenderer, itemStack, pos.x + 1, pos.y + 1, null);
GlStateManager.enableAlpha();
GlStateManager.popMatrix();
RenderHelper.disableStandardItemLighting();
}
if (isActive()) {
if (slotReference instanceof ISlotWidget) {
if (isMouseOverElement(mouseX, mouseY)) {
Expand All @@ -116,8 +132,22 @@ public void drawInBackground(int mouseX, int mouseY, float partialTicks, IRender

@Override
public boolean mouseClicked(int mouseX, int mouseY, int button) {
if (canTakeItems && isMouseOverElement(mouseX, mouseY) && gui != null) {
gui.needNativeClick = true;
if (isMouseOverElement(mouseX, mouseY) && gui != null) {
ModularUIGui modularUIGui = gui.getModularUIGui();
boolean last = modularUIGui.getDragSplitting();
gui.getModularUIGui().superMouseClicked(mouseX, mouseY, button);
if (last != modularUIGui.getDragSplitting()) {
modularUIGui.dragSplittingButton = button;
if (button == 0) {
modularUIGui.dragSplittingLimit = 0;
}
else if (button == 1) {
modularUIGui.dragSplittingLimit = 1;
}
else if (Minecraft.getMinecraft().gameSettings.keyBindPickBlock.isActiveAndMatches(button - 100)) {
modularUIGui.dragSplittingLimit = 2;
}
}
return true;
}
return false;
Expand All @@ -126,15 +156,19 @@ public boolean mouseClicked(int mouseX, int mouseY, int button) {
@Override
public boolean mouseReleased(int mouseX, int mouseY, int button) {
if (isMouseOverElement(mouseX, mouseY) && gui != null) {
gui.needNativeClick = true;
gui.getModularUIGui().superMouseReleased(mouseX, mouseY, button);
return true;
}
return super.mouseReleased(mouseX, mouseY, button);
return false;
}

@Override
public boolean mouseDragged(int mouseX, int mouseY, int button, long timeDragged) {
return super.mouseDragged(mouseX, mouseY, button, timeDragged);
if (isMouseOverElement(mouseX, mouseY) && gui != null) {
gui.getModularUIGui().superMouseClickMove(mouseX, mouseY, button, timeDragged);
return true;
}
return false;
}

@Override
Expand Down Expand Up @@ -201,7 +235,7 @@ public void onSlotChanged() {

@Override
public ItemStack slotClick(int dragType, ClickType clickTypeIn, EntityPlayer player) {
return INativeWidget.VANILLA_LOGIC;
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public PhantomWidget(int x, int y, Object defaultObj) {
}
}, true).setBackgroundTexture(TerminalTheme.COLOR_B_2).showTip(true)
.setFluidStackSupplier(() -> fluidStack,true);
slotWidget = new PhantomSlotWidget(itemHandler, 0, 0, 0);
slotWidget = new PhantomSlotWidget(itemHandler, 0, 0, 0) {
@Override
public boolean isEnabled() {
return isActive();
}
};
slotWidget.setChangeListener(()-> {
if (!itemHandler.getStackInSlot(0).isEmpty()) {
fluidStack = null;
Expand Down

0 comments on commit b446ee1

Please sign in to comment.