Skip to content

Commit

Permalink
Merge pull request #7 from Skiftcha/chisel-gui-scroll
Browse files Browse the repository at this point in the history
Chisel gui scroll
  • Loading branch information
Dream-Master authored Dec 15, 2020
2 parents acdd403 + f728eb1 commit 53c72a9
Show file tree
Hide file tree
Showing 7 changed files with 322 additions and 124 deletions.
258 changes: 146 additions & 112 deletions src/main/java/team/chisel/client/gui/GuiChisel.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.GL11;
import team.chisel.client.gui.widgets.GuiButtonChiselMode;
import team.chisel.inventory.ContainerChisel;
Expand All @@ -28,116 +29,149 @@
@SideOnly(Side.CLIENT)
public class GuiChisel extends GuiContainer {

public static final ResourceLocation TEXTURE_RESOURCE_LOCATION = new ResourceLocation(MOD_ID, "textures/chisel2GuiScroll.png");
public static final int TEXTURE_WIDTH = 266;
public static final int TEXTURE_HEIGHT = 250;
public static final int GUI_WIDTH = 266;
public static final int GUI_HEIGHT = 202;
public static final int CHISEL_INPUT_SIZE = 48;
private EntityPlayer player;
private ContainerChisel container;
private IChiselMode currentMode;

public GuiChisel(InventoryPlayer iinventory, InventoryChiselSelection menu) {
super(new ContainerChisel(iinventory, menu));
player = iinventory.player;
xSize = GUI_WIDTH;
ySize = GUI_HEIGHT;

container = (ContainerChisel) inventorySlots;
}

@Override
public void onGuiClosed() {
super.onGuiClosed();
inventorySlots.onContainerClosed(player);
}

@Override
@SuppressWarnings("unchecked")
public void initGui() {
super.initGui();
currentMode = General.getChiselMode(container.chisel);

if (showMode()) {
buttonList.add(new GuiButtonChiselMode(0, guiLeft + 12, guiTop + 132, this));
}
}

@Override
public void updateScreen() {
super.updateScreen();
final ItemStack held = player.getCurrentEquippedItem();
if (held == null || !(held.getItem() instanceof IChiselItem)) {
mc.displayGuiScreen(null);
}
}

private boolean showMode() {
return container.chisel != null
&& container.chisel.getItem() instanceof IChiselItem
&& ((IChiselItem) container.chisel.getItem()).hasModes(container.chisel);
}

@Override
protected void drawGuiContainerForegroundLayer(final int mouseX, final int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

final String titleText = I18n.format(container.inventory.getInventoryName() + ".title");
fontRendererObj.drawSplitString(titleText, 50 - fontRendererObj.getStringWidth(titleText) / 2, 60, 40, 0x404040);

if (showMode()) {
final String modeText = I18n.format(container.inventory.getInventoryName() + ".mode");
fontRendererObj.drawString(modeText, 12 + (GuiButtonChiselMode.BUTTON_WIDTH - fontRendererObj.getStringWidth(modeText)) / 2, 122, 0x404040);
}
}

@Override
protected void drawGuiContainerBackgroundLayer(final float partialTicks, final int mouseX, final int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glDisable(GL11.GL_LIGHTING);

mc.getTextureManager().bindTexture(TEXTURE_RESOURCE_LOCATION);
// drawModalRectWithCustomSizedTexture: x, y, u, v, width, height, textureWidth, textureHeight
func_146110_a(guiLeft, guiTop, 0, 0, xSize, ySize, TEXTURE_WIDTH, TEXTURE_HEIGHT);

Slot main = (Slot) container.inventorySlots.get(InventoryChiselSelection.normalSlots);
if (main.getStack() == null) {
func_146110_a(guiLeft + 8, guiTop + 8, 0, GUI_HEIGHT, CHISEL_INPUT_SIZE, CHISEL_INPUT_SIZE, TEXTURE_WIDTH, TEXTURE_HEIGHT);
}
}

@Override
protected void actionPerformed(final GuiButton button) {
if (button.id == 0) {
if (container.chisel != null && container.chisel.getItem() instanceof IAdvancedChisel) {
final IAdvancedChisel item = (IAdvancedChisel) container.chisel.getItem();
currentMode = item.getNextMode(container.chisel, currentMode);
} else {
currentMode = ChiselMode.next(currentMode);
}
PacketHandler.INSTANCE.sendToServer(new MessageChiselMode(currentMode));
}
super.actionPerformed(button);
}

@Override
protected void func_146977_a(final Slot slot) {
if (slot instanceof SlotChiselInput) {
GL11.glPushMatrix();
GL11.glScalef(2, 2, 2);
slot.xDisplayPosition -= 16;
slot.yDisplayPosition -= 16;
super.func_146977_a(slot);
slot.xDisplayPosition += 16;
slot.yDisplayPosition += 16;
GL11.glPopMatrix();
} else {
super.func_146977_a(slot);
}
}

public IChiselMode getCurrentMode() {
return currentMode;
}
public static final ResourceLocation TEXTURE_RESOURCE_LOCATION = new ResourceLocation(MOD_ID, "textures/chisel2GuiScroll.png");
public static final int TEXTURE_WIDTH = 266;
public static final int TEXTURE_HEIGHT = 250;
public static final int GUI_WIDTH = 266;
public static final int GUI_HEIGHT = 202;
public static final int CHISEL_INPUT_SIZE = 48;
private EntityPlayer player;
private ContainerChisel container;
private IChiselMode currentMode;
private GuiScrollbar scrollBar;
private InventoryChiselSelection menu;

public GuiChisel(InventoryPlayer iinventory, InventoryChiselSelection menu) {
super(new ContainerChisel(iinventory, menu));
scrollBar = new GuiScrollbar();
this.menu = menu;
player = iinventory.player;
xSize = GUI_WIDTH;
ySize = GUI_HEIGHT;

container = (ContainerChisel) inventorySlots;
scrollBar.setContainerChisel(container);
}

@Override
public void onGuiClosed() {
super.onGuiClosed();
inventorySlots.onContainerClosed(player);
}

@Override
@SuppressWarnings("unchecked")
public void initGui() {
super.initGui();
currentMode = General.getChiselMode(container.chisel);

if (showMode()) {
buttonList.add(new GuiButtonChiselMode(0, guiLeft + 12, guiTop + 132, this));
}

scrollBar.setLeft(246);
scrollBar.setTop(8);
scrollBar.setScrollHeight(106);
}

@Override
public void updateScreen() {
super.updateScreen();
final ItemStack held = player.getCurrentEquippedItem();
if (held == null || !(held.getItem() instanceof IChiselItem)) {
mc.displayGuiScreen(null);
}
}

private boolean showMode() {
return container.chisel != null
&& container.chisel.getItem() instanceof IChiselItem
&& ((IChiselItem) container.chisel.getItem()).hasModes(container.chisel);
}

@Override
protected void drawGuiContainerForegroundLayer(final int mouseX, final int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

final String titleText = I18n.format(container.inventory.getInventoryName() + ".title");
fontRendererObj.drawSplitString(titleText, 50 - fontRendererObj.getStringWidth(titleText) / 2, 60, 40, 0x404040);

if (showMode()) {
final String modeText = I18n.format(container.inventory.getInventoryName() + ".mode");
fontRendererObj.drawString(modeText, 12 + (GuiButtonChiselMode.BUTTON_WIDTH - fontRendererObj.getStringWidth(modeText)) / 2, 122, 0x404040);
}

scrollBar.draw(this);
}

@Override
protected void drawGuiContainerBackgroundLayer(final float partialTicks, final int mouseX, final int mouseY) {
GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
GL11.glDisable(GL11.GL_LIGHTING);

mc.getTextureManager().bindTexture(TEXTURE_RESOURCE_LOCATION);
// drawModalRectWithCustomSizedTexture: x, y, u, v, width, height, textureWidth, textureHeight
func_146110_a(guiLeft, guiTop, 0, 0, xSize, ySize, TEXTURE_WIDTH, TEXTURE_HEIGHT);

Slot main = (Slot) container.inventorySlots.get(InventoryChiselSelection.normalSlots);
if (main.getStack() == null) {
func_146110_a(guiLeft + 8, guiTop + 8, 0, GUI_HEIGHT, CHISEL_INPUT_SIZE, CHISEL_INPUT_SIZE, TEXTURE_WIDTH, TEXTURE_HEIGHT);
}
}

@Override
protected void actionPerformed(final GuiButton button) {
if (button.id == 0) {
if (container.chisel != null && container.chisel.getItem() instanceof IAdvancedChisel) {
final IAdvancedChisel item = (IAdvancedChisel) container.chisel.getItem();
currentMode = item.getNextMode(container.chisel, currentMode);
} else {
currentMode = ChiselMode.next(currentMode);
}
PacketHandler.INSTANCE.sendToServer(new MessageChiselMode(currentMode));
}
super.actionPerformed(button);
}

@Override
protected void func_146977_a(final Slot slot) {
if (slot instanceof SlotChiselInput) {
GL11.glPushMatrix();
GL11.glScalef(2, 2, 2);
slot.xDisplayPosition -= 16;
slot.yDisplayPosition -= 16;
super.func_146977_a(slot);
slot.xDisplayPosition += 16;
slot.yDisplayPosition += 16;
GL11.glPopMatrix();
} else {
super.func_146977_a(slot);
}
}

@Override
protected void mouseClickMove(int x, int y, int c, long d) {
scrollBar.click(x - guiLeft, y - guiTop);
super.mouseClickMove(x, y, c, d);
}

@Override
protected void mouseClicked(int xCoord, int yCoord, int btn) {
scrollBar.click(xCoord - guiLeft, yCoord - guiTop);
super.mouseClicked(xCoord, yCoord, btn);
}

@Override
public void handleMouseInput() {
super.handleMouseInput();

int i = Mouse.getEventDWheel();
if (i != 0 && scrollBar != null) {
scrollBar.wheel(i);
}
}

public IChiselMode getCurrentMode() {
return currentMode;
}
}
95 changes: 95 additions & 0 deletions src/main/java/team/chisel/client/gui/GuiScrollbar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package team.chisel.client.gui;

import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;
import team.chisel.inventory.ContainerChisel;
import team.chisel.network.PacketHandler;
import team.chisel.network.message.MessageScrollUpdate;

public class GuiScrollbar {
public static final ResourceLocation TEXTURE_RESOURCE_LOCATION = new ResourceLocation("minecraft", "textures/gui/container/creative_inventory/tabs.png");

private int displayX = 0;
private int displayY = 0;
private int width = 12;
private int height = 15;

private int rowSize = 10;
private int rowsOnPage = 6;

private int scrollHeight = height;
private ContainerChisel container;

public void draw(GuiChisel g) {
// chisel2GuiScroll scroll button texture coordinates work strange (maybe because of alignment) so taking vanilla one
g.mc.getTextureManager().bindTexture(TEXTURE_RESOURCE_LOCATION);
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

if (getMaxScroll() == 0) {
g.drawTexturedModalRect(displayX, displayY, 232 + width, 0, width, height);
} else {
int offset = getCurrentScroll() * (scrollHeight - height) / getMaxScroll();
g.drawTexturedModalRect(displayX, offset + displayY, 232, 0, width, height);
}
}

public GuiScrollbar setScrollHeight(int scrollHeight) {
this.scrollHeight = scrollHeight;
return this;
}

public GuiScrollbar setLeft(int displayX) {
this.displayX = displayX;
return this;
}

public GuiScrollbar setTop(int displayY) {
this.displayY = displayY;
return this;
}

private int getMaxScroll() {
int maxScroll = (container.inventory.getMaxScroll() + rowSize - 1) / rowSize - rowsOnPage;

if (maxScroll <= 0) {
maxScroll = 0;
}

return maxScroll;
}

public int getCurrentScroll() {
return container.inventory.getCurrentScroll() / rowSize;
}

private void setCurrentScroll(int scroll) {
container.inventory.setCurrentScroll(scroll * rowSize);
PacketHandler.INSTANCE.sendToServer(new MessageScrollUpdate(scroll * rowSize));
}

private int applyRange(int scroll) {
return Math.max(Math.min(scroll, getMaxScroll()), 0);
}

void click(int x, int y) {
if (getMaxScroll() == 0) {
return;
}

if (x > displayX && x <= displayX + width) {
if (y > displayY && y <= displayY + scrollHeight) {
int currentScroll = (y - displayY - height / 2);
currentScroll = Math.round(1.0f * currentScroll * getMaxScroll() / (scrollHeight - height));
setCurrentScroll(applyRange(currentScroll));
}
}
}

void wheel(int delta) {
setCurrentScroll(applyRange(getCurrentScroll() + Math.max(Math.min(-delta, 1), -1)));
}

public void setContainerChisel(ContainerChisel container) {
this.container = container;
}
}
6 changes: 6 additions & 0 deletions src/main/java/team/chisel/compat/Compatibility.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import java.util.List;
import java.util.Map;

import codechicken.nei.api.GuiInfo;
import net.minecraft.block.Block;
import net.minecraft.item.ItemStack;
import team.chisel.carving.Carving;
import team.chisel.client.gui.GuiChisel;
import team.chisel.utils.RecipeUtil;

import com.cricketcraft.chisel.api.carving.ICarvingGroup;
Expand Down Expand Up @@ -86,6 +88,10 @@ public static void init(FMLPostInitializationEvent event) {
if (Loader.isModLoaded("EE3")) {
loadEE3Values();
}
if (Loader.isModLoaded("NotEnoughItems")) {
// disable chiseling by scrolling over itemstack
GuiInfo.customSlotGuis.add(GuiChisel.class);
}
}


Expand Down
Loading

0 comments on commit 53c72a9

Please sign in to comment.