add file picker interface for mods
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -4,7 +4,9 @@ import arc.*;
|
||||
import arc.assets.*;
|
||||
import arc.audio.*;
|
||||
import arc.files.*;
|
||||
import arc.func.*;
|
||||
import arc.mock.*;
|
||||
import arc.scene.ui.*;
|
||||
import arc.struct.*;
|
||||
import arc.util.*;
|
||||
import arc.util.Log.*;
|
||||
@@ -116,6 +118,44 @@ public class Scripts implements Disposable{
|
||||
return sound;
|
||||
}
|
||||
|
||||
/** 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){
|
||||
|
||||
Reference in New Issue
Block a user