InternalWidgets.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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.impl;
  24. import com.google.common.collect.Lists;
  25. import it.unimi.dsi.fastutil.ints.IntList;
  26. import me.shedaniel.math.api.Rectangle;
  27. import me.shedaniel.math.impl.PointHelper;
  28. import me.shedaniel.rei.api.*;
  29. import me.shedaniel.rei.api.widgets.Button;
  30. import me.shedaniel.rei.api.widgets.Widgets;
  31. import me.shedaniel.rei.gui.toast.CopyRecipeIdentifierToast;
  32. import me.shedaniel.rei.gui.widget.LateRenderable;
  33. import me.shedaniel.rei.gui.widget.Widget;
  34. import me.shedaniel.rei.gui.widget.WidgetWithBounds;
  35. import me.shedaniel.rei.utils.CollectionUtils;
  36. import net.minecraft.client.MinecraftClient;
  37. import net.minecraft.client.gui.Element;
  38. import net.minecraft.client.gui.screen.ingame.HandledScreen;
  39. import net.minecraft.client.resource.language.I18n;
  40. import net.minecraft.util.Formatting;
  41. import org.jetbrains.annotations.ApiStatus;
  42. import org.jetbrains.annotations.NotNull;
  43. import java.util.Collections;
  44. import java.util.List;
  45. import java.util.Objects;
  46. import java.util.function.Supplier;
  47. @ApiStatus.Internal
  48. public final class InternalWidgets {
  49. private InternalWidgets() {}
  50. public static Widget createAutoCraftingButtonWidget(Rectangle displayBounds, me.shedaniel.math.Rectangle rectangle, String text, Supplier<RecipeDisplay> displaySupplier, List<Widget> setupDisplay, RecipeCategory<?> category) {
  51. HandledScreen<?> handledScreen = ScreenHelper.getLastHandledScreen();
  52. boolean[] visible = {false};
  53. List<String>[] errorTooltip = new List[]{null};
  54. Button autoCraftingButton = Widgets.createButton(rectangle, text)
  55. .focusable(false)
  56. .onClick(button -> {
  57. AutoTransferHandler.Context context = AutoTransferHandler.Context.create(true, handledScreen, displaySupplier.get());
  58. for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler())
  59. try {
  60. AutoTransferHandler.Result result = autoTransferHandler.handle(context);
  61. if (result.isSuccessful())
  62. return;
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. MinecraftClient.getInstance().openScreen(handledScreen);
  67. ScreenHelper.getLastOverlay().init();
  68. })
  69. .onRender(button -> {
  70. button.setEnabled(false);
  71. List<String> error = null;
  72. int color = 0;
  73. visible[0] = false;
  74. IntList redSlots = null;
  75. AutoTransferHandler.Context context = AutoTransferHandler.Context.create(false, handledScreen, displaySupplier.get());
  76. for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler()) {
  77. try {
  78. AutoTransferHandler.Result result = autoTransferHandler.handle(context);
  79. if (result.isApplicable())
  80. visible[0] = true;
  81. if (result.isSuccessful()) {
  82. button.setEnabled(true);
  83. error = null;
  84. color = 0;
  85. redSlots = null;
  86. break;
  87. } else if (result.isApplicable()) {
  88. if (error == null) {
  89. error = Lists.newArrayList();
  90. }
  91. error.add(result.getErrorKey());
  92. color = result.getColor();
  93. if (result.getIntegers() != null && !result.getIntegers().isEmpty())
  94. redSlots = result.getIntegers();
  95. }
  96. } catch (Exception e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. if (!visible[0]) {
  101. button.setEnabled(false);
  102. if (error == null) {
  103. error = Lists.newArrayList();
  104. } else {
  105. error.clear();
  106. }
  107. error.add("error.rei.no.handlers.applicable");
  108. }
  109. if ((button.containsMouse(PointHelper.ofMouse()) || button.isFocused()) && category instanceof TransferRecipeCategory && redSlots != null) {
  110. ((TransferRecipeCategory<RecipeDisplay>) category).renderRedSlots(setupDisplay, displayBounds, displaySupplier.get(), redSlots);
  111. }
  112. errorTooltip[0] = error == null || error.isEmpty() ? null : Lists.newArrayList();
  113. if (errorTooltip[0] != null) {
  114. for (String s : error) {
  115. if (errorTooltip[0].stream().noneMatch(ss -> ss.equalsIgnoreCase(s)))
  116. errorTooltip[0].add(s);
  117. }
  118. }
  119. button.setTint(color);
  120. })
  121. .textColor((button, mouse) -> {
  122. if (!visible[0]) {
  123. return 10526880;
  124. } else if (button.isEnabled() && (button.containsMouse(mouse) || button.isFocused())) {
  125. return 16777120;
  126. }
  127. return 14737632;
  128. })
  129. .textureId((button, mouse) -> !visible[0] ? 0 : (button.containsMouse(mouse) || button.isFocused()) && button.isEnabled() ? 4 : 1)
  130. .tooltipSupplier(button -> {
  131. String str = "";
  132. if (errorTooltip[0] == null) {
  133. if (((ClientHelperImpl) ClientHelper.getInstance()).isYog.get())
  134. str += I18n.translate("text.auto_craft.move_items.yog");
  135. else
  136. str += I18n.translate("text.auto_craft.move_items");
  137. } else {
  138. if (errorTooltip[0].size() > 1)
  139. str += Formatting.RED.toString() + I18n.translate("error.rei.multi.errors") + "\n";
  140. str += CollectionUtils.mapAndJoinToString(errorTooltip[0], s -> Formatting.RED.toString() + (errorTooltip[0].size() > 1 ? "- " : "") + I18n.translate(s), "\n");
  141. }
  142. if (MinecraftClient.getInstance().options.advancedItemTooltips) {
  143. str += displaySupplier.get().getRecipeLocation().isPresent() ? I18n.translate("text.rei.recipe_id", Formatting.GRAY.toString(), displaySupplier.get().getRecipeLocation().get().toString()) : "";
  144. }
  145. return str;
  146. });
  147. return new WidgetWithBounds() {
  148. @Override
  149. public @NotNull Rectangle getBounds() {
  150. return autoCraftingButton.getBounds();
  151. }
  152. @Override
  153. public List<? extends Element> children() {
  154. return Collections.singletonList(autoCraftingButton);
  155. }
  156. @Override
  157. public void render(int mouseX, int mouseY, float delta) {
  158. autoCraftingButton.render(mouseX, mouseY, delta);
  159. }
  160. @Override
  161. public boolean keyPressed(int int_1, int int_2, int int_3) {
  162. if (displaySupplier.get().getRecipeLocation().isPresent() && ConfigObject.getInstance().getCopyRecipeIdentifierKeybind().matchesKey(int_1, int_2) && containsMouse(PointHelper.ofMouse())) {
  163. minecraft.keyboard.setClipboard(displaySupplier.get().getRecipeLocation().get().toString());
  164. if (ConfigObject.getInstance().isToastDisplayedOnCopyIdentifier()) {
  165. CopyRecipeIdentifierToast.addToast(I18n.translate("msg.rei.copied_recipe_id"), I18n.translate("msg.rei.recipe_id_details", displaySupplier.get().getRecipeLocation().get().toString()));
  166. }
  167. return true;
  168. }
  169. return super.keyPressed(int_1, int_2, int_3);
  170. }
  171. };
  172. }
  173. public static LateRenderable wrapLateRenderable(WidgetWithBounds widget) {
  174. return new LateRenderableWidgetWithBounds(widget);
  175. }
  176. public static LateRenderable wrapLateRenderable(Widget widget) {
  177. return new LateRenderableWidget(widget);
  178. }
  179. public static Widget mergeWidgets(Widget widget1, Widget widget2) {
  180. return new MergedWidget(widget2, widget1);
  181. }
  182. private static class MergedWidget extends Widget {
  183. private final List<Widget> widgets;
  184. public MergedWidget(Widget widget1, Widget widget2) {
  185. this.widgets = Lists.newArrayList(Objects.requireNonNull(widget1), Objects.requireNonNull(widget2));
  186. }
  187. @Override
  188. public void render(int mouseX, int mouseY, float delta) {
  189. for (Widget widget : widgets) {
  190. widget.setZ(getZ());
  191. widget.render(mouseX, mouseY, delta);
  192. }
  193. }
  194. @Override
  195. public List<? extends Element> children() {
  196. return widgets;
  197. }
  198. }
  199. private static class LateRenderableWidget extends Widget implements LateRenderable {
  200. private final Widget widget;
  201. private LateRenderableWidget(Widget widget) {
  202. this.widget = widget;
  203. }
  204. @Override
  205. public void lateRender(int mouseX, int mouseY, float delta) {
  206. this.widget.setZ(getZ());
  207. this.widget.render(mouseX, mouseY, delta);
  208. }
  209. @Override
  210. public void render(int mouseX, int mouseY, float delta) {}
  211. @Override
  212. public List<? extends Element> children() {
  213. return Collections.singletonList(this.widget);
  214. }
  215. }
  216. private static class LateRenderableWidgetWithBounds extends WidgetWithBounds implements LateRenderable {
  217. private final WidgetWithBounds widget;
  218. private LateRenderableWidgetWithBounds(WidgetWithBounds widget) {
  219. this.widget = widget;
  220. }
  221. @Override
  222. public void lateRender(int mouseX, int mouseY, float delta) {
  223. this.widget.setZ(getZ());
  224. this.widget.render(mouseX, mouseY, delta);
  225. }
  226. @Override
  227. public @NotNull Rectangle getBounds() {
  228. return this.widget.getBounds();
  229. }
  230. @Override
  231. public void render(int mouseX, int mouseY, float delta) {}
  232. @Override
  233. public List<? extends Element> children() {
  234. return Collections.singletonList(this.widget);
  235. }
  236. }
  237. }