PreRecipeViewingScreen.java 5.4 KB

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