RecipeViewingScreen.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package me.shedaniel.rei.gui;
  2. import com.google.common.collect.Lists;
  3. import com.mojang.blaze3d.platform.GlStateManager;
  4. import me.shedaniel.cloth.api.ClientUtils;
  5. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  6. import me.shedaniel.rei.api.*;
  7. import me.shedaniel.rei.client.ClientHelper;
  8. import me.shedaniel.rei.client.ScreenHelper;
  9. import me.shedaniel.rei.gui.widget.*;
  10. import net.minecraft.client.MinecraftClient;
  11. import net.minecraft.client.audio.PositionedSoundInstance;
  12. import net.minecraft.client.gui.ContainerScreen;
  13. import net.minecraft.client.gui.Element;
  14. import net.minecraft.client.gui.Screen;
  15. import net.minecraft.client.render.GuiLighting;
  16. import net.minecraft.client.resource.language.I18n;
  17. import net.minecraft.client.util.Window;
  18. import net.minecraft.sound.SoundEvents;
  19. import net.minecraft.text.StringTextComponent;
  20. import net.minecraft.text.TranslatableTextComponent;
  21. import net.minecraft.util.Identifier;
  22. import net.minecraft.util.math.MathHelper;
  23. import java.awt.*;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Optional;
  28. import java.util.function.Supplier;
  29. public class RecipeViewingScreen extends Screen {
  30. public static final Identifier CHEST_GUI_TEXTURE = new Identifier("roughlyenoughitems", "textures/gui/recipecontainer.png");
  31. public static final Color SUB_COLOR = new Color(159, 159, 159);
  32. private static final Identifier CREATIVE_INVENTORY_TABS = new Identifier("textures/gui/container/creative_inventory/tabs.png");
  33. private final List<Widget> widgets;
  34. private final List<TabWidget> tabs;
  35. private final Map<RecipeCategory, List<RecipeDisplay>> categoriesMap;
  36. private final List<RecipeCategory> categories;
  37. public int guiWidth;
  38. public int guiHeight;
  39. public int page, categoryPages;
  40. public int largestWidth, largestHeight;
  41. public boolean choosePageActivated;
  42. public RecipeChoosePageWidget recipeChoosePageWidget;
  43. private Window window;
  44. private Rectangle bounds;
  45. private RecipeCategory selectedCategory;
  46. private ButtonWidget recipeBack, recipeNext, categoryBack, categoryNext;
  47. public RecipeViewingScreen(Window window, Map<RecipeCategory, List<RecipeDisplay>> categoriesMap) {
  48. super(new StringTextComponent(""));
  49. this.categoryPages = 0;
  50. this.window = window;
  51. this.widgets = Lists.newArrayList();
  52. this.bounds = new Rectangle(window.getScaledWidth() / 2 - guiWidth / 2, window.getScaledHeight() / 2 - guiHeight / 2, 176, 186);
  53. this.categoriesMap = categoriesMap;
  54. this.categories = Lists.newArrayList();
  55. RecipeHelper.getInstance().getAllCategories().forEach(category -> {
  56. if (categoriesMap.containsKey(category))
  57. categories.add(category);
  58. });
  59. this.selectedCategory = categories.get(0);
  60. this.tabs = new ArrayList<>();
  61. this.choosePageActivated = false;
  62. }
  63. public static SpeedCraftFunctional getSpeedCraftFunctionalByCategory(ContainerScreen containerScreen, RecipeCategory category) {
  64. for(SpeedCraftFunctional functional : RecipeHelper.getInstance().getSpeedCraftFunctional(category))
  65. for(Class aClass : functional.getFunctioningFor())
  66. if (containerScreen.getClass().isAssignableFrom(aClass))
  67. return functional;
  68. return null;
  69. }
  70. @Override
  71. public boolean keyPressed(int int_1, int int_2, int int_3) {
  72. if (int_1 == 256 && choosePageActivated) {
  73. choosePageActivated = false;
  74. init();
  75. return true;
  76. }
  77. if ((int_1 == 256 || this.minecraft.options.keyInventory.matchesKey(int_1, int_2)) && this.shouldCloseOnEsc()) {
  78. MinecraftClient.getInstance().openScreen(ScreenHelper.getLastContainerScreen());
  79. ScreenHelper.getLastOverlay().onInitialized();
  80. return true;
  81. }
  82. if (int_1 == 258) {
  83. boolean boolean_1 = !hasShiftDown();
  84. if (!this.method_20087(boolean_1))
  85. this.method_20087(boolean_1);
  86. return true;
  87. }
  88. if (choosePageActivated) {
  89. if (recipeChoosePageWidget.keyPressed(int_1, int_2, int_3))
  90. return true;
  91. return false;
  92. }
  93. for(Widget widget : widgets)
  94. if (widget.keyPressed(int_1, int_2, int_3))
  95. return true;
  96. return super.keyPressed(int_1, int_2, int_3);
  97. }
  98. @Override
  99. public boolean isPauseScreen() {
  100. return false;
  101. }
  102. @Override
  103. public void init() {
  104. super.init();
  105. this.children.clear();
  106. this.tabs.clear();
  107. this.widgets.clear();
  108. this.largestWidth = window.getScaledWidth() - 100;
  109. this.largestHeight = window.getScaledHeight() - 40;
  110. this.guiWidth = MathHelper.clamp(getCurrentDisplayed().stream().map(display -> selectedCategory.getDisplayWidth(display)).max(Integer::compareTo).orElse(150) + 30, 0, largestWidth);
  111. this.guiHeight = MathHelper.floor(MathHelper.clamp((selectedCategory.getDisplayHeight() + 7d) * (getRecipesPerPage() + 1d) + 40d, 186d, (double) largestHeight));
  112. this.bounds = new Rectangle(window.getScaledWidth() / 2 - guiWidth / 2, window.getScaledHeight() / 2 - guiHeight / 2, guiWidth, guiHeight);
  113. this.page = MathHelper.clamp(page, 0, getTotalPages(selectedCategory) - 1);
  114. widgets.add(categoryBack = new ButtonWidget((int) bounds.getX() + 5, (int) bounds.getY() + 5, 12, 12, new TranslatableTextComponent("text.rei.left_arrow")) {
  115. @Override
  116. public void onPressed() {
  117. int currentCategoryIndex = categories.indexOf(selectedCategory);
  118. currentCategoryIndex--;
  119. if (currentCategoryIndex < 0)
  120. currentCategoryIndex = categories.size() - 1;
  121. selectedCategory = categories.get(currentCategoryIndex);
  122. categoryPages = MathHelper.floor(currentCategoryIndex / 6d);
  123. page = 0;
  124. RecipeViewingScreen.this.init();
  125. }
  126. @Override
  127. public Optional<String> getTooltips() {
  128. return Optional.ofNullable(I18n.translate("text.rei.previous_category"));
  129. }
  130. });
  131. widgets.add(new ClickableLabelWidget((int) bounds.getCenterX(), (int) bounds.getY() + 7, "") {
  132. @Override
  133. public void render(int mouseX, int mouseY, float partialTicks) {
  134. this.text = selectedCategory.getCategoryName();
  135. super.render(mouseX, mouseY, partialTicks);
  136. }
  137. @Override
  138. public Optional<String> getTooltips() {
  139. return Optional.ofNullable(I18n.translate("text.rei.view_all_categories"));
  140. }
  141. @Override
  142. public void onLabelClicked() {
  143. MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  144. ClientHelper.executeViewAllRecipesKeyBind();
  145. }
  146. });
  147. widgets.add(categoryNext = new ButtonWidget((int) bounds.getMaxX() - 17, (int) bounds.getY() + 5, 12, 12, new TranslatableTextComponent("text.rei.right_arrow")) {
  148. @Override
  149. public void onPressed() {
  150. int currentCategoryIndex = categories.indexOf(selectedCategory);
  151. currentCategoryIndex++;
  152. if (currentCategoryIndex >= categories.size())
  153. currentCategoryIndex = 0;
  154. selectedCategory = categories.get(currentCategoryIndex);
  155. categoryPages = MathHelper.floor(currentCategoryIndex / 6d);
  156. page = 0;
  157. RecipeViewingScreen.this.init();
  158. }
  159. @Override
  160. public Optional<String> getTooltips() {
  161. return Optional.ofNullable(I18n.translate("text.rei.next_category"));
  162. }
  163. });
  164. categoryBack.enabled = categories.size() > 1;
  165. categoryNext.enabled = categories.size() > 1;
  166. widgets.add(recipeBack = new ButtonWidget((int) bounds.getX() + 5, (int) bounds.getY() + 21, 12, 12, new TranslatableTextComponent("text.rei.left_arrow")) {
  167. @Override
  168. public void onPressed() {
  169. page--;
  170. if (page < 0)
  171. page = getTotalPages(selectedCategory) - 1;
  172. RecipeViewingScreen.this.init();
  173. }
  174. @Override
  175. public Optional<String> getTooltips() {
  176. return Optional.ofNullable(I18n.translate("text.rei.previous_page"));
  177. }
  178. });
  179. widgets.add(new ClickableLabelWidget((int) bounds.getCenterX(), (int) bounds.getY() + 23, "") {
  180. @Override
  181. public void render(int mouseX, int mouseY, float partialTicks) {
  182. this.text = String.format("%d/%d", page + 1, getTotalPages(selectedCategory));
  183. super.render(mouseX, mouseY, partialTicks);
  184. }
  185. @Override
  186. public Optional<String> getTooltips() {
  187. return Optional.ofNullable(I18n.translate("text.rei.choose_page"));
  188. }
  189. @Override
  190. public void onLabelClicked() {
  191. MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  192. RecipeViewingScreen.this.choosePageActivated = true;
  193. RecipeViewingScreen.this.init();
  194. }
  195. });
  196. widgets.add(recipeNext = new ButtonWidget((int) bounds.getMaxX() - 17, (int) bounds.getY() + 21, 12, 12, new TranslatableTextComponent("text.rei.right_arrow")) {
  197. @Override
  198. public void onPressed() {
  199. page++;
  200. if (page >= getTotalPages(selectedCategory))
  201. page = 0;
  202. RecipeViewingScreen.this.init();
  203. }
  204. @Override
  205. public Optional<String> getTooltips() {
  206. return Optional.ofNullable(I18n.translate("text.rei.next_page"));
  207. }
  208. });
  209. int recipesPerPageByHeight = getRecipesPerPageByHeight();
  210. recipeBack.enabled = categoriesMap.get(selectedCategory).size() > recipesPerPageByHeight;
  211. recipeNext.enabled = categoriesMap.get(selectedCategory).size() > recipesPerPageByHeight;
  212. for(int i = 0; i < 6; i++) {
  213. int j = i + categoryPages * 6;
  214. if (categories.size() > j) {
  215. TabWidget tab;
  216. tabs.add(tab = new TabWidget(i, this, new Rectangle(bounds.x + 4 + 28 * i, bounds.y - 28, 28, 28)) {
  217. @Override
  218. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  219. if (getBounds().contains(mouseX, mouseY)) {
  220. MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  221. if (getId() + categoryPages * 6 == categories.indexOf(selectedCategory))
  222. return false;
  223. selectedCategory = categories.get(getId() + categoryPages * 6);
  224. page = 0;
  225. RecipeViewingScreen.this.init();
  226. return true;
  227. }
  228. return false;
  229. }
  230. });
  231. tab.setItem(categories.get(j).getCategoryIcon(), categories.get(j).getCategoryName(), tab.getId() + categoryPages * 6 == categories.indexOf(selectedCategory));
  232. }
  233. }
  234. Optional<ButtonAreaSupplier> supplier = RecipeHelper.getInstance().getSpeedCraftButtonArea(selectedCategory);
  235. final SpeedCraftFunctional functional = getSpeedCraftFunctionalByCategory(ScreenHelper.getLastContainerScreen(), selectedCategory);
  236. int recipeHeight = selectedCategory.getDisplayHeight();
  237. List<RecipeDisplay> currentDisplayed = getCurrentDisplayed();
  238. for(int i = 0; i < currentDisplayed.size(); i++) {
  239. int finalI = i;
  240. final Supplier<RecipeDisplay> displaySupplier = () -> currentDisplayed.get(finalI);
  241. int displayWidth = selectedCategory.getDisplayWidth(displaySupplier.get());
  242. final Rectangle displayBounds = new Rectangle((int) getBounds().getCenterX() - displayWidth / 2, getBounds().y + 40 + recipeHeight * i + 7 * i, displayWidth, recipeHeight);
  243. widgets.addAll(selectedCategory.setupDisplay(displaySupplier, displayBounds));
  244. if (supplier.isPresent())
  245. widgets.add(new SpeedCraftingButtonWidget(supplier.get().get(displayBounds), supplier.get().getButtonText(), functional, displaySupplier));
  246. }
  247. if (choosePageActivated)
  248. recipeChoosePageWidget = new RecipeChoosePageWidget(this, page, getTotalPages(selectedCategory));
  249. else
  250. recipeChoosePageWidget = null;
  251. children.addAll(tabs);
  252. children.add(ScreenHelper.getLastOverlay(true, false));
  253. children.addAll(widgets);
  254. }
  255. public List<Widget> getWidgets() {
  256. return widgets;
  257. }
  258. public List<RecipeDisplay> getCurrentDisplayed() {
  259. List<RecipeDisplay> list = Lists.newArrayList();
  260. int recipesPerPage = getRecipesPerPage();
  261. for(int i = 0; i <= recipesPerPage; i++)
  262. if (page * (recipesPerPage + 1) + i < categoriesMap.get(selectedCategory).size())
  263. list.add(categoriesMap.get(selectedCategory).get(page * (recipesPerPage + 1) + i));
  264. return list;
  265. }
  266. public RecipeCategory getSelectedCategory() {
  267. return selectedCategory;
  268. }
  269. public int getPage() {
  270. return page;
  271. }
  272. public int getCategoryPage() {
  273. return categoryPages;
  274. }
  275. private int getRecipesPerPage() {
  276. if (selectedCategory.getDisplaySettings().getFixedRecipesPerPage() > 0)
  277. return selectedCategory.getDisplaySettings().getFixedRecipesPerPage() - 1;
  278. int height = selectedCategory.getDisplayHeight();
  279. return MathHelper.clamp(MathHelper.floor(((double) largestHeight - 40d) / ((double) height + 7d)) - 1, 0, Math.min(RoughlyEnoughItemsCore.getConfigManager().getConfig().maxRecipePerPage - 1, selectedCategory.getMaximumRecipePerPage() - 1));
  280. }
  281. private int getRecipesPerPageByHeight() {
  282. int height = selectedCategory.getDisplayHeight();
  283. return MathHelper.clamp(MathHelper.floor(((double) guiHeight - 40d) / ((double) height + 7d)) - 1, 0, Math.min(RoughlyEnoughItemsCore.getConfigManager().getConfig().maxRecipePerPage - 1, selectedCategory.getMaximumRecipePerPage() - 1));
  284. }
  285. @Override
  286. public void render(int mouseX, int mouseY, float delta) {
  287. this.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
  288. if (selectedCategory != null)
  289. selectedCategory.drawCategoryBackground(bounds, mouseX, mouseY, delta);
  290. else {
  291. new RecipeBaseWidget(bounds).render();
  292. fill(bounds.x + 17, bounds.y + 5, bounds.x + bounds.width - 17, bounds.y + 17, SUB_COLOR.getRGB());
  293. fill(bounds.x + 17, bounds.y + 21, bounds.x + bounds.width - 17, bounds.y + 33, SUB_COLOR.getRGB());
  294. }
  295. tabs.stream().filter(tabWidget -> {
  296. return !tabWidget.isSelected();
  297. }).forEach(tabWidget -> tabWidget.render(mouseX, mouseY, delta));
  298. GuiLighting.disable();
  299. super.render(mouseX, mouseY, delta);
  300. widgets.forEach(widget -> {
  301. GuiLighting.disable();
  302. widget.render(mouseX, mouseY, delta);
  303. });
  304. GlStateManager.color4f(1.0F, 1.0F, 1.0F, 1.0F);
  305. GuiLighting.disable();
  306. tabs.stream().filter(TabWidget::isSelected).forEach(tabWidget -> tabWidget.render(mouseX, mouseY, delta));
  307. ScreenHelper.getLastOverlay().render(mouseX, mouseY, delta);
  308. if (choosePageActivated) {
  309. blitOffset = 500.0f;
  310. this.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
  311. blitOffset = 0.0f;
  312. recipeChoosePageWidget.render(mouseX, mouseY, delta);
  313. }
  314. }
  315. public int getTotalPages(RecipeCategory category) {
  316. return MathHelper.ceil(categoriesMap.get(category).size() / (double) (getRecipesPerPage() + 1));
  317. }
  318. public Rectangle getBounds() {
  319. return bounds;
  320. }
  321. @Override
  322. public boolean charTyped(char char_1, int int_1) {
  323. if (choosePageActivated) {
  324. if (recipeChoosePageWidget.charTyped(char_1, int_1))
  325. return true;
  326. return false;
  327. }
  328. for(Element listener : children())
  329. if (listener.charTyped(char_1, int_1))
  330. return true;
  331. return super.charTyped(char_1, int_1);
  332. }
  333. @Override
  334. public boolean mouseDragged(double double_1, double double_2, int int_1, double double_3, double double_4) {
  335. if (choosePageActivated) {
  336. if (recipeChoosePageWidget.mouseDragged(double_1, double_2, int_1, double_3, double_4))
  337. return true;
  338. return false;
  339. }
  340. return super.mouseDragged(double_1, double_2, int_1, double_3, double_4);
  341. }
  342. @Override
  343. public boolean mouseReleased(double double_1, double double_2, int int_1) {
  344. if (choosePageActivated) {
  345. if (recipeChoosePageWidget.mouseReleased(double_1, double_2, int_1))
  346. return true;
  347. return false;
  348. }
  349. return super.mouseReleased(double_1, double_2, int_1);
  350. }
  351. @Override
  352. public boolean mouseScrolled(double i, double j, double amount) {
  353. for(Element listener : children())
  354. if (listener.mouseScrolled(i, j, amount))
  355. return true;
  356. if (getBounds().contains(ClientUtils.getMouseLocation())) {
  357. if (amount > 0 && recipeBack.enabled)
  358. recipeBack.onPressed();
  359. else if (amount < 0 && recipeNext.enabled)
  360. recipeNext.onPressed();
  361. }
  362. if ((new Rectangle(bounds.x, bounds.y - 28, bounds.width, 28)).contains(ClientUtils.getMouseLocation())) {
  363. if (amount > 0 && categoryBack.enabled)
  364. categoryBack.onPressed();
  365. else if (amount < 0 && categoryNext.enabled)
  366. categoryNext.onPressed();
  367. }
  368. return super.mouseScrolled(i, j, amount);
  369. }
  370. @Override
  371. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  372. if (choosePageActivated)
  373. if (recipeChoosePageWidget.isHighlighted(double_1, double_2)) {
  374. if (recipeChoosePageWidget.mouseClicked(double_1, double_2, int_1))
  375. return true;
  376. return false;
  377. } else {
  378. choosePageActivated = false;
  379. init();
  380. return false;
  381. }
  382. for(Element entry : children())
  383. if (entry.mouseClicked(double_1, double_2, int_1)) {
  384. method_20084(entry);
  385. if (int_1 == 0)
  386. setDragging(true);
  387. return true;
  388. }
  389. return false;
  390. }
  391. @Override
  392. public Element getFocused() {
  393. if (choosePageActivated)
  394. return recipeChoosePageWidget;
  395. return super.getFocused();
  396. }
  397. }