111 lines
3.4 KiB
Java
111 lines
3.4 KiB
Java
package net.minecraft.client.gui.screens;
|
|
|
|
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.gui.components.Button;
|
|
import net.minecraft.client.gui.components.MultiLineTextWidget;
|
|
import net.minecraft.client.gui.components.StringWidget;
|
|
import net.minecraft.client.gui.layouts.FrameLayout;
|
|
import net.minecraft.client.gui.layouts.LinearLayout;
|
|
import net.minecraft.network.chat.CommonComponents;
|
|
import net.minecraft.network.chat.Component;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class ConfirmScreen extends Screen {
|
|
private final Component message;
|
|
protected LinearLayout layout = LinearLayout.vertical().spacing(8);
|
|
protected Component yesButtonComponent;
|
|
protected Component noButtonComponent;
|
|
/**
|
|
* The text shown for the first button in GuiYesNo
|
|
*/
|
|
@Nullable
|
|
protected Button yesButton;
|
|
/**
|
|
* The text shown for the second button in GuiYesNo
|
|
*/
|
|
@Nullable
|
|
protected Button noButton;
|
|
private int delayTicker;
|
|
protected final BooleanConsumer callback;
|
|
|
|
public ConfirmScreen(BooleanConsumer callback, Component title, Component message) {
|
|
this(callback, title, message, CommonComponents.GUI_YES, CommonComponents.GUI_NO);
|
|
}
|
|
|
|
public ConfirmScreen(BooleanConsumer callback, Component title, Component message, Component yesButton, Component noButton) {
|
|
super(title);
|
|
this.callback = callback;
|
|
this.message = message;
|
|
this.yesButtonComponent = yesButton;
|
|
this.noButtonComponent = noButton;
|
|
}
|
|
|
|
@Override
|
|
public Component getNarrationMessage() {
|
|
return CommonComponents.joinForNarration(super.getNarrationMessage(), this.message);
|
|
}
|
|
|
|
@Override
|
|
protected void init() {
|
|
super.init();
|
|
this.layout.defaultCellSetting().alignHorizontallyCenter();
|
|
this.layout.addChild(new StringWidget(this.title, this.font));
|
|
this.layout.addChild(new MultiLineTextWidget(this.message, this.font).setMaxWidth(this.width - 50).setMaxRows(15).setCentered(true));
|
|
this.addAdditionalText();
|
|
LinearLayout linearLayout = this.layout.addChild(LinearLayout.horizontal().spacing(4));
|
|
linearLayout.defaultCellSetting().paddingTop(16);
|
|
this.addButtons(linearLayout);
|
|
this.layout.visitWidgets(this::addRenderableWidget);
|
|
this.repositionElements();
|
|
}
|
|
|
|
@Override
|
|
protected void repositionElements() {
|
|
this.layout.arrangeElements();
|
|
FrameLayout.centerInRectangle(this.layout, this.getRectangle());
|
|
}
|
|
|
|
protected void addAdditionalText() {
|
|
}
|
|
|
|
protected void addButtons(LinearLayout layout) {
|
|
this.yesButton = layout.addChild(Button.builder(this.yesButtonComponent, button -> this.callback.accept(true)).build());
|
|
this.noButton = layout.addChild(Button.builder(this.noButtonComponent, button -> this.callback.accept(false)).build());
|
|
}
|
|
|
|
/**
|
|
* Sets the number of ticks to wait before enabling the buttons.
|
|
*/
|
|
public void setDelay(int ticksUntilEnable) {
|
|
this.delayTicker = ticksUntilEnable;
|
|
this.yesButton.active = false;
|
|
this.noButton.active = false;
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
super.tick();
|
|
if (--this.delayTicker == 0) {
|
|
this.yesButton.active = true;
|
|
this.noButton.active = true;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean shouldCloseOnEsc() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
|
|
if (keyCode == 256) {
|
|
this.callback.accept(false);
|
|
return true;
|
|
} else {
|
|
return super.keyPressed(keyCode, scanCode, modifiers);
|
|
}
|
|
}
|
|
}
|