2019-07-23 12:54:53 +02:00
|
|
|
package com.simibubi.create.foundation.gui;
|
2019-07-17 17:13:09 +02:00
|
|
|
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
|
|
|
import org.lwjgl.glfw.GLFW;
|
|
|
|
|
2019-09-16 12:27:28 +02:00
|
|
|
import com.simibubi.create.ScreenResources;
|
2019-09-14 18:21:30 +02:00
|
|
|
import com.simibubi.create.foundation.utility.Lang;
|
|
|
|
|
2019-07-17 17:13:09 +02:00
|
|
|
import net.minecraft.client.gui.widget.TextFieldWidget;
|
|
|
|
import net.minecraft.client.gui.widget.button.Button;
|
|
|
|
|
2019-07-19 17:50:23 +02:00
|
|
|
public class TextInputPromptScreen extends AbstractSimiScreen {
|
2019-07-17 17:13:09 +02:00
|
|
|
|
2019-09-14 18:21:30 +02:00
|
|
|
private final String defaultConfirm = Lang.translate("action.confirm");
|
|
|
|
private final String defaultAbort = Lang.translate("action.abort");
|
|
|
|
|
2019-07-17 17:13:09 +02:00
|
|
|
private Consumer<String> callback;
|
|
|
|
private Consumer<String> abortCallback;
|
|
|
|
|
|
|
|
private TextFieldWidget nameField;
|
|
|
|
private Button confirm;
|
|
|
|
private Button abort;
|
|
|
|
|
|
|
|
private String buttonTextConfirm;
|
|
|
|
private String buttonTextAbort;
|
|
|
|
private String title;
|
|
|
|
|
|
|
|
private boolean confirmed;
|
|
|
|
|
2019-07-19 17:50:23 +02:00
|
|
|
public TextInputPromptScreen(Consumer<String> callBack, Consumer<String> abortCallback) {
|
2019-07-17 17:13:09 +02:00
|
|
|
super();
|
|
|
|
this.callback = callBack;
|
|
|
|
this.abortCallback = abortCallback;
|
|
|
|
|
2019-09-14 18:21:30 +02:00
|
|
|
buttonTextConfirm = defaultConfirm;
|
|
|
|
buttonTextAbort = defaultAbort;
|
2019-07-17 17:13:09 +02:00
|
|
|
confirmed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void init() {
|
|
|
|
super.init();
|
2019-07-19 17:50:23 +02:00
|
|
|
setWindowSize(ScreenResources.TEXT_INPUT.width, ScreenResources.TEXT_INPUT.height + 30);
|
2019-07-17 17:13:09 +02:00
|
|
|
|
2019-08-26 20:17:16 +02:00
|
|
|
this.nameField = new TextFieldWidget(font, guiLeft + 33, guiTop + 26, 128, 8, "");
|
2019-07-17 17:13:09 +02:00
|
|
|
this.nameField.setTextColor(-1);
|
|
|
|
this.nameField.setDisabledTextColour(-1);
|
|
|
|
this.nameField.setEnableBackgroundDrawing(false);
|
|
|
|
this.nameField.setMaxStringLength(35);
|
|
|
|
this.nameField.changeFocus(true);
|
|
|
|
|
2019-08-26 20:17:16 +02:00
|
|
|
confirm = new Button(guiLeft - 5, guiTop + 50, 100, 20, buttonTextConfirm, button -> {
|
2019-07-17 17:13:09 +02:00
|
|
|
callback.accept(nameField.getText());
|
|
|
|
confirmed = true;
|
|
|
|
minecraft.displayGuiScreen(null);
|
|
|
|
});
|
|
|
|
|
2019-08-26 20:17:16 +02:00
|
|
|
abort = new Button(guiLeft + 100, guiTop + 50, 100, 20, buttonTextAbort, button -> {
|
2019-07-17 17:13:09 +02:00
|
|
|
minecraft.displayGuiScreen(null);
|
|
|
|
});
|
|
|
|
|
|
|
|
widgets.add(confirm);
|
|
|
|
widgets.add(abort);
|
|
|
|
widgets.add(nameField);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void renderWindow(int mouseX, int mouseY, float partialTicks) {
|
2019-08-26 20:17:16 +02:00
|
|
|
ScreenResources.TEXT_INPUT.draw(this, guiLeft, guiTop);
|
|
|
|
font.drawString(title, guiLeft + (sWidth / 2) - (font.getStringWidth(title) / 2), guiTop + 11,
|
2019-07-19 17:50:23 +02:00
|
|
|
ScreenResources.FONT_COLOR);
|
2019-07-17 17:13:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removed() {
|
|
|
|
if (!confirmed)
|
|
|
|
abortCallback.accept(nameField.getText());
|
|
|
|
super.removed();
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setButtonTextConfirm(String buttonTextConfirm) {
|
|
|
|
this.buttonTextConfirm = buttonTextConfirm;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setButtonTextAbort(String buttonTextAbort) {
|
|
|
|
this.buttonTextAbort = buttonTextAbort;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean keyPressed(int keyCode, int p_keyPressed_2_, int p_keyPressed_3_) {
|
|
|
|
if (keyCode == GLFW.GLFW_KEY_ENTER) {
|
|
|
|
confirm.onPress();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (keyCode == 256 && this.shouldCloseOnEsc()) {
|
|
|
|
this.onClose();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return nameField.keyPressed(keyCode, p_keyPressed_2_, p_keyPressed_3_);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|