InternalWidgets.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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.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.fabricmc.api.EnvType;
  37. import net.fabricmc.api.Environment;
  38. import net.minecraft.client.MinecraftClient;
  39. import net.minecraft.client.gui.Element;
  40. import net.minecraft.client.gui.screen.ingame.ContainerScreen;
  41. import net.minecraft.client.resource.language.I18n;
  42. import net.minecraft.client.util.math.MatrixStack;
  43. import net.minecraft.text.Text;
  44. import net.minecraft.util.Formatting;
  45. import org.jetbrains.annotations.ApiStatus;
  46. import org.jetbrains.annotations.NotNull;
  47. import java.util.Collections;
  48. import java.util.List;
  49. import java.util.Objects;
  50. import java.util.function.Supplier;
  51. @ApiStatus.Internal
  52. @Environment(EnvType.CLIENT)
  53. public final class InternalWidgets {
  54. private InternalWidgets() {}
  55. public static Widget createAutoCraftingButtonWidget(Rectangle displayBounds, Rectangle rectangle, Text text, Supplier<RecipeDisplay> displaySupplier, List<Widget> setupDisplay, RecipeCategory<?> category) {
  56. ContainerScreen<?> containerScreen = REIHelper.getInstance().getPreviousContainerScreen();
  57. boolean[] visible = {false};
  58. List<String>[] errorTooltip = new List[]{null};
  59. Button autoCraftingButton = Widgets.createButton(rectangle, text)
  60. .focusable(false)
  61. .onClick(button -> {
  62. AutoTransferHandler.Context context = AutoTransferHandler.Context.create(true, containerScreen, displaySupplier.get());
  63. for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler())
  64. try {
  65. AutoTransferHandler.Result result = autoTransferHandler.handle(context);
  66. if (result.isSuccessful()) {
  67. if (result.isReturningToScreen()) {
  68. break; // Same as failing, but doesn't ask other handlers
  69. }
  70. return;
  71. }
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. MinecraftClient.getInstance().openScreen(containerScreen);
  76. ScreenHelper.getLastOverlay().init();
  77. })
  78. .onRender((matrices, button) -> {
  79. button.setEnabled(false);
  80. List<String> error = null;
  81. int color = 0;
  82. visible[0] = false;
  83. IntList redSlots = null;
  84. AutoTransferHandler.Context context = AutoTransferHandler.Context.create(false, containerScreen, displaySupplier.get());
  85. for (AutoTransferHandler autoTransferHandler : RecipeHelper.getInstance().getSortedAutoCraftingHandler()) {
  86. try {
  87. AutoTransferHandler.Result result = autoTransferHandler.handle(context);
  88. if (result.isApplicable())
  89. visible[0] = true;
  90. if (result.isSuccessful()) {
  91. button.setEnabled(true);
  92. error = null;
  93. color = 0;
  94. redSlots = null;
  95. break;
  96. } else if (result.isApplicable()) {
  97. if (error == null) {
  98. error = Lists.newArrayList();
  99. }
  100. error.add(result.getErrorKey());
  101. color = result.getColor();
  102. if (result.getIntegers() != null && !result.getIntegers().isEmpty())
  103. redSlots = result.getIntegers();
  104. }
  105. } catch (Exception e) {
  106. e.printStackTrace();
  107. }
  108. }
  109. if (!visible[0]) {
  110. button.setEnabled(false);
  111. if (error == null) {
  112. error = Lists.newArrayList();
  113. } else {
  114. error.clear();
  115. }
  116. error.add("error.rei.no.handlers.applicable");
  117. }
  118. if ((button.containsMouse(PointHelper.ofMouse()) || button.isFocused()) && category instanceof TransferRecipeCategory && redSlots != null) {
  119. ((TransferRecipeCategory<RecipeDisplay>) category).renderRedSlots(matrices, setupDisplay, displayBounds, displaySupplier.get(), redSlots);
  120. }
  121. errorTooltip[0] = error == null || error.isEmpty() ? null : Lists.newArrayList();
  122. if (errorTooltip[0] != null) {
  123. for (String s : error) {
  124. if (errorTooltip[0].stream().noneMatch(ss -> ss.equalsIgnoreCase(s)))
  125. errorTooltip[0].add(s);
  126. }
  127. }
  128. button.setTint(color);
  129. })
  130. .textColor((button, mouse) -> {
  131. if (!visible[0]) {
  132. return 10526880;
  133. } else if (button.isEnabled() && (button.containsMouse(mouse) || button.isFocused())) {
  134. return 16777120;
  135. }
  136. return 14737632;
  137. })
  138. .textureId((button, mouse) -> !visible[0] ? 0 : (button.containsMouse(mouse) || button.isFocused()) && button.isEnabled() ? 4 : 1)
  139. .tooltipSupplier(button -> {
  140. String str = "";
  141. if (errorTooltip[0] == null) {
  142. if (((ClientHelperImpl) ClientHelper.getInstance()).isYog.get())
  143. str += I18n.translate("text.auto_craft.move_items.yog");
  144. else
  145. str += I18n.translate("text.auto_craft.move_items");
  146. } else {
  147. if (errorTooltip[0].size() > 1)
  148. str += Formatting.RED.toString() + I18n.translate("error.rei.multi.errors") + "\n";
  149. str += CollectionUtils.mapAndJoinToString(errorTooltip[0], s -> Formatting.RED.toString() + (errorTooltip[0].size() > 1 ? "- " : "") + I18n.translate(s), "\n");
  150. }
  151. if (MinecraftClient.getInstance().options.advancedItemTooltips) {
  152. str += displaySupplier.get().getRecipeLocation().isPresent() ? I18n.translate("text.rei.recipe_id", Formatting.GRAY.toString(), displaySupplier.get().getRecipeLocation().get().toString()) : "";
  153. }
  154. return str;
  155. });
  156. return new WidgetWithBounds() {
  157. @Override
  158. public @NotNull Rectangle getBounds() {
  159. return autoCraftingButton.getBounds();
  160. }
  161. @Override
  162. public List<? extends Element> children() {
  163. return Collections.singletonList(autoCraftingButton);
  164. }
  165. @Override
  166. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  167. autoCraftingButton.render(matrices, mouseX, mouseY, delta);
  168. }
  169. @Override
  170. public boolean keyPressed(int int_1, int int_2, int int_3) {
  171. if (displaySupplier.get().getRecipeLocation().isPresent() && ConfigObject.getInstance().getCopyRecipeIdentifierKeybind().matchesKey(int_1, int_2) && containsMouse(PointHelper.ofMouse())) {
  172. minecraft.keyboard.setClipboard(displaySupplier.get().getRecipeLocation().get().toString());
  173. if (ConfigObject.getInstance().isToastDisplayedOnCopyIdentifier()) {
  174. CopyRecipeIdentifierToast.addToast(I18n.translate("msg.rei.copied_recipe_id"), I18n.translate("msg.rei.recipe_id_details", displaySupplier.get().getRecipeLocation().get().toString()));
  175. }
  176. return true;
  177. }
  178. return super.keyPressed(int_1, int_2, int_3);
  179. }
  180. };
  181. }
  182. public static WidgetWithBounds wrapLateRenderable(WidgetWithBounds widget) {
  183. return new LateRenderableWidgetWithBounds(widget);
  184. }
  185. public static WidgetWithBounds wrapTranslate(WidgetWithBounds widget, float x, float y, float z) {
  186. return new WidgetWithBoundsWithTranslate(widget, x, y, z);
  187. }
  188. public static Widget wrapLateRenderable(Widget widget) {
  189. return new LateRenderableWidget(widget);
  190. }
  191. public static Widget mergeWidgets(Widget widget1, Widget widget2) {
  192. return new MergedWidget(widget2, widget1);
  193. }
  194. private static class MergedWidget extends Widget {
  195. private final List<Widget> widgets;
  196. public MergedWidget(Widget widget1, Widget widget2) {
  197. this.widgets = Lists.newArrayList(Objects.requireNonNull(widget1), Objects.requireNonNull(widget2));
  198. }
  199. @Override
  200. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  201. for (Widget widget : widgets) {
  202. widget.setZ(getZ());
  203. widget.render(matrices, mouseX, mouseY, delta);
  204. }
  205. }
  206. @Override
  207. public List<? extends Element> children() {
  208. return widgets;
  209. }
  210. @Override
  211. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  212. for (Widget widget : this.widgets) {
  213. if (widget.mouseScrolled(mouseX, mouseY, amount))
  214. return true;
  215. }
  216. return false;
  217. }
  218. }
  219. private static class LateRenderableWidget extends Widget implements LateRenderable {
  220. private final Widget widget;
  221. private LateRenderableWidget(Widget widget) {
  222. this.widget = widget;
  223. }
  224. @Override
  225. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  226. this.widget.setZ(getZ());
  227. this.widget.render(matrices, mouseX, mouseY, delta);
  228. }
  229. @Override
  230. public List<? extends Element> children() {
  231. return Collections.singletonList(this.widget);
  232. }
  233. @Override
  234. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  235. return this.widget.mouseScrolled(mouseX, mouseY, amount);
  236. }
  237. }
  238. private static class LateRenderableWidgetWithBounds extends WidgetWithBounds implements LateRenderable {
  239. private final WidgetWithBounds widget;
  240. private LateRenderableWidgetWithBounds(WidgetWithBounds widget) {
  241. this.widget = widget;
  242. }
  243. @Override
  244. public @NotNull Rectangle getBounds() {
  245. return this.widget.getBounds();
  246. }
  247. @Override
  248. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  249. this.widget.setZ(getZ());
  250. this.widget.render(matrices, mouseX, mouseY, delta);
  251. }
  252. @Override
  253. public boolean containsMouse(double mouseX, double mouseY) {
  254. return this.widget.containsMouse(mouseX, mouseY);
  255. }
  256. @Override
  257. public List<? extends Element> children() {
  258. return Collections.singletonList(this.widget);
  259. }
  260. @Override
  261. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  262. return this.widget.mouseScrolled(mouseX, mouseY, amount);
  263. }
  264. }
  265. private static class WidgetWithBoundsWithTranslate extends WidgetWithBounds implements LateRenderable {
  266. private final WidgetWithBounds widget;
  267. private final float x, y, z;
  268. public WidgetWithBoundsWithTranslate(WidgetWithBounds widget, float x, float y, float z) {
  269. this.widget = widget;
  270. this.x = x;
  271. this.y = y;
  272. this.z = z;
  273. }
  274. @Override
  275. public @NotNull Rectangle getBounds() {
  276. return this.widget.getBounds();
  277. }
  278. @Override
  279. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  280. matrices.push();
  281. matrices.translate(x, y, z);
  282. this.widget.setZ(getZ());
  283. this.widget.render(matrices, mouseX, mouseY, delta);
  284. matrices.pop();
  285. }
  286. @Override
  287. public boolean containsMouse(double mouseX, double mouseY) {
  288. return this.widget.containsMouse(mouseX, mouseY);
  289. }
  290. @Override
  291. public List<? extends Element> children() {
  292. return Collections.singletonList(this.widget);
  293. }
  294. @Override
  295. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  296. return this.widget.mouseScrolled(mouseX, mouseY, amount);
  297. }
  298. }
  299. }