SearchFieldWidget.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.gui.widget;
  6. import com.mojang.blaze3d.platform.GlStateManager;
  7. import net.minecraft.client.render.GuiLighting;
  8. import net.minecraft.client.sound.PositionedSoundInstance;
  9. import net.minecraft.sound.SoundEvents;
  10. public class SearchFieldWidget extends TextFieldWidget {
  11. public static boolean isSearching = false;
  12. protected long lastClickedTime = -1;
  13. public SearchFieldWidget(int x, int y, int width, int height) {
  14. super(x, y, width, height);
  15. }
  16. public void laterRender(int int_1, int int_2, float float_1) {
  17. GuiLighting.disable();
  18. GlStateManager.disableDepthTest();
  19. setEditableColor(isSearching ? -1313241 : 14737632);
  20. super.render(int_1, int_2, float_1);
  21. GlStateManager.enableDepthTest();
  22. }
  23. @Override
  24. public void renderBorder() {
  25. if (!isSearching)
  26. super.renderBorder();
  27. else {
  28. fill(this.getBounds().x - 1, this.getBounds().y - 1, this.getBounds().x + this.getBounds().width + 1, this.getBounds().y + this.getBounds().height + 1, -1313241);
  29. fill(this.getBounds().x, this.getBounds().y, this.getBounds().x + this.getBounds().width, this.getBounds().y + this.getBounds().height, -16777216);
  30. }
  31. }
  32. @Override
  33. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  34. boolean contains = isHighlighted(double_1, double_2);
  35. if (isVisible() && contains && int_1 == 1)
  36. setText("");
  37. if (contains && int_1 == 0)
  38. if (lastClickedTime == -1)
  39. lastClickedTime = System.currentTimeMillis();
  40. else if (System.currentTimeMillis() - lastClickedTime > 1200)
  41. lastClickedTime = -1;
  42. else {
  43. lastClickedTime = -1;
  44. isSearching = !isSearching;
  45. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  46. }
  47. return super.mouseClicked(double_1, double_2, int_1);
  48. }
  49. @Override
  50. public boolean keyPressed(int int_1, int int_2, int int_3) {
  51. if (this.isVisible() && this.isFocused())
  52. if (int_1 == 257 || int_1 == 335) {
  53. setFocused(false);
  54. return true;
  55. }
  56. return super.keyPressed(int_1, int_2, int_3);
  57. }
  58. @Override
  59. public void render(int int_1, int int_2, float float_1) {
  60. }
  61. }