Merge remote-tracking branch 'origin/master'

This commit is contained in:
Anuken
2020-11-15 12:52:33 -05:00
6 changed files with 67 additions and 13 deletions

View File

@@ -117,9 +117,10 @@ public interface Platform{
* @param cons Selection listener
* @param open Whether to open or save files
* @param extension File extension to filter
* @param title The title of the native dialog
*/
default void showFileChooser(boolean open, String extension, Cons<Fi> cons){
new FileChooser(open ? "@open" : "@save", file -> file.extEquals(extension), open, file -> {
default void showFileChooser(boolean open, String title, String extension, Cons<Fi> cons){
new FileChooser(title, file -> file.extEquals(extension), open, file -> {
if(!open){
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
}else{
@@ -128,6 +129,10 @@ public interface Platform{
}).show();
}
default void showFileChooser(boolean open, String extension, Cons<Fi> cons){
showFileChooser(open, open ? "@open": "@save", extension, cons);
}
/**
* Show a file chooser for multiple file types.
* @param cons Selection listener

View File

@@ -6,6 +6,7 @@ import arc.assets.loaders.MusicLoader.*;
import arc.assets.loaders.SoundLoader.*;
import arc.audio.*;
import arc.files.*;
import arc.func.*;
import arc.struct.*;
import arc.util.*;
import arc.util.Log.*;
@@ -115,6 +116,44 @@ public class Scripts implements Disposable{
return music;
}
/** Ask the user to select a file to read for a certain purpose like "Please upload a sprite" */
public void readFile(String purpose, String ext, Cons<String> cons){
selectFile(true, purpose, ext, fi -> cons.get(fi.readString()));
}
/** readFile but for a byte[] */
public void readBinFile(String purpose, String ext, Cons<byte[]> cons){
selectFile(true, purpose, ext, fi -> cons.get(fi.readBytes()));
}
/** Ask the user to write a file. */
public void writeFile(String purpose, String ext, String contents){
if(contents == null) contents = "";
final String fContents = contents;
selectFile(false, purpose, ext, fi -> fi.writeString(fContents));
}
/** writeFile but for a byte[] */
public void writeBinFile(String purpose, String ext, byte[] contents){
if(contents == null) contents = new byte[0];
final byte[] fContents = contents;
selectFile(false, purpose, ext, fi -> fi.writeBytes(fContents));
}
private void selectFile(boolean open, String purpose, String ext, Cons<Fi> cons){
purpose = purpose.startsWith("@") ? Core.bundle.get(purpose.substring(1)) : purpose;
//add purpose and extension at the top
String title = Core.bundle.get(open ? "open" : "save") + " - " + purpose + " (." + ext + ")";
Vars.platform.showFileChooser(open, title, ext, fi -> {
try{
cons.get(fi);
}catch(Exception e){
Log.err("Failed to select file '@' for a mod", fi);
Log.err(e);
}
});
}
//endregion
public void run(LoadedMod mod, Fi file){