001 package net.minecraft.client.gui; 002 003 import cpw.mods.fml.common.Side; 004 import cpw.mods.fml.common.asm.SideOnly; 005 import java.awt.Toolkit; 006 import java.awt.datatransfer.ClipboardOwner; 007 import java.awt.datatransfer.DataFlavor; 008 import java.awt.datatransfer.StringSelection; 009 import java.awt.datatransfer.Transferable; 010 import java.util.ArrayList; 011 import java.util.List; 012 import net.minecraft.client.Minecraft; 013 import net.minecraft.client.renderer.Tessellator; 014 import net.minecraft.util.EnumOS; 015 import org.lwjgl.input.Keyboard; 016 import org.lwjgl.input.Mouse; 017 import org.lwjgl.opengl.GL11; 018 019 @SideOnly(Side.CLIENT) 020 public class GuiScreen extends Gui 021 { 022 public static final boolean field_90017_e = Minecraft.getOs() == EnumOS.MACOS; 023 024 /** Reference to the Minecraft object. */ 025 protected Minecraft mc; 026 027 /** The width of the screen object. */ 028 public int width; 029 030 /** The height of the screen object. */ 031 public int height; 032 033 /** A list of all the controls added to this container. */ 034 protected List controlList = new ArrayList(); 035 public boolean allowUserInput = false; 036 037 /** The FontRenderer used by GuiScreen */ 038 protected FontRenderer fontRenderer; 039 public GuiParticle guiParticles; 040 041 /** The button that was just pressed. */ 042 private GuiButton selectedButton = null; 043 private int field_85042_b = 0; 044 private long field_85043_c = 0L; 045 046 /** 047 * Draws the screen and all the components in it. 048 */ 049 public void drawScreen(int par1, int par2, float par3) 050 { 051 for (int var4 = 0; var4 < this.controlList.size(); ++var4) 052 { 053 GuiButton var5 = (GuiButton)this.controlList.get(var4); 054 var5.drawButton(this.mc, par1, par2); 055 } 056 } 057 058 /** 059 * Fired when a key is typed. This is the equivalent of KeyListener.keyTyped(KeyEvent e). 060 */ 061 protected void keyTyped(char par1, int par2) 062 { 063 if (par2 == 1) 064 { 065 this.mc.displayGuiScreen((GuiScreen)null); 066 this.mc.setIngameFocus(); 067 } 068 } 069 070 /** 071 * Returns a string stored in the system clipboard. 072 */ 073 public static String getClipboardString() 074 { 075 try 076 { 077 Transferable var0 = Toolkit.getDefaultToolkit().getSystemClipboard().getContents((Object)null); 078 079 if (var0 != null && var0.isDataFlavorSupported(DataFlavor.stringFlavor)) 080 { 081 return (String)var0.getTransferData(DataFlavor.stringFlavor); 082 } 083 } 084 catch (Exception var1) 085 { 086 ; 087 } 088 089 return ""; 090 } 091 092 /** 093 * store a string in the system clipboard 094 */ 095 public static void setClipboardString(String par0Str) 096 { 097 try 098 { 099 StringSelection var1 = new StringSelection(par0Str); 100 Toolkit.getDefaultToolkit().getSystemClipboard().setContents(var1, (ClipboardOwner)null); 101 } 102 catch (Exception var2) 103 { 104 ; 105 } 106 } 107 108 /** 109 * Called when the mouse is clicked. 110 */ 111 protected void mouseClicked(int par1, int par2, int par3) 112 { 113 if (par3 == 0) 114 { 115 for (int var4 = 0; var4 < this.controlList.size(); ++var4) 116 { 117 GuiButton var5 = (GuiButton)this.controlList.get(var4); 118 119 if (var5.mousePressed(this.mc, par1, par2)) 120 { 121 this.selectedButton = var5; 122 this.mc.sndManager.playSoundFX("random.click", 1.0F, 1.0F); 123 this.actionPerformed(var5); 124 } 125 } 126 } 127 } 128 129 /** 130 * Called when the mouse is moved or a mouse button is released. Signature: (mouseX, mouseY, which) which==-1 is 131 * mouseMove, which==0 or which==1 is mouseUp 132 */ 133 protected void mouseMovedOrUp(int par1, int par2, int par3) 134 { 135 if (this.selectedButton != null && par3 == 0) 136 { 137 this.selectedButton.mouseReleased(par1, par2); 138 this.selectedButton = null; 139 } 140 } 141 142 protected void func_85041_a(int par1, int par2, int par3, long par4) {} 143 144 /** 145 * Fired when a control is clicked. This is the equivalent of ActionListener.actionPerformed(ActionEvent e). 146 */ 147 protected void actionPerformed(GuiButton par1GuiButton) {} 148 149 /** 150 * Causes the screen to lay out its subcomponents again. This is the equivalent of the Java call 151 * Container.validate() 152 */ 153 public void setWorldAndResolution(Minecraft par1Minecraft, int par2, int par3) 154 { 155 this.guiParticles = new GuiParticle(par1Minecraft); 156 this.mc = par1Minecraft; 157 this.fontRenderer = par1Minecraft.fontRenderer; 158 this.width = par2; 159 this.height = par3; 160 this.controlList.clear(); 161 this.initGui(); 162 } 163 164 /** 165 * Adds the buttons (and other controls) to the screen in question. 166 */ 167 public void initGui() {} 168 169 /** 170 * Delegates mouse and keyboard input. 171 */ 172 public void handleInput() 173 { 174 while (Mouse.next()) 175 { 176 this.handleMouseInput(); 177 } 178 179 while (Keyboard.next()) 180 { 181 this.handleKeyboardInput(); 182 } 183 } 184 185 /** 186 * Handles mouse input. 187 */ 188 public void handleMouseInput() 189 { 190 int var1 = Mouse.getEventX() * this.width / this.mc.displayWidth; 191 int var2 = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1; 192 193 if (Mouse.getEventButtonState()) 194 { 195 this.field_85042_b = Mouse.getEventButton(); 196 this.field_85043_c = Minecraft.getSystemTime(); 197 this.mouseClicked(var1, var2, this.field_85042_b); 198 } 199 else if (Mouse.getEventButton() != -1) 200 { 201 this.field_85042_b = -1; 202 this.mouseMovedOrUp(var1, var2, Mouse.getEventButton()); 203 } 204 else if (this.mc.gameSettings.field_85185_A && this.field_85042_b != -1 && this.field_85043_c > 0L) 205 { 206 long var3 = Minecraft.getSystemTime() - this.field_85043_c; 207 this.func_85041_a(var1, var2, this.field_85042_b, var3); 208 } 209 } 210 211 /** 212 * Handles keyboard input. 213 */ 214 public void handleKeyboardInput() 215 { 216 if (Keyboard.getEventKeyState()) 217 { 218 int var1 = Keyboard.getEventKey(); 219 char var2 = Keyboard.getEventCharacter(); 220 221 if (var1 == 87) 222 { 223 this.mc.toggleFullscreen(); 224 return; 225 } 226 227 if (field_90017_e && var1 == 28 && var2 == 0) 228 { 229 var1 = 29; 230 } 231 232 this.keyTyped(var2, var1); 233 } 234 } 235 236 /** 237 * Called from the main game loop to update the screen. 238 */ 239 public void updateScreen() {} 240 241 /** 242 * Called when the screen is unloaded. Used to disable keyboard repeat events 243 */ 244 public void onGuiClosed() {} 245 246 /** 247 * Draws either a gradient over the background screen (when it exists) or a flat gradient over background.png 248 */ 249 public void drawDefaultBackground() 250 { 251 this.drawWorldBackground(0); 252 } 253 254 public void drawWorldBackground(int par1) 255 { 256 if (this.mc.theWorld != null) 257 { 258 this.drawGradientRect(0, 0, this.width, this.height, -1072689136, -804253680); 259 } 260 else 261 { 262 this.drawBackground(par1); 263 } 264 } 265 266 /** 267 * Draws the background (i is always 0 as of 1.2.2) 268 */ 269 public void drawBackground(int par1) 270 { 271 GL11.glDisable(GL11.GL_LIGHTING); 272 GL11.glDisable(GL11.GL_FOG); 273 Tessellator var2 = Tessellator.instance; 274 GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.mc.renderEngine.getTexture("/gui/background.png")); 275 GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F); 276 float var3 = 32.0F; 277 var2.startDrawingQuads(); 278 var2.setColorOpaque_I(4210752); 279 var2.addVertexWithUV(0.0D, (double)this.height, 0.0D, 0.0D, (double)((float)this.height / var3 + (float)par1)); 280 var2.addVertexWithUV((double)this.width, (double)this.height, 0.0D, (double)((float)this.width / var3), (double)((float)this.height / var3 + (float)par1)); 281 var2.addVertexWithUV((double)this.width, 0.0D, 0.0D, (double)((float)this.width / var3), (double)par1); 282 var2.addVertexWithUV(0.0D, 0.0D, 0.0D, 0.0D, (double)par1); 283 var2.draw(); 284 } 285 286 /** 287 * Returns true if this GUI should pause the game when it is displayed in single-player 288 */ 289 public boolean doesGuiPauseGame() 290 { 291 return true; 292 } 293 294 public void confirmClicked(boolean par1, int par2) {} 295 296 public static boolean isCtrlKeyDown() 297 { 298 boolean var0 = Keyboard.isKeyDown(28) && Keyboard.getEventCharacter() == 0; 299 return Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157) || field_90017_e && (var0 || Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220)); 300 } 301 302 public static boolean isShiftKeyDown() 303 { 304 return Keyboard.isKeyDown(42) || Keyboard.isKeyDown(54); 305 } 306 }