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