RecipeGui.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. package me.shedaniel.gui;
  2. import com.mojang.blaze3d.platform.GlStateManager;
  3. import me.shedaniel.api.IDisplayCategory;
  4. import me.shedaniel.api.IRecipe;
  5. import me.shedaniel.gui.widget.Button;
  6. import me.shedaniel.gui.widget.Control;
  7. import me.shedaniel.gui.widget.REISlot;
  8. import me.shedaniel.gui.widget.Tab;
  9. import me.shedaniel.impl.REIRecipeManager;
  10. import net.minecraft.client.MinecraftClient;
  11. import net.minecraft.client.gui.ContainerGui;
  12. import net.minecraft.client.gui.Gui;
  13. import net.minecraft.client.render.GuiLighting;
  14. import net.minecraft.client.util.Window;
  15. import net.minecraft.container.Container;
  16. import net.minecraft.util.Identifier;
  17. import net.minecraft.util.math.MathHelper;
  18. import java.util.*;
  19. public class RecipeGui extends ContainerGui {
  20. private static final Identifier CREATIVE_INVENTORY_TABS = new Identifier("textures/gui/container/creative_inventory/tabs.png");
  21. private static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
  22. private final Window mainWindow;
  23. private final Container container;
  24. private final Gui prevScreen;
  25. public final Map<IDisplayCategory, List<IRecipe>> recipes;
  26. public final int guiWidth = 176;
  27. public final int guiHeight = 222;
  28. ArrayList<IDisplayCategory> categories = new ArrayList<>();
  29. private int categoryTabPage = 0;
  30. public IDisplayCategory selectedCategory;
  31. private int recipePointer = 0;
  32. public List<REISlot> slots;
  33. List<Control> controls = new LinkedList<>();
  34. private List<Tab> tabs;
  35. private boolean tabsEnabled = false;
  36. private Button btnCategoryPageLeft, btnCategoryPageRight;
  37. public Button btnRecipeLeft, btnRecipeRight;
  38. public RecipeGui(Container p_i1072_1_, Gui prevScreen, Map<IDisplayCategory, List<IRecipe>> recipes) {
  39. super(new RecipeContainer());
  40. this.container = p_i1072_1_;
  41. this.prevScreen = prevScreen;
  42. this.recipes = recipes;
  43. this.client = MinecraftClient.getInstance();
  44. this.itemRenderer = client.getItemRenderer();
  45. this.fontRenderer = client.fontRenderer;
  46. this.mainWindow = client.window;
  47. setupCategories();
  48. }
  49. private void setupCategories() {
  50. for(IDisplayCategory adapter : REIRecipeManager.instance().getDisplayAdapters())
  51. if (recipes.containsKey(adapter))
  52. categories.add(adapter);
  53. selectedCategory = categories.get(0);
  54. categoryTabPage = 0;
  55. tabs = new ArrayList<>();
  56. for(int i = 0; i < 6; i++)
  57. tabs.add(new Tab(i, 0, 0, 0, 28, 32));
  58. tabs.forEach(tab -> tab.setOnClick(i -> {
  59. if (tab.getId() + categoryTabPage * 6 == categories.indexOf(selectedCategory))
  60. return false;
  61. selectedCategory = categories.get(tab.getId() + categoryTabPage * 6);
  62. recipePointer = 0;
  63. updateRecipe();
  64. return false;
  65. }));
  66. updateRecipe();
  67. }
  68. @Override
  69. public void draw(int mouseX, int mouseY, float partialTicks) {
  70. drawBackground();
  71. super.draw(mouseX, mouseY, partialTicks);
  72. int y = (int) ((mainWindow.getScaledHeight() / 2 - this.guiHeight / 2));
  73. drawStringCentered(this.fontRenderer, selectedCategory.getDisplayName(), left + guiWidth / 2, y + 11, -1);
  74. drawStringCentered(this.fontRenderer, String.format("%d/%d", 1 + getCurrentPage(), getTotalPages()), left + guiWidth / 2, y + 34, -1);
  75. controls.forEach(Control::draw);
  76. }
  77. private int getCurrentPage() {
  78. return recipePointer / 2;
  79. }
  80. @Override
  81. public void update() {
  82. super.update();
  83. slots.forEach(REISlot::tick);
  84. controls.forEach(Control::tick);
  85. }
  86. @Override
  87. public void onScaleChanged(MinecraftClient p_onResize_1_, int p_onResize_2_, int p_onResize_3_) {
  88. super.onScaleChanged(p_onResize_1_, p_onResize_2_, p_onResize_3_);
  89. updateRecipe();
  90. }
  91. private void updateRecipe() {
  92. int categoryPointer = categories.indexOf(selectedCategory);
  93. IRecipe recipe = recipes.get(categories.get(categoryPointer)).get(recipePointer);
  94. selectedCategory.resetRecipes();
  95. selectedCategory.addRecipe(recipe);
  96. slots = selectedCategory.setupDisplay(0);
  97. if (recipes.get(selectedCategory).size() >= recipePointer + 2) {
  98. IRecipe recipe2 = recipes.get(selectedCategory).get(recipePointer + 1);
  99. selectedCategory.addRecipe(recipe2);
  100. slots.addAll(selectedCategory.setupDisplay(1));
  101. }
  102. left = (int) ((mainWindow.getScaledWidth() / 2 - this.guiWidth / 2));
  103. top = (int) ((mainWindow.getScaledHeight() / 2 - this.guiHeight / 2));
  104. slots.forEach(reiSlot -> reiSlot.move(left, top));
  105. Button btnCategoryLeft = new Button(left + 10, top + 5, 15, 20, "<");
  106. Button btnCategoryRight = new Button(left + guiWidth - 25, top + 5, 15, 20, ">");
  107. btnCategoryRight.onClick = this::btnCategoryRight;
  108. btnCategoryLeft.onClick = this::btnCategoryLeft;
  109. btnRecipeLeft = new Button(left + 10, top + 28, 15, 20, "<");
  110. btnRecipeRight = new Button(left + guiWidth - 25, top + 28, 15, 20, ">");
  111. btnRecipeLeft.setEnabled(recipes.get(selectedCategory).size() > 2);
  112. btnRecipeRight.setEnabled(recipes.get(selectedCategory).size() > 2);
  113. btnRecipeRight.onClick = this::btnRecipeRight;
  114. btnRecipeLeft.onClick = this::btnRecipeLeft;
  115. controls.clear();
  116. controls.add(btnCategoryLeft);
  117. controls.add(btnCategoryRight);
  118. if (categories.size() <= 1) {
  119. btnCategoryLeft.setEnabled(false);
  120. btnCategoryRight.setEnabled(false);
  121. } else if (categories.size() > 6) {
  122. btnCategoryPageLeft = new Button(left, top - 52, 20, 20, "<");
  123. btnCategoryPageRight = new Button(left + guiWidth - 20, top - 52, 20, 20, ">");
  124. btnCategoryPageLeft.setOnClick(i -> {
  125. categoryTabPage--;
  126. if (categoryTabPage <= 0)
  127. categoryTabPage = MathHelper.ceil(categories.size() / 6d);
  128. updateRecipe();
  129. return true;
  130. });
  131. btnCategoryPageRight.setOnClick(i -> {
  132. categoryTabPage++;
  133. if (categoryTabPage >= MathHelper.ceil(categories.size() / 6d))
  134. categoryTabPage = 0;
  135. updateRecipe();
  136. return true;
  137. });
  138. if (top - 52 >= 2)
  139. controls.addAll(Arrays.asList(btnCategoryPageLeft, btnCategoryPageRight));
  140. }
  141. controls.add(btnRecipeLeft);
  142. controls.add(btnRecipeRight);
  143. List<Control> newControls = new LinkedList<>();
  144. categories.get(categoryPointer).addWidget(newControls, 0);
  145. if (recipes.get(categories.get(categoryPointer)).size() >= recipePointer + 2)
  146. categories.get(categoryPointer).addWidget(newControls, 1);
  147. newControls.forEach(f -> f.move(left, top));
  148. controls.addAll(newControls);
  149. updateTabs();
  150. }
  151. private void updateTabs() {
  152. tabsEnabled = top - 28 > 4;
  153. if (tabsEnabled) {
  154. tabs.forEach(tab -> tab.moveTo(left + 4, left + 2 + tabs.indexOf(tab) * 28, top - 28));
  155. for(int i = 0; i < tabs.size(); i++) {
  156. int ref = i + categoryTabPage * 6;
  157. if (categories.size() > ref) {
  158. tabs.get(i).setItem(categories.get(ref).getCategoryIcon(), categories.get(ref).getDisplayName(), categories.get(ref).equals(selectedCategory));
  159. } else tabs.get(i).setItem(null, null, false);
  160. }
  161. controls.addAll(tabs);
  162. }
  163. }
  164. @Override
  165. protected void drawBackground(float v, int i, int i1) {
  166. //Tabs
  167. if (tabsEnabled) {
  168. GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  169. GuiLighting.enableForItems();
  170. this.client.getTextureManager().bindTexture(CREATIVE_INVENTORY_TABS);
  171. tabs.stream().filter(tab -> tab.getId() + categoryTabPage * 6 == categories.indexOf(selectedCategory)).forEach(Tab::drawTab);
  172. }
  173. drawBackground();
  174. GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  175. GuiLighting.disable();
  176. this.client.getTextureManager().bindTexture(CHEST_GUI_TEXTURE);
  177. int lvt_4_1_ = (int) ((mainWindow.getScaledWidth() / 2 - this.guiWidth / 2));
  178. int lvt_5_1_ = (int) ((mainWindow.getScaledHeight() / 2 - this.guiHeight / 2));
  179. this.drawTexturedRect(lvt_4_1_, lvt_5_1_, 0, 0, this.guiWidth, this.guiHeight);
  180. slots.forEach(reiSlot -> {
  181. GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  182. GuiLighting.disable();
  183. reiSlot.draw();
  184. });
  185. if (tabsEnabled)
  186. tabs.stream().filter(tab -> tab.getId() + categoryTabPage * 6 != categories.indexOf(selectedCategory)).forEach(tab -> {
  187. GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  188. GuiLighting.enableForItems();
  189. this.client.getTextureManager().bindTexture(CREATIVE_INVENTORY_TABS);
  190. tab.drawTab();
  191. });
  192. }
  193. @Override
  194. public boolean keyPressed(int p_keyPressed_1_, int p_keyPressed_2_, int p_keyPressed_3_) {
  195. if (p_keyPressed_1_ == 256 && prevScreen != null) {
  196. this.client.openGui(prevScreen);
  197. return true;
  198. }
  199. return super.keyPressed(p_keyPressed_1_, p_keyPressed_2_, p_keyPressed_3_);
  200. }
  201. private boolean btnCategoryLeft(int button) {
  202. recipePointer = 0;
  203. int categoryPointer = categories.indexOf(selectedCategory);
  204. categoryPointer--;
  205. if (categoryPointer <= 0)
  206. categoryPointer = categories.size() - 1;
  207. selectedCategory = categories.get(categoryPointer);
  208. categoryTabPage = categoryPointer / 6;
  209. updateRecipe();
  210. return true;
  211. }
  212. private boolean btnCategoryRight(int button) {
  213. recipePointer = 0;
  214. int categoryPointer = categories.indexOf(selectedCategory);
  215. categoryPointer++;
  216. if (categoryPointer >= categories.size())
  217. categoryPointer = 0;
  218. selectedCategory = categories.get(categoryPointer);
  219. categoryTabPage = categoryPointer / 6;
  220. updateRecipe();
  221. return true;
  222. }
  223. private boolean btnRecipeLeft(int button) {
  224. recipePointer -= 2;
  225. if (recipePointer <= 0)
  226. recipePointer = MathHelper.floor((recipes.get(selectedCategory).size() - 1) / 2d) * 2;
  227. updateRecipe();
  228. return true;
  229. }
  230. private boolean btnRecipeRight(int button) {
  231. recipePointer += 2;
  232. if (recipePointer >= recipes.get(selectedCategory).size())
  233. recipePointer = 0;
  234. updateRecipe();
  235. return true;
  236. }
  237. private int riseDoublesToInt(double i) {
  238. return MathHelper.ceil(i);
  239. }
  240. private int getTotalPages() {
  241. return MathHelper.clamp(riseDoublesToInt(recipes.get(selectedCategory).size() / 2d), 1, Integer.MAX_VALUE);
  242. }
  243. }