OverlaySearchField.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.gui;
  24. import com.google.common.collect.Lists;
  25. import com.mojang.blaze3d.systems.RenderSystem;
  26. import me.shedaniel.math.Point;
  27. import me.shedaniel.math.impl.PointHelper;
  28. import me.shedaniel.rei.api.REIHelper;
  29. import me.shedaniel.rei.gui.widget.TextFieldWidget;
  30. import me.shedaniel.rei.impl.ScreenHelper;
  31. import net.minecraft.client.MinecraftClient;
  32. import net.minecraft.client.resource.language.I18n;
  33. import net.minecraft.client.sound.PositionedSoundInstance;
  34. import net.minecraft.client.util.InputUtil;
  35. import net.minecraft.client.util.math.MatrixStack;
  36. import net.minecraft.sound.SoundEvents;
  37. import net.minecraft.util.Pair;
  38. import org.jetbrains.annotations.ApiStatus;
  39. import java.util.List;
  40. @ApiStatus.Internal
  41. public class OverlaySearchField extends TextFieldWidget {
  42. public static boolean isSearching = false;
  43. public long keybindFocusTime = -1;
  44. public int keybindFocusKey = -1;
  45. public boolean isMain = true;
  46. protected Pair<Long, Point> lastClickedDetails = null;
  47. private List<String> history = Lists.newArrayListWithCapacity(100);
  48. public OverlaySearchField(int x, int y, int width, int height) {
  49. super(x, y, width, height);
  50. setMaxLength(10000);
  51. }
  52. @Override
  53. public void setFocused(boolean boolean_1) {
  54. if (isFocused() != boolean_1 && isMain)
  55. addToHistory(getText());
  56. super.setFocused(boolean_1);
  57. }
  58. @ApiStatus.Internal
  59. public void addToHistory(String text) {
  60. if (!text.isEmpty()) {
  61. history.removeIf(str -> str.equalsIgnoreCase(text));
  62. history.add(text);
  63. if (history.size() > 100)
  64. history.remove(0);
  65. }
  66. }
  67. public void laterRender(MatrixStack matrices, int int_1, int int_2, float float_1) {
  68. RenderSystem.disableDepthTest();
  69. setEditableColor(isMain && ContainerScreenOverlay.getEntryListWidget().getAllStacks().isEmpty() && !getText().isEmpty() ? 16733525 : isSearching && isMain ? -852212 : (containsMouse(PointHelper.ofMouse()) || isFocused()) ? (REIHelper.getInstance().isDarkThemeEnabled() ? -17587 : -1) : -6250336);
  70. setSuggestion(!isFocused() && getText().isEmpty() ? I18n.translate("text.rei.search.field.suggestion") : null);
  71. super.render(matrices, int_1, int_2, float_1);
  72. RenderSystem.enableDepthTest();
  73. }
  74. @Override
  75. protected void renderSuggestion(MatrixStack matrices, int x, int y) {
  76. if (containsMouse(PointHelper.ofMouse()) || isFocused())
  77. this.font.drawWithShadow(matrices, this.font.trimToWidth(this.getSuggestion(), this.getWidth()), x, y, REIHelper.getInstance().isDarkThemeEnabled() ? 0xccddaa3d : 0xddeaeaea);
  78. else
  79. this.font.drawWithShadow(matrices, this.font.trimToWidth(this.getSuggestion(), this.getWidth()), x, y, -6250336);
  80. }
  81. @Override
  82. public void renderBorder(MatrixStack matrices) {
  83. if (isMain && isSearching) {
  84. fill(matrices, this.getBounds().x - 1, this.getBounds().y - 1, this.getBounds().x + this.getBounds().width + 1, this.getBounds().y + this.getBounds().height + 1, -852212);
  85. } else if (isMain && ContainerScreenOverlay.getEntryListWidget().getAllStacks().isEmpty() && !getText().isEmpty()) {
  86. fill(matrices, this.getBounds().x - 1, this.getBounds().y - 1, this.getBounds().x + this.getBounds().width + 1, this.getBounds().y + this.getBounds().height + 1, -43691);
  87. } else {
  88. super.renderBorder(matrices);
  89. return;
  90. }
  91. fill(matrices, this.getBounds().x, this.getBounds().y, this.getBounds().x + this.getBounds().width, this.getBounds().y + this.getBounds().height, -16777216);
  92. }
  93. public int getManhattanDistance(Point point1, Point point2) {
  94. int e = Math.abs(point1.getX() - point2.getX());
  95. int f = Math.abs(point1.getY() - point2.getY());
  96. return e + f;
  97. }
  98. @Override
  99. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  100. boolean contains = containsMouse(double_1, double_2);
  101. if (isVisible() && contains && int_1 == 1)
  102. setText("");
  103. if (contains && int_1 == 0 && isMain)
  104. if (lastClickedDetails == null)
  105. lastClickedDetails = new Pair<>(System.currentTimeMillis(), new Point(double_1, double_2));
  106. else if (System.currentTimeMillis() - lastClickedDetails.getLeft() > 1500)
  107. lastClickedDetails = null;
  108. else if (getManhattanDistance(lastClickedDetails.getRight(), new Point(double_1, double_2)) <= 25) {
  109. lastClickedDetails = null;
  110. isSearching = !isSearching;
  111. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  112. } else {
  113. lastClickedDetails = new Pair<>(System.currentTimeMillis(), new Point(double_1, double_2));
  114. }
  115. return super.mouseClicked(double_1, double_2, int_1);
  116. }
  117. @Override
  118. public boolean keyPressed(int int_1, int int_2, int int_3) {
  119. if (this.isVisible() && this.isFocused() && isMain)
  120. if (int_1 == 257 || int_1 == 335) {
  121. addToHistory(getText());
  122. setFocused(false);
  123. return true;
  124. } else if (int_1 == 265) {
  125. int i = history.indexOf(getText()) - 1;
  126. if (i < -1 && getText().isEmpty())
  127. i = history.size() - 1;
  128. else if (i < -1) {
  129. addToHistory(getText());
  130. i = history.size() - 2;
  131. }
  132. if (i >= 0) {
  133. setText(history.get(i));
  134. return true;
  135. }
  136. } else if (int_1 == 264) {
  137. int i = history.indexOf(getText()) + 1;
  138. if (i > 0) {
  139. setText(i < history.size() ? history.get(i) : "");
  140. return true;
  141. }
  142. }
  143. return super.keyPressed(int_1, int_2, int_3);
  144. }
  145. @Override
  146. public boolean charTyped(char char_1, int int_1) {
  147. if (System.currentTimeMillis() - keybindFocusTime < 1000 && InputUtil.isKeyPressed(MinecraftClient.getInstance().getWindow().getHandle(), keybindFocusKey)) {
  148. keybindFocusTime = -1;
  149. keybindFocusKey = -1;
  150. return true;
  151. }
  152. return super.charTyped(char_1, int_1);
  153. }
  154. @Override
  155. public boolean containsMouse(double mouseX, double mouseY) {
  156. return (!isMain || ScreenHelper.getLastOverlay().isNotInExclusionZones(mouseX, mouseY)) && super.containsMouse(mouseX, mouseY);
  157. }
  158. @Override
  159. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  160. }
  161. }