ProjectZombie/src/projectzombie/menu/MenuWorldDelete.java

151 lines
3.5 KiB
Java

package projectzombie.menu;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import bdf.types.BdfArray;
import bdf.types.BdfNamedList;
import bdf.types.BdfObject;
import bdf.types.BdfReader;
import gl_engine.vec.Vec2d;
import projectzombie.Main;
import projectzombie.init.Models;
import projectzombie.input.types.InputGUI;
import projectzombie.menu.gui.GUI;
import projectzombie.menu.gui.GUIAlignment;
import projectzombie.menu.gui.GUIBackToMenu;
import projectzombie.menu.gui.GUIButtonBasic;
import projectzombie.menu.gui.GUILabel;
import projectzombie.menu.gui.GUITextBox;
import projectzombie.worker.WorkerTasks;
public class MenuWorldDelete extends Menu
{
private MenuSaves parent;
private String path;
private GUI gui;
public MenuWorldDelete(MenuSaves parent, String name, String path)
{
this.parent = parent;
this.path = path;
keepMouse = false;
doGameloop = parent.doGameloop;
doGameRender = parent.doGameRender;
showIngameGUI = parent.showIngameGUI;
gui = new GUIBackToMenu(parent);
input = new InputGUI(gui);
GUIButtonBasic buttonCancel = new GUIButtonBasic("Cancel", button -> {
Main.menu = parent;
});
GUIButtonBasic buttonDelete = new GUIButtonBasic("Delete", button -> {
deleteSave();
});
buttonCancel.setAlign(GUIAlignment.RIGHT);
buttonDelete.setAlign(GUIAlignment.LEFT);
buttonCancel.setPos(new Vec2d(-0.5, -4));
buttonDelete.setPos(new Vec2d(0.5, -4));
{
// Delete world title
GUILabel label = new GUILabel();
label.setText("Delete World");
label.setSize(new Vec2d(1, 1));
label.setPos(new Vec2d(0, 4));
gui.add(label);
}
{
// Delete world confirm message
GUILabel label = new GUILabel();
label.setText("Are you sure you want to delete \"" + name + "\"?");
label.setSize(new Vec2d(0.4, 0.4));
label.setPos(new Vec2d(0, 3));
gui.add(label);
}
gui.add(buttonCancel);
gui.add(buttonDelete);
}
private void deleteSave()
{
BdfObject bdf = Main.bdf_saves.getObject();
BdfNamedList nl = bdf.getNamedList();
BdfArray array = nl.get("saves").getArray();
try
{
Files.walkFileTree(Paths.get("./saves/" + path), new FileVisitor<Path>()
{
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
catch(IOException e) {
e.printStackTrace();
}
for(int i=0;i<array.size();i++) {
BdfNamedList world_nl = array.get(i).getNamedList();
if(world_nl.get("path").getString().contentEquals(path)) {
array.remove(i);
break;
}
}
Main.menu = parent;
WorkerTasks.saveToFile("./saves.bdf.gz", Main.bdf_saves);
parent.reloadDatabase(Main.bdf_saves);
}
@Override
public void render() {
gui.render();
}
@Override
public void update() {
super.update();
parent.update();
}
}