EntryWidget.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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.systems.RenderSystem;
  7. import me.shedaniel.math.api.Rectangle;
  8. import me.shedaniel.math.impl.PointHelper;
  9. import me.shedaniel.rei.api.ClientHelper;
  10. import me.shedaniel.rei.api.ConfigObject;
  11. import me.shedaniel.rei.api.EntryStack;
  12. import me.shedaniel.rei.impl.ScreenHelper;
  13. import net.minecraft.client.gui.Element;
  14. import net.minecraft.util.Identifier;
  15. import net.minecraft.util.math.MathHelper;
  16. import java.util.ArrayList;
  17. import java.util.Collection;
  18. import java.util.Collections;
  19. import java.util.List;
  20. public class EntryWidget extends WidgetWithBounds {
  21. protected static final Identifier RECIPE_GUI = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
  22. protected static final Identifier RECIPE_GUI_DARK = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer_dark.png");
  23. protected boolean highlight = true;
  24. protected boolean tooltips = true;
  25. protected boolean background = true;
  26. protected boolean interactable = true;
  27. private Rectangle bounds;
  28. private List<EntryStack> entryStacks;
  29. protected EntryWidget(int x, int y) {
  30. this.bounds = new Rectangle(x - 1, y - 1, 18, 18);
  31. this.entryStacks = new ArrayList<>();
  32. }
  33. public static EntryWidget create(int x, int y) {
  34. return new EntryWidget(x, y);
  35. }
  36. public EntryWidget disableInteractions() {
  37. return interactable(false);
  38. }
  39. public EntryWidget interactable(boolean b) {
  40. interactable = b;
  41. return this;
  42. }
  43. public EntryWidget noHighlight() {
  44. return highlight(false);
  45. }
  46. public EntryWidget highlight(boolean b) {
  47. highlight = b;
  48. return this;
  49. }
  50. public EntryWidget noTooltips() {
  51. return tooltips(false);
  52. }
  53. public EntryWidget tooltips(boolean b) {
  54. tooltips = b;
  55. return this;
  56. }
  57. public EntryWidget noBackground() {
  58. return background(false);
  59. }
  60. public EntryWidget background(boolean b) {
  61. background = b;
  62. return this;
  63. }
  64. public EntryWidget clearStacks() {
  65. entryStacks.clear();
  66. return this;
  67. }
  68. public EntryWidget entry(EntryStack stack) {
  69. entryStacks.add(stack);
  70. return this;
  71. }
  72. public EntryWidget entries(Collection<EntryStack> stacks) {
  73. entryStacks.addAll(stacks);
  74. return this;
  75. }
  76. protected EntryStack getCurrentEntry() {
  77. if (entryStacks.isEmpty())
  78. return EntryStack.empty();
  79. if (entryStacks.size() == 1)
  80. return entryStacks.get(0);
  81. return entryStacks.get(MathHelper.floor((System.currentTimeMillis() / 500 % (double) entryStacks.size()) / 1f));
  82. }
  83. public List<EntryStack> entries() {
  84. return entryStacks;
  85. }
  86. @Override
  87. public Rectangle getBounds() {
  88. return bounds;
  89. }
  90. protected Rectangle getInnerBounds() {
  91. return new Rectangle(bounds.x + 1, bounds.y + 1, bounds.width - 2, bounds.height - 2);
  92. }
  93. @Override
  94. public void render(int mouseX, int mouseY, float delta) {
  95. if (background) {
  96. drawBackground(mouseX, mouseY, delta);
  97. }
  98. drawCurrentEntry(mouseX, mouseY, delta);
  99. boolean highlighted = containsMouse(mouseX, mouseY);
  100. if (tooltips && highlighted) {
  101. queueTooltip(mouseX, mouseY, delta);
  102. }
  103. if (highlight && highlighted) {
  104. drawHighlighted(mouseX, mouseY, delta);
  105. }
  106. }
  107. protected void drawBackground(int mouseX, int mouseY, float delta) {
  108. minecraft.getTextureManager().bindTexture(ScreenHelper.isDarkModeEnabled() ? RECIPE_GUI_DARK : RECIPE_GUI);
  109. blit(bounds.x, bounds.y, 0, 222, bounds.width, bounds.height);
  110. }
  111. protected void drawCurrentEntry(int mouseX, int mouseY, float delta) {
  112. EntryStack entry = getCurrentEntry();
  113. entry.setZ(100);
  114. entry.render(getInnerBounds(), mouseX, mouseY, delta);
  115. }
  116. protected void queueTooltip(int mouseX, int mouseY, float delta) {
  117. QueuedTooltip tooltip = getCurrentTooltip(mouseX, mouseY);
  118. if (tooltip != null) {
  119. ScreenHelper.getLastOverlay().addTooltip(tooltip);
  120. }
  121. }
  122. public QueuedTooltip getCurrentTooltip(int mouseX, int mouseY) {
  123. return getCurrentEntry().getTooltip(mouseX, mouseY);
  124. }
  125. protected void drawHighlighted(int mouseX, int mouseY, float delta) {
  126. RenderSystem.disableDepthTest();
  127. RenderSystem.colorMask(true, true, true, false);
  128. int color = ScreenHelper.isDarkModeEnabled() ? -1877929711 : -2130706433;
  129. setZ(300);
  130. Rectangle bounds = getInnerBounds();
  131. fillGradient(bounds.x, bounds.y, bounds.getMaxX(), bounds.getMaxY(), color, color);
  132. setZ(0);
  133. RenderSystem.colorMask(true, true, true, true);
  134. RenderSystem.enableDepthTest();
  135. }
  136. @Override
  137. public List<? extends Element> children() {
  138. return Collections.emptyList();
  139. }
  140. @Override
  141. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  142. if (!interactable)
  143. return false;
  144. if (containsMouse(mouseX, mouseY))
  145. if (button == 0)
  146. return ClientHelper.getInstance().executeRecipeKeyBind(getCurrentEntry());
  147. else if (button == 1)
  148. return ClientHelper.getInstance().executeUsageKeyBind(getCurrentEntry());
  149. return false;
  150. }
  151. @Override
  152. public boolean keyPressed(int int_1, int int_2, int int_3) {
  153. if (!interactable)
  154. return false;
  155. if (containsMouse(PointHelper.fromMouse()))
  156. if (ConfigObject.getInstance().getRecipeKeybind().matchesKey(int_1, int_2))
  157. return ClientHelper.getInstance().executeRecipeKeyBind(getCurrentEntry());
  158. else if (ConfigObject.getInstance().getUsageKeybind().matchesKey(int_1, int_2))
  159. return ClientHelper.getInstance().executeUsageKeyBind(getCurrentEntry());
  160. return false;
  161. }
  162. }