InternalWidgets.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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.isBlocking()) {
  67. if (result.isReturningToScreen()) {
  68. break;
  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. } else if (result.isApplicable()) {
  96. if (error == null) {
  97. error = Lists.newArrayList();
  98. }
  99. error.add(result.getErrorKey());
  100. color = result.getColor();
  101. if (result.getIntegers() != null && !result.getIntegers().isEmpty())
  102. redSlots = result.getIntegers();
  103. }
  104. if (result.isBlocking()) break;
  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 keyCode, int scanCode, int modifiers) {
  171. if (displaySupplier.get().getRecipeLocation().isPresent() && ConfigObject.getInstance().getCopyRecipeIdentifierKeybind().matchesKey(keyCode, scanCode) && 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(keyCode, scanCode, modifiers);
  179. }
  180. @Override
  181. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  182. if (displaySupplier.get().getRecipeLocation().isPresent() && ConfigObject.getInstance().getCopyRecipeIdentifierKeybind().matchesMouse(button) && containsMouse(PointHelper.ofMouse())) {
  183. minecraft.keyboard.setClipboard(displaySupplier.get().getRecipeLocation().get().toString());
  184. if (ConfigObject.getInstance().isToastDisplayedOnCopyIdentifier()) {
  185. CopyRecipeIdentifierToast.addToast(I18n.translate("msg.rei.copied_recipe_id"), I18n.translate("msg.rei.recipe_id_details", displaySupplier.get().getRecipeLocation().get().toString()));
  186. }
  187. return true;
  188. }
  189. return super.mouseClicked(mouseX, mouseY, button);
  190. }
  191. };
  192. }
  193. public static WidgetWithBounds wrapLateRenderable(WidgetWithBounds widget) {
  194. return new LateRenderableWidgetWithBounds(widget);
  195. }
  196. public static WidgetWithBounds wrapTranslate(WidgetWithBounds widget, float x, float y, float z) {
  197. return new WidgetWithBoundsWithTranslate(widget, x, y, z);
  198. }
  199. public static Widget wrapLateRenderable(Widget widget) {
  200. return new LateRenderableWidget(widget);
  201. }
  202. public static Widget mergeWidgets(Widget widget1, Widget widget2) {
  203. return new MergedWidget(widget2, widget1);
  204. }
  205. private static class MergedWidget extends Widget {
  206. private final List<Widget> widgets;
  207. public MergedWidget(Widget widget1, Widget widget2) {
  208. this.widgets = Lists.newArrayList(Objects.requireNonNull(widget1), Objects.requireNonNull(widget2));
  209. }
  210. @Override
  211. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  212. for (Widget widget : widgets) {
  213. widget.setZ(getZ());
  214. widget.render(matrices, mouseX, mouseY, delta);
  215. }
  216. }
  217. @Override
  218. public List<? extends Element> children() {
  219. return widgets;
  220. }
  221. @Override
  222. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  223. for (Widget widget : this.widgets) {
  224. if (widget.mouseScrolled(mouseX, mouseY, amount))
  225. return true;
  226. }
  227. return false;
  228. }
  229. }
  230. private static class LateRenderableWidget extends Widget implements LateRenderable {
  231. private final Widget widget;
  232. private LateRenderableWidget(Widget widget) {
  233. this.widget = widget;
  234. }
  235. @Override
  236. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  237. this.widget.setZ(getZ());
  238. this.widget.render(matrices, mouseX, mouseY, delta);
  239. }
  240. @Override
  241. public List<? extends Element> children() {
  242. return Collections.singletonList(this.widget);
  243. }
  244. @Override
  245. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  246. return this.widget.mouseScrolled(mouseX, mouseY, amount);
  247. }
  248. }
  249. private static class LateRenderableWidgetWithBounds extends WidgetWithBounds implements LateRenderable {
  250. private final WidgetWithBounds widget;
  251. private LateRenderableWidgetWithBounds(WidgetWithBounds widget) {
  252. this.widget = widget;
  253. }
  254. @Override
  255. public @NotNull Rectangle getBounds() {
  256. return this.widget.getBounds();
  257. }
  258. @Override
  259. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  260. this.widget.setZ(getZ());
  261. this.widget.render(matrices, mouseX, mouseY, delta);
  262. }
  263. @Override
  264. public boolean containsMouse(double mouseX, double mouseY) {
  265. return this.widget.containsMouse(mouseX, mouseY);
  266. }
  267. @Override
  268. public List<? extends Element> children() {
  269. return Collections.singletonList(this.widget);
  270. }
  271. @Override
  272. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  273. return this.widget.mouseScrolled(mouseX, mouseY, amount);
  274. }
  275. }
  276. private static class WidgetWithBoundsWithTranslate extends WidgetWithBounds implements LateRenderable {
  277. private final WidgetWithBounds widget;
  278. private final float x, y, z;
  279. public WidgetWithBoundsWithTranslate(WidgetWithBounds widget, float x, float y, float z) {
  280. this.widget = widget;
  281. this.x = x;
  282. this.y = y;
  283. this.z = z;
  284. }
  285. @Override
  286. public @NotNull Rectangle getBounds() {
  287. return this.widget.getBounds();
  288. }
  289. @Override
  290. public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
  291. matrices.push();
  292. matrices.translate(x, y, z);
  293. this.widget.setZ(getZ());
  294. this.widget.render(matrices, mouseX, mouseY, delta);
  295. matrices.pop();
  296. }
  297. @Override
  298. public boolean containsMouse(double mouseX, double mouseY) {
  299. return this.widget.containsMouse(mouseX, mouseY);
  300. }
  301. @Override
  302. public List<? extends Element> children() {
  303. return Collections.singletonList(this.widget);
  304. }
  305. @Override
  306. public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
  307. return this.widget.mouseScrolled(mouseX, mouseY, amount);
  308. }
  309. }
  310. }