Brush import export

This commit is contained in:
luboslenco
2020-03-13 23:27:00 +01:00
parent e624b0e6ea
commit 747c24d75f
5 changed files with 123 additions and 4 deletions
+32
View File
@@ -112,6 +112,38 @@ class ExportArm {
Krom.fileSaveBytes(path, bytes.getData());
}
public static function runBrush(path: String) {
var bnodes: Array<TNodeCanvas> = [];
var b = Context.brush;
var c: TNodeCanvas = Json.parse(Json.stringify(b.canvas));
var assets: Array<TAsset> = [];
for (n in c.nodes) {
if (n.type == "TEX_IMAGE") {
var index = n.buttons[0].default_value;
n.buttons[0].data = App.enumTexts(n.type)[index];
var asset = Project.assets[index];
if (assets.indexOf(asset) == -1) {
assets.push(asset);
}
}
}
bnodes.push(c);
var texture_files = assetsToFiles(assets);
var raw = {
version: App.version,
brush_nodes: bnodes,
brush_icons: [Lz4.encode(b.image.getPixels())],
assets: texture_files
};
var bytes = ArmPack.encode(raw);
if (!path.endsWith(".arm")) path += ".arm";
Krom.fileSaveBytes(path, bytes.getData());
}
static function assetsToFiles(assets: Array<TAsset>): Array<String> {
var texture_files: Array<String> = [];
for (a in assets) {
+46
View File
@@ -316,6 +316,52 @@ class ImportArm {
Data.deleteBlob(path);
}
public static function runBrush(path: String) {
Data.getBlob(path, function(b: Blob) {
var project: TProjectFormat = ArmPack.decode(b.toBytes());
if (project.version == null) { Data.deleteBlob(path); return; }
runBrushFromProject(project, path);
});
}
public static function runBrushFromProject(project: TProjectFormat, path: String) {
var base = Path.baseDir(path);
for (file in project.assets) {
#if krom_windows
file = file.replace("/", "\\");
#else
file = file.replace("\\", "/");
#end
// Convert image path from relative to absolute
var abs = Data.isAbsolute(file) ? file : base + file;
if (!File.exists(abs)) {
makePink(abs);
}
arm.io.ImportTexture.run(abs);
}
var imported: Array<BrushSlot> = [];
for (n in project.brush_nodes) {
initNodes(n.nodes);
Context.brush = new BrushSlot(n);
Project.brushes.push(Context.brush);
imported.push(Context.brush);
}
function makeBrushPreview(_) {
for (b in imported) {
Context.setBrush(b);
RenderUtil.makeBrushPreview();
}
iron.App.removeRender(makeBrushPreview);
}
iron.App.notifyOnRender(makeBrushPreview);
UITrait.inst.hwnd1.redraws = 2;
Data.deleteBlob(path);
}
static function makePink(abs: String) {
Log.error(Strings.error2 + abs);
var b = Bytes.alloc(4);