| 1 | package net.minecraft.client.gui;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import net.minecraft.client.Minecraft;
|
| 5 | import net.minecraft.client.resources.I18n;
|
| 6 | import net.minecraft.client.settings.GameSettings;
|
| 7 | import net.minecraft.client.settings.KeyBinding;
|
| 8 |
|
| 9 | public class GuiControls extends GuiScreen
|
| 10 | {
|
| 11 | private static final GameSettings.Options[] optionsArr = new GameSettings.Options[] {GameSettings.Options.INVERT_MOUSE, GameSettings.Options.SENSITIVITY, GameSettings.Options.TOUCHSCREEN};
|
| 12 |
|
| 13 | /**
|
| 14 | * A reference to the screen object that created this. Used for navigating between screens.
|
| 15 | */
|
| 16 | private GuiScreen parentScreen;
|
| 17 | protected String screenTitle = "Controls";
|
| 18 |
|
| 19 | /** Reference to the GameSettings object. */
|
| 20 | private GameSettings options;
|
| 21 |
|
| 22 | /** The ID of the button that has been pressed. */
|
| 23 | public KeyBinding buttonId = null;
|
| 24 | public long time;
|
| 25 | private GuiKeyBindingList keyBindingList;
|
| 26 | private GuiButton buttonReset;
|
| 27 |
|
| 28 | public GuiControls(GuiScreen screen, GameSettings settings)
|
| 29 | {
|
| 30 | this.parentScreen = screen;
|
| 31 | this.options = settings;
|
| 32 | }
|
| 33 |
|
| 34 | /**
|
| 35 | * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
|
| 36 | * window resizes, the buttonList is cleared beforehand.
|
| 37 | */
|
| 38 | public void initGui()
|
| 39 | {
|
| 40 | this.keyBindingList = new GuiKeyBindingList(this, this.mc);
|
| 41 | this.buttonList.add(new GuiButton(200, this.width / 2 - 155, this.height - 29, 150, 20, I18n.format("gui.done", new Object[0])));
|
| 42 | this.buttonList.add(this.buttonReset = new GuiButton(201, this.width / 2 - 155 + 160, this.height - 29, 150, 20, I18n.format("controls.resetAll", new Object[0])));
|
| 43 | this.screenTitle = I18n.format("controls.title", new Object[0]);
|
| 44 | int i = 0;
|
| 45 |
|
| 46 | for (GameSettings.Options gamesettings$options : optionsArr)
|
| 47 | {
|
| 48 | if (gamesettings$options.getEnumFloat())
|
| 49 | {
|
| 50 | this.buttonList.add(new GuiOptionSlider(gamesettings$options.returnEnumOrdinal(), this.width / 2 - 155 + i % 2 * 160, 18 + 24 * (i >> 1), gamesettings$options));
|
| 51 | }
|
| 52 | else
|
| 53 | {
|
| 54 | this.buttonList.add(new GuiOptionButton(gamesettings$options.returnEnumOrdinal(), this.width / 2 - 155 + i % 2 * 160, 18 + 24 * (i >> 1), gamesettings$options, this.options.getKeyBinding(gamesettings$options)));
|
| 55 | }
|
| 56 |
|
| 57 | ++i;
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | /**
|
| 62 | * Handles mouse input.
|
| 63 | */
|
| 64 | public void handleMouseInput() throws IOException
|
| 65 | {
|
| 66 | super.handleMouseInput();
|
| 67 | this.keyBindingList.handleMouseInput();
|
| 68 | }
|
| 69 |
|
| 70 | /**
|
| 71 | * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
|
| 72 | */
|
| 73 | protected void actionPerformed(GuiButton button) throws IOException
|
| 74 | {
|
| 75 | if (button.id == 200)
|
| 76 | {
|
| 77 | this.mc.displayGuiScreen(this.parentScreen);
|
| 78 | }
|
| 79 | else if (button.id == 201)
|
| 80 | {
|
| 81 | for (KeyBinding keybinding : this.mc.gameSettings.keyBindings)
|
| 82 | {
|
| 83 | keybinding.setKeyCode(keybinding.getKeyCodeDefault());
|
| 84 | }
|
| 85 |
|
| 86 | KeyBinding.resetKeyBindingArrayAndHash();
|
| 87 | }
|
| 88 | else if (button.id < 100 && button instanceof GuiOptionButton)
|
| 89 | {
|
| 90 | this.options.setOptionValue(((GuiOptionButton)button).returnEnumOptions(), 1);
|
| 91 | button.displayString = this.options.getKeyBinding(GameSettings.Options.getEnumOptions(button.id));
|
| 92 | }
|
| 93 | }
|
| 94 |
|
| 95 | /**
|
| 96 | * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
|
| 97 | */
|
| 98 | protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException
|
| 99 | {
|
| 100 | if (this.buttonId != null)
|
| 101 | {
|
| 102 | this.options.setOptionKeyBinding(this.buttonId, -100 + mouseButton);
|
| 103 | this.buttonId = null;
|
| 104 | KeyBinding.resetKeyBindingArrayAndHash();
|
| 105 | }
|
| 106 | else if (mouseButton != 0 || !this.keyBindingList.mouseClicked(mouseX, mouseY, mouseButton))
|
| 107 | {
|
| 108 | super.mouseClicked(mouseX, mouseY, mouseButton);
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | /**
|
| 113 | * Called when a mouse button is released. Args : mouseX, mouseY, releaseButton
|
| 114 | */
|
| 115 | protected void mouseReleased(int mouseX, int mouseY, int state)
|
| 116 | {
|
| 117 | if (state != 0 || !this.keyBindingList.mouseReleased(mouseX, mouseY, state))
|
| 118 | {
|
| 119 | super.mouseReleased(mouseX, mouseY, state);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | /**
|
| 124 | * Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
|
| 125 | * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
|
| 126 | */
|
| 127 | protected void keyTyped(char typedChar, int keyCode) throws IOException
|
| 128 | {
|
| 129 | if (this.buttonId != null)
|
| 130 | {
|
| 131 | if (keyCode == 1)
|
| 132 | {
|
| 133 | this.options.setOptionKeyBinding(this.buttonId, 0);
|
| 134 | }
|
| 135 | else if (keyCode != 0)
|
| 136 | {
|
| 137 | this.options.setOptionKeyBinding(this.buttonId, keyCode);
|
| 138 | }
|
| 139 | else if (typedChar > 0)
|
| 140 | {
|
| 141 | this.options.setOptionKeyBinding(this.buttonId, typedChar + 256);
|
| 142 | }
|
| 143 |
|
| 144 | this.buttonId = null;
|
| 145 | this.time = Minecraft.getSystemTime();
|
| 146 | KeyBinding.resetKeyBindingArrayAndHash();
|
| 147 | }
|
| 148 | else
|
| 149 | {
|
| 150 | super.keyTyped(typedChar, keyCode);
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | /**
|
| 155 | * Draws the screen and all the components in it. Args : mouseX, mouseY, renderPartialTicks
|
| 156 | */
|
| 157 | public void drawScreen(int mouseX, int mouseY, float partialTicks)
|
| 158 | {
|
| 159 | this.drawDefaultBackground();
|
| 160 | this.keyBindingList.drawScreen(mouseX, mouseY, partialTicks);
|
| 161 | this.drawCenteredString(this.fontRendererObj, this.screenTitle, this.width / 2, 8, 16777215);
|
| 162 | boolean flag = true;
|
| 163 |
|
| 164 | for (KeyBinding keybinding : this.options.keyBindings)
|
| 165 | {
|
| 166 | if (keybinding.getKeyCode() != keybinding.getKeyCodeDefault())
|
| 167 | {
|
| 168 | flag = false;
|
| 169 | break;
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | this.buttonReset.enabled = !flag;
|
| 174 | super.drawScreen(mouseX, mouseY, partialTicks);
|
| 175 | }
|
| 176 | }
|
| 177 |
|