WarningAndErrorScreen.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 me.shedaniel.clothconfig2.gui.widget.DynamicNewSmoothScrollingEntryListWidget;
  25. import me.shedaniel.rei.RoughlyEnoughItemsState;
  26. import net.minecraft.client.MinecraftClient;
  27. import net.minecraft.client.gui.DrawableHelper;
  28. import net.minecraft.client.gui.screen.Screen;
  29. import net.minecraft.client.gui.widget.AbstractButtonWidget;
  30. import net.minecraft.client.gui.widget.ButtonWidget;
  31. import net.minecraft.client.sound.PositionedSoundInstance;
  32. import net.minecraft.client.util.NarratorManager;
  33. import net.minecraft.client.util.math.MatrixStack;
  34. import net.minecraft.sound.SoundEvents;
  35. import net.minecraft.text.LiteralText;
  36. import net.minecraft.text.Text;
  37. import net.minecraft.util.Formatting;
  38. import net.minecraft.util.Lazy;
  39. import net.minecraft.util.Pair;
  40. import net.minecraft.util.Util;
  41. import org.jetbrains.annotations.ApiStatus;
  42. import java.net.URI;
  43. import java.net.URISyntaxException;
  44. @ApiStatus.Internal
  45. public class WarningAndErrorScreen extends Screen {
  46. public static final Lazy<WarningAndErrorScreen> INSTANCE = new Lazy<>(WarningAndErrorScreen::new);
  47. private AbstractButtonWidget buttonExit;
  48. private StringEntryListWidget listWidget;
  49. private Screen parent;
  50. private WarningAndErrorScreen() {
  51. super(NarratorManager.EMPTY);
  52. }
  53. @Override
  54. public boolean shouldCloseOnEsc() {
  55. return false;
  56. }
  57. public void setParent(Screen parent) {
  58. this.parent = parent;
  59. }
  60. private void addText(Text string) {
  61. for (Text s : textRenderer.wrapLines(string, width - 80)) {
  62. listWidget.creditsAddEntry(new TextItem(s));
  63. }
  64. }
  65. private void addLink(Text string, String link) {
  66. for (Text s : textRenderer.wrapLines(string, width - 80)) {
  67. listWidget.creditsAddEntry(new LinkItem(s.getString(), link));
  68. }
  69. }
  70. @Override
  71. public void init() {
  72. children.add(listWidget = new StringEntryListWidget(client, width, height, 32, height - 32));
  73. listWidget.max = 80;
  74. listWidget.creditsClearEntries();
  75. listWidget.creditsAddEntry(new EmptyItem());
  76. if (!RoughlyEnoughItemsState.getWarnings().isEmpty())
  77. listWidget.creditsAddEntry(new TextItem(new LiteralText("Warnings:").formatted(Formatting.RED)));
  78. for (Pair<String, String> pair : RoughlyEnoughItemsState.getWarnings()) {
  79. addText(new LiteralText(pair.getLeft()));
  80. if (pair.getRight() != null)
  81. addLink(new LiteralText(pair.getRight()), pair.getRight());
  82. for (int i = 0; i < 2; i++) {
  83. listWidget.creditsAddEntry(new EmptyItem());
  84. }
  85. }
  86. if (!RoughlyEnoughItemsState.getWarnings().isEmpty() && !RoughlyEnoughItemsState.getErrors().isEmpty()) {
  87. listWidget.creditsAddEntry(new EmptyItem());
  88. }
  89. if (!RoughlyEnoughItemsState.getErrors().isEmpty())
  90. listWidget.creditsAddEntry(new TextItem(new LiteralText("Errors:").formatted(Formatting.RED)));
  91. for (Pair<String, String> pair : RoughlyEnoughItemsState.getErrors()) {
  92. addText(new LiteralText(pair.getLeft()));
  93. if (pair.getRight() != null)
  94. addLink(new LiteralText(pair.getRight()), pair.getRight());
  95. for (int i = 0; i < 2; i++) {
  96. listWidget.creditsAddEntry(new EmptyItem());
  97. }
  98. }
  99. for (StringItem child : listWidget.children()) {
  100. listWidget.max = Math.max(listWidget.max, child.getWidth());
  101. }
  102. children.add(buttonExit = new ButtonWidget(width / 2 - 100, height - 26, 200, 20,
  103. new LiteralText(RoughlyEnoughItemsState.getErrors().isEmpty() ? "Continue" : "Exit"),
  104. button -> {
  105. if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
  106. RoughlyEnoughItemsState.clear();
  107. RoughlyEnoughItemsState.continues();
  108. MinecraftClient.getInstance().openScreen(parent);
  109. setParent(null);
  110. } else {
  111. MinecraftClient.getInstance().scheduleStop();
  112. }
  113. }));
  114. }
  115. @Override
  116. public boolean mouseScrolled(double double_1, double double_2, double double_3) {
  117. return listWidget.mouseScrolled(double_1, double_2, double_3) || super.mouseScrolled(double_1, double_2, double_3);
  118. }
  119. @Override
  120. public void render(MatrixStack matrices, int int_1, int int_2, float float_1) {
  121. this.renderDirtBackground(0);
  122. this.listWidget.render(matrices, int_1, int_2, float_1);
  123. if (RoughlyEnoughItemsState.getErrors().isEmpty()) {
  124. this.drawCenteredString(matrices, this.textRenderer, "Warnings during Roughly Enough Items' initialization", this.width / 2, 16, 16777215);
  125. } else {
  126. this.drawCenteredString(matrices, this.textRenderer, "Errors during Roughly Enough Items' initialization", this.width / 2, 16, 16777215);
  127. }
  128. super.render(matrices, int_1, int_2, float_1);
  129. this.buttonExit.render(matrices, int_1, int_2, float_1);
  130. }
  131. private static class StringEntryListWidget extends DynamicNewSmoothScrollingEntryListWidget<StringItem> {
  132. private boolean inFocus;
  133. private int max = 80;
  134. public StringEntryListWidget(MinecraftClient client, int width, int height, int startY, int endY) {
  135. super(client, width, height, startY, endY, DrawableHelper.BACKGROUND_TEXTURE);
  136. }
  137. @Override
  138. public boolean changeFocus(boolean boolean_1) {
  139. if (!this.inFocus && this.getItemCount() == 0) {
  140. return false;
  141. } else {
  142. this.inFocus = !this.inFocus;
  143. if (this.inFocus && this.getFocused() == null && this.getItemCount() > 0) {
  144. this.moveSelection(1);
  145. } else if (this.inFocus && this.getFocused() != null) {
  146. this.moveSelection(0);
  147. }
  148. return this.inFocus;
  149. }
  150. }
  151. public void creditsClearEntries() {
  152. clearItems();
  153. }
  154. private StringItem rei_getEntry(int int_1) {
  155. return this.children().get(int_1);
  156. }
  157. public void creditsAddEntry(StringItem entry) {
  158. addItem(entry);
  159. }
  160. @Override
  161. public int getItemWidth() {
  162. return max;
  163. }
  164. @Override
  165. protected int getScrollbarPosition() {
  166. return width - 40;
  167. }
  168. }
  169. private abstract static class StringItem extends DynamicNewSmoothScrollingEntryListWidget.Entry<StringItem> {
  170. public abstract int getWidth();
  171. }
  172. private static class EmptyItem extends StringItem {
  173. @Override
  174. public void render(MatrixStack matrixStack, int i, int i1, int i2, int i3, int i4, int i5, int i6, boolean b, float v) {
  175. }
  176. @Override
  177. public int getItemHeight() {
  178. return 5;
  179. }
  180. @Override
  181. public int getWidth() {
  182. return 0;
  183. }
  184. }
  185. private static class TextItem extends StringItem {
  186. private Text text;
  187. public TextItem(Text text) {
  188. this.text = text;
  189. }
  190. @Override
  191. public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
  192. MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x + 5, y, -1);
  193. }
  194. @Override
  195. public int getItemHeight() {
  196. return 12;
  197. }
  198. @Override
  199. public boolean changeFocus(boolean boolean_1) {
  200. return false;
  201. }
  202. @Override
  203. public int getWidth() {
  204. return MinecraftClient.getInstance().textRenderer.getWidth(text) + 10;
  205. }
  206. }
  207. private class LinkItem extends StringItem {
  208. private String text;
  209. private String link;
  210. private boolean contains;
  211. public LinkItem(String text, String link) {
  212. this.text = text;
  213. this.link = link;
  214. }
  215. @Override
  216. public void render(MatrixStack matrices, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean isSelected, float delta) {
  217. contains = mouseX >= x && mouseX <= x + entryWidth && mouseY >= y && mouseY <= y + entryHeight;
  218. if (contains) {
  219. WarningAndErrorScreen.this.renderTooltip(matrices, new LiteralText("Click to open link."), mouseX, mouseY);
  220. MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, "§n" + text, x + 5, y, 0xff1fc3ff);
  221. } else {
  222. MinecraftClient.getInstance().textRenderer.drawWithShadow(matrices, text, x + 5, y, 0xff1fc3ff);
  223. }
  224. }
  225. @Override
  226. public int getItemHeight() {
  227. return 12;
  228. }
  229. @Override
  230. public boolean changeFocus(boolean boolean_1) {
  231. return false;
  232. }
  233. @Override
  234. public int getWidth() {
  235. return MinecraftClient.getInstance().textRenderer.getWidth(text) + 10;
  236. }
  237. @Override
  238. public boolean mouseClicked(double mouseX, double mouseY, int button) {
  239. if (contains && button == 0) {
  240. MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
  241. try {
  242. Util.getOperatingSystem().open(new URI(link));
  243. return true;
  244. } catch (URISyntaxException e) {
  245. e.printStackTrace();
  246. }
  247. }
  248. return false;
  249. }
  250. }
  251. }