PreRecipeViewingScreen.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Roughly Enough Items by Danielshe.
  3. * Licensed under the MIT License.
  4. */
  5. package me.shedaniel.rei.gui;
  6. import com.google.common.collect.Lists;
  7. import me.shedaniel.rei.RoughlyEnoughItemsCore;
  8. import me.shedaniel.rei.api.ClientHelper;
  9. import me.shedaniel.rei.api.RecipeCategory;
  10. import me.shedaniel.rei.api.RecipeDisplay;
  11. import me.shedaniel.rei.impl.ScreenHelper;
  12. import me.shedaniel.rei.gui.config.RecipeScreenType;
  13. import me.shedaniel.rei.gui.widget.ButtonWidget;
  14. import me.shedaniel.rei.gui.widget.Widget;
  15. import me.shedaniel.rei.gui.widget.WidgetWithBounds;
  16. import me.zeroeightsix.fiber.exception.FiberException;
  17. import net.minecraft.client.MinecraftClient;
  18. import net.minecraft.client.gui.Element;
  19. import net.minecraft.client.gui.screen.Screen;
  20. import net.minecraft.client.render.GuiLighting;
  21. import net.minecraft.client.resource.language.I18n;
  22. import net.minecraft.client.sound.PositionedSoundInstance;
  23. import net.minecraft.sound.SoundEvents;
  24. import net.minecraft.text.TranslatableText;
  25. import net.minecraft.util.Formatting;
  26. import net.minecraft.util.Identifier;
  27. import java.awt.*;
  28. import java.io.IOException;
  29. import java.util.Collections;
  30. import java.util.List;
  31. import java.util.Map;
  32. public class PreRecipeViewingScreen extends Screen {
  33. private static final Identifier IDENTIFIER = new Identifier("roughlyenoughitems", "textures/gui/screenshot.png");
  34. private final List<Widget> widgets;
  35. private boolean original;
  36. private Map<RecipeCategory<?>, List<RecipeDisplay>> map;
  37. public PreRecipeViewingScreen(Map<RecipeCategory<?>, List<RecipeDisplay>> map) {
  38. super(new TranslatableText("text.rei.recipe_screen_type.selection"));
  39. this.widgets = Lists.newArrayList();
  40. this.original = true;
  41. this.map = map;
  42. }
  43. @Override
  44. protected void init() {
  45. this.children.clear();
  46. this.widgets.clear();
  47. this.widgets.add(new ButtonWidget(width / 2 - 100, height - 40, 200, 20, I18n.translate("text.rei.select")) {
  48. @Override
  49. public void onPressed() {
  50. RoughlyEnoughItemsCore.getConfigManager().getConfig().setRecipeScreenType(original ? RecipeScreenType.ORIGINAL : RecipeScreenType.VILLAGER);
  51. try {
  52. RoughlyEnoughItemsCore.getConfigManager().saveConfig();
  53. } catch (IOException | FiberException e) {
  54. e.printStackTrace();
  55. }
  56. ClientHelper.getInstance().openRecipeViewingScreen(map);
  57. }
  58. });
  59. this.widgets.add(new ScreenTypeSelection(width / 2 - 200 - 5, height / 2 - 112 / 2 - 10, 0));
  60. this.widgets.add(new ScreenTypeSelection(width / 2 + 5, height / 2 - 112 / 2 - 10, 112));
  61. this.children.addAll(widgets);
  62. }
  63. @Override
  64. public void render(int int_1, int int_2, float float_1) {
  65. this.renderBackground();
  66. this.drawCenteredString(this.font, this.title.asFormattedString(), this.width / 2, 20, 16777215);
  67. int i = 30;
  68. for (String s : this.font.wrapStringToWidthAsList(I18n.translate("text.rei.recipe_screen_type.selection.sub"), width - 30)) {
  69. this.drawCenteredString(this.font, Formatting.GRAY.toString() + s, width / 2, i, -1);
  70. i += 10;
  71. }
  72. super.render(int_1, int_2, float_1);
  73. this.widgets.forEach(widget -> {
  74. GuiLighting.disable();
  75. widget.render(int_1, int_2, float_1);
  76. });
  77. }
  78. @Override
  79. public boolean keyPressed(int int_1, int int_2, int int_3) {
  80. if ((int_1 == 256 || this.minecraft.options.keyInventory.matchesKey(int_1, int_2)) && this.shouldCloseOnEsc()) {
  81. MinecraftClient.getInstance().openScreen(ScreenHelper.getLastContainerScreen());
  82. ScreenHelper.getLastOverlay().init();
  83. return true;
  84. }
  85. return super.keyPressed(int_1, int_2, int_3);
  86. }
  87. public class ScreenTypeSelection extends WidgetWithBounds {
  88. private Rectangle bounds;
  89. private int u, v;
  90. public ScreenTypeSelection(int x, int y, int v) {
  91. this.bounds = new Rectangle(x - 4, y - 4, 208, 120);
  92. this.u = 0;
  93. this.v = v;
  94. }
  95. @Override
  96. public Rectangle getBounds() {
  97. return bounds;
  98. }
  99. @Override
  100. public void render(int i, int i1, float delta) {
  101. MinecraftClient.getInstance().getTextureManager().bindTexture(IDENTIFIER);
  102. blit(bounds.x + 4, bounds.y + 4, u, v, 200, 112);
  103. if (original == (v == 0)) {
  104. fillGradient(bounds.x, bounds.y, bounds.x + bounds.width, bounds.y + 2, 0xFFFFFFFF, 0xFFFFFFFF);
  105. fillGradient(bounds.x, bounds.y + bounds.height - 2, bounds.x + bounds.width, bounds.y + bounds.height, 0xFFFFFFFF, 0xFFFFFFFF);
  106. fillGradient(bounds.x, bounds.y, bounds.x + 2, bounds.y + bounds.height, 0xFFFFFFFF, 0xFFFFFFFF);
  107. fillGradient(bounds.x + bounds.width - 2, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height, 0xFFFFFFFF, 0xFFFFFFFF);
  108. }
  109. }
  110. @Override
  111. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  112. if (containsMouse(double_1, double_2)) {
  113. minecraft.getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  114. original = (v == 0);
  115. return true;
  116. }
  117. return false;
  118. }
  119. @Override
  120. public List<? extends Element> children() {
  121. return Collections.emptyList();
  122. }
  123. }
  124. }