Skip to content

Commit

Permalink
Workbenches now have a background, render tool tips correctly, and ha…
Browse files Browse the repository at this point in the history
…ve support for the crafting book. Fixes #380
  • Loading branch information
alexbegt committed Jul 8, 2017
1 parent c534e2a commit 77612d8
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 31 deletions.
Expand Up @@ -28,6 +28,17 @@ public FurnaceGui(InventoryPlayer playerInv, IInventory furnaceInv)
this.tileFurnace = furnaceInv;
}

/**
* Draws the screen and all the components in it.
*/
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
this.drawDefaultBackground();
super.drawScreen(mouseX, mouseY, partialTicks);
this.renderHoveredToolTip(mouseX, mouseY);
}

/**
* Draw the foreground layer for the GuiContainer (everything in front of the items)
*/
Expand Down
161 changes: 155 additions & 6 deletions src/main/java/com/progwml6/natura/common/gui/client/WorkbenchGui.java
@@ -1,30 +1,93 @@
package com.progwml6.natura.common.gui.client;

import com.progwml6.natura.common.gui.common.WorkbenchContainer;
import java.io.IOException;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiButtonImage;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.gui.recipebook.GuiRecipeBook;
import net.minecraft.client.gui.recipebook.IRecipeShownListener;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.resources.I18n;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.ContainerWorkbench;
import net.minecraft.inventory.Slot;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class WorkbenchGui extends GuiContainer
public class WorkbenchGui extends GuiContainer implements IRecipeShownListener
{
private static final ResourceLocation CRAFTING_TABLE_GUI_TEXTURES = new ResourceLocation("textures/gui/container/crafting_table.png");

private GuiButtonImage recipeButton;

private final GuiRecipeBook recipeBookGui;

private boolean widthTooNarrow;

public WorkbenchGui(InventoryPlayer playerInv, World worldIn)
{
this(playerInv, worldIn, BlockPos.ORIGIN);
}

public WorkbenchGui(InventoryPlayer playerInv, World worldIn, BlockPos blockPosition)
{
super(new WorkbenchContainer(playerInv, worldIn, blockPosition));
super(new ContainerWorkbench(playerInv, worldIn, blockPosition));
this.recipeBookGui = new GuiRecipeBook();
}

/**
* Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
* window resizes, the buttonList is cleared beforehand.
*/
@Override
public void initGui()
{
super.initGui();
this.widthTooNarrow = this.width < 379;
this.recipeBookGui.init(this.width, this.height, this.mc, this.widthTooNarrow, this.inventorySlots, ((ContainerWorkbench) this.inventorySlots).craftMatrix);
this.guiLeft = this.recipeBookGui.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
this.recipeButton = new GuiButtonImage(10, this.guiLeft + 5, this.height / 2 - 49, 20, 18, 0, 168, 19, CRAFTING_TABLE_GUI_TEXTURES);
this.buttonList.add(this.recipeButton);
}

/**
* Called from the main game loop to update the screen.
*/
@Override
public void updateScreen()
{
super.updateScreen();
this.recipeBookGui.tick();
}

/**
* Draws the screen and all the components in it.
*/
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks)
{
this.drawDefaultBackground();

if (this.recipeBookGui.isVisible() && this.widthTooNarrow)
{
this.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
this.recipeBookGui.render(mouseX, mouseY, partialTicks);
}
else
{
this.recipeBookGui.render(mouseX, mouseY, partialTicks);
super.drawScreen(mouseX, mouseY, partialTicks);
this.recipeBookGui.renderGhostRecipe(this.guiLeft, this.guiTop, true, partialTicks);
}

this.renderHoveredToolTip(mouseX, mouseY);
this.recipeBookGui.renderTooltip(this.guiLeft, this.guiTop, mouseX, mouseY);
}

/**
Expand All @@ -33,8 +96,8 @@ public WorkbenchGui(InventoryPlayer playerInv, World worldIn, BlockPos blockPosi
@Override
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
{
this.fontRenderer.drawString(I18n.format("container.crafting", new Object[0]), 28, 6, 4210752);
this.fontRenderer.drawString(I18n.format("container.inventory", new Object[0]), 8, this.ySize - 96 + 2, 4210752);
this.fontRenderer.drawString(I18n.format("container.crafting"), 28, 6, 4210752);
this.fontRenderer.drawString(I18n.format("container.inventory"), 8, this.ySize - 96 + 2, 4210752);
}

/**
Expand All @@ -45,8 +108,94 @@ protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, i
{
GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
this.mc.getTextureManager().bindTexture(CRAFTING_TABLE_GUI_TEXTURES);
int i = (this.width - this.xSize) / 2;
int i = this.guiLeft;
int j = (this.height - this.ySize) / 2;
this.drawTexturedModalRect(i, j, 0, 0, this.xSize, this.ySize);
}

/**
* Test if the 2D point is in a rectangle (relative to the GUI). Args : rectX, rectY, rectWidth, rectHeight, pointX,
* pointY
*/
@Override
protected boolean isPointInRegion(int rectX, int rectY, int rectWidth, int rectHeight, int pointX, int pointY)
{
return (!this.widthTooNarrow || !this.recipeBookGui.isVisible()) && super.isPointInRegion(rectX, rectY, rectWidth, rectHeight, pointX, pointY);
}

/**
* Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
*/
@Override
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
{
if (!this.recipeBookGui.mouseClicked(mouseX, mouseY, mouseButton))
{
if (!this.widthTooNarrow || !this.recipeBookGui.isVisible())
{
super.mouseClicked(mouseX, mouseY, mouseButton);
}
}
}

@Override
protected boolean hasClickedOutside(int p_193983_1_, int p_193983_2_, int p_193983_3_, int p_193983_4_)
{
boolean flag = p_193983_1_ < p_193983_3_ || p_193983_2_ < p_193983_4_ || p_193983_1_ >= p_193983_3_ + this.xSize || p_193983_2_ >= p_193983_4_ + this.ySize;
return this.recipeBookGui.hasClickedOutside(p_193983_1_, p_193983_2_, this.guiLeft, this.guiTop, this.xSize, this.ySize) && flag;
}

/**
* Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
*/
@Override
protected void actionPerformed(GuiButton button) throws IOException
{
if (button.id == 10)
{
this.recipeBookGui.initVisuals(this.widthTooNarrow, ((ContainerWorkbench) this.inventorySlots).craftMatrix);
this.recipeBookGui.toggleVisibility();
this.guiLeft = this.recipeBookGui.updateScreenPosition(this.widthTooNarrow, this.width, this.xSize);
this.recipeButton.setPosition(this.guiLeft + 5, this.height / 2 - 49);
}
}

/**
* Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
* KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
*/
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException
{
if (!this.recipeBookGui.keyPressed(typedChar, keyCode))
{
super.keyTyped(typedChar, keyCode);
}
}

/**
* Called when the mouse is clicked over a slot or outside the gui.
*/
@Override
protected void handleMouseClick(Slot slotIn, int slotId, int mouseButton, ClickType type)
{
super.handleMouseClick(slotIn, slotId, mouseButton, type);
this.recipeBookGui.slotClicked(slotIn);
}

@Override
public void recipesUpdated()
{
this.recipeBookGui.recipesUpdated();
}

/**
* Called when the screen is unloaded. Used to disable keyboard repeat events
*/
@Override
public void onGuiClosed()
{
this.recipeBookGui.removed();
super.onGuiClosed();
}
}
Expand Up @@ -113,10 +113,6 @@ public boolean canInteractWith(EntityPlayer playerIn)
/**
* Handle when the stack in slot {@code index} is shift-clicked. Normally this moves the stack between the player
* inventory and the other inventory(s).
*
* @param playerIn Player that interacted with this {@code Container}.
* @param index Index of the {@link Slot}. This index is relative to the list of slots in this {@code Container},
* {@link #inventorySlots}.
*/
@Override
public ItemStack transferStackInSlot(EntityPlayer playerIn, int index)
Expand Down
Expand Up @@ -33,7 +33,6 @@ public WorkbenchContainer(InventoryPlayer playerInventory, World worldIn, BlockP
this.world = worldIn;
this.pos = posIn;
this.player = playerInventory.player;

this.addSlotToContainer(new SlotCrafting(playerInventory.player, this.craftMatrix, this.craftResult, 0, 124, 35));

for (int i = 0; i < 3; ++i)
Expand All @@ -56,8 +55,6 @@ public WorkbenchContainer(InventoryPlayer playerInventory, World worldIn, BlockP
{
this.addSlotToContainer(new Slot(playerInventory, l, 8 + l * 18, 142));
}

this.onCraftMatrixChanged(this.craftMatrix);
}

/**
Expand Down
Expand Up @@ -130,24 +130,27 @@ public void randomDisplayTick(IBlockState stateIn, World worldIn, BlockPos pos,
switch (enumfacing)
{
case WEST:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 - 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 - 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 - 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
break;
case EAST:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + 0.52D, d1, d2 + d4, 0.0D, 0.0D, 0.0D);
break;
case NORTH:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - 0.52D, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 - 0.52D, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 - 0.52D, 0.0D, 0.0D, 0.0D);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 - 0.52D, 0.0D, 0.0D, 0.0D);
break;
case SOUTH:
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + 0.52D, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 + 0.52D, 0.0D, 0.0D, 0.0D, new int[0]);
worldIn.spawnParticle(EnumParticleTypes.SMOKE_NORMAL, d0 + d4, d1, d2 + 0.52D, 0.0D, 0.0D, 0.0D);
worldIn.spawnParticle(EnumParticleTypes.FLAME, d0 + d4, d1, d2 + 0.52D, 0.0D, 0.0D, 0.0D);
}
}
}

/**
* Called when the block is right clicked by a player.
*/
@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ)
{
Expand Down

0 comments on commit 77612d8

Please sign in to comment.