GuiNewControls.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. package us.getfluxed.controlsearch.client.gui;
  2. import net.minecraft.client.Minecraft;
  3. import net.minecraft.client.gui.*;
  4. import net.minecraft.client.resources.I18n;
  5. import net.minecraft.client.settings.*;
  6. import net.minecraftforge.client.settings.KeyModifier;
  7. import org.lwjgl.input.*;
  8. import java.io.IOException;
  9. import java.util.*;
  10. public class GuiNewControls extends GuiControls {
  11. private static final GameSettings.Options[] OPTIONS_ARR = new GameSettings.Options[]{GameSettings.Options.INVERT_MOUSE, GameSettings.Options.SENSITIVITY, GameSettings.Options.TOUCHSCREEN, GameSettings.Options.AUTO_JUMP};
  12. /**
  13. * A reference to the screen object that created this. Used for navigating between screens.
  14. */
  15. private final GuiScreen parentScreen;
  16. /**
  17. * Reference to the GameSettings object.
  18. */
  19. private final GameSettings options;
  20. /**
  21. * The ID of the button that has been pressed.
  22. */
  23. public GuiNewKeyBindingList keyBindingList;
  24. private GuiButton buttonReset;
  25. private GuiTextField search;
  26. private String lastFilterText = "";
  27. private boolean conflicts = false;
  28. public int availableTime;
  29. public GuiButton conflictsButton;
  30. public GuiNewControls(GuiScreen screen, GameSettings settings) {
  31. super(screen, settings);
  32. this.parentScreen = screen;
  33. this.options = settings;
  34. }
  35. /**
  36. * Adds the buttons (and other controls) to the screen in question. Called when the GUI is displayed and when the
  37. * window resizes, the buttonList is cleared beforehand.
  38. */
  39. public void initGui() {
  40. this.keyBindingList = new GuiNewKeyBindingList(this, this.mc);
  41. this.buttonList.add(new GuiButton(200, this.width / 2 - 155, this.height - 29, 150, 20, I18n.format("gui.done")));
  42. this.buttonReset = this.addButton(new GuiButton(201, this.width / 2 - 155 + 160, this.height - 29, 150, 20, I18n.format("controls.resetAll")));
  43. this.screenTitle = I18n.format("controls.title");
  44. int i = 0;
  45. for(GameSettings.Options gamesettings$options : OPTIONS_ARR) {
  46. if(gamesettings$options.getEnumFloat()) {
  47. this.buttonList.add(new GuiOptionSlider(gamesettings$options.returnEnumOrdinal(), this.width / 2 - 155 + i % 2 * 160, 18 + 24 * (i >> 1), gamesettings$options));
  48. } else {
  49. 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)));
  50. }
  51. ++i;
  52. }
  53. search = new GuiTextField(0, mc.fontRendererObj, this.width / 2 - 155, this.height - 29 - 28, 150, 18);
  54. search.setCanLoseFocus(true);
  55. conflictsButton = new GuiButton(2906, this.width / 2 - 155 + 160, this.height - 29 - 29, 150, 20, I18n.format("options.showConflicts"));
  56. this.buttonList.add(conflictsButton);
  57. this.conflicts = false;
  58. }
  59. @Override
  60. public void updateScreen() {
  61. search.updateCursorCounter();
  62. if(!search.getText().equals(lastFilterText)) {
  63. reloadKeys(0);
  64. }
  65. }
  66. private void reloadKeys(int type) {
  67. if(type == 0) {
  68. LinkedList<GuiListExtended.IGuiListEntry> newList = new LinkedList<>();
  69. for(GuiListExtended.IGuiListEntry entry : keyBindingList.getListEntriesAll()) {
  70. if(entry instanceof GuiNewKeyBindingList.KeyEntry) {
  71. GuiNewKeyBindingList.KeyEntry ent = (GuiNewKeyBindingList.KeyEntry) entry;
  72. if(ent.getKeybinding().getKeyDescription().toLowerCase().contains(search.getText().toLowerCase())) {
  73. newList.add(entry);
  74. }
  75. }
  76. }
  77. keyBindingList.setListEntries(newList);
  78. lastFilterText = search.getText();
  79. if(lastFilterText.isEmpty()) {
  80. keyBindingList.setListEntries(keyBindingList.getListEntriesAll());
  81. }
  82. } else if(type == 1) {
  83. LinkedList<GuiListExtended.IGuiListEntry> conflicts = new LinkedList<>();
  84. for(GuiListExtended.IGuiListEntry entry : keyBindingList.getListEntriesAll()) {
  85. if(entry instanceof GuiNewKeyBindingList.KeyEntry) {
  86. GuiNewKeyBindingList.KeyEntry ent = (GuiNewKeyBindingList.KeyEntry) entry;
  87. for(GuiListExtended.IGuiListEntry entry1 : keyBindingList.getListEntriesAll()) {
  88. if(!entry.equals(entry1))
  89. if(entry1 instanceof GuiNewKeyBindingList.KeyEntry) {
  90. GuiNewKeyBindingList.KeyEntry ent1 = (GuiNewKeyBindingList.KeyEntry) entry1;
  91. if(ent.getKeybinding().conflicts(ent1.getKeybinding())) {
  92. if(!conflicts.contains(ent))
  93. conflicts.add(ent);
  94. if(!conflicts.contains(ent1))
  95. conflicts.add(ent1);
  96. }
  97. }
  98. }
  99. }
  100. }
  101. keyBindingList.setListEntries(conflicts);
  102. if(conflicts.isEmpty() || !this.conflicts) {
  103. keyBindingList.setListEntries(keyBindingList.getListEntriesAll());
  104. }
  105. }
  106. }
  107. /**
  108. * Handles mouse input.
  109. */
  110. public void handleMouseInput() throws IOException {
  111. superSuperHandleMouseInput();
  112. this.keyBindingList.handleMouseInput();
  113. }
  114. /**
  115. * Handles mouse input.
  116. */
  117. public void superSuperHandleMouseInput() throws IOException {
  118. int i = Mouse.getEventX() * this.width / this.mc.displayWidth;
  119. int j = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1;
  120. int k = Mouse.getEventButton();
  121. if(Mouse.getEventButtonState()) {
  122. if(this.mc.gameSettings.touchscreen && this.touchValue++ > 0) {
  123. return;
  124. }
  125. this.eventButton = k;
  126. this.lastMouseEvent = Minecraft.getSystemTime();
  127. this.mouseClicked(i, j, this.eventButton);
  128. } else if(k != -1) {
  129. if(this.mc.gameSettings.touchscreen && --this.touchValue > 0) {
  130. return;
  131. }
  132. this.eventButton = -1;
  133. this.mouseReleased(i, j, k);
  134. } else if(this.eventButton != -1 && this.lastMouseEvent > 0L) {
  135. long l = Minecraft.getSystemTime() - this.lastMouseEvent;
  136. this.mouseClickMove(i, j, this.eventButton, l);
  137. }
  138. }
  139. /**
  140. * Called by the controls from the buttonList when activated. (Mouse pressed for buttons)
  141. */
  142. protected void actionPerformed(GuiButton button) throws IOException {
  143. if(button.id == 200) {
  144. this.mc.displayGuiScreen(this.parentScreen);
  145. } else if(button.id == 201) {
  146. for(KeyBinding keybinding : this.mc.gameSettings.keyBindings) {
  147. keybinding.setToDefault();
  148. }
  149. KeyBinding.resetKeyBindingArrayAndHash();
  150. availableTime = 0;
  151. } else if(button.id < 100 && button instanceof GuiOptionButton) {
  152. this.options.setOptionValue(((GuiOptionButton) button).returnEnumOptions(), 1);
  153. button.displayString = this.options.getKeyBinding(GameSettings.Options.getEnumOptions(button.id));
  154. } else if(button.id == 2906) {
  155. availableTime = 0;
  156. if(!conflicts) {
  157. conflicts = true;
  158. conflictsButton.displayString = I18n.format("options.showAll");
  159. reloadKeys(1);
  160. } else {
  161. conflicts = false;
  162. conflictsButton.displayString = I18n.format("options.showConflicts");
  163. reloadKeys(1);
  164. }
  165. }
  166. }
  167. /**
  168. * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
  169. */
  170. protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
  171. if(this.buttonId != null) {
  172. this.buttonId.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.getActiveModifier(), -100 + mouseButton);
  173. this.options.setOptionKeyBinding(this.buttonId, -100 + mouseButton);
  174. this.buttonId = null;
  175. KeyBinding.resetKeyBindingArrayAndHash();
  176. } else if(mouseButton != 0 || !this.keyBindingList.mouseClicked(mouseX, mouseY, mouseButton)) {
  177. superSuperMouseClicked(mouseX, mouseY, mouseButton);
  178. }
  179. search.mouseClicked(mouseX, mouseY, mouseButton);
  180. if(mouseButton == 1 && mouseX >= search.xPosition && mouseX < search.xPosition + search.width && mouseY >= search.yPosition && mouseY < search.yPosition + search.height) {
  181. search.setText("");
  182. }
  183. }
  184. /**
  185. * Called when the mouse is clicked. Args : mouseX, mouseY, clickedButton
  186. */
  187. protected void superSuperMouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
  188. if(mouseButton == 0) {
  189. for(int i = 0; i < this.buttonList.size(); ++i) {
  190. GuiButton guibutton = this.buttonList.get(i);
  191. if(guibutton.mousePressed(this.mc, mouseX, mouseY)) {
  192. net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent.Pre event = new net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent.Pre(this, guibutton, this.buttonList);
  193. if(net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(event))
  194. break;
  195. guibutton = event.getButton();
  196. this.selectedButton = guibutton;
  197. guibutton.playPressSound(this.mc.getSoundHandler());
  198. this.actionPerformed(guibutton);
  199. if(this.equals(this.mc.currentScreen))
  200. net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.client.event.GuiScreenEvent.ActionPerformedEvent.Post(this, event.getButton(), this.buttonList));
  201. }
  202. }
  203. }
  204. }
  205. /**
  206. * Called when a mouse button is released.
  207. */
  208. protected void mouseReleased(int mouseX, int mouseY, int state) {
  209. if(state != 0 || !this.keyBindingList.mouseReleased(mouseX, mouseY, state)) {
  210. superSuperMouseReleased(mouseX, mouseY, state);
  211. }
  212. }
  213. protected void superSuperMouseReleased(int mouseX, int mouseY, int state) {
  214. if(this.selectedButton != null && state == 0) {
  215. this.selectedButton.mouseReleased(mouseX, mouseY);
  216. this.selectedButton = null;
  217. }
  218. }
  219. /**
  220. * Fired when a key is typed (except F11 which toggles full screen). This is the equivalent of
  221. * KeyListener.keyTyped(KeyEvent e). Args : character (character on the key), keyCode (lwjgl Keyboard key code)
  222. */
  223. protected void keyTyped(char typedChar, int keyCode) throws IOException {
  224. if(this.buttonId != null) {
  225. if(keyCode == 1) {
  226. this.buttonId.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.NONE, 0);
  227. this.options.setOptionKeyBinding(this.buttonId, 0);
  228. } else if(keyCode != 0) {
  229. this.buttonId.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.getActiveModifier(), keyCode);
  230. this.options.setOptionKeyBinding(this.buttonId, keyCode);
  231. } else if(typedChar > 0) {
  232. this.buttonId.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.getActiveModifier(), typedChar + 256);
  233. this.options.setOptionKeyBinding(this.buttonId, typedChar + 256);
  234. }
  235. if(!KeyModifier.isKeyCodeModifier(keyCode)) {
  236. this.buttonId = null;
  237. }
  238. this.time = Minecraft.getSystemTime();
  239. KeyBinding.resetKeyBindingArrayAndHash();
  240. } else {
  241. if(search.isFocused())
  242. search.textboxKeyTyped(typedChar, keyCode);
  243. else if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL)) {
  244. if(availableTime > 0 && availableTime<40) {
  245. availableTime = 0;
  246. } else {
  247. availableTime = 40;
  248. }
  249. } else {
  250. superSuperKeyTyped(typedChar, keyCode);
  251. }
  252. }
  253. }
  254. protected void superSuperKeyTyped(char typedChar, int keyCode) throws IOException {
  255. if(keyCode == 1) {
  256. this.mc.displayGuiScreen((GuiScreen) null);
  257. if(this.mc.currentScreen == null) {
  258. this.mc.setIngameFocus();
  259. }
  260. }
  261. }
  262. /**
  263. * Draws the screen and all the components in it.
  264. */
  265. public void drawScreen(int mouseX, int mouseY, float partialTicks) {
  266. this.drawDefaultBackground();
  267. this.keyBindingList.drawScreen(mouseX, mouseY, partialTicks);
  268. this.drawCenteredString(this.fontRendererObj, this.screenTitle, this.width / 2, 8, 16777215);
  269. this.drawCenteredString(this.fontRendererObj, I18n.format("options.search"), this.width / 2 - (155 / 2), this.height - 29 - 44, 16777215);
  270. boolean flag = false;
  271. for(KeyBinding keybinding : this.options.keyBindings) {
  272. if(!keybinding.isSetToDefaultValue()) {
  273. flag = true;
  274. break;
  275. }
  276. }
  277. this.buttonReset.enabled = flag;
  278. superSuperDrawScreen(mouseX, mouseY, partialTicks);
  279. search.drawTextBox();
  280. if(availableTime > 0) {
  281. drawRect(keyBindingList.left, keyBindingList.top, keyBindingList.right, keyBindingList.bottom + 18, 0xFF000000);
  282. LinkedList<Integer> keyCodes = new LinkedList<>();
  283. for(int i = 2; i < 219; i++) {
  284. keyCodes.add(i);
  285. }
  286. keyCodes.add(-98);
  287. keyCodes.add(-99);
  288. keyCodes.add(-100);
  289. List<Integer> removed = new ArrayList<>();
  290. keyBindingList.getListEntriesAll().forEach(i -> {
  291. if(i instanceof GuiNewKeyBindingList.KeyEntry) {
  292. removed.add(((GuiNewKeyBindingList.KeyEntry) i).getKeybinding().getKeyCode());
  293. }
  294. });
  295. int[] rem = new int[]{0xDB, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x70, 0x71, 0x29, 0x79, 0x57, 0x7B, 0x7D, 0x8D, 0x90, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x9C, 0xA7, 0xB3, 0xC5, 0x91, 0xC4, 0xDA};
  296. for(int i : rem) {
  297. removed.add(i);
  298. }
  299. keyCodes.forEach(i -> {
  300. if(i >= 0)
  301. if(Keyboard.getKeyName(i) == null || Keyboard.getKeyName(i).isEmpty()) {
  302. removed.add(i);
  303. }
  304. });
  305. keyCodes.removeAll(removed);
  306. Collections.sort(keyCodes);
  307. final int[] x = {0};
  308. final int[] y = {0};
  309. final int[] count = {0};
  310. fontRendererObj.drawString(I18n.format("options.availableKeys") + ":", width / 2, keyBindingList.top + 2, 0xFFFFFF);
  311. List<String> codes = new ArrayList<>();
  312. keyCodes.forEach(key -> {
  313. if(key >= 0) {
  314. codes.add(Keyboard.getKeyName(key));
  315. fontRendererObj.drawString(Keyboard.getKeyName(key), keyBindingList.left + (x[0] * 65), keyBindingList.top + 12 + (y[0]++ * fontRendererObj.FONT_HEIGHT), 0xFF55FF);
  316. } else {
  317. codes.add(Mouse.getButtonName(key + 100));
  318. switch(key + 100) {
  319. case 0:
  320. fontRendererObj.drawString("Button 1", keyBindingList.left + (x[0] * 65), keyBindingList.top + 12 + (y[0]++ * fontRendererObj.FONT_HEIGHT), 0x55FF55);
  321. break;
  322. case 1:
  323. fontRendererObj.drawString("Button 2", keyBindingList.left + (x[0] * 65), keyBindingList.top + 12 + (y[0]++ * fontRendererObj.FONT_HEIGHT), 0x55FF55);
  324. break;
  325. case 2:
  326. fontRendererObj.drawString("Button 3", keyBindingList.left + (x[0] * 65), keyBindingList.top + 12 + (y[0]++ * fontRendererObj.FONT_HEIGHT), 0x55FF55);
  327. break;
  328. }
  329. }
  330. count[0]++;
  331. if(count[0] > keyBindingList.height / 30) {
  332. count[0] = 0;
  333. x[0]++;
  334. y[0] = 0;
  335. }
  336. });
  337. availableTime--;
  338. }
  339. }
  340. public void superSuperDrawScreen(int mouseX, int mouseY, float partialTicks) {
  341. for(GuiButton aButtonList : this.buttonList) {
  342. aButtonList.func_191745_a(this.mc, mouseX, mouseY, partialTicks);
  343. }
  344. for(GuiLabel aLabelList : this.labelList) {
  345. aLabelList.drawLabel(this.mc, mouseX, mouseY);
  346. }
  347. }
  348. }