OverlaySearchField.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.gui;
  6. import me.shedaniel.math.compat.RenderHelper;
  7. import me.shedaniel.rei.gui.widget.TextFieldWidget;
  8. import me.shedaniel.rei.impl.ScreenHelper;
  9. import net.minecraft.client.MinecraftClient;
  10. import net.minecraft.client.render.GuiLighting;
  11. import net.minecraft.client.resource.language.I18n;
  12. import net.minecraft.client.sound.PositionedSoundInstance;
  13. import net.minecraft.client.util.InputUtil;
  14. import net.minecraft.sound.SoundEvents;
  15. public class OverlaySearchField extends TextFieldWidget {
  16. public static boolean isSearching = false;
  17. public long keybindFocusTime = -1;
  18. public int keybindFocusKey = -1;
  19. protected long lastClickedTime = -1;
  20. OverlaySearchField(int x, int y, int width, int height) {
  21. super(x, y, width, height);
  22. setMaxLength(10000);
  23. }
  24. public void laterRender(int int_1, int int_2, float float_1) {
  25. GuiLighting.disable();
  26. RenderHelper.disableDepthTest();
  27. setEditableColor(ContainerScreenOverlay.getEntryListWidget().children().isEmpty() && !getText().isEmpty() ? 16733525 : isSearching ? -1313241 : 14737632);
  28. setSuggestion(!isFocused() && getText().isEmpty() ? I18n.translate("text.rei.search.field.suggestion") : null);
  29. super.render(int_1, int_2, float_1);
  30. RenderHelper.enableDepthTest();
  31. }
  32. @Override
  33. public void renderBorder() {
  34. if (!isSearching)
  35. super.renderBorder();
  36. else if (this.hasBorder()) {
  37. fill(this.getBounds().x - 1, this.getBounds().y - 1, this.getBounds().x + this.getBounds().width + 1, this.getBounds().y + this.getBounds().height + 1, -1313241);
  38. fill(this.getBounds().x, this.getBounds().y, this.getBounds().x + this.getBounds().width, this.getBounds().y + this.getBounds().height, -16777216);
  39. }
  40. }
  41. @Override
  42. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  43. boolean contains = containsMouse(double_1, double_2);
  44. if (isVisible() && contains && int_1 == 1)
  45. setText("");
  46. if (contains && int_1 == 0)
  47. if (lastClickedTime == -1)
  48. lastClickedTime = System.currentTimeMillis();
  49. else if (System.currentTimeMillis() - lastClickedTime > 1200)
  50. lastClickedTime = -1;
  51. else {
  52. lastClickedTime = -1;
  53. isSearching = !isSearching;
  54. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  55. }
  56. return super.mouseClicked(double_1, double_2, int_1);
  57. }
  58. @Override
  59. public boolean keyPressed(int int_1, int int_2, int int_3) {
  60. if (this.isVisible() && this.isFocused())
  61. if (int_1 == 257 || int_1 == 335) {
  62. setFocused(false);
  63. return true;
  64. }
  65. return super.keyPressed(int_1, int_2, int_3);
  66. }
  67. @Override
  68. public boolean charTyped(char char_1, int int_1) {
  69. if (System.currentTimeMillis() - keybindFocusTime < 1000 && InputUtil.isKeyPressed(MinecraftClient.getInstance().window.getHandle(), keybindFocusKey)) {
  70. keybindFocusTime = -1;
  71. keybindFocusKey = -1;
  72. return true;
  73. }
  74. return super.charTyped(char_1, int_1);
  75. }
  76. @Override
  77. public boolean containsMouse(double mouseX, double mouseY) {
  78. return ScreenHelper.getLastOverlay().isNotInExclusionZones(mouseX, mouseY) && super.containsMouse(mouseX, mouseY);
  79. }
  80. @Override
  81. public void render(int int_1, int int_2, float float_1) {
  82. }
  83. }