Rewrite GuiRenameReplay for new GUI API
This commit is contained in:
@@ -1,100 +1,98 @@
|
|||||||
package com.replaymod.replay.gui.screen;
|
package com.replaymod.replay.gui.screen;
|
||||||
|
|
||||||
|
import de.johni0702.minecraft.gui.container.AbstractGuiScreen;
|
||||||
|
import de.johni0702.minecraft.gui.container.GuiPanel;
|
||||||
|
import de.johni0702.minecraft.gui.element.GuiButton;
|
||||||
|
import de.johni0702.minecraft.gui.element.GuiLabel;
|
||||||
|
import de.johni0702.minecraft.gui.element.GuiTextField;
|
||||||
|
import de.johni0702.minecraft.gui.layout.CustomLayout;
|
||||||
|
import de.johni0702.minecraft.gui.layout.VerticalLayout;
|
||||||
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
import eu.crushedpixel.replaymod.utils.ReplayFileIO;
|
||||||
import net.minecraft.client.gui.GuiButton;
|
|
||||||
import net.minecraft.client.gui.GuiErrorScreen;
|
import net.minecraft.client.gui.GuiErrorScreen;
|
||||||
import net.minecraft.client.gui.GuiScreen;
|
import net.minecraft.client.gui.GuiScreen;
|
||||||
import net.minecraft.client.gui.GuiTextField;
|
|
||||||
import net.minecraft.client.resources.I18n;
|
import net.minecraft.client.resources.I18n;
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
import org.lwjgl.input.Keyboard;
|
import org.lwjgl.util.Color;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class GuiRenameReplay extends GuiScreen {
|
public class GuiRenameReplay extends AbstractGuiScreen<GuiRenameReplay> {
|
||||||
private GuiScreen parent;
|
public final GuiScreen parent;
|
||||||
private GuiTextField replayNameInput;
|
public final File file;
|
||||||
private File file;
|
|
||||||
|
public final GuiLabel nameLabel = new GuiLabel().setI18nText("replaymod.gui.viewer.rename.name")
|
||||||
|
.setColor(new Color(0xa0, 0xa0, 0xa0));
|
||||||
|
public final GuiTextField nameField = new GuiTextField().setSize(200, 20).setFocused(true).onEnter(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
renameButton.onClick();
|
||||||
|
}
|
||||||
|
}).onTextChanged(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
renameButton.setEnabled(!nameField.getText().isEmpty());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
public final GuiButton renameButton = new GuiButton().setSize(200, 20).setI18nLabel("replaymod.gui.rename")
|
||||||
|
.onClick(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
// Sanitize their input
|
||||||
|
String name = nameField.getText().trim().replace("[^a-zA-Z0-9\\.\\- ]", "_");
|
||||||
|
// This file is what they want
|
||||||
|
File targetFile = new File(file.getParentFile(), name + ".zip");
|
||||||
|
// But if it's already used, this is what they get
|
||||||
|
File renamed = ReplayFileIO.getNextFreeFile(targetFile);
|
||||||
|
try {
|
||||||
|
// Finally, try to move it
|
||||||
|
FileUtils.moveFile(file, renamed);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// We failed (might also be their OS)
|
||||||
|
e.printStackTrace();
|
||||||
|
getMinecraft().displayGuiScreen(new GuiErrorScreen(
|
||||||
|
I18n.format("replaymod.gui.viewer.delete.failed1"),
|
||||||
|
I18n.format("replaymod.gui.viewer.delete.failed2")
|
||||||
|
));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
getMinecraft().displayGuiScreen(parent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
public final GuiButton cancelButton = new GuiButton().setSize(200, 20).setI18nLabel("replaymod.gui.cancel")
|
||||||
|
.onClick(new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
getMinecraft().displayGuiScreen(parent);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
public final GuiPanel inputPanel = new GuiPanel(this).setLayout(new VerticalLayout().setSpacing(5))
|
||||||
|
.addElements(null, nameLabel, nameField);
|
||||||
|
public final GuiPanel buttonPanel = new GuiPanel(this).setLayout(new VerticalLayout().setSpacing(5))
|
||||||
|
.addElements(null, renameButton, cancelButton);
|
||||||
|
|
||||||
public GuiRenameReplay(GuiScreen parent, File file) {
|
public GuiRenameReplay(GuiScreen parent, File file) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
this.file = file;
|
this.file = file;
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
nameField.setText(FilenameUtils.getBaseName(file.getName()));
|
||||||
public void updateScreen() {
|
|
||||||
this.replayNameInput.updateCursorCounter();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
setLayout(new CustomLayout<GuiRenameReplay>() {
|
||||||
public void initGui() {
|
@Override
|
||||||
Keyboard.enableRepeatEvents(true);
|
protected void layout(GuiRenameReplay container, int width, int height) {
|
||||||
@SuppressWarnings("unchecked")
|
pos(inputPanel, width / 2 - width(inputPanel) / 2, 60);
|
||||||
List<GuiButton> buttonList = this.buttonList;
|
pos(buttonPanel, width / 2 - width(buttonPanel) / 2, height / 4 + 100);
|
||||||
buttonList.clear();
|
|
||||||
buttonList.add(new GuiButton(0, this.width / 2 - 100, this.height / 4 + 96 + 12, I18n.format("replaymod.gui.rename")));
|
|
||||||
buttonList.add(new GuiButton(1, this.width / 2 - 100, this.height / 4 + 120 + 12, I18n.format("replaymod.gui.cancel")));
|
|
||||||
String s = FilenameUtils.getBaseName(file.getAbsolutePath());
|
|
||||||
this.replayNameInput = new GuiTextField(2, this.fontRendererObj, this.width / 2 - 100, 60, 200, 20);
|
|
||||||
this.replayNameInput.setFocused(true);
|
|
||||||
this.replayNameInput.setText(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onGuiClosed() {
|
|
||||||
Keyboard.enableRepeatEvents(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void actionPerformed(GuiButton button) throws IOException {
|
|
||||||
if(button.enabled) {
|
|
||||||
if(button.id == 1) {
|
|
||||||
this.mc.displayGuiScreen(this.parent);
|
|
||||||
} else if(button.id == 0) {
|
|
||||||
File folder = ReplayFileIO.getReplayFolder();
|
|
||||||
|
|
||||||
File initRenamed = new File(folder, (this.replayNameInput.getText().trim() + ".zip").replaceAll("[^a-zA-Z0-9\\.\\- ]", "_"));
|
|
||||||
File renamed = ReplayFileIO.getNextFreeFile(initRenamed);
|
|
||||||
try {
|
|
||||||
FileUtils.moveFile(file, renamed);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
mc.displayGuiScreen(new GuiErrorScreen(
|
|
||||||
I18n.format("replaymod.gui.viewer.delete.failed1"),
|
|
||||||
I18n.format("replaymod.gui.viewer.delete.failed2")
|
|
||||||
));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.mc.displayGuiScreen(this.parent);
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
setTitle(new GuiLabel().setI18nText("replaymod.gui.viewer.rename.title"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void keyTyped(char typedChar, int keyCode) throws IOException {
|
protected GuiRenameReplay getThis() {
|
||||||
this.replayNameInput.textboxKeyTyped(typedChar, keyCode);
|
return this;
|
||||||
((GuiButton) this.buttonList.get(0)).enabled = this.replayNameInput.getText().trim().length() > 0;
|
|
||||||
|
|
||||||
if(keyCode == 28 || keyCode == 156) {
|
|
||||||
this.actionPerformed((GuiButton) this.buttonList.get(0));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException {
|
|
||||||
super.mouseClicked(mouseX, mouseY, mouseButton);
|
|
||||||
this.replayNameInput.mouseClicked(mouseX, mouseY, mouseButton);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
|
|
||||||
this.drawDefaultBackground();
|
|
||||||
this.drawCenteredString(this.fontRendererObj, I18n.format("replaymod.gui.viewer.rename.title"), this.width / 2, 20, 16777215);
|
|
||||||
this.drawString(this.fontRendererObj, I18n.format("replaymod.gui.viewer.rename.name"), this.width / 2 - 100, 47, 10526880);
|
|
||||||
this.replayNameInput.drawTextBox();
|
|
||||||
super.drawScreen(mouseX, mouseY, partialTicks);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
|
|||||||
new GuiReplaySettings(this, ReplayMod.instance.getSettingsRegistry()).display();
|
new GuiReplaySettings(this, ReplayMod.instance.getSettingsRegistry()).display();
|
||||||
} else if(button.id == RENAME_BUTTON_ID) {
|
} else if(button.id == RENAME_BUTTON_ID) {
|
||||||
File file = replayFileList.get(replayGuiList.selected).first().first();
|
File file = replayFileList.get(replayGuiList.selected).first().first();
|
||||||
this.mc.displayGuiScreen(new GuiRenameReplay(this, file));
|
new GuiRenameReplay(this, file).display();
|
||||||
} else if(button.id == UPLOAD_BUTTON_ID) {
|
} else if(button.id == UPLOAD_BUTTON_ID) {
|
||||||
File file = replayFileList.get(replayGuiList.selected).first().first();
|
File file = replayFileList.get(replayGuiList.selected).first().first();
|
||||||
this.mc.displayGuiScreen(new GuiUploadFile(file, this));
|
this.mc.displayGuiScreen(new GuiUploadFile(file, this));
|
||||||
|
|||||||
Reference in New Issue
Block a user