mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-21 21:28:28 +07:00
add file picker interface for mods
This commit is contained in:
@ -77,11 +77,11 @@ public class AndroidLauncher extends AndroidApplication{
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showFileChooser(boolean open, String extension, Cons<Fi> cons){
|
public void showFileChooser(boolean open, String title, String extension, Cons<Fi> cons){
|
||||||
showFileChooser(open, cons, extension);
|
showFileChooser(open, title, cons, extension);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showFileChooser(boolean open, Cons<Fi> cons, String... extensions){
|
void showFileChooser(boolean open, String title, Cons<Fi> cons, String... extensions){
|
||||||
String extension = extensions[0];
|
String extension = extensions[0];
|
||||||
|
|
||||||
if(VERSION.SDK_INT >= VERSION_CODES.Q){
|
if(VERSION.SDK_INT >= VERSION_CODES.Q){
|
||||||
@ -118,7 +118,7 @@ public class AndroidLauncher extends AndroidApplication{
|
|||||||
});
|
});
|
||||||
}else if(VERSION.SDK_INT >= VERSION_CODES.M && !(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
|
}else if(VERSION.SDK_INT >= VERSION_CODES.M && !(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
|
||||||
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
|
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)){
|
||||||
chooser = new FileChooser(open ? "@open" : "@save", file -> Structs.contains(extensions, file.extension().toLowerCase()), open, file -> {
|
chooser = new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), open, file -> {
|
||||||
if(!open){
|
if(!open){
|
||||||
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
|
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
|
||||||
}else{
|
}else{
|
||||||
@ -136,7 +136,7 @@ public class AndroidLauncher extends AndroidApplication{
|
|||||||
requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE);
|
requestPermissions(perms.toArray(new String[0]), PERMISSION_REQUEST_CODE);
|
||||||
}else{
|
}else{
|
||||||
if(open){
|
if(open){
|
||||||
new FileChooser("@open", file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show();
|
new FileChooser(title, file -> Structs.contains(extensions, file.extension().toLowerCase()), true, cons).show();
|
||||||
}else{
|
}else{
|
||||||
super.showFileChooser(open, extension, cons);
|
super.showFileChooser(open, extension, cons);
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,18 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
const log = function(context, obj){
|
const log = function(context, obj){
|
||||||
Vars.mods.getScripts().log(context, String(obj))
|
Vars.mods.scripts.log(context, String(obj))
|
||||||
}
|
}
|
||||||
|
|
||||||
const readString = path => Vars.mods.getScripts().readString(path)
|
const readString = path => Vars.mods.scripts.readString(path)
|
||||||
const readBytes = path => Vars.mods.getScripts().readBytes(path)
|
const readBytes = path => Vars.mods.scripts.readBytes(path)
|
||||||
const loadMusic = path => Vars.mods.getScripts().loadMusic(path)
|
const loadMusic = path => Vars.mods.scripts.loadMusic(path)
|
||||||
const loadSound = path => Vars.mods.getScripts().loadSound(path)
|
const loadSound = path => Vars.mods.scripts.loadSound(path)
|
||||||
|
|
||||||
|
const readFile = (purpose, ext, cons) => Vars.mods.scripts.readFile(purpose, ext, cons);
|
||||||
|
const readBinFile = (purpose, ext, cons) => Vars.mods.scripts.readBinFile(purpose, ext, cons);
|
||||||
|
const writeFile = (purpose, ext, str) => Vars.mods.scripts.writeFile(purpose, ext, str);
|
||||||
|
const writeBinFile = (purpose, ext, bytes) => Vars.mods.scripts.writeBinFile(purpose, ext, bytes);
|
||||||
|
|
||||||
let scriptName = "base.js"
|
let scriptName = "base.js"
|
||||||
let modName = "none"
|
let modName = "none"
|
||||||
|
@ -11,6 +11,11 @@ const readBytes = path => Vars.mods.getScripts().readBytes(path)
|
|||||||
const loadMusic = path => Vars.mods.getScripts().loadMusic(path)
|
const loadMusic = path => Vars.mods.getScripts().loadMusic(path)
|
||||||
const loadSound = path => Vars.mods.getScripts().loadSound(path)
|
const loadSound = path => Vars.mods.getScripts().loadSound(path)
|
||||||
|
|
||||||
|
const readFile = (purpose, ext, cons) => Vars.mods.scripts.readFile(purpose, ext, cons);
|
||||||
|
const readBinFile = (purpose, ext, cons) => Vars.mods.scripts.readBinFile(purpose, ext, cons);
|
||||||
|
const writeFile = (purpose, ext, str) => Vars.mods.scripts.writeFile(purpose, ext, str);
|
||||||
|
const writeBinFile = (purpose, ext, bytes) => Vars.mods.scripts.writeBinFile(purpose, ext, bytes);
|
||||||
|
|
||||||
let scriptName = "base.js"
|
let scriptName = "base.js"
|
||||||
let modName = "none"
|
let modName = "none"
|
||||||
|
|
||||||
|
@ -117,9 +117,10 @@ public interface Platform{
|
|||||||
* @param cons Selection listener
|
* @param cons Selection listener
|
||||||
* @param open Whether to open or save files
|
* @param open Whether to open or save files
|
||||||
* @param extension File extension to filter
|
* @param extension File extension to filter
|
||||||
|
* @param title The title of the native dialog
|
||||||
*/
|
*/
|
||||||
default void showFileChooser(boolean open, String extension, Cons<Fi> cons){
|
default void showFileChooser(boolean open, String title, String extension, Cons<Fi> cons){
|
||||||
new FileChooser(open ? "@open" : "@save", file -> file.extEquals(extension), open, file -> {
|
new FileChooser(title, file -> file.extEquals(extension), open, file -> {
|
||||||
if(!open){
|
if(!open){
|
||||||
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
|
cons.get(file.parent().child(file.nameWithoutExtension() + "." + extension));
|
||||||
}else{
|
}else{
|
||||||
@ -128,6 +129,10 @@ public interface Platform{
|
|||||||
}).show();
|
}).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.
|
* Show a file chooser for multiple file types.
|
||||||
* @param cons Selection listener
|
* @param cons Selection listener
|
||||||
|
@ -4,7 +4,9 @@ import arc.*;
|
|||||||
import arc.assets.*;
|
import arc.assets.*;
|
||||||
import arc.audio.*;
|
import arc.audio.*;
|
||||||
import arc.files.*;
|
import arc.files.*;
|
||||||
|
import arc.func.*;
|
||||||
import arc.mock.*;
|
import arc.mock.*;
|
||||||
|
import arc.scene.ui.*;
|
||||||
import arc.struct.*;
|
import arc.struct.*;
|
||||||
import arc.util.*;
|
import arc.util.*;
|
||||||
import arc.util.Log.*;
|
import arc.util.Log.*;
|
||||||
@ -116,6 +118,44 @@ public class Scripts implements Disposable{
|
|||||||
return sound;
|
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
|
//endregion
|
||||||
|
|
||||||
public void run(LoadedMod mod, Fi file){
|
public void run(LoadedMod mod, Fi file){
|
||||||
|
@ -42,7 +42,7 @@ public class IOSLauncher extends IOSApplication.Delegate{
|
|||||||
return new IOSApplication(new ClientLauncher(){
|
return new IOSApplication(new ClientLauncher(){
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void showFileChooser(boolean open, String extension, Cons<Fi> cons){
|
public void showFileChooser(boolean open, String titleIgn, String extension, Cons<Fi> cons){
|
||||||
if(!open){ //when exporting, just share it.
|
if(!open){ //when exporting, just share it.
|
||||||
//ask for export name
|
//ask for export name
|
||||||
Core.input.getTextInput(new TextInput(){{
|
Core.input.getTextInput(new TextInput(){{
|
||||||
|
Reference in New Issue
Block a user