OverlaySearchField.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2018, 2019, 2020 shedaniel
  3. * Licensed under the MIT License (the "License").
  4. */
  5. package me.shedaniel.rei.gui;
  6. import com.google.common.collect.Lists;
  7. import com.mojang.blaze3d.systems.RenderSystem;
  8. import me.shedaniel.math.api.Point;
  9. import me.shedaniel.math.impl.PointHelper;
  10. import me.shedaniel.rei.gui.widget.TextFieldWidget;
  11. import me.shedaniel.rei.impl.ScreenHelper;
  12. import net.minecraft.client.MinecraftClient;
  13. import net.minecraft.client.resource.language.I18n;
  14. import net.minecraft.client.sound.PositionedSoundInstance;
  15. import net.minecraft.client.util.InputUtil;
  16. import net.minecraft.sound.SoundEvents;
  17. import net.minecraft.util.Pair;
  18. import org.jetbrains.annotations.ApiStatus;
  19. import java.util.List;
  20. @ApiStatus.Internal
  21. public class OverlaySearchField extends TextFieldWidget {
  22. public static boolean isSearching = false;
  23. public long keybindFocusTime = -1;
  24. public int keybindFocusKey = -1;
  25. protected Pair<Long, Point> lastClickedDetails = null;
  26. private List<String> history = Lists.newArrayListWithCapacity(100);
  27. OverlaySearchField(int x, int y, int width, int height) {
  28. super(x, y, width, height);
  29. setMaxLength(10000);
  30. }
  31. @Override
  32. public void setFocused(boolean boolean_1) {
  33. if (isFocused() != boolean_1)
  34. addToHistory(getText());
  35. super.setFocused(boolean_1);
  36. }
  37. @ApiStatus.Internal
  38. public void addToHistory(String text) {
  39. if (!text.isEmpty()) {
  40. history.removeIf(str -> str.equalsIgnoreCase(text));
  41. history.add(text);
  42. if (history.size() > 100)
  43. history.remove(0);
  44. }
  45. }
  46. public void laterRender(int int_1, int int_2, float float_1) {
  47. RenderSystem.disableDepthTest();
  48. setEditableColor(ContainerScreenOverlay.getEntryListWidget().getAllStacks().isEmpty() && !getText().isEmpty() ? 16733525 : isSearching ? -852212 : (containsMouse(PointHelper.fromMouse()) || isFocused()) ? (ScreenHelper.isDarkModeEnabled() ? -17587 : -1) : -6250336);
  49. setSuggestion(!isFocused() && getText().isEmpty() ? I18n.translate("text.rei.search.field.suggestion") : null);
  50. super.render(int_1, int_2, float_1);
  51. RenderSystem.enableDepthTest();
  52. }
  53. @Override
  54. protected void renderSuggestion(int x, int y) {
  55. if (containsMouse(PointHelper.fromMouse()) || isFocused())
  56. this.font.drawWithShadow(this.font.trimToWidth(this.getSuggestion(), this.getWidth()), x, y, ScreenHelper.isDarkModeEnabled() ? 0xccddaa3d : 0xddeaeaea);
  57. else
  58. this.font.drawWithShadow(this.font.trimToWidth(this.getSuggestion(), this.getWidth()), x, y, -6250336);
  59. }
  60. @Override
  61. public void renderBorder() {
  62. boolean hasError = ContainerScreenOverlay.getEntryListWidget().getAllStacks().isEmpty() && !getText().isEmpty();
  63. if (isSearching) {
  64. fill(this.getBounds().x - 1, this.getBounds().y - 1, this.getBounds().x + this.getBounds().width + 1, this.getBounds().y + this.getBounds().height + 1, -852212);
  65. } else if (hasError) {
  66. fill(this.getBounds().x - 1, this.getBounds().y - 1, this.getBounds().x + this.getBounds().width + 1, this.getBounds().y + this.getBounds().height + 1, -43691);
  67. } else {
  68. super.renderBorder();
  69. return;
  70. }
  71. fill(this.getBounds().x, this.getBounds().y, this.getBounds().x + this.getBounds().width, this.getBounds().y + this.getBounds().height, -16777216);
  72. }
  73. public int getManhattanDistance(Point point1, Point point2) {
  74. int e = Math.abs(point1.getX() - point2.getX());
  75. int f = Math.abs(point1.getY() - point2.getY());
  76. return e + f;
  77. }
  78. @Override
  79. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  80. boolean contains = containsMouse(double_1, double_2);
  81. if (isVisible() && contains && int_1 == 1)
  82. setText("");
  83. if (contains && int_1 == 0)
  84. if (lastClickedDetails == null)
  85. lastClickedDetails = new Pair<>(System.currentTimeMillis(), new Point(double_1, double_2));
  86. else if (System.currentTimeMillis() - lastClickedDetails.getLeft() > 1500)
  87. lastClickedDetails = null;
  88. else if (getManhattanDistance(lastClickedDetails.getRight(), new Point(double_1, double_2)) <= 25) {
  89. lastClickedDetails = null;
  90. isSearching = !isSearching;
  91. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  92. } else {
  93. lastClickedDetails = new Pair<>(System.currentTimeMillis(), new Point(double_1, double_2));
  94. }
  95. return super.mouseClicked(double_1, double_2, int_1);
  96. }
  97. @Override
  98. public boolean keyPressed(int int_1, int int_2, int int_3) {
  99. if (this.isVisible() && this.isFocused())
  100. if (int_1 == 257 || int_1 == 335) {
  101. addToHistory(getText());
  102. setFocused(false);
  103. return true;
  104. } else if (int_1 == 265) {
  105. int i = history.indexOf(getText()) - 1;
  106. if (i < -1 && getText().isEmpty())
  107. i = history.size() - 1;
  108. else if (i < -1) {
  109. addToHistory(getText());
  110. i = history.size() - 2;
  111. }
  112. if (i >= 0) {
  113. setText(history.get(i));
  114. return true;
  115. }
  116. } else if (int_1 == 264) {
  117. int i = history.indexOf(getText()) + 1;
  118. if (i > 0) {
  119. setText(i < history.size() ? history.get(i) : "");
  120. return true;
  121. }
  122. }
  123. return super.keyPressed(int_1, int_2, int_3);
  124. }
  125. @Override
  126. public boolean charTyped(char char_1, int int_1) {
  127. if (System.currentTimeMillis() - keybindFocusTime < 1000 && InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), keybindFocusKey)) {
  128. keybindFocusTime = -1;
  129. keybindFocusKey = -1;
  130. return true;
  131. }
  132. return super.charTyped(char_1, int_1);
  133. }
  134. @Override
  135. public boolean containsMouse(double mouseX, double mouseY) {
  136. return ScreenHelper.getLastOverlay().isNotInExclusionZones(mouseX, mouseY) && super.containsMouse(mouseX, mouseY);
  137. }
  138. @Override
  139. public void render(int mouseX, int mouseY, float delta) {
  140. }
  141. }