GuiNewControls.java 19 KB

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