From 7e18d737e2916875e5d69f6713e6af9ee2f968b7 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Mon, 11 Nov 2019 15:35:00 +0100 Subject: [PATCH] Cleanup --- Sources/Main.hx | 2 + Sources/arm/App.hx | 38 ++----- Sources/arm/Context.hx | 19 +--- Sources/arm/History.hx | 15 ++- Sources/arm/Layers.hx | 55 +--------- Sources/arm/Project.hx | 9 +- Sources/arm/Tool.hx | 103 ------------------ Sources/arm/data/LayerSlot.hx | 49 +++++++++ Sources/arm/io/ImportFolder.hx | 19 ++-- Sources/arm/io/ImportPlugin.hx | 2 +- Sources/arm/io/ImportTexture.hx | 2 +- Sources/arm/io/Importer.hx | 14 +-- Sources/arm/ui/TabBrowser.hx | 2 +- Sources/arm/ui/TabLayers.hx | 8 +- Sources/arm/ui/TabMaterials.hx | 3 +- Sources/arm/ui/TabOutliner.hx | 2 +- Sources/arm/ui/UIFiles.hx | 5 +- Sources/arm/ui/UIMenu.hx | 9 +- Sources/arm/ui/UINodes.hx | 4 +- Sources/arm/ui/UITrait.hx | 8 +- Sources/arm/{Macro.hx => util/BuildMacros.hx} | 40 +++---- Sources/arm/util/MaterialUtil.hx | 13 +++ Sources/arm/util/ParticleUtil.hx | 95 ++++++++++++++++ Sources/arm/util/Path.hx | 42 +++++-- Sources/arm/util/RenderUtil.hx | 10 +- 25 files changed, 289 insertions(+), 279 deletions(-) rename Sources/arm/{Macro.hx => util/BuildMacros.hx} (71%) create mode 100644 Sources/arm/util/MaterialUtil.hx create mode 100644 Sources/arm/util/ParticleUtil.hx diff --git a/Sources/Main.hx b/Sources/Main.hx index 2e894f1b..9e1ada92 100644 --- a/Sources/Main.hx +++ b/Sources/Main.hx @@ -11,7 +11,9 @@ import arm.render.Inc; import arm.render.RenderPathDeferred; import arm.render.Uniforms; import arm.Config; +#if arm_player using StringTools; +#end class Main { diff --git a/Sources/arm/App.hx b/Sources/arm/App.hx index 9d5e0a34..af9a11ea 100644 --- a/Sources/arm/App.hx +++ b/Sources/arm/App.hx @@ -65,7 +65,7 @@ class App { System.notifyOnDropFiles(function(filePath:String) { #if krom_windows - if (!checkAscii(filePath)) filePath = shortPath(filePath); + if (!Path.isAscii(filePath)) filePath = Path.shortPath(filePath); #end dropPath = filePath; #if krom_linux @@ -126,10 +126,10 @@ class App { // File to open passed as argument if (Krom.getArgCount() > 1) { var path = Krom.getArg(1); - if (Path.checkProjectFormat(path) || - Path.checkMeshFormat(path) || - Path.checkTextureFormat(path) || - Path.checkFontFormat(path)) { + if (Path.isProject(path) || + Path.isMesh(path) || + Path.isTexture(path) || + Path.isFont(path)) { fileArg = path; } } @@ -139,11 +139,11 @@ class App { new UINodes(); new UIView2D(); new Camera(); - iron.App.notifyOnRender2D(@:privateAccess UITrait.inst.renderCursor); - iron.App.notifyOnUpdate(@:privateAccess UINodes.inst.update); - iron.App.notifyOnRender2D(@:privateAccess UINodes.inst.render); - iron.App.notifyOnUpdate(@:privateAccess UITrait.inst.update); - iron.App.notifyOnRender2D(@:privateAccess UITrait.inst.render); + iron.App.notifyOnRender2D(UITrait.inst.renderCursor); + iron.App.notifyOnUpdate(UINodes.inst.update); + iron.App.notifyOnRender2D(UINodes.inst.render); + iron.App.notifyOnUpdate(UITrait.inst.update); + iron.App.notifyOnRender2D(UITrait.inst.render); iron.App.notifyOnRender2D(render); appx = UITrait.inst.toolbarw; appy = UITrait.inst.headerh * 2; @@ -155,10 +155,10 @@ class App { #end if (fileArg != "") { Importer.importFile(fileArg); - // if (Path.checkMeshFormat(fileArg)) { + // if (Path.isMesh(fileArg)) { // UITrait.inst.toggleDistractFree(); // } - // else if (Path.checkTextureFormat(fileArg)) { + // else if (Path.isTexture(fileArg)) { // UITrait.inst.show2DView(1); // } } @@ -428,18 +428,4 @@ class App { } return 0; } - - #if krom_windows - public static function checkAscii(s:String):Bool { - for (i in 0...s.length) if (s.charCodeAt(i) > 127) return false; - return true; - } - - public static function shortPath(s:String):String { - var cmd = 'for %I in ("' + s + '") do echo %~sI'; - var save = Krom.getFilesLocation() + "\\data\\tmp.txt"; - Krom.sysCommand(cmd + ' > "' + save + '"'); - return haxe.io.Bytes.ofData(Krom.loadBlob(save)).toString().rtrim(); - } - #end } diff --git a/Sources/arm/Context.hx b/Sources/arm/Context.hx index 98b179c9..d66124c5 100644 --- a/Sources/arm/Context.hx +++ b/Sources/arm/Context.hx @@ -8,6 +8,7 @@ import arm.data.LayerSlot; import arm.data.BrushSlot; import arm.util.UVUtil; import arm.util.RenderUtil; +import arm.util.ParticleUtil; import arm.ui.UITrait; import arm.ui.UINodes; import arm.ui.UIFiles; @@ -118,7 +119,7 @@ class Context { } if (tool == ToolParticle) { - Tool.initParticle(); + ParticleUtil.initParticle(); MaterialParser.parseParticleMaterial(); } } @@ -159,20 +160,4 @@ class Context { for (po in Project.paintObjects) if (po.children.length > 0) return po; return Project.paintObjects[0]; } - - public static function removeMaterialCache() { - Data.cachedMaterials.remove("SceneMaterial2"); - Data.cachedShaders.remove("Material2_data"); - Data.cachedSceneRaws.remove("Material2_data"); - // Data.cachedBlobs.remove("Material2_data.arm"); - } - - public static function importMesh() { - UIFiles.show = true; - UIFiles.isSave = false; - UIFiles.filters = "obj,fbx,stl,blend,arm"; - UIFiles.filesDone = function(path:String) { - Importer.importFile(path); - } - } } diff --git a/Sources/arm/History.hx b/Sources/arm/History.hx index 8b5d2eb2..5f7ef2fa 100644 --- a/Sources/arm/History.hx +++ b/Sources/arm/History.hx @@ -1,6 +1,5 @@ package arm; -import iron.RenderPath; import arm.ui.UITrait; import arm.ui.UIView2D; import arm.ui.UIFiles; @@ -23,7 +22,7 @@ class History { if (step.name == "New Layer") { Context.layer = Project.layers[step.layer]; - Layers.deleteSelectedLayer(); + Context.layer.delete(); } else if (step.name == "Delete Layer") { var l = Layers.newLayer(false); @@ -43,7 +42,7 @@ class History { } else if (step.name == "Duplicate Layer") { Context.layer = Project.layers[step.layer + 1]; - Layers.deleteSelectedLayer(); + Context.layer.delete(); } else if (step.name == "Order Layers") { var target = Project.layers[step.prev_order]; @@ -52,7 +51,7 @@ class History { } else if (step.name == "Merge Layers") { Context.layer = Project.layers[step.layer]; - Layers.deleteSelectedLayer(); + Context.layer.delete(); Context.layer = Layers.newLayer(false); Project.layers.insert(step.layer, Project.layers.pop()); @@ -94,7 +93,7 @@ class History { } else if (step.name == "Apply Mask") { Context.layer = Project.layers[step.layer]; - Layers.deleteSelectedLayer(); + Context.layer.delete(); Context.layer = Layers.newLayer(false); Project.layers.insert(step.layer, Project.layers.pop()); @@ -108,7 +107,7 @@ class History { Context.setLayer(Context.layer, true); } else if (step.name == "To Fill Layer") { - Layers.toPaintLayer(Context.layer); + Context.layer.toPaintLayer(); undoI = undoI - 1 < 0 ? Config.raw.undo_steps - 1 : undoI - 1; var lay = undoLayers[undoI]; Context.layer.swap(lay); @@ -161,7 +160,7 @@ class History { else if (step.name == "Delete Layer") { Context.layer = Project.layers[step.layer]; swapActive(); - Layers.deleteSelectedLayer(); + Context.layer.delete(); } else if (step.name == "Duplicate Layer") { Context.layer = Project.layers[step.layer]; @@ -211,7 +210,7 @@ class History { undoI = (undoI + 1) % Config.raw.undo_steps; } else if (step.name == "To Paint Layer") { - Layers.toPaintLayer(Context.layer); + Context.layer.toPaintLayer(); var lay = undoLayers[undoI]; Context.layer.swap(lay); undoI = (undoI + 1) % Config.raw.undo_steps; diff --git a/Sources/arm/Layers.hx b/Sources/arm/Layers.hx index c6874180..b4b1a90b 100644 --- a/Sources/arm/Layers.hx +++ b/Sources/arm/Layers.hx @@ -79,30 +79,6 @@ class Layers { Context.ddirty = 3; } - public static function clearSelectedLayer(g:kha.graphics4.Graphics) { - g.end(); - - var l = Context.layer; - l.texpaint.g4.begin(); - l.texpaint.g4.clear(kha.Color.fromFloats(0.0, 0.0, 0.0, 0.0)); // Base - l.texpaint.g4.end(); - - l.texpaint_nor.g4.begin(); - l.texpaint_nor.g4.clear(kha.Color.fromFloats(0.5, 0.5, 1.0, 0.0)); // Nor - l.texpaint_nor.g4.end(); - - l.texpaint_pack.g4.begin(); - l.texpaint_pack.g4.clear(kha.Color.fromFloats(1.0, 0.0, 0.0, 0.0)); // Occ, rough, met - l.texpaint_pack.g4.end(); - - g.begin(); - iron.App.removeRender(clearSelectedLayer); - - #if krom_linux - Context.layerPreviewDirty = true; - #end - } - public static function resizeLayers(g:kha.graphics4.Graphics) { var C = Config.raw; if (UITrait.inst.resHandle.position >= 4) { // Save memory for >=16k @@ -143,14 +119,6 @@ class Layers { iron.App.removeRender(setLayerBits); } - public static function deleteSelectedLayer() { - Context.layer.unload(); - var lpos = Project.layers.indexOf(Context.layer); - Project.layers.remove(Context.layer); - // Undo can remove base layer and then restore it from undo layers - if (lpos > 0) Context.setLayer(Project.layers[lpos - 1]); - } - public static function makePipe() { pipe = new PipelineState(); pipe.vertexShader = VertexShader.fromSource(ConstData.layerMergeVert); @@ -304,7 +272,7 @@ class Layers { g.begin(); - deleteSelectedLayer(); + Context.layer.delete(); iron.App.removeRender(mergeSelectedLayer); Context.setLayer(l0); Context.layerPreviewDirty = true; @@ -387,28 +355,11 @@ class Layers { var l = new LayerSlot(); Project.layers.push(l); Context.setLayer(l); - if (clear) iron.App.notifyOnRender(Layers.clearSelectedLayer); + if (clear) iron.App.notifyOnRender(l.clear); Context.layerPreviewDirty = true; return l; } - public static function toFillLayer(l:LayerSlot) { - Context.setLayer(l); - l.material_mask = Context.material; - Layers.updateFillLayers(4); - MaterialParser.parsePaintMaterial(); - Context.layerPreviewDirty = true; - UITrait.inst.hwnd.redraws = 2; - } - - public static function toPaintLayer(l:LayerSlot) { - Context.setLayer(l); - l.material_mask = null; - MaterialParser.parsePaintMaterial(); - Context.layerPreviewDirty = true; - UITrait.inst.hwnd.redraws = 2; - } - public static function createFillLayer() { function makeFill(g:kha.graphics4.Graphics) { g.end(); @@ -416,7 +367,7 @@ class Layers { History.newLayer(); l.objectMask = UITrait.inst.layerFilter; History.toFillLayer(); - toFillLayer(l); + l.toFillLayer(); g.begin(); iron.App.removeRender(makeFill); } diff --git a/Sources/arm/Project.hx b/Sources/arm/Project.hx index 4795164b..16262baa 100644 --- a/Sources/arm/Project.hx +++ b/Sources/arm/Project.hx @@ -9,10 +9,8 @@ import iron.data.MeshData; import iron.data.Data; import iron.object.MeshObject; import iron.Scene; -import arm.util.MeshUtil; import arm.util.RenderUtil; import arm.util.ViewportUtil; -import arm.util.Path; import arm.ui.UITrait; import arm.ui.UINodes; import arm.ui.UIFiles; @@ -266,7 +264,12 @@ class Project { if (ui.button("Import") || ui.isReturnDown) { UIBox.show = false; App.redrawUI(); - Context.importMesh(); + UIFiles.show = true; + UIFiles.isSave = false; + UIFiles.filters = "obj,fbx,stl,blend,arm"; + UIFiles.filesDone = function(path:String) { + Importer.importFile(path); + } } } }); diff --git a/Sources/arm/Tool.hx b/Sources/arm/Tool.hx index 76932019..5513e16b 100644 --- a/Sources/arm/Tool.hx +++ b/Sources/arm/Tool.hx @@ -1,108 +1,5 @@ package arm; -import haxe.Json; -import kha.Font; -import kha.arrays.Float32Array; -import iron.RenderPath; -import iron.Scene; -import iron.data.SceneFormat; -import iron.data.MaterialData; -import iron.object.Object; -import iron.object.MeshObject; -import arm.ui.UITrait; -import arm.io.Importer; - -class Tool { - - static function f32(ar:Array):Float32Array { - var res = new Float32Array(ar.length); - for (i in 0...ar.length) res[i] = ar[i]; - return res; - } - - public static function initParticle() { - if (UITrait.inst.particleMaterial != null) return; - - var raw:TParticleData = { - name: "Particles", - type: 0, - loop: false, - render_emitter: false, - count: 1000, - frame_start: 0, - frame_end: 1000, - lifetime: 400, - lifetime_random: 0.5, - emit_from: 1, - object_align_factor: f32([0, 0, -40]), - factor_random: 2.0, - physics_type: 0, - particle_size: 1.0, - size_random: 0, - mass: 1, - instance_object: ".Particle", - weight_gravity: 1 - }; - Scene.active.raw.particle_datas = [raw]; - var particle_refs:Array = [ - { - name: "Particles", - particle: "Particles", - seed: 0 - } - ]; - - { - var t = new RenderTargetRaw(); - t.name = "texparticle"; - t.width = 0; - t.height = 0; - t.format = 'R8'; - t.scale = arm.render.Inc.getSuperSampling(); - RenderPath.active.createRenderTarget(t); - } - - for (mat in Scene.active.raw.material_datas) { - if (mat.name == 'Material2') { - var m:TMaterialData = Json.parse(Json.stringify(mat)); - m.name = 'MaterialParticle'; - Scene.active.raw.material_datas.push(m); - break; - } - } - - iron.data.Data.getMaterial("Scene", "MaterialParticle", function(md:MaterialData) { - UITrait.inst.particleMaterial = md; - - for (obj in Scene.active.raw.objects) { - if (obj.name == '.Sphere') { - var particle:TObj = Json.parse(Json.stringify(obj)); - particle.name = '.Particle'; - particle.is_particle = true; - particle.material_refs = ['MaterialParticle']; - Scene.active.raw.objects.push(particle); - for (i in 0...16) particle.transform.values[i] *= 0.01; - break; - } - } - - Scene.active.spawnObject(".Sphere", null, function(o:Object) { - var mo:MeshObject = cast o; - mo.name = ".ParticleEmitter"; - mo.raw.particle_refs = particle_refs; - mo.setupParticleSystem("Scene", particle_refs[0]); - }); - }); - } - - @:access(zui.Zui) - public static function getTextToolFont():Font { - var fontName = Importer.fontList[UITrait.inst.textToolHandle.position]; - if (fontName == 'default.ttf') return UITrait.inst.ui.ops.font; - return Importer.fontMap.get(fontName); - } -} - @:enum abstract PaintTool(Int) from Int to Int { var ToolBrush = 0; var ToolEraser = 1; diff --git a/Sources/arm/data/LayerSlot.hx b/Sources/arm/data/LayerSlot.hx index cc4aa278..79b974ee 100644 --- a/Sources/arm/data/LayerSlot.hx +++ b/Sources/arm/data/LayerSlot.hx @@ -5,6 +5,7 @@ import kha.Image; import iron.RenderPath; import iron.object.MeshObject; import arm.ui.UITrait; +import arm.nodes.MaterialParser; class LayerSlot { public var id = 0; @@ -104,6 +105,14 @@ class LayerSlot { texpaint_preview = Image.createRenderTarget(200, 200, TextureFormat.RGBA32); } + public function delete() { + unload(); + var lpos = Project.layers.indexOf(this); + Project.layers.remove(this); + // Undo can remove base layer and then restore it from undo layers + if (lpos > 0) Context.setLayer(Project.layers[lpos - 1]); + } + public function unload() { texpaint.unload(); texpaint_nor.unload(); @@ -325,4 +334,44 @@ class LayerSlot { rts.get("texpaint_mask" + this.ext).image = this.texpaint_mask; } } + + public function clear(g:kha.graphics4.Graphics) { + g.end(); + + texpaint.g4.begin(); + texpaint.g4.clear(kha.Color.fromFloats(0.0, 0.0, 0.0, 0.0)); // Base + texpaint.g4.end(); + + texpaint_nor.g4.begin(); + texpaint_nor.g4.clear(kha.Color.fromFloats(0.5, 0.5, 1.0, 0.0)); // Nor + texpaint_nor.g4.end(); + + texpaint_pack.g4.begin(); + texpaint_pack.g4.clear(kha.Color.fromFloats(1.0, 0.0, 0.0, 0.0)); // Occ, rough, met + texpaint_pack.g4.end(); + + g.begin(); + iron.App.removeRender(clear); + + #if krom_linux + Context.layerPreviewDirty = true; + #end + } + + public function toFillLayer() { + Context.setLayer(this); + material_mask = Context.material; + Layers.updateFillLayers(4); + MaterialParser.parsePaintMaterial(); + Context.layerPreviewDirty = true; + UITrait.inst.hwnd.redraws = 2; + } + + public function toPaintLayer() { + Context.setLayer(this); + material_mask = null; + MaterialParser.parsePaintMaterial(); + Context.layerPreviewDirty = true; + UITrait.inst.hwnd.redraws = 2; + } } diff --git a/Sources/arm/io/ImportFolder.hx b/Sources/arm/io/ImportFolder.hx index 6e3f469c..1243d18c 100644 --- a/Sources/arm/io/ImportFolder.hx +++ b/Sources/arm/io/ImportFolder.hx @@ -7,6 +7,7 @@ import iron.data.MaterialData; import arm.ui.UITrait; import arm.ui.UINodes; import arm.util.RenderUtil; +import arm.util.MaterialUtil; import arm.util.Path; import arm.nodes.NodesMaterial; import arm.nodes.MaterialParser; @@ -44,7 +45,7 @@ class ImportFolder { for (f in files) { if (f.length == 0) continue; f = f.rtrim(); - if (!Path.checkTextureFormat(f)) continue; + if (!Path.isTexture(f)) continue; f = path + sep + f; #if krom_windows @@ -55,31 +56,31 @@ class ImportFolder { var base = f.substr(0, f.lastIndexOf(".")).toLowerCase(); var valid = false; - if (mapbase == "" && Path.checkBaseTex(base)) { + if (mapbase == "" && Path.isBaseTex(base)) { mapbase = f; valid = true; } - if (mapopac == "" && Path.checkOpacTex(base)) { + if (mapopac == "" && Path.isOpacTex(base)) { mapopac = f; valid = true; } - if (mapnor == "" && Path.checkNorTex(base)) { + if (mapnor == "" && Path.isNorTex(base)) { mapnor = f; valid = true; } - if (mapocc == "" && Path.checkOccTex(base)) { + if (mapocc == "" && Path.isOccTex(base)) { mapocc = f; valid = true; } - if (maprough == "" && Path.checkRoughTex(base)) { + if (maprough == "" && Path.isRoughTex(base)) { maprough = f; valid = true; } - if (mapmet == "" && Path.checkMetTex(base)) { + if (mapmet == "" && Path.isMetTex(base)) { mapmet = f; valid = true; } - if (mapheight == "" && Path.checkDispTex(base)) { + if (mapheight == "" && Path.isDispTex(base)) { mapheight = f; valid = true; } @@ -89,7 +90,7 @@ class ImportFolder { // Create material var isScene = UITrait.inst.worktab.position == SpaceScene; if (isScene) { - Context.removeMaterialCache(); + MaterialUtil.removeMaterialCache(); Data.getMaterial("Scene", "Material2", function(md:MaterialData) { Context.materialScene = new MaterialSlot(md); Project.materialsScene.push(Context.materialScene); diff --git a/Sources/arm/io/ImportPlugin.hx b/Sources/arm/io/ImportPlugin.hx index a22b90aa..44af7d39 100644 --- a/Sources/arm/io/ImportPlugin.hx +++ b/Sources/arm/io/ImportPlugin.hx @@ -7,7 +7,7 @@ using StringTools; class ImportPlugin { public static function run(path:String) { - if (!Path.checkPluginFormat(path)) { + if (!Path.isPlugin(path)) { Log.showError(Strings.error1); return; } diff --git a/Sources/arm/io/ImportTexture.hx b/Sources/arm/io/ImportTexture.hx index 8fce9aa2..0cf6462b 100644 --- a/Sources/arm/io/ImportTexture.hx +++ b/Sources/arm/io/ImportTexture.hx @@ -10,7 +10,7 @@ using StringTools; class ImportTexture { public static function run(path:String) { - if (!Path.checkTextureFormat(path)) { + if (!Path.isTexture(path)) { Log.showError(Strings.error1); return; } diff --git a/Sources/arm/io/Importer.hx b/Sources/arm/io/Importer.hx index 44566a1d..51163703 100644 --- a/Sources/arm/io/Importer.hx +++ b/Sources/arm/io/Importer.hx @@ -26,11 +26,11 @@ class Importer { public static function importFile(path:String, dropX = -1.0, dropY = -1.0) { // Mesh - if (Path.checkMeshFormat(path)) { + if (Path.isMesh(path)) { importMesh(path); } // Image - else if (Path.checkTextureFormat(path)) { + else if (Path.isTexture(path)) { ImportTexture.run(path); // Place image node var x0 = UINodes.inst.wx; @@ -42,19 +42,19 @@ class Importer { } } // Font - else if (Path.checkFontFormat(path)) { + else if (Path.isFont(path)) { ImportFont.run(path); } // Project - else if (Path.checkProjectFormat(path)) { + else if (Path.isProject(path)) { ImportArm.runProject(path); } // Plugin - else if (Path.checkPluginFormat(path)) { + else if (Path.isPlugin(path)) { ImportPlugin.run(path); } // Folder - else if (path.indexOf(".") == -1) { + else if (Path.isFolder(path)) { ImportFolder.run(path); } else { @@ -63,7 +63,7 @@ class Importer { } public static function importMesh(path:String, _clearLayers = true) { - if (!Path.checkMeshFormat(path)) { + if (!Path.isMesh(path)) { Log.showError(Strings.error1); return; } diff --git a/Sources/arm/ui/TabBrowser.hx b/Sources/arm/ui/TabBrowser.hx index 691d1d3b..e36689f8 100644 --- a/Sources/arm/ui/TabBrowser.hx +++ b/Sources/arm/ui/TabBrowser.hx @@ -9,7 +9,7 @@ class TabBrowser { @:access(zui.Zui) public static function draw() { var ui = UITrait.inst.ui; - if (ui.tab(UITrait.inst.htab, "Browser")) { + if (ui.tab(UITrait.inst.htab2, "Browser")) { var h = Id.handle(); h.text = ui.textInput(h, "Path"); Ext.fileBrowser(ui, h); diff --git a/Sources/arm/ui/TabLayers.hx b/Sources/arm/ui/TabLayers.hx index d2602e18..592c6f8f 100644 --- a/Sources/arm/ui/TabLayers.hx +++ b/Sources/arm/ui/TabLayers.hx @@ -190,7 +190,7 @@ class TabLayers { function makeFill(g:kha.graphics4.Graphics) { g.end(); History.toFillLayer(); - Layers.toFillLayer(l); + l.toFillLayer(); g.begin(); iron.App.removeRender(makeFill); } @@ -200,7 +200,7 @@ class TabLayers { function makePaint(g:kha.graphics4.Graphics) { g.end(); History.toPaintLayer(); - Layers.toPaintLayer(l); + l.toPaintLayer(); g.begin(); iron.App.removeRender(makePaint); } @@ -213,7 +213,7 @@ class TabLayers { if (ui.button("Delete", Left)) { Context.layer = l; History.deleteLayer(); - Layers.deleteSelectedLayer(); + l.delete(); } if (ui.button("Move Up", Left)) { if (i < Project.layers.length - 1) { @@ -339,7 +339,7 @@ class TabLayers { if (h.changed) { Context.setLayer(l); if (l.material_mask != null) { // Fill layer - iron.App.notifyOnRender(Layers.clearSelectedLayer); + iron.App.notifyOnRender(l.clear); iron.App.notifyOnRender(function(_){ Layers.updateFillLayers(4); }); diff --git a/Sources/arm/ui/TabMaterials.hx b/Sources/arm/ui/TabMaterials.hx index cbf01745..7ce5c70b 100644 --- a/Sources/arm/ui/TabMaterials.hx +++ b/Sources/arm/ui/TabMaterials.hx @@ -10,6 +10,7 @@ import iron.data.Data; import arm.nodes.MaterialParser; import arm.data.MaterialSlot; import arm.util.RenderUtil; +import arm.util.MaterialUtil; import arm.io.ImportBlend; import arm.io.ImportArm; import arm.io.Exporter; @@ -31,7 +32,7 @@ class TabMaterials { if (ui.button("New")) { if (isScene) { if (Context.object != Context.paintObject && Std.is(Context.object, MeshObject)) { - Context.removeMaterialCache(); + MaterialUtil.removeMaterialCache(); Data.getMaterial("Scene", "Material2", function(md:iron.data.MaterialData) { ui.g.end(); md.name = "Material2." + materials.length; diff --git a/Sources/arm/ui/TabOutliner.hx b/Sources/arm/ui/TabOutliner.hx index f2bb2c33..0d52882d 100644 --- a/Sources/arm/ui/TabOutliner.hx +++ b/Sources/arm/ui/TabOutliner.hx @@ -13,7 +13,7 @@ class TabOutliner { var ui = UITrait.inst.ui; if (ui.tab(UITrait.inst.htab, "Outliner")) { ui.row([1/4]); - if (ui.button("Import")) Context.importMesh(); + if (ui.button("Import")) Project.importMesh(); var i = 0; function drawList(h:Handle, o:Object) { diff --git a/Sources/arm/ui/UIFiles.hx b/Sources/arm/ui/UIFiles.hx index e06cc030..3b77695c 100644 --- a/Sources/arm/ui/UIFiles.hx +++ b/Sources/arm/ui/UIFiles.hx @@ -19,6 +19,7 @@ class UIFiles { show = false; if (!UITrait.inst.nativeBrowser) { + if (path == null) path = '/'; renderCustom(g); return; } @@ -26,7 +27,7 @@ class UIFiles { path = isSave ? Krom.saveDialog(filters, "") : Krom.openDialog(filters, ""); if (path != null) { #if krom_windows - if (!App.checkAscii(path)) path = App.shortPath(path); + if (!Path.isAscii(path)) path = Path.shortPath(path); #end path = path.replace("\\\\", "\\"); path = path.replace("\r", ""); @@ -51,7 +52,7 @@ class UIFiles { ui.row([6/10, 2/10, 2/10]); filename = ui.textInput(fileHandle, "File"); ui.text('*.' + filters, Center); - var known = Path.checkTextureFormat(path) || Path.checkMeshFormat(path) || Path.checkProjectFormat(path); + var known = Path.isTexture(path) || Path.isMesh(path) || Path.isProject(path); if (ui.button(isSave ? "Save" : "Open") || known || ui.isReturnDown) { UIBox.show = false; #if krom_windows diff --git a/Sources/arm/ui/UIMenu.hx b/Sources/arm/ui/UIMenu.hx index c1292a22..60267633 100644 --- a/Sources/arm/ui/UIMenu.hx +++ b/Sources/arm/ui/UIMenu.hx @@ -7,6 +7,7 @@ import zui.Zui; import iron.system.Input; import iron.data.Data; import arm.util.ViewportUtil; +import arm.util.BuildMacros; using StringTools; class UIMenu { @@ -144,7 +145,7 @@ class UIMenu { } if (ui.button("Report Bug", Left)) { var ver = App.version; - var sha = Macro.buildSha(); + var sha = BuildMacros.sha(); sha = sha.substr(1, sha.length - 2); var os = System.systemId; var url = "https://github.com/armory3d/armorpaint/issues/new?labels=bug&template=bug_report.md&body=*ArmorPaint%20" + ver + "-" + sha + ",%20" + os + "*"; @@ -171,7 +172,7 @@ class UIMenu { var update = Json.parse(blob.toString()); var updateVersion = Std.int(update.version); if (updateVersion > 0) { - var date = Macro.buildDate().split(" ")[0].substr(2); // 2019 -> 19 + var date = BuildMacros.date().split(" ")[0].substr(2); // 2019 -> 19 var dateInt = Std.parseInt(date.replace("-", "")); if (updateVersion > dateInt) { UIBox.showMessage("Update", "Update is available!\nPlease visit armorpaint.org to download."); @@ -184,9 +185,9 @@ class UIMenu { }); } if (ui.button("About...", Left)) { - var sha = Macro.buildSha(); + var sha = BuildMacros.sha(); sha = sha.substr(1, sha.length - 2); - var date = Macro.buildDate().split(" ")[0]; + var date = BuildMacros.date().split(" ")[0]; var gapi = #if (kha_direct3d11) "Direct3D11" #elseif (kha_direct3d12) "Direct3D12" #else "OpenGL" #end; var msg = "ArmorPaint.org - v" + App.version + " (" + date + ") - " + sha + "\n"; msg += System.systemId + " - " + gapi; diff --git a/Sources/arm/ui/UINodes.hx b/Sources/arm/ui/UINodes.hx index fea13335..28ca76a0 100644 --- a/Sources/arm/ui/UINodes.hx +++ b/Sources/arm/ui/UINodes.hx @@ -154,7 +154,7 @@ class UINodes { else return canvasBrush; } - function update() { + public function update() { updateCanvasMap(); updateCanvasBrushMap(); @@ -318,7 +318,7 @@ class UINodes { grid.g2.end(); } - function render(g:kha.graphics2.Graphics) { + public function render(g:kha.graphics2.Graphics) { if (recompileMat) { recompileMat = false; if (Layers.isFillMaterial()) { diff --git a/Sources/arm/ui/UITrait.hx b/Sources/arm/ui/UITrait.hx index 9023630b..f5b1ed20 100644 --- a/Sources/arm/ui/UITrait.hx +++ b/Sources/arm/ui/UITrait.hx @@ -404,7 +404,7 @@ class UITrait { } } - function update() { + public function update() { if (textureExport) { textureExport = false; Exporter.exportTextures(textureExportPath); @@ -748,13 +748,13 @@ class UITrait { lastTooltip = ui.tooltipImg; } - function render(g:kha.graphics2.Graphics) { + public function render(g:kha.graphics2.Graphics) { if (System.windowWidth() == 0 || System.windowHeight() == 0) return; renderUI(g); } - function renderCursor(g:kha.graphics2.Graphics) { + public function renderCursor(g:kha.graphics2.Graphics) { // if (cursorImg == null) { // g.end(); // cursorImg = Image.createRenderTarget(256, 256); @@ -1186,7 +1186,6 @@ class UITrait { TabLayers.draw(); TabHistory.draw(); TabPlugins.draw(); - // TabBrowser.draw(); } if (ui.window(hwnd1, tabx, tabh, windowW, tabh1)) { Context.object = Context.paintObject; @@ -1197,6 +1196,7 @@ class UITrait { if (ui.window(hwnd2, tabx, tabh + tabh1, windowW, tabh2)) { TabTextures.draw(); TabMeshes.draw(); + TabBrowser.draw(); TabViewport.draw(); #if arm_creator TabWorld.draw(); diff --git a/Sources/arm/Macro.hx b/Sources/arm/util/BuildMacros.hx similarity index 71% rename from Sources/arm/Macro.hx rename to Sources/arm/util/BuildMacros.hx index e13c6885..c6c3d15a 100644 --- a/Sources/arm/Macro.hx +++ b/Sources/arm/util/BuildMacros.hx @@ -1,20 +1,20 @@ -package arm; - -import haxe.macro.Context; - -class Macro { - - macro public static function buildDate():ExprOf { - return Context.makeExpr(Date.now().toString(), Context.currentPos()); - } - - macro public static function buildSha():ExprOf { - var proc = new sys.io.Process('git', ['log', "--pretty=format:'%h'", '-n', '1']); - try { - return Context.makeExpr(proc.stdout.readLine(), Context.currentPos()); - } - catch (e:Dynamic) { - throw "- use 'git clone --recursive https://github.com/armory3d/armorpaint' to compile armorpaint"; - } - } -} +package arm.util; + +import haxe.macro.Context; + +class BuildMacros { + + macro public static function date():ExprOf { + return Context.makeExpr(Date.now().toString(), Context.currentPos()); + } + + macro public static function sha():ExprOf { + var proc = new sys.io.Process('git', ['log', "--pretty=format:'%h'", '-n', '1']); + try { + return Context.makeExpr(proc.stdout.readLine(), Context.currentPos()); + } + catch (e:Dynamic) { + throw "- use 'git clone --recursive https://github.com/armory3d/armorpaint' to compile armorpaint"; + } + } +} diff --git a/Sources/arm/util/MaterialUtil.hx b/Sources/arm/util/MaterialUtil.hx new file mode 100644 index 00000000..863a100a --- /dev/null +++ b/Sources/arm/util/MaterialUtil.hx @@ -0,0 +1,13 @@ +package arm.util; + +import iron.data.Data; + +class MaterialUtil { + + public static function removeMaterialCache() { + Data.cachedMaterials.remove("SceneMaterial2"); + Data.cachedShaders.remove("Material2_data"); + Data.cachedSceneRaws.remove("Material2_data"); + // Data.cachedBlobs.remove("Material2_data.arm"); + } +} diff --git a/Sources/arm/util/ParticleUtil.hx b/Sources/arm/util/ParticleUtil.hx new file mode 100644 index 00000000..49467a59 --- /dev/null +++ b/Sources/arm/util/ParticleUtil.hx @@ -0,0 +1,95 @@ +package arm.util; + +import haxe.Json; +import kha.arrays.Float32Array; +import iron.RenderPath; +import iron.Scene; +import iron.data.SceneFormat; +import iron.data.MaterialData; +import iron.object.Object; +import iron.object.MeshObject; +import arm.ui.UITrait; + +class ParticleUtil { + + static function f32(ar:Array):Float32Array { + var res = new Float32Array(ar.length); + for (i in 0...ar.length) res[i] = ar[i]; + return res; + } + + public static function initParticle() { + if (UITrait.inst.particleMaterial != null) return; + + var raw:TParticleData = { + name: "Particles", + type: 0, + loop: false, + render_emitter: false, + count: 1000, + frame_start: 0, + frame_end: 1000, + lifetime: 400, + lifetime_random: 0.5, + emit_from: 1, + object_align_factor: f32([0, 0, -40]), + factor_random: 2.0, + physics_type: 0, + particle_size: 1.0, + size_random: 0, + mass: 1, + instance_object: ".Particle", + weight_gravity: 1 + }; + Scene.active.raw.particle_datas = [raw]; + var particle_refs:Array = [ + { + name: "Particles", + particle: "Particles", + seed: 0 + } + ]; + + { + var t = new RenderTargetRaw(); + t.name = "texparticle"; + t.width = 0; + t.height = 0; + t.format = 'R8'; + t.scale = arm.render.Inc.getSuperSampling(); + RenderPath.active.createRenderTarget(t); + } + + for (mat in Scene.active.raw.material_datas) { + if (mat.name == 'Material2') { + var m:TMaterialData = Json.parse(Json.stringify(mat)); + m.name = 'MaterialParticle'; + Scene.active.raw.material_datas.push(m); + break; + } + } + + iron.data.Data.getMaterial("Scene", "MaterialParticle", function(md:MaterialData) { + UITrait.inst.particleMaterial = md; + + for (obj in Scene.active.raw.objects) { + if (obj.name == '.Sphere') { + var particle:TObj = Json.parse(Json.stringify(obj)); + particle.name = '.Particle'; + particle.is_particle = true; + particle.material_refs = ['MaterialParticle']; + Scene.active.raw.objects.push(particle); + for (i in 0...16) particle.transform.values[i] *= 0.01; + break; + } + } + + Scene.active.spawnObject(".Sphere", null, function(o:Object) { + var mo:MeshObject = cast o; + mo.name = ".ParticleEmitter"; + mo.raw.particle_refs = particle_refs; + mo.setupParticleSystem("Scene", particle_refs[0]); + }); + }); + } +} diff --git a/Sources/arm/util/Path.hx b/Sources/arm/util/Path.hx index 582dabe1..3fbcf36d 100644 --- a/Sources/arm/util/Path.hx +++ b/Sources/arm/util/Path.hx @@ -29,7 +29,7 @@ class Path { return base; } - public static function checkMeshFormat(path:String):Bool { + public static function isMesh(path:String):Bool { var p = path.toLowerCase(); return p.endsWith(".obj") || p.endsWith(".fbx") || @@ -37,7 +37,7 @@ class Path { p.endsWith(".blend"); } - public static function checkTextureFormat(path:String):Bool { + public static function isTexture(path:String):Bool { var p = path.toLowerCase(); return p.endsWith(".jpg") || p.endsWith(".jpeg") || @@ -49,24 +49,24 @@ class Path { p.endsWith(".hdr"); } - public static function checkFontFormat(path:String):Bool { + public static function isFont(path:String):Bool { var p = path.toLowerCase(); return p.endsWith(".ttf"); } - public static function checkProjectFormat(path:String):Bool { + public static function isProject(path:String):Bool { var p = path.toLowerCase(); return p.endsWith(".arm"); } - public static function checkPluginFormat(path:String):Bool { + public static function isPlugin(path:String):Bool { var p = path.toLowerCase(); return p.endsWith(".js"); // p.endsWith(".wasm") || // p.endsWith(".zip"); } - public static function checkBaseTex(p:String):Bool { + public static function isBaseTex(p:String):Bool { return p.endsWith("_albedo") || p.endsWith("_alb") || p.endsWith("_basecol") || @@ -80,20 +80,20 @@ class Path { p.endsWith("_col"); } - public static function checkOpacTex(p:String):Bool { + public static function isOpacTex(p:String):Bool { return p.endsWith("_opac") || p.endsWith("_alpha") || p.endsWith("_opacity"); } - public static function checkNorTex(p:String):Bool { + public static function isNorTex(p:String):Bool { return p.endsWith("_normal") || p.endsWith("_nor") || p.endsWith("_n") || p.endsWith("_nrm"); } - public static function checkOccTex(p:String):Bool { + public static function isOccTex(p:String):Bool { return p.endsWith("_ao") || p.endsWith("_occlusion") || p.endsWith("_ambientOcclusion") || @@ -101,7 +101,7 @@ class Path { p.endsWith("_occ"); } - public static function checkRoughTex(p:String):Bool { + public static function isRoughTex(p:String):Bool { return p.endsWith("_roughness") || p.endsWith("_roug") || p.endsWith("_r") || @@ -109,7 +109,7 @@ class Path { p.endsWith("_rgh"); } - public static function checkMetTex(p:String):Bool { + public static function isMetTex(p:String):Bool { return p.endsWith("_metallic") || p.endsWith("_metal") || p.endsWith("_metalness") || @@ -117,10 +117,28 @@ class Path { p.endsWith("_met"); } - public static function checkDispTex(p:String):Bool { + public static function isDispTex(p:String):Bool { return p.endsWith("_displacement") || p.endsWith("_height") || p.endsWith("_h") || p.endsWith("_disp"); } + + public static function isFolder(p:String):Bool { + return p.indexOf(".") == -1; + } + + #if krom_windows + public static function isAscii(s:String):Bool { + for (i in 0...s.length) if (s.charCodeAt(i) > 127) return false; + return true; + } + + public static function shortPath(s:String):String { + var cmd = 'for %I in ("' + s + '") do echo %~sI'; + var save = Krom.getFilesLocation() + "\\data\\tmp.txt"; + Krom.sysCommand(cmd + ' > "' + save + '"'); + return haxe.io.Bytes.ofData(Krom.loadBlob(save)).toString().rtrim(); + } + #end } diff --git a/Sources/arm/util/RenderUtil.hx b/Sources/arm/util/RenderUtil.hx index 2597fc5c..b45f1675 100644 --- a/Sources/arm/util/RenderUtil.hx +++ b/Sources/arm/util/RenderUtil.hx @@ -1,6 +1,7 @@ package arm.util; import kha.Image; +import kha.Font; import kha.graphics4.TextureFormat; import iron.Scene; import iron.RenderPath; @@ -10,6 +11,7 @@ import arm.ui.UITrait; import arm.render.RenderPathPreview; import arm.render.RenderPathDeferred; import arm.nodes.MaterialParser; +import arm.io.Importer; import arm.Tool; class RenderUtil { @@ -149,7 +151,7 @@ class RenderUtil { public static function makeTextPreview() { var text = UITrait.inst.textToolText; - var font = Tool.getTextToolFont(); + var font = getTextToolFont(); var fontSize = 200; var textW = Std.int(font.width(fontSize, text)); var textH = Std.int(font.height(fontSize)); @@ -171,6 +173,12 @@ class RenderUtil { g2.end(); } + static function getTextToolFont():Font { + var fontName = Importer.fontList[UITrait.inst.textToolHandle.position]; + if (fontName == 'default.ttf') return UITrait.inst.ui.ops.font; + return Importer.fontMap.get(fontName); + } + public static function makeDecalMaskPreview() { var texw = 1024; var off = 12;