122 lines
3.6 KiB
Java
122 lines
3.6 KiB
Java
package net.minecraft.client.gui.screens;
|
|
|
|
import com.google.common.collect.Lists;
|
|
import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
|
|
import java.util.List;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.client.gui.GuiGraphics;
|
|
import net.minecraft.client.gui.components.Button;
|
|
import net.minecraft.client.gui.components.MultiLineLabel;
|
|
import net.minecraft.network.chat.CommonComponents;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.util.Mth;
|
|
|
|
@Environment(EnvType.CLIENT)
|
|
public class ConfirmScreen extends Screen {
|
|
private static final int MARGIN = 20;
|
|
private final Component message;
|
|
private MultiLineLabel multilineMessage = MultiLineLabel.EMPTY;
|
|
/**
|
|
* The text shown for the first button in GuiYesNo
|
|
*/
|
|
protected Component yesButton;
|
|
/**
|
|
* The text shown for the second button in GuiYesNo
|
|
*/
|
|
protected Component noButton;
|
|
private int delayTicker;
|
|
protected final BooleanConsumer callback;
|
|
private final List<Button> exitButtons = Lists.<Button>newArrayList();
|
|
|
|
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.yesButton = yesButton;
|
|
this.noButton = noButton;
|
|
}
|
|
|
|
@Override
|
|
public Component getNarrationMessage() {
|
|
return CommonComponents.joinForNarration(super.getNarrationMessage(), this.message);
|
|
}
|
|
|
|
@Override
|
|
protected void init() {
|
|
super.init();
|
|
this.multilineMessage = MultiLineLabel.create(this.font, this.message, this.width - 50);
|
|
int i = Mth.clamp(this.messageTop() + this.messageHeight() + 20, this.height / 6 + 96, this.height - 24);
|
|
this.exitButtons.clear();
|
|
this.addButtons(i);
|
|
}
|
|
|
|
protected void addButtons(int y) {
|
|
this.addExitButton(Button.builder(this.yesButton, button -> this.callback.accept(true)).bounds(this.width / 2 - 155, y, 150, 20).build());
|
|
this.addExitButton(Button.builder(this.noButton, button -> this.callback.accept(false)).bounds(this.width / 2 - 155 + 160, y, 150, 20).build());
|
|
}
|
|
|
|
protected void addExitButton(Button exitButton) {
|
|
this.exitButtons.add(this.addRenderableWidget(exitButton));
|
|
}
|
|
|
|
@Override
|
|
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
|
|
super.render(guiGraphics, mouseX, mouseY, partialTick);
|
|
guiGraphics.drawCenteredString(this.font, this.title, this.width / 2, this.titleTop(), 16777215);
|
|
this.multilineMessage.renderCentered(guiGraphics, this.width / 2, this.messageTop());
|
|
}
|
|
|
|
private int titleTop() {
|
|
int i = (this.height - this.messageHeight()) / 2;
|
|
return Mth.clamp(i - 20 - 9, 10, 80);
|
|
}
|
|
|
|
private int messageTop() {
|
|
return this.titleTop() + 20;
|
|
}
|
|
|
|
private int messageHeight() {
|
|
return this.multilineMessage.getLineCount() * 9;
|
|
}
|
|
|
|
/**
|
|
* Sets the number of ticks to wait before enabling the buttons.
|
|
*/
|
|
public void setDelay(int ticksUntilEnable) {
|
|
this.delayTicker = ticksUntilEnable;
|
|
|
|
for (Button button : this.exitButtons) {
|
|
button.active = false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
super.tick();
|
|
if (--this.delayTicker == 0) {
|
|
for (Button button : this.exitButtons) {
|
|
button.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);
|
|
}
|
|
}
|
|
}
|