Advertisement
yarinch

Untitled

Sep 29th, 2014
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.49 KB | None | 0 0
  1. package com.yarinch.modularmachines.client.interfaces;
  2.  
  3. import com.yarinch.modularmachines.block.BlockMachineBase;
  4. import com.yarinch.modularmachines.block.MachineComponents;
  5. import com.yarinch.modularmachines.tileentity.TileEntityMachineCore;
  6. import net.minecraft.client.Minecraft;
  7. import net.minecraft.client.gui.GuiButton;
  8. import net.minecraft.client.gui.inventory.GuiContainer;
  9. import net.minecraft.entity.player.InventoryPlayer;
  10. import net.minecraft.util.ResourceLocation;
  11. import org.lwjgl.opengl.GL11;
  12.  
  13. public class GuiMachine extends GuiContainer {
  14.  
  15.     private TileEntityMachineCore core;
  16.     private boolean up = false;
  17.     private boolean down = false;
  18.     private int activeY;
  19.     private MachineComponents[] unSaved;
  20.     public boolean update = false;
  21.  
  22.     public GuiMachine(InventoryPlayer playerInv, TileEntityMachineCore core) {
  23.         super(new ContainerMachineCore(playerInv, core));
  24.  
  25.         unSaved = core.getComponents();
  26.         this.core = core;
  27.  
  28.         xSize = 256;
  29.         ySize = 228;
  30.     }
  31.  
  32.     private static final ResourceLocation texture = new ResourceLocation("modularmachines", "textures/gui/machine.png");
  33.  
  34.     @Override
  35.     protected void drawGuiContainerBackgroundLayer(float f, int x, int y) {
  36.         GL11.glColor4f(1, 1, 1, 1);
  37.  
  38.         Minecraft.getMinecraft().renderEngine.bindTexture(texture);
  39.         drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
  40.  
  41.         //draws the boxes
  42.         for (int i = 0; i < unSaved.length; i++) {
  43.             int xStart = guiLeft + 64;
  44.             int yStart = guiTop + 40 + 19 * (i + 1);
  45.             int rectWidth = 128;
  46.             int rectHeight = 12;
  47.             boolean active = activeY == yStart || inRect(x, y, xStart, yStart, rectWidth, rectHeight);
  48.                 drawTexturedModalRect(xStart, yStart, active ? 128 : 0, 244, rectWidth, rectHeight);
  49.             }
  50.  
  51.         //checks how the arrows should be drawn
  52.         arrowCheck: {
  53.             if (activeY == 0) {
  54.                 up = down = false;
  55.             }else{
  56.                 for (int i = 0; i < unSaved.length; i++) {
  57.                     if (activeY == guiTop + 40 + 19 * (i + 1)) {
  58.                         up = i != 0 && unSaved.length > 1;
  59.                         down = i != unSaved.length - 1 && unSaved.length > 1;
  60.                         break arrowCheck;
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.  
  66.         //draws the arrows
  67.         for (int i = 0; i < 2; i++) {
  68.             int xStart = guiLeft + 214;
  69.             int yStart = guiTop + 44 + 24 * i;
  70.             int srcY = 228;
  71.             int srcX = i * 16;
  72.  
  73.             if (i == 0 ? !up : !down) {
  74.                 srcX += 64;
  75.             }else if (inRect(x, y, xStart, yStart, 16, 16)) {
  76.                 srcX += 32;
  77.             }
  78.             drawTexturedModalRect(xStart, yStart, srcX, srcY, 16, 16);
  79.         }
  80.     }
  81.  
  82.     @Override
  83.     protected void drawGuiContainerForegroundLayer(int x, int y) {
  84.         fontRendererObj.drawString("Text test! Ting Tang Walla Walla Bing Bang", 9, 5, 0xCC00FF);
  85.  
  86.         //draws the boxes names
  87.         for (int i = 0; i < unSaved.length; i++) {
  88.             int srcX = 64;
  89.             String str = unSaved[i].getLocalizedName();
  90.             srcX += (128 - fontRendererObj.getStringWidth(str))/2;
  91.             fontRendererObj.drawString(str, srcX, 42 + 19 * (i + 1), 0x3F7F47);
  92.         }
  93.     }
  94.  
  95.     @Override
  96.     public void initGui() {
  97.         super.initGui();
  98.         buttonList.clear();
  99.         buttonList.add(new GuiButton(0, guiLeft + 32, guiTop + 32, fontRendererObj.getStringWidth("Save Changes") + 10, 20, "Save Changes"));
  100.     }
  101.  
  102.     private boolean inRect(int mouseX, int mouseY, int xStart, int yStart, int width, int height) {
  103.         return xStart <= mouseX && mouseX <= xStart + width && yStart <= mouseY && mouseY <= yStart + height; //checks if the mouse is inside the rectangle
  104.     }
  105.  
  106.     @Override
  107.     protected void mouseClicked(int x, int y, int timeSinceClicked) {
  108.         super.mouseClicked(x, y, timeSinceClicked);
  109.  
  110.         for (int i = 0; i < unSaved.length; i++) {
  111.             if (inRect(x, y, guiLeft + 64, guiTop + 40 + 19 * (i + 1), 128, 12)) {
  112.                 int temp = guiTop + 40 + 19 * (i + 1);
  113.                 if (temp == activeY) {
  114.                     activeY = 0;
  115.                     up = down = false;
  116.                 } else {
  117.                     activeY = temp;
  118.                     up = i != 0 && unSaved.length > 1;
  119.                     down = i != unSaved.length - 1 && unSaved.length > 1;
  120.                     return;
  121.                 }
  122.             }
  123.         }
  124.  
  125.         for (int i = 0; i < 2; i++) {
  126.             if (i == 0 ? up : down) {
  127.                 if (inRect(x, y, guiLeft + 214, guiTop + 43 + 24 * i, 16, 16)) {
  128.                     int j = (activeY - guiTop - 40) / 19 - 1;
  129.                     MachineComponents temp = unSaved[j];
  130.                     if (i == 1) {
  131.                         unSaved[j] = unSaved[j + 1];
  132.                         unSaved[j + 1] = temp;
  133.                         activeY += 19;
  134.                     }else{
  135.                         unSaved[j] = unSaved[j - 1];
  136.                         unSaved[j - 1] = temp;
  137.                         activeY -= 19;
  138.                     }
  139.                     return;
  140.                 }
  141.             }
  142.         }
  143.     }
  144.  
  145.     @Override
  146.     protected void actionPerformed(GuiButton button) {
  147.         if (button.id == 0) {
  148.             core.setComponents(unSaved);
  149.             core.markDirty();
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement