    001package net.minecraft.client.gui;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.awt.Toolkit;
    006import java.awt.datatransfer.ClipboardOwner;
    007import java.awt.datatransfer.DataFlavor;
    008import java.awt.datatransfer.StringSelection;
    009import java.awt.datatransfer.Transferable;
    010import java.util.ArrayList;
    011import java.util.List;
    012import net.minecraft.client.Minecraft;
    013import net.minecraft.client.renderer.Tessellator;
    014import net.minecraft.util.EnumOS;
    015import org.lwjgl.input.Keyboard;
    016import org.lwjgl.input.Mouse;
    017import org.lwjgl.opengl.GL11;
    018
    019@SideOnly(Side.CLIENT)
    020public 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 buttons in this container. */
    034    protected List buttonList = 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 k = 0; k < this.buttonList.size(); ++k)
    053        {
    054            GuiButton guibutton = (GuiButton)this.buttonList.get(k);
    055            guibutton.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 transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents((Object)null);
    079
    080            if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.stringFlavor))
    081            {
    082                return (String)transferable.getTransferData(DataFlavor.stringFlavor);
    083            }
    084        }
    085        catch (Exception exception)
    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 stringselection = new StringSelection(par0Str);
    101            Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringselection, (ClipboardOwner)null);
    102        }
    103        catch (Exception exception)
    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 l = 0; l < this.buttonList.size(); ++l)
    117            {
    118                GuiButton guibutton = (GuiButton)this.buttonList.get(l);
    119
    120                if (guibutton.mousePressed(this.mc, par1, par2))
    121                {
    122                    this.selectedButton = guibutton;
    123                    this.mc.sndManager.playSoundFX("random.click", 1.0F, 1.0F);
    124                    this.actionPerformed(guibutton);
    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.buttonList.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 i = Mouse.getEventX() * this.width / this.mc.displayWidth;
    192        int j = 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(i, j, 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(i, j, Mouse.getEventButton());
    214        }
    215        else if (this.field_85042_b != -1 && this.field_85043_c > 0L)
    216        {
    217            long k = Minecraft.getSystemTime() - this.field_85043_c;
    218            this.func_85041_a(i, j, this.field_85042_b, k);
    219        }
    220    }
    221
    222    /**
    223     * Handles keyboard input.
    224     */
    225    public void handleKeyboardInput()
    226    {
    227        if (Keyboard.getEventKeyState())
    228        {
    229            int i = Keyboard.getEventKey();
    230            char c0 = Keyboard.getEventCharacter();
    231
    232            if (i == 87)
    233            {
    234                this.mc.toggleFullscreen();
    235                return;
    236            }
    237
    238            if (isMacOs && i == 28 && c0 == 0)
    239            {
    240                i = 29;
    241            }
    242
    243            this.keyTyped(c0, i);
    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 tessellator = Tessellator.instance;
    285        this.mc.renderEngine.func_98187_b("/gui/background.png");
    286        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    287        float f = 32.0F;
    288        tessellator.startDrawingQuads();
    289        tessellator.setColorOpaque_I(4210752);
    290        tessellator.addVertexWithUV(0.0D, (double)this.height, 0.0D, 0.0D, (double)((float)this.height / f + (float)par1));
    291        tessellator.addVertexWithUV((double)this.width, (double)this.height, 0.0D, (double)((float)this.width / f), (double)((float)this.height / f + (float)par1));
    292        tessellator.addVertexWithUV((double)this.width, 0.0D, 0.0D, (double)((float)this.width / f), (double)par1);
    293        tessellator.addVertexWithUV(0.0D, 0.0D, 0.0D, 0.0D, (double)par1);
    294        tessellator.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 flag = Keyboard.isKeyDown(28) && Keyboard.getEventCharacter() == 0;
    310        return Keyboard.isKeyDown(29) || Keyboard.isKeyDown(157) || isMacOs && (flag || Keyboard.isKeyDown(219) || Keyboard.isKeyDown(220));
    311    }
    312
    313    public static boolean isShiftKeyDown()
    314    {
    315        return Keyboard.isKeyDown(42) || Keyboard.isKeyDown(54);
    316    }
    317}