Added native file choosers for importing/exporting save

This commit is contained in:
Anuken
2018-05-24 23:16:03 -04:00
parent 519d2c1714
commit f524be6738
4 changed files with 23 additions and 13 deletions
@@ -64,7 +64,15 @@ public abstract class Platform {
} }
/**Only used for iOS or android: open the share menu for a map or save.*/ /**Only used for iOS or android: open the share menu for a map or save.*/
public void shareFile(FileHandle file){} public void shareFile(FileHandle file){}
/**Show a file chooser. Desktop only.*/
/**Show a file chooser. Desktop only.
*
* @param text File chooser title text
* @param content Type of files to be loaded
* @param cons Selection listener
* @param open Whether to open or save files.
* @param filetypes List of file extensions to filter.
*/
public void showFileChooser(String text, String content, Consumer<FileHandle> cons, boolean open, String... filetypes){} public void showFileChooser(String text, String content, Consumer<FileHandle> cons, boolean open, String... filetypes){}
/**Use the default thread provider from the kryonet module for this.*/ /**Use the default thread provider from the kryonet module for this.*/
public ThreadProvider getThreadProvider(){ public ThreadProvider getThreadProvider(){
@@ -87,14 +87,14 @@ public class LoadDialog extends FloatingDialog{
if(!gwt) { if(!gwt) {
t.addImageButton("icon-save", "empty", 14 * 3, () -> { t.addImageButton("icon-save", "empty", 14 * 3, () -> {
if(!ios) { if(!ios) {
new FileChooser("$text.save.export", false, file -> { Platform.instance.showFileChooser(Bundles.get("text.save.export"), "Mindustry Save", file -> {
try { try {
slot.exportFile(file); slot.exportFile(file);
setup(); setup();
} catch (IOException e) { } catch (IOException e) {
ui.showError(Bundles.format("text.save.export.fail", Strings.parseException(e, false))); ui.showError(Bundles.format("text.save.export.fail", Strings.parseException(e, false)));
} }
}).show(); }, false, "mins");
}else{ }else{
try { try {
FileHandle file = Gdx.files.local("save-" + slot.getName() + ".mins"); FileHandle file = Gdx.files.local("save-" + slot.getName() + ".mins");
@@ -150,7 +150,7 @@ public class LoadDialog extends FloatingDialog{
if(gwt || ios) return; if(gwt || ios) return;
slots.addImageTextButton("$text.save.import", "icon-add", "clear", 14*3, () -> { slots.addImageTextButton("$text.save.import", "icon-add", "clear", 14*3, () -> {
new FileChooser("$text.save.import", f -> f.extension().equals("mins"), true, file -> { Platform.instance.showFileChooser(Bundles.get("text.save.import"), "Mindustry Save", file -> {
if(SaveIO.isSaveValid(file)){ if(SaveIO.isSaveValid(file)){
try{ try{
control.getSaves().importSave(file); control.getSaves().importSave(file);
@@ -161,7 +161,7 @@ public class LoadDialog extends FloatingDialog{
}else{ }else{
ui.showError("$text.save.import.invalid"); ui.showError("$text.save.import.invalid");
} }
}).show(); }, true, "mins");
}).fillX().margin(10f).minWidth(300f).height(70f).pad(4f).padRight(-4); }).fillX().margin(10f).minWidth(300f).height(70f).pad(4f).padRight(-4);
} }
@@ -20,8 +20,9 @@ public class DesktopLauncher {
config.setMaximized(true); config.setMaximized(true);
config.setWindowedMode(960, 540); config.setWindowedMode(960, 540);
config.setWindowIcon("sprites/icon.png"); config.setWindowIcon("sprites/icon.png");
if(OS.isMac) { if(OS.isMac) {
config.setPreferencesConfig(UCore.getProperty("user.home") + "/Library/Application Support/", FileType.Absolute); config.setPreferencesConfig(UCore.getProperty("user.home") + "/Library/Application Support/Mindustry", FileType.Absolute);
} }
Platform.instance = new DesktopPlatform(arg); Platform.instance = new DesktopPlatform(arg);
@@ -55,6 +55,7 @@ public class DesktopPlatform extends Platform {
FxApp.cons = cons; FxApp.cons = cons;
FxApp.filter = filter; FxApp.filter = filter;
FxApp.open = open; FxApp.open = open;
FxApp.content = content;
FxApp.open(); FxApp.open();
}){{ }){{
setDaemon(true); setDaemon(true);
@@ -208,7 +209,7 @@ public class DesktopPlatform extends Platform {
file = chooser.showSaveDialog(null); file = chooser.showSaveDialog(null);
} }
if (file != null) { if (file != null) {
cons.accept(Gdx.files.absolute(file.getAbsolutePath())); Gdx.app.postRunnable(() -> cons.accept(Gdx.files.absolute(file.getAbsolutePath())));
} }
} }
} }