| 1 | package net.minecraft.client.gui.screens;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.gui.components.Button;
|
| 7 | import net.minecraft.client.gui.components.MultiLineTextWidget;
|
| 8 | import net.minecraft.client.gui.components.StringWidget;
|
| 9 | import net.minecraft.client.gui.layouts.FrameLayout;
|
| 10 | import net.minecraft.client.gui.layouts.LayoutElement;
|
| 11 | import net.minecraft.client.gui.layouts.LinearLayout;
|
| 12 | import net.minecraft.client.input.KeyEvent;
|
| 13 | import net.minecraft.network.chat.CommonComponents;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class ConfirmScreen extends Screen {
|
| 19 | protected final Component message;
|
| 20 | protected final LinearLayout layout = LinearLayout.vertical().spacing(8);
|
| 21 | protected Component yesButtonComponent;
|
| 22 | protected Component noButtonComponent;
|
| 23 | protected @Nullable Button yesButton;
|
| 24 | protected @Nullable Button noButton;
|
| 25 | private int delayTicker;
|
| 26 | protected final BooleanConsumer callback;
|
| 27 |
|
| 28 | public ConfirmScreen(final BooleanConsumer callback, final Component title, final Component message) {
|
| 29 | this(callback, title, message, CommonComponents.GUI_YES, CommonComponents.GUI_NO);
|
| 30 | }
|
| 31 |
|
| 32 | public ConfirmScreen(
|
| 33 | final BooleanConsumer callback, final Component title, final Component message, final Component yesButtonComponent, final Component noButtonComponent
|
| 34 | ) {
|
| 35 | super(title);
|
| 36 | this.callback = callback;
|
| 37 | this.message = message;
|
| 38 | this.yesButtonComponent = yesButtonComponent;
|
| 39 | this.noButtonComponent = noButtonComponent;
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public Component getNarrationMessage() {
|
| 44 | return CommonComponents.joinForNarration(super.getNarrationMessage(), this.message);
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected void init() {
|
| 49 | super.init();
|
| 50 | this.layout.defaultCellSetting().alignHorizontallyCenter();
|
| 51 | this.layout.addChild(new StringWidget(this.title, this.font));
|
| 52 | this.layout.addChild(this.addMessage());
|
| 53 | this.addAdditionalText();
|
| 54 | LinearLayout buttonLayout = this.layout.addChild(LinearLayout.horizontal().spacing(4));
|
| 55 | buttonLayout.defaultCellSetting().paddingTop(16);
|
| 56 | this.addButtons(buttonLayout);
|
| 57 | this.layout.visitWidgets(this::addRenderableWidget);
|
| 58 | this.repositionElements();
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | protected void repositionElements() {
|
| 63 | this.layout.arrangeElements();
|
| 64 | FrameLayout.centerInRectangle(this.layout, this.getRectangle());
|
| 65 | }
|
| 66 |
|
| 67 | protected void addAdditionalText() {
|
| 68 | }
|
| 69 |
|
| 70 | protected LayoutElement addMessage() {
|
| 71 | return new MultiLineTextWidget(this.message, this.font).setMaxWidth(this.width - 50).setMaxRows(15).setCentered(true);
|
| 72 | }
|
| 73 |
|
| 74 | protected void addButtons(final LinearLayout buttonLayout) {
|
| 75 | this.yesButton = buttonLayout.addChild(Button.builder(this.yesButtonComponent, button -> this.callback.accept(true)).build());
|
| 76 | this.noButton = buttonLayout.addChild(Button.builder(this.noButtonComponent, button -> this.callback.accept(false)).build());
|
| 77 | }
|
| 78 |
|
| 79 | public void setDelay(final int delay) {
|
| 80 | this.delayTicker = delay;
|
| 81 | this.yesButton.active = false;
|
| 82 | this.noButton.active = false;
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public void tick() {
|
| 87 | super.tick();
|
| 88 | if (--this.delayTicker == 0) {
|
| 89 | this.yesButton.active = true;
|
| 90 | this.noButton.active = true;
|
| 91 | }
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean shouldCloseOnEsc() {
|
| 96 | return false;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean keyPressed(final KeyEvent event) {
|
| 101 | if (this.delayTicker <= 0 && event.isEscape()) {
|
| 102 | this.callback.accept(false);
|
| 103 | return true;
|
| 104 | } else {
|
| 105 | return super.keyPressed(event);
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|