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