Files
armorpaint/Sources/arm/Project.hx
T

216 lines
6.0 KiB
Haxe
Raw Normal View History

2019-02-02 18:49:19 +01:00
package arm;
2019-06-20 11:23:01 +02:00
import kha.Window;
2019-06-19 22:42:17 +02:00
import zui.Nodes;
2019-06-23 14:50:41 +02:00
import zui.Canvas;
2019-02-02 18:49:19 +01:00
import iron.data.SceneFormat;
import iron.data.MeshData;
2019-06-20 11:23:01 +02:00
import iron.data.Data;
import iron.Scene;
2019-05-10 19:28:59 +02:00
import arm.util.MeshUtil;
import arm.util.RenderUtil;
import arm.util.ViewportUtil;
import arm.util.Path;
import arm.ui.UITrait;
import arm.ui.UINodes;
2019-05-12 17:33:58 +02:00
import arm.ui.UIFiles;
2019-06-19 22:42:17 +02:00
import arm.data.LayerSlot;
import arm.data.BrushSlot;
import arm.data.MaterialSlot;
2019-06-20 11:23:01 +02:00
import arm.nodes.MaterialParser;
import arm.io.ImportArm;
2019-06-20 12:30:31 +02:00
using StringTools;
2019-02-02 18:49:19 +01:00
class Project {
2019-06-23 14:50:41 +02:00
public static var raw:TProjectFormat;
public static var filepath = "";
public static var assets:Array<TAsset> = [];
public static var assetNames:Array<String> = [];
public static var assetId = 0;
public static var materials:Array<MaterialSlot> = null;
public static var materialsScene:Array<MaterialSlot> = null;
public static var brushes:Array<BrushSlot> = null;
public static var layers:Array<LayerSlot> = null;
2019-02-02 18:49:19 +01:00
public static function projectOpen() {
2019-06-20 11:23:01 +02:00
App.showFiles = true;
App.whandle.redraws = 2;
App.foldersOnly = false;
App.showFilename = false;
2019-05-12 17:33:58 +02:00
UIFiles.filters = "arm";
2019-06-20 11:23:01 +02:00
App.filesDone = function(path:String) {
2019-06-20 12:30:31 +02:00
if (!path.endsWith(".arm")) {
2019-06-17 21:05:15 +02:00
UITrait.inst.showError(Strings.error5);
2019-02-02 18:49:19 +01:00
return;
}
2019-02-27 22:34:24 +01:00
var current = @:privateAccess kha.graphics4.Graphics2.current;
if (current != null) current.end();
2019-06-20 11:23:01 +02:00
ImportArm.runProject(path);
2019-02-27 22:34:24 +01:00
if (current != null) current.begin(false);
2019-02-02 18:49:19 +01:00
};
}
public static function projectSave() {
2019-06-23 14:50:41 +02:00
if (filepath == "") {
2019-02-02 18:49:19 +01:00
projectSaveAs();
return;
}
2019-06-20 11:23:01 +02:00
Window.get(0).title = App.filenameHandle.text + " - ArmorPaint";
2019-02-02 18:49:19 +01:00
UITrait.inst.projectExport = true;
}
public static function projectSaveAs() {
2019-06-20 11:23:01 +02:00
App.showFiles = true;
App.whandle.redraws = 2;
App.foldersOnly = true;
App.showFilename = true;
2019-05-12 17:33:58 +02:00
UIFiles.filters = "arm";
2019-06-20 11:23:01 +02:00
App.filesDone = function(path:String) {
var f = App.filenameHandle.text;
2019-02-02 18:49:19 +01:00
if (f == "") f = "untitled";
2019-06-23 14:50:41 +02:00
filepath = path + "/" + f;
if (!filepath.endsWith(".arm")) filepath += ".arm";
2019-02-02 18:49:19 +01:00
projectSave();
};
}
public static function projectNew(resetLayers = true) {
2019-06-20 11:23:01 +02:00
Window.get(0).title = "ArmorPaint";
2019-06-23 14:50:41 +02:00
filepath = "";
if (Context.mergedObject != null) {
Context.mergedObject.remove();
Data.deleteMesh(Context.mergedObject.data.handle);
Context.mergedObject = null;
2019-02-02 18:49:19 +01:00
}
2019-02-26 13:28:53 +01:00
2019-06-13 08:29:21 +02:00
ViewportUtil.resetViewport();
2019-06-23 14:50:41 +02:00
Context.layerPreviewDirty = true;
2019-03-28 15:21:45 +01:00
LayerSlot.counter = 0;
2019-06-23 14:50:41 +02:00
Context.paintObject = Context.mainObject();
2019-02-26 13:28:53 +01:00
2019-06-23 14:50:41 +02:00
Context.selectPaintObject(Context.mainObject());
for (i in 1...Context.paintObjects.length) {
var p = Context.paintObjects[i];
if (p == Context.paintObject) continue;
2019-06-20 11:23:01 +02:00
Data.deleteMesh(p.data.handle);
2019-02-02 18:49:19 +01:00
p.remove();
}
2019-06-20 11:23:01 +02:00
var meshes = Scene.active.meshes;
2019-06-12 01:12:19 +02:00
var len = meshes.length;
for (i in 0...len) {
var m = meshes[len - i - 1];
if (UITrait.inst.projectObjects.indexOf(m) == -1) {
2019-06-20 11:23:01 +02:00
Data.deleteMesh(m.data.handle);
2019-06-12 01:12:19 +02:00
m.remove();
}
}
2019-06-23 14:50:41 +02:00
var handle = Context.paintObject.data.handle;
2019-05-15 11:26:06 +02:00
if (handle != "SceneSphere" && handle != "ScenePlane") {
2019-06-20 11:23:01 +02:00
Data.deleteMesh(handle);
2019-03-24 15:44:25 +01:00
}
2019-05-21 22:28:08 +02:00
2019-06-19 12:03:32 +02:00
if (UITrait.inst.projectType > 0) {
var mesh:Dynamic = UITrait.inst.projectType == 1 ?
new iron.format.proc.Sphere(1, 512, 256) :
new iron.format.proc.Plane(1, 1, 512, 512);
2019-06-13 08:29:21 +02:00
var raw = {
2019-06-19 12:03:32 +02:00
name: "Tesselated",
2019-06-13 08:29:21 +02:00
vertex_arrays: [
{ values: mesh.posa, attrib: "pos" },
{ values: mesh.nora, attrib: "nor" },
{ values: mesh.texa, attrib: "tex" }
],
index_arrays: [
{ values: mesh.inda, material: 0 }
],
scale_pos: mesh.scalePos,
scale_tex: mesh.scaleTex
};
var md = new MeshData(raw, function(md:MeshData) {});
2019-06-20 11:23:01 +02:00
Data.cachedMeshes.set("SceneTesselated", md);
2019-06-13 08:29:21 +02:00
2019-06-19 12:03:32 +02:00
if (UITrait.inst.projectType == 2) {
ViewportUtil.setView(0, 0, 1, 0, 0, 0); // Top
ViewportUtil.orbit(0, Math.PI / 6); // Orbit down
}
2019-06-13 08:29:21 +02:00
}
2019-06-19 12:03:32 +02:00
var n = UITrait.inst.projectType == 0 ? "Cube" : "Tesselated";
2019-06-20 11:23:01 +02:00
Data.getMesh("Scene", n, function(md:MeshData) {
2019-02-12 16:34:33 +01:00
2019-02-27 22:34:24 +01:00
var current = @:privateAccess kha.graphics4.Graphics2.current;
if (current != null) current.end();
2019-02-12 16:34:33 +01:00
2019-03-21 11:15:43 +01:00
UITrait.inst.pickerMaskHandle.position = 0;
2019-06-23 14:50:41 +02:00
Context.paintObject.setData(md);
Context.paintObject.transform.scale.set(1, 1, 1);
Context.paintObject.transform.buildMatrix();
Context.paintObject.name = n;
Context.paintObjects = [Context.paintObject];
2019-06-20 11:23:01 +02:00
Data.getMaterial("Scene", "Material", function(m:iron.data.MaterialData) {
2019-06-23 14:50:41 +02:00
materials = [new MaterialSlot(m)];
2019-03-05 13:30:11 +01:00
});
2019-06-23 14:50:41 +02:00
Context.material = materials[0];
2019-02-02 18:49:19 +01:00
UINodes.inst.canvasMap = new Map();
UINodes.inst.canvasBrushMap = new Map();
2019-06-23 14:50:41 +02:00
brushes = [new BrushSlot()];
Context.brush = brushes[0];
2019-05-11 15:59:56 +02:00
History.reset();
2019-02-02 18:49:19 +01:00
UINodes.inst.updateCanvasMap();
2019-06-20 11:23:01 +02:00
MaterialParser.parsePaintMaterial();
2019-02-02 18:49:19 +01:00
RenderUtil.makeMaterialPreview();
2019-06-23 14:50:41 +02:00
for (a in assets) Data.deleteImage(a.file);
assets = [];
assetNames = [];
assetId = 0;
Context.ddirty = 4;
2019-03-21 11:15:43 +01:00
UITrait.inst.hwnd.redraws = 2;
2019-03-27 14:26:51 +01:00
UITrait.inst.hwnd1.redraws = 2;
UITrait.inst.hwnd2.redraws = 2;
2019-02-12 16:34:33 +01:00
2019-06-13 08:29:21 +02:00
if (resetLayers) {
// for (l in layers) l.unload();
var layer = new LayerSlot();
2019-06-23 14:50:41 +02:00
layers = [layer];
Context.setLayer(layer);
2019-06-13 08:29:21 +02:00
if (UITrait.inst.projectType == 1) {
2019-06-23 14:50:41 +02:00
layer.material_mask = materials[0];
2019-06-13 08:29:21 +02:00
Layers.updateFillLayers(4);
}
else {
iron.App.notifyOnRender(Layers.initLayers);
}
}
2019-02-27 22:34:24 +01:00
if (current != null) current.begin(false);
2019-02-02 18:49:19 +01:00
});
}
}
2019-06-19 22:42:17 +02:00
typedef TProjectFormat = {
public var version:String;
public var brush_nodes:Array<TNodeCanvas>;
public var material_nodes:Array<TNodeCanvas>;
public var assets:Array<String>;
public var layer_datas:Array<TLayerData>;
public var mesh_datas:Array<TMeshData>;
}
typedef TLayerData = {
public var res:Int;
public var texpaint:haxe.io.Bytes;
public var texpaint_nor:haxe.io.Bytes;
public var texpaint_pack:haxe.io.Bytes;
public var texpaint_mask:haxe.io.Bytes;
public var opacity_mask:Float;
public var material_mask:Int;
public var object_mask:Int;
}