PreRecipeViewingScreen.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.Point;
  29. import me.shedaniel.math.Rectangle;
  30. import me.shedaniel.rei.api.widgets.Widgets;
  31. import me.shedaniel.rei.gui.config.RecipeScreenType;
  32. import me.shedaniel.rei.gui.widget.Widget;
  33. import me.shedaniel.rei.gui.widget.WidgetWithBounds;
  34. import me.shedaniel.rei.impl.ScreenHelper;
  35. import net.minecraft.client.MinecraftClient;
  36. import net.minecraft.client.gui.Element;
  37. import net.minecraft.client.gui.screen.Screen;
  38. import net.minecraft.client.gui.screen.ingame.HandledScreen;
  39. import net.minecraft.client.resource.language.I18n;
  40. import net.minecraft.client.util.NarratorManager;
  41. import net.minecraft.text.TranslatableText;
  42. import net.minecraft.util.Formatting;
  43. import net.minecraft.util.Identifier;
  44. import net.minecraft.util.math.MathHelper;
  45. import org.jetbrains.annotations.ApiStatus;
  46. import org.jetbrains.annotations.NotNull;
  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(Widgets.createButton(new Rectangle(width / 2 - 100, height - 40, 200, 20), NarratorManager.EMPTY)
  96. .onRender(button -> {
  97. button.setEnabled(isSet);
  98. button.setText(isSet ? I18n.translate("text.rei.select") : I18n.translate("config.roughlyenoughitems.recipeScreenType.unset"));
  99. })
  100. .onClick(button -> callback.accept(original)));
  101. this.widgets.add(new ScreenTypeSelection(width / 2 - 200 - 5, height / 2 - 112 / 2 - 10, 0));
  102. this.widgets.add(Widgets.createLabel(new Point(width / 2 - 200 - 5 + 104, height / 2 - 112 / 2 + 115), I18n.translate("config.roughlyenoughitems.recipeScreenType.original")).noShadow().color(-1124073473));
  103. this.widgets.add(new ScreenTypeSelection(width / 2 + 5, height / 2 - 112 / 2 - 10, 112));
  104. this.widgets.add(Widgets.createLabel(new Point(width / 2 + 5 + 104, height / 2 - 112 / 2 + 115), I18n.translate("config.roughlyenoughitems.recipeScreenType.villager")).noShadow().color(-1124073473));
  105. this.children.addAll(widgets);
  106. }
  107. @Override
  108. public void render(int int_1, int int_2, float float_1) {
  109. if (this.client.world != null) {
  110. this.fillGradient(0, 0, this.width, this.height, -1072689136, -804253680);
  111. } else {
  112. this.fillGradient(0, 0, this.width, this.height, -16777216, -16777216);
  113. }
  114. this.drawCenteredString(this.textRenderer, this.title.asFormattedString(), this.width / 2, 20, 16777215);
  115. if (showTips) {
  116. int i = 30;
  117. for (String s : this.textRenderer.wrapStringToWidthAsList(I18n.translate("text.rei.recipe_screen_type.selection.sub"), width - 30)) {
  118. this.drawCenteredString(this.textRenderer, Formatting.GRAY.toString() + s, width / 2, i, -1);
  119. i += 10;
  120. }
  121. }
  122. super.render(int_1, int_2, float_1);
  123. for (Widget widget : widgets) {
  124. widget.render(int_1, int_2, float_1);
  125. }
  126. if (isSet) {
  127. updateFramePosition(float_1);
  128. int x = (int) (width / 2 - 205 + (210 * frame));
  129. int y = height / 2 - 112 / 2 - 10;
  130. fillGradient(x - 2, y - 4, x - 6 + 208, y - 4 + 2, -1778384897, -1778384897);
  131. fillGradient(x - 2, y - 4 + 120 - 2, x - 6 + 208, y - 4 + 120, -1778384897, -1778384897);
  132. fillGradient(x - 4, y - 4, x - 4 + 2, y - 4 + 120, -1778384897, -1778384897);
  133. fillGradient(x - 4 + 208 - 2, y - 4, x - 4 + 208, y - 4 + 120, -1778384897, -1778384897);
  134. }
  135. }
  136. private void updateFramePosition(float delta) {
  137. target = clamp(target);
  138. if (!DynamicNewSmoothScrollingEntryListWidget.Precision.almostEquals(frame, target, DynamicNewSmoothScrollingEntryListWidget.Precision.FLOAT_EPSILON))
  139. frame = ease(frame, target, Math.min((System.currentTimeMillis() - start) / (double) duration * delta * 3.0D, 1));
  140. else
  141. frame = target;
  142. }
  143. private double ease(double start, double end, double amount) {
  144. return start + (end - start) * EasingMethod.EasingMethodImpl.LINEAR.apply(amount);
  145. }
  146. @Override
  147. public boolean keyPressed(int int_1, int int_2, int int_3) {
  148. if (int_1 == 256 || this.client.options.keyInventory.matchesKey(int_1, int_2)) {
  149. MinecraftClient.getInstance().openScreen(parent);
  150. if (parent instanceof HandledScreen)
  151. ScreenHelper.getLastOverlay().init();
  152. return true;
  153. }
  154. return super.keyPressed(int_1, int_2, int_3);
  155. }
  156. public class ScreenTypeSelection extends WidgetWithBounds {
  157. private Rectangle bounds;
  158. private int u, v;
  159. public ScreenTypeSelection(int x, int y, int v) {
  160. this.bounds = new Rectangle(x - 4, y - 4, 208, 120);
  161. this.u = 0;
  162. this.v = v;
  163. }
  164. @NotNull
  165. @Override
  166. public Rectangle getBounds() {
  167. return bounds;
  168. }
  169. @Override
  170. public void render(int i, int i1, float delta) {
  171. MinecraftClient.getInstance().getTextureManager().bindTexture(IDENTIFIER);
  172. blit(bounds.x + 4, bounds.y + 4, u, v, 200, 112);
  173. }
  174. @Override
  175. public boolean mouseClicked(double double_1, double double_2, int int_1) {
  176. if (containsMouse(double_1, double_2)) {
  177. original = (v == 0);
  178. if (!isSet) {
  179. moveFrameTo(original ? 0 : 1, false, 0);
  180. }
  181. isSet = true;
  182. moveFrameTo(original ? 0 : 1, true, 500);
  183. return true;
  184. }
  185. return false;
  186. }
  187. @Override
  188. public List<? extends Element> children() {
  189. return Collections.emptyList();
  190. }
  191. }
  192. }