Rewrite GuiRenameReplay for new GUI API

This commit is contained in:
johni0702
2015-10-09 17:06:40 +02:00
parent f3a90cfa56
commit 2c776adc88
2 changed files with 75 additions and 77 deletions

View File

@@ -1,100 +1,98 @@
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 net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiErrorScreen;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.resources.I18n;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.lwjgl.input.Keyboard;
import org.lwjgl.util.Color;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class GuiRenameReplay extends GuiScreen {
private GuiScreen parent;
private GuiTextField replayNameInput;
private File file;
public class GuiRenameReplay extends AbstractGuiScreen<GuiRenameReplay> {
public final GuiScreen parent;
public final 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) {
this.parent = parent;
this.file = file;
}
@Override
public void updateScreen() {
this.replayNameInput.updateCursorCounter();
}
nameField.setText(FilenameUtils.getBaseName(file.getName()));
@Override
public void initGui() {
Keyboard.enableRepeatEvents(true);
@SuppressWarnings("unchecked")
List<GuiButton> buttonList = this.buttonList;
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);
setLayout(new CustomLayout<GuiRenameReplay>() {
@Override
protected void layout(GuiRenameReplay container, int width, int height) {
pos(inputPanel, width / 2 - width(inputPanel) / 2, 60);
pos(buttonPanel, width / 2 - width(buttonPanel) / 2, height / 4 + 100);
}
}
});
setTitle(new GuiLabel().setI18nText("replaymod.gui.viewer.rename.title"));
}
@Override
protected void keyTyped(char typedChar, int keyCode) throws IOException {
this.replayNameInput.textboxKeyTyped(typedChar, keyCode);
((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);
protected GuiRenameReplay getThis() {
return this;
}
}

View File

@@ -225,7 +225,7 @@ public class GuiReplayViewer extends GuiScreen implements GuiYesNoCallback {
new GuiReplaySettings(this, ReplayMod.instance.getSettingsRegistry()).display();
} else if(button.id == RENAME_BUTTON_ID) {
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) {
File file = replayFileList.get(replayGuiList.selected).first().first();
this.mc.displayGuiScreen(new GuiUploadFile(file, this));