PreRecipeViewingScreen.java 5.5 KB

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