| 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.Mth;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class AlertScreen extends Screen {
|
| 16 | private static final int LABEL_Y = 90;
|
| 17 | private final Component messageText;
|
| 18 | private MultiLineLabel message = MultiLineLabel.EMPTY;
|
| 19 | private final Runnable callback;
|
| 20 | private final Component okButton;
|
| 21 | private final boolean shouldCloseOnEsc;
|
| 22 |
|
| 23 | public AlertScreen(final Runnable callback, final Component title, final Component messageText) {
|
| 24 | this(callback, title, messageText, CommonComponents.GUI_BACK, true);
|
| 25 | }
|
| 26 |
|
| 27 | public AlertScreen(final Runnable callback, final Component title, final Component messageText, final Component okButton, final boolean shouldCloseOnEsc) {
|
| 28 | super(title);
|
| 29 | this.callback = callback;
|
| 30 | this.messageText = messageText;
|
| 31 | this.okButton = okButton;
|
| 32 | this.shouldCloseOnEsc = shouldCloseOnEsc;
|
| 33 | }
|
| 34 |
|
| 35 | @Override
|
| 36 | public Component getNarrationMessage() {
|
| 37 | return CommonComponents.joinForNarration(super.getNarrationMessage(), this.messageText);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | protected void init() {
|
| 42 | super.init();
|
| 43 | this.message = MultiLineLabel.create(this.font, this.messageText, this.width - 50);
|
| 44 | int textHeight = this.message.getLineCount() * 9;
|
| 45 | int buttonY = Mth.clamp(90 + textHeight + 12, this.height / 6 + 96, this.height - 24);
|
| 46 | int buttonWidth = 150;
|
| 47 | this.addRenderableWidget(Button.builder(this.okButton, button -> this.callback.run()).bounds((this.width - 150) / 2, buttonY, 150, 20).build());
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public void extractRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 52 | super.extractRenderState(graphics, mouseX, mouseY, a);
|
| 53 | ActiveTextCollector textRenderer = graphics.textRenderer();
|
| 54 | graphics.centeredText(this.font, this.title, this.width / 2, 70, -1);
|
| 55 | this.message.visitLines(TextAlignment.CENTER, this.width / 2, 90, 9, textRenderer);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public boolean shouldCloseOnEsc() {
|
| 60 | return this.shouldCloseOnEsc;
|
| 61 | }
|
| 62 | }
|
| 63 |
|