| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 6 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 7 | import net.minecraft.client.gui.TextAlignment;
|
| 8 | import net.minecraft.client.gui.components.Button;
|
| 9 | import net.minecraft.client.gui.components.MultiLineLabel;
|
| 10 | import net.minecraft.network.chat.CommonComponents;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.util.Util;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class GenericWaitingScreen extends Screen {
|
| 17 | private static final int TITLE_Y = 80;
|
| 18 | private static final int MESSAGE_Y = 120;
|
| 19 | private static final int MESSAGE_MAX_WIDTH = 360;
|
| 20 | private final boolean showLoadingDots;
|
| 21 | private final Component messageText;
|
| 22 | private final Component buttonLabel;
|
| 23 | private final Runnable buttonCallback;
|
| 24 | private final boolean showButton;
|
| 25 | private final boolean closeOnEscape;
|
| 26 | private final MultiLineLabel message;
|
| 27 | private @Nullable Button button;
|
| 28 | private int disableButtonTicks;
|
| 29 |
|
| 30 | public static GenericWaitingScreen createWaitingWithoutButton(final Component title, final Component messageText) {
|
| 31 | return new GenericWaitingScreen(title, true, messageText, Component.empty(), () -> {}, 0, false, false);
|
| 32 | }
|
| 33 |
|
| 34 | public static GenericWaitingScreen createWaiting(final Component title, final Component buttonLabel, final Runnable buttonCallback) {
|
| 35 | return new GenericWaitingScreen(title, true, Component.empty(), buttonLabel, buttonCallback, 0, true, false);
|
| 36 | }
|
| 37 |
|
| 38 | public static GenericWaitingScreen createCompleted(
|
| 39 | final Component title, final Component messageText, final Component buttonLabel, final Runnable buttonCallback
|
| 40 | ) {
|
| 41 | return new GenericWaitingScreen(title, false, messageText, buttonLabel, buttonCallback, 20, true, true);
|
| 42 | }
|
| 43 |
|
| 44 | protected GenericWaitingScreen(
|
| 45 | final Component title,
|
| 46 | final boolean showLoadingDots,
|
| 47 | final Component messageText,
|
| 48 | final Component buttonLabel,
|
| 49 | final Runnable buttonCallback,
|
| 50 | final int disableButtonTicks,
|
| 51 | final boolean showButton,
|
| 52 | final boolean closeOnEscape
|
| 53 | ) {
|
| 54 | super(title);
|
| 55 | this.showLoadingDots = showLoadingDots;
|
| 56 | this.messageText = messageText;
|
| 57 | this.buttonLabel = buttonLabel;
|
| 58 | this.buttonCallback = buttonCallback;
|
| 59 | this.disableButtonTicks = disableButtonTicks;
|
| 60 | this.showButton = showButton;
|
| 61 | this.closeOnEscape = closeOnEscape;
|
| 62 | this.message = MultiLineLabel.create(this.font, messageText, 360);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | protected void init() {
|
| 67 | super.init();
|
| 68 | int buttonWidth = 150;
|
| 69 | int buttonHeight = 20;
|
| 70 | int lineCount = this.message.getLineCount() + 1;
|
| 71 | int messageButtonSpacing = Math.max(lineCount, 5) * 9;
|
| 72 | int buttonY = Math.min(120 + messageButtonSpacing, this.height - 40);
|
| 73 | if (this.showButton) {
|
| 74 | this.button = this.addRenderableWidget(Button.builder(this.buttonLabel, b -> this.onClose()).bounds((this.width - 150) / 2, buttonY, 150, 20).build());
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | @Override
|
| 79 | public void tick() {
|
| 80 | if (this.disableButtonTicks > 0) {
|
| 81 | this.disableButtonTicks--;
|
| 82 | }
|
| 83 |
|
| 84 | if (this.button != null) {
|
| 85 | this.button.active = this.disableButtonTicks == 0;
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 91 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 92 | ActiveTextCollector textRenderer = graphics.textRenderer();
|
| 93 | graphics.centeredText(this.font, this.title, this.width / 2, 80, -1);
|
| 94 | int messageY = 120;
|
| 95 | if (this.showLoadingDots) {
|
| 96 | String loadingDots = LoadingDotsText.get(Util.getMillis());
|
| 97 | graphics.centeredText(this.font, loadingDots, this.width / 2, messageY, -6250336);
|
| 98 | messageY += 9 + 3;
|
| 99 | }
|
| 100 |
|
| 101 | this.message.visitLines(TextAlignment.CENTER, this.width / 2, messageY, 9, textRenderer);
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public boolean shouldCloseOnEsc() {
|
| 106 | return this.closeOnEscape && this.button != null && this.button.active;
|
| 107 | }
|
| 108 |
|
| 109 | @Override
|
| 110 | public void onClose() {
|
| 111 | this.buttonCallback.run();
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public Component getNarrationMessage() {
|
| 116 | return CommonComponents.joinForNarration(this.title, this.messageText);
|
| 117 | }
|
| 118 | }
|
| 119 |
|