PreRecipeViewingScreen.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * This file is licensed under the MIT License, part of Roughly Enough Items.
  3. * Copyright (c) 2018, 2019, 2020 shedaniel
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package me.shedaniel.rei.gui;
  24. import com.google.common.collect.Lists;
  25. import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
  26. import me.shedaniel.clothconfig2.gui.widget.DynamicNewSmoothScrollingEntryListWidget;
  27. import me.shedaniel.clothconfig2.impl.EasingMethod;
  28. import me.shedaniel.math.api.Point;
  29. import me.shedaniel.math.api.Rectangle;
  30. import me.shedaniel.rei.gui.config.RecipeScreenType;
  31. import me.shedaniel.rei.gui.widget.ButtonWidget;
  32. import me.shedaniel.rei.gui.widget.LabelWidget;
  33. import me.shedaniel.rei.gui.widget.Widget;
  34. import me.shedaniel.rei.gui.widget.WidgetWithBounds;
  35. import me.shedaniel.rei.impl.ScreenHelper;
  36. import net.minecraft.client.MinecraftClient;
  37. import net.minecraft.client.gui.Element;
  38. import net.minecraft.client.gui.screen.Screen;
  39. import net.minecraft.client.gui.screen.ingame.ContainerScreen;
  40. import net.minecraft.client.resource.language.I18n;
  41. import net.minecraft.client.util.NarratorManager;
  42. import net.minecraft.text.TranslatableText;
  43. import net.minecraft.util.Formatting;
  44. import net.minecraft.util.Identifier;
  45. import net.minecraft.util.math.MathHelper;
  46. import org.jetbrains.annotations.ApiStatus;
  47. import java.util.Collections;
  48. import java.util.List;
  49. @ApiStatus.Internal
  50. public class PreRecipeViewingScreen extends Screen {
  51. private static final Identifier IDENTIFIER = new Identifier("roughlyenoughitems", "textures/gui/screenshot.png");
  52. private final List<Widget> widgets;
  53. protected long start;
  54. protected long duration;
  55. private boolean isSet;
  56. private boolean original;
  57. private double frame = 0;
  58. private double target = 0;
  59. private BooleanConsumer callback;
  60. private Screen parent;
  61. private boolean showTips;
  62. public PreRecipeViewingScreen(Screen parent, RecipeScreenType type, boolean showTips, BooleanConsumer callback) {
  63. super(new TranslatableText("text.rei.recipe_screen_type.selection"));
  64. this.widgets = Lists.newArrayList();
  65. if (type == RecipeScreenType.UNSET) {
  66. this.isSet = false;
  67. this.original = true;
  68. } else {
  69. this.isSet = true;
  70. this.original = type == RecipeScreenType.ORIGINAL;
  71. moveFrameTo(original ? 0 : 1, false, 0);
  72. }
  73. this.callback = callback;
  74. this.parent = parent;
  75. this.showTips = showTips;
  76. }
  77. public final double clamp(double v) {
  78. return clamp(v, 30);
  79. }
  80. public final double clamp(double v, double clampExtension) {
  81. return MathHelper.clamp(v, -clampExtension, 1 + clampExtension);
  82. }
  83. private void moveFrameTo(double value, boolean animated, long duration) {
  84. target = clamp(value);
  85. if (animated) {
  86. start = System.currentTimeMillis();
  87. this.duration = duration;
  88. } else
  89. frame = target;
  90. }
  91. @Override
  92. protected void init() {
  93. this.children.clear();
  94. this.widgets.clear();
  95. this.widgets.add(new ButtonWidget(new Rectangle(width / 2 - 100, height - 40, 200, 20), NarratorManager.EMPTY) {
  96. @Override
  97. public void render(int mouseX, int mouseY, float delta) {
  98. enabled = isSet;
  99. setText(enabled ? I18n.translate("text.rei.select") : I18n.translate("config.roughlyenoughitems.recipeScreenType.unset"));
  100. super.render(mouseX, mouseY, delta);
  101. }
  102. @Override
  103. public void onPressed() {
  104. callback.accept(original);
  105. }
  106. });
  107. this.widgets.add(new ScreenTypeSelection(width / 2 - 200 - 5, height / 2 - 112 / 2 - 10, 0));
  108. this.widgets.add(LabelWidget.create(new Point(width / 2 - 200 - 5 + 104, height / 2 - 112 / 2 + 115), I18n.translate("config.roughlyenoughitems.recipeScreenType.original")).noShadow().color(-1124073473));
  109. this.widgets.add(new ScreenTypeSelection(width / 2 + 5, height / 2 - 112 / 2 - 10, 112));
  110. this.widgets.add(LabelWidget.create(new Point(width / 2 + 5 + 104, height / 2 - 112 / 2 + 115), I18n.translate("config.roughlyenoughitems.recipeScreenType.villager")).noShadow().color(-1124073473));
  111. this.children.addAll(widgets);
  112. }
  113. @Override
  114. public void render(int int_1, int int_2, float float_1) {
  115. if (this.minecraft.world != null) {
  116. this.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
  117. } else {
  118. this.fillGradient(0, 0, this.width, this.height, -16777216, -16777216);
  119. }
  120. this.drawCenteredString(this.font, this.title.asFormattedString(), this.width / 2, 20, 16777215);
  121. if (showTips) {
  122. int i = 30;
  123. for (String s : this.font.wrapStringToWidthAsList(I18n.translate("text.rei.recipe_screen_type.selection.sub"), width - 30)) {
  124. this.drawCenteredString(this.font, Formatting.GRAY.toString() + s, width / 2, i, -1);
  125. i += 10;
  126. }
  127. }
  128. super.render(int_1, int_2, float_1);
  129. for (Widget widget : widgets) {
  130. widget.render(int_1, int_2, float_1);
  131. }
  132. if (isSet) {
  133. updateFramePosition(float_1);
  134. int x = (int) (width / 2 - 205 + (210 * frame));
  135. int y = height / 2 - 112 / 2 - 10;
  136. fillGradient(x - 2, y - 4, x - 6 + 208, y - 4 + 2, -1778384897, -1778384897);
  137. fillGradient(x - 2, y - 4 + 120 - 2, x - 6 + 208, y - 4 + 120, -1778384897, -1778384897);
  138. fillGradient(x - 4, y - 4, x - 4 + 2, y - 4 + 120, -1778384897, -1778384897);
  139. fillGradient(x - 4 + 208 - 2, y - 4, x - 4 + 208, y - 4 + 120, -1778384897, -1778384897);
  140. }
  141. }
  142. private void updateFramePosition(float delta) {
  143. target = clamp(target);
  144. if (!DynamicNewSmoothScrollingEntryListWidget.Precision.almostEquals(frame, target, DynamicNewSmoothScrollingEntryListWidget.Precision.FLOAT_EPSILON))
  145. frame = ease(frame, target, Math.min((System.currentTimeMillis() - start) / ((double) duration), 1));
  146. else
  147. frame = target;
  148. }
  149. private double ease(double start, double end, double amount) {
  150. return start + (end - start) * EasingMethod.EasingMethodImpl.LINEAR.apply(amount);
  151. }
  152. @Override
  153. public boolean keyPressed(int int_1, int int_2, int int_3) {
  154. if (int_1 == 256 || this.minecraft.options.keyInventory.matchesKey(int_1, int_2)) {
  155. MinecraftClient.getInstance().openScreen(parent);
  156. if (parent instanceof ContainerScreen)
  157. ScreenHelper.getLastOverlay().init();
  158. return true;
  159. }
  160. return super.keyPressed(int_1, int_2, int_3);
  161. }
  162. public class ScreenTypeSelection extends WidgetWithBounds {
  163. private Rectangle bounds;
  164. private int u, v;
  165. public ScreenTypeSelection(int x, int y, int v) {
  166. this.bounds = new Rectangle(x - 4, y - 4, 208, 120);
  167. this.u = 0;
  168. this.v = v;
  169. }
  170. @Override
  171. public Rectangle getBounds() {
  172. return bounds;
  173. }
  174. @Override
  175. public void render(int i, int i1, float delta) {
  176. MinecraftClient.getInstance().getTextureManager().bindTexture(IDENTIFIER);
  177. blit(bounds.x + 4, bounds.y + 4, u, v, 200, 112);
  178. }
  179. @Override
  180. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  181. if (containsMouse(double_1, double_2)) {
  182. original = (v == 0);
  183. if (!isSet) {
  184. moveFrameTo(!original ? 0 : 1, false, 0);
  185. }
  186. isSet = true;
  187. moveFrameTo(original ? 0 : 1, true, 1000);
  188. return true;
  189. }
  190. return false;
  191. }
  192. @Override
  193. public List<? extends Element> children() {
  194. return Collections.emptyList();
  195. }
  196. }
  197. }