Files
armorpaint/Sources/arm/io/ExportArm.hx
T

88 lines
2.3 KiB
Haxe
Raw Normal View History

2019-06-19 09:40:14 +02:00
package arm.io;
2019-06-20 11:23:01 +02:00
import haxe.Json;
import zui.Nodes;
2019-06-19 09:40:14 +02:00
import iron.data.SceneFormat;
2019-06-20 11:23:01 +02:00
import iron.system.ArmPack;
2019-06-19 09:40:14 +02:00
import arm.ui.UITrait;
2019-06-19 18:45:36 +02:00
import arm.ui.UINodes;
import arm.util.Lz4;
import arm.util.Path;
2019-06-19 22:42:17 +02:00
import arm.Project;
2019-06-20 12:30:31 +02:00
using StringTools;
2019-06-19 09:40:14 +02:00
class ExportArm {
public static function run(path:String) {
2019-06-23 14:50:41 +02:00
var raw:TSceneFormat = { mesh_datas: [ Context.paintObject.data.raw ] };
2019-06-20 11:23:01 +02:00
var b = ArmPack.encode(raw);
2019-06-20 12:30:31 +02:00
if (!path.endsWith(".arm")) path += ".arm";
2019-06-19 09:40:14 +02:00
#if kha_krom
Krom.fileSaveBytes(path, b.getData());
#end
}
2019-06-19 18:45:36 +02:00
public static function runProject() {
2019-06-20 11:23:01 +02:00
var mnodes:Array<TNodeCanvas> = [];
var bnodes:Array<TNodeCanvas> = [];
2019-06-19 18:45:36 +02:00
2019-06-23 14:50:41 +02:00
for (m in Project.materials) {
2019-06-20 11:23:01 +02:00
var c:TNodeCanvas = Json.parse(Json.stringify(UINodes.inst.canvasMap.get(m)));
2019-06-19 18:45:36 +02:00
for (n in c.nodes) {
if (n.type == "TEX_IMAGE") { // Convert image path from absolute to relative
2019-06-23 14:50:41 +02:00
var sameDrive = Project.filepath.charAt(0) == n.buttons[0].data.charAt(0);
2019-06-19 18:45:36 +02:00
if (sameDrive) {
2019-06-23 14:50:41 +02:00
n.buttons[0].data = Path.toRelative(Project.filepath, n.buttons[0].data);
2019-06-19 18:45:36 +02:00
}
}
}
mnodes.push(c);
}
2019-06-23 14:50:41 +02:00
for (b in Project.brushes) bnodes.push(UINodes.inst.canvasBrushMap.get(b));
2019-06-19 18:45:36 +02:00
var md:Array<TMeshData> = [];
2019-06-24 22:35:17 +02:00
for (p in Project.paintObjects) md.push(p.data.raw);
2019-06-19 18:45:36 +02:00
var asset_files:Array<String> = [];
2019-06-23 14:50:41 +02:00
for (a in Project.assets) {
2019-06-19 18:45:36 +02:00
// Convert image path from absolute to relative
2019-06-23 14:50:41 +02:00
var sameDrive = Project.filepath.charAt(0) == a.file.charAt(0);
2019-06-19 18:45:36 +02:00
if (sameDrive) {
2019-06-23 14:50:41 +02:00
asset_files.push(Path.toRelative(Project.filepath, a.file));
2019-06-19 18:45:36 +02:00
}
else {
asset_files.push(a.file);
}
}
var ld:Array<TLayerData> = [];
2019-06-23 14:50:41 +02:00
for (l in Project.layers) {
2019-06-19 18:45:36 +02:00
ld.push({
res: l.texpaint.width,
texpaint: Lz4.encode(l.texpaint.getPixels()),
texpaint_nor: Lz4.encode(l.texpaint_nor.getPixels()),
texpaint_pack: Lz4.encode(l.texpaint_pack.getPixels()),
texpaint_mask: l.texpaint_mask != null ? Lz4.encode(l.texpaint_mask.getPixels()) : null,
opacity_mask: l.maskOpacity,
2019-06-23 14:50:41 +02:00
material_mask: l.material_mask != null ? Project.materials.indexOf(l.material_mask) : -1,
2019-06-19 18:45:36 +02:00
object_mask: l.objectMask
});
}
2019-06-23 14:50:41 +02:00
Project.raw = {
2019-06-20 11:23:01 +02:00
version: App.version,
2019-06-19 18:45:36 +02:00
material_nodes: mnodes,
brush_nodes: bnodes,
mesh_datas: md,
layer_datas: ld,
assets: asset_files
};
2019-06-23 14:50:41 +02:00
var bytes = ArmPack.encode(Project.raw);
2019-06-19 18:45:36 +02:00
#if kha_krom
2019-06-23 14:50:41 +02:00
Krom.fileSaveBytes(Project.filepath, bytes.getData());
2019-06-19 18:45:36 +02:00
#end
}
2019-06-19 09:40:14 +02:00
}