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