From 127016abfaa13c0fec7575084f33c37efdb04a9f Mon Sep 17 00:00:00 2001 From: Clement Espeute Date: Mon, 29 Jan 2024 11:51:29 +0100 Subject: [PATCH] Prefab new is now extracted into __newInit() --- hrt/prefab/Curve.hx | 2 +- hrt/prefab/DynamicShader.hx | 2 +- hrt/prefab/Light.hx | 2 +- hrt/prefab/Macros.hx | 74 ++++++++++++++++++++++++++++ hrt/prefab/Material.hx | 2 +- hrt/prefab/Model.hx | 2 +- hrt/prefab/Prefab.hx | 7 ++- hrt/prefab/RenderProps.hx | 2 +- hrt/prefab/Shader.hx | 2 +- hrt/prefab/World.hx | 2 +- hrt/prefab/fx/Emitter.hx | 2 +- hrt/prefab/fx/Event.hx | 2 +- hrt/prefab/l2d/NoiseGenerator.hx | 2 +- hrt/prefab/l3d/Camera.hx | 12 +++-- hrt/prefab/l3d/Instance.hx | 2 +- hrt/prefab/l3d/Particles3D.hx | 2 +- hrt/prefab/l3d/Trail.hx | 2 +- hrt/prefab/l3d/Trails.hx | 6 +-- hrt/prefab/pbr/Anisotropy.hx | 2 +- hrt/prefab/rfx/Bloom.hx | 2 +- hrt/prefab/rfx/Border.hx | 13 +++-- hrt/prefab/rfx/Configurator.hx | 2 +- hrt/prefab/rfx/DistanceBlur.hx | 2 +- hrt/prefab/rfx/GenFog.hx | 2 +- hrt/prefab/rfx/Refraction.hx | 2 +- hrt/prefab/rfx/SSR.hx | 2 +- hrt/prefab/rfx/Sao.hx | 2 +- hrt/prefab/rfx/ScreenShaderGraph.hx | 2 +- hrt/prefab/rfx/TemporalBloom.hx | 2 +- hrt/prefab/vlm/LightProbe.hx | 2 +- hrt/prefab/vlm/VolumetricLightmap.hx | 2 +- hrt/shader/Refraction.hx | 2 +- 32 files changed, 124 insertions(+), 42 deletions(-) diff --git a/hrt/prefab/Curve.hx b/hrt/prefab/Curve.hx index 7764b51fc..f47c1c316 100644 --- a/hrt/prefab/Curve.hx +++ b/hrt/prefab/Curve.hx @@ -75,7 +75,7 @@ class Curve extends Prefab { return keys[keys.length-1].time; } - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/prefab/DynamicShader.hx b/hrt/prefab/DynamicShader.hx index 341194eb0..04223ab51 100644 --- a/hrt/prefab/DynamicShader.hx +++ b/hrt/prefab/DynamicShader.hx @@ -7,7 +7,7 @@ class DynamicShader extends Shader { @:s var isInstance : Bool = false; var isShadergraph : Bool = false; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/prefab/Light.hx b/hrt/prefab/Light.hx index d1578a679..7f0accd51 100644 --- a/hrt/prefab/Light.hx +++ b/hrt/prefab/Light.hx @@ -99,7 +99,7 @@ class Light extends Object3D { }; } - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); range = 10; zNear = 0.02; diff --git a/hrt/prefab/Macros.hx b/hrt/prefab/Macros.hx index 0ffb6fff1..1f5f71e6a 100644 --- a/hrt/prefab/Macros.hx +++ b/hrt/prefab/Macros.hx @@ -3,6 +3,7 @@ package hrt.prefab; import haxe.macro.Context; import haxe.macro.Expr; import haxe.macro.Type; +import haxe.macro.ExprTools; using Lambda; @@ -341,9 +342,82 @@ class Macros { for (f in buildFields2) { buildFields.push(f); } + + replaceNew(buildFields); + return buildFields; } + static public function replaceNew(fields: Array) { + final newName = "__newInit"; + var localClass = Context.getLocalClass().get(); + var isRoot = localClass.superClass == null; + + var newReplaced = false; + for (f in fields) { + if (f.name == "new") { + newReplaced = true; + f.name = newName; + if (!isRoot) { + f.access = f.access ?? []; + if (!f.access.contains(AOverride)) + f.access.push(AOverride); + } + + f.meta = f.meta ?? []; + if (f.meta.find((m) -> m.name == ":noCompletion") == null) { + f.meta.push({name: ":noCompletion", pos:Context.currentPos()}); + } + + switch (f.kind) { + case FFun(func): { + function rec(e: Expr){ + switch (e.expr) { + case ECall(subE = {expr: EConst(CIdent("super"))}, _): + subE.expr = EField({pos:e.pos, expr: EConst(CIdent("super"))}, newName); + case _: + ExprTools.iter(e, rec); + } + }; + ExprTools.iter(func.expr, rec); + + //trace(ExprTools.toString(func.expr)); + } + default: + throw "new is not a function"; + } + } + } + + if (newReplaced) { + var newExpr = null; + var access = [APublic]; + if (isRoot) { + newExpr = macro { + this.$newName(parent, contextShared); + }; + } + else { + access.push(AOverride); + newExpr = macro { + super(parent, contextShared); + }; + } + + var newDecl : Field = { + name: "new", + access: access, + kind: FFun({args:[ + {name: "parent", type: {macro: hrt.prefab.Prefab;}}, + {name: "contextShared", type: {macro: hrt.prefab.ContextShared;}}, + ],expr: newExpr}), + pos: Context.currentPos(), + } + + fields.push(newDecl); + } + } + static public function buildSerializable() { var buildFields = Context.getBuildFields(); var ser = buildSerializableInternal(buildFields); diff --git a/hrt/prefab/Material.hx b/hrt/prefab/Material.hx index 588be0f6d..994ae4f45 100644 --- a/hrt/prefab/Material.hx +++ b/hrt/prefab/Material.hx @@ -17,7 +17,7 @@ class Material extends Prefab { @:s public var refMatLib : String; @:s public var overrides : Array = []; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); props = {}; } diff --git a/hrt/prefab/Model.hx b/hrt/prefab/Model.hx index 59ee9e23f..11541fd82 100644 --- a/hrt/prefab/Model.hx +++ b/hrt/prefab/Model.hx @@ -7,7 +7,7 @@ class Model extends Object3D { @:s var retargetAnim : Bool = false; @:s var retargetIgnore : String; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/prefab/Prefab.hx b/hrt/prefab/Prefab.hx index 04d1f09e6..1e9157ab8 100644 --- a/hrt/prefab/Prefab.hx +++ b/hrt/prefab/Prefab.hx @@ -187,9 +187,12 @@ class Prefab { var thisClass = Type.getClass(this); + // We bypass the normal new function to avoid initializing the + // serializable fields, because they will be initialized by the copy function var inst = Type.createEmptyInstance(thisClass); - inst.initParentShared(parent, sh); - inst.postCloneInit(); // Macro function that init all the non serializable fields of a prefab + inst.postCloneInit(); // Macro function that init all the non serializable fields of a prefab + inst.__newInit(parent, sh);// Macro function that contains the code of the new function + inst.copy(this); inst.children = []; if (withChildren) { diff --git a/hrt/prefab/RenderProps.hx b/hrt/prefab/RenderProps.hx index fe5ff6e40..51c4aaeb7 100644 --- a/hrt/prefab/RenderProps.hx +++ b/hrt/prefab/RenderProps.hx @@ -8,7 +8,7 @@ class RenderProps extends Object3D { @:s var isDefault = false; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); props = {}; } diff --git a/hrt/prefab/Shader.hx b/hrt/prefab/Shader.hx index d28795e20..dfcd2aaed 100644 --- a/hrt/prefab/Shader.hx +++ b/hrt/prefab/Shader.hx @@ -11,7 +11,7 @@ class Shader extends Prefab { public var shader : hxsl.Shader; - function new(?parent, sh: ContextShared) { + function new(parent, sh: ContextShared) { super(parent, sh); props = {}; } diff --git a/hrt/prefab/World.hx b/hrt/prefab/World.hx index 1964b5487..b5d68a52d 100644 --- a/hrt/prefab/World.hx +++ b/hrt/prefab/World.hx @@ -34,7 +34,7 @@ class World extends Object3D { } var datDir : String; - public function new(?parent, ?shared) { + public function new(parent, shared) { super(parent, shared); chunkPrefabs = []; } diff --git a/hrt/prefab/fx/Emitter.hx b/hrt/prefab/fx/Emitter.hx index 0505f3f1a..2855b9ecf 100644 --- a/hrt/prefab/fx/Emitter.hx +++ b/hrt/prefab/fx/Emitter.hx @@ -1333,7 +1333,7 @@ class EmitterObject extends h3d.scene.Object { class Emitter extends Object3D { - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); props = { }; for(param in emitterParams) { diff --git a/hrt/prefab/fx/Event.hx b/hrt/prefab/fx/Event.hx index 791e5d86f..5bb33763a 100644 --- a/hrt/prefab/fx/Event.hx +++ b/hrt/prefab/fx/Event.hx @@ -23,7 +23,7 @@ class Event extends hrt.prefab.Prefab implements IEvent { public var lock:Bool = false; public var selected:Bool = false; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/prefab/l2d/NoiseGenerator.hx b/hrt/prefab/l2d/NoiseGenerator.hx index 47195802f..a3b36cfeb 100644 --- a/hrt/prefab/l2d/NoiseGenerator.hx +++ b/hrt/prefab/l2d/NoiseGenerator.hx @@ -37,7 +37,7 @@ class NoiseGenerator extends Object2D { var tex : h3d.mat.Texture; - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); seed = Std.random(100); } diff --git a/hrt/prefab/l3d/Camera.hx b/hrt/prefab/l3d/Camera.hx index b7020ebdf..f561f045b 100644 --- a/hrt/prefab/l3d/Camera.hx +++ b/hrt/prefab/l3d/Camera.hx @@ -36,7 +36,7 @@ class Camera extends Object3D { var beforePreviewCam : h3d.Camera; // Used to save scene camera controller's values #end - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } @@ -249,7 +249,8 @@ class Camera extends Object3D { editModeButton.val(preview ? "Preview Mode : Enabled" : "Preview Mode : Disabled"); editModeButton.toggleClass("editModeEnabled", preview); }; - editModeButton.click(function(_) { + + function onclick(e: js.jquery.Event) : Void { preview = !preview; setEditModeButton(); var cam = ctx.scene.s3d.camera; @@ -266,7 +267,9 @@ class Camera extends Object3D { } ctx.scene.editor.cameraController.lockZPlanes = true; ctx.scene.editor.cameraController.loadFromCamera(); - renderer.effects.push(new hrt.prefab.rfx.Border(null, null, 0.02, 0x0000ff, 0.5)); + var border = new hrt.prefab.rfx.Border(null, null); + border.setParams(0.02, 0x0000ff, 0.5); + renderer.effects.push(border); } else { for ( effect in findAll(hrt.prefab.rfx.RendererFX) ) @@ -319,7 +322,8 @@ class Camera extends Object3D { this.rotationZ = floatToStringPrecision(floatToStringPrecision(this.rotationZ)); }); } - }); + }; + editModeButton.click(onclick); var deprecationButton = props.find(".upgrade"); deprecationButton.click(function(_) { diff --git a/hrt/prefab/l3d/Instance.hx b/hrt/prefab/l3d/Instance.hx index c5d7d9f39..01c03fb1f 100644 --- a/hrt/prefab/l3d/Instance.hx +++ b/hrt/prefab/l3d/Instance.hx @@ -7,7 +7,7 @@ class Instance extends Object3D { var model : h3d.scene.Object; var icon : h2d.Object; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); props = {}; } diff --git a/hrt/prefab/l3d/Particles3D.hx b/hrt/prefab/l3d/Particles3D.hx index 1b8a3f6b0..98cadea4b 100644 --- a/hrt/prefab/l3d/Particles3D.hx +++ b/hrt/prefab/l3d/Particles3D.hx @@ -6,7 +6,7 @@ class Particles3D extends Object3D { @:s var data : Any; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/prefab/l3d/Trail.hx b/hrt/prefab/l3d/Trail.hx index 90fbc57a1..4d30f1f7b 100644 --- a/hrt/prefab/l3d/Trail.hx +++ b/hrt/prefab/l3d/Trail.hx @@ -4,7 +4,7 @@ class Trail extends Object3D { @:s var data : Dynamic; - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); data = new h3d.scene.Trail().save(); } diff --git a/hrt/prefab/l3d/Trails.hx b/hrt/prefab/l3d/Trails.hx index ea19974ec..f3e264229 100644 --- a/hrt/prefab/l3d/Trails.hx +++ b/hrt/prefab/l3d/Trails.hx @@ -812,13 +812,9 @@ class Trails extends Object3D { // Override this before calling make() to change how many trails are instancied public var numTrails : Int = 1; - function new(?parent, shared, ?numTrails : Int) { + function new(parent, shared) { super(parent, shared); name = "Trails"; - - if (numTrails != null) { - this.numTrails = numTrails; - } } public function create( ?parent : h3d.scene.Object, ?numTrails : Int ) { diff --git a/hrt/prefab/pbr/Anisotropy.hx b/hrt/prefab/pbr/Anisotropy.hx index eb3da3e2d..ce10d6378 100644 --- a/hrt/prefab/pbr/Anisotropy.hx +++ b/hrt/prefab/pbr/Anisotropy.hx @@ -23,7 +23,7 @@ class Anisotropy extends Prefab { @:s public var noiseDirectionPath : String = null; @:s public var rotationOffset : Float = 0.0; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent,shared); } diff --git a/hrt/prefab/rfx/Bloom.hx b/hrt/prefab/rfx/Bloom.hx index 335e00e87..f602cf6e3 100644 --- a/hrt/prefab/rfx/Bloom.hx +++ b/hrt/prefab/rfx/Bloom.hx @@ -26,7 +26,7 @@ class Bloom extends RendererFX { @:s public var blurQuality : Float = 1; @:s public var blurLinear : Float; - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); size = 0.5; blur = 3; diff --git a/hrt/prefab/rfx/Border.hx b/hrt/prefab/rfx/Border.hx index 3ddc69a70..707b8a231 100644 --- a/hrt/prefab/rfx/Border.hx +++ b/hrt/prefab/rfx/Border.hx @@ -15,15 +15,20 @@ class BorderShader extends h3d.shader.ScreenShader { class Border extends RendererFX { public var pass : h3d.pass.ScreenFx; + public var shader : BorderShader; - public function new(parent: Prefab, shared: ContextShared, size = 0.1, color: Int = 0, alpha = 1.0) { + public function new(parent: Prefab, shared: ContextShared) { super(parent, shared); - var shader = new BorderShader(); + shader = new BorderShader(); + setParams(); + pass = new h3d.pass.ScreenFx(shader); + pass.pass.setBlendMode(Alpha); + } + + public function setParams( size = 0.1, color: Int = 0, alpha = 1.0) { shader.size = size; shader.alpha = alpha; shader.color = h3d.Vector.fromColor(color); - pass = new h3d.pass.ScreenFx(shader); - pass.pass.setBlendMode(Alpha); } public override function begin( r : h3d.scene.Renderer, step : h3d.impl.RendererFX.Step ) { diff --git a/hrt/prefab/rfx/Configurator.hx b/hrt/prefab/rfx/Configurator.hx index 9450309b6..80e14fd0e 100644 --- a/hrt/prefab/rfx/Configurator.hx +++ b/hrt/prefab/rfx/Configurator.hx @@ -130,7 +130,7 @@ class Configurator extends RendererFX { #end var rootPrefab : Prefab; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/prefab/rfx/DistanceBlur.hx b/hrt/prefab/rfx/DistanceBlur.hx index 73a63386c..50aed5b18 100644 --- a/hrt/prefab/rfx/DistanceBlur.hx +++ b/hrt/prefab/rfx/DistanceBlur.hx @@ -70,7 +70,7 @@ class DistanceBlur extends RendererFX { @:s public var blurTextureSize : Float; @:s public var blurRange : Int; - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); nearEndDistance = 10; farStartDistance = 100; diff --git a/hrt/prefab/rfx/GenFog.hx b/hrt/prefab/rfx/GenFog.hx index c4b2d9352..f5a70f8f8 100644 --- a/hrt/prefab/rfx/GenFog.hx +++ b/hrt/prefab/rfx/GenFog.hx @@ -107,7 +107,7 @@ class GenFog extends RendererFX { @:s public var lightColorAmount : Float; @:s public var lightAngle : Float = 90.0; - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); renderMode = AfterTonemapping; endDistance = 100; diff --git a/hrt/prefab/rfx/Refraction.hx b/hrt/prefab/rfx/Refraction.hx index 5f8ceb2c6..b66fa7bd2 100644 --- a/hrt/prefab/rfx/Refraction.hx +++ b/hrt/prefab/rfx/Refraction.hx @@ -33,7 +33,7 @@ class RefractionShader extends hxsl.Shader { class Refraction extends RendererFX { var refractionShader : RefractionShader; - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); refractionShader = new RefractionShader(); diff --git a/hrt/prefab/rfx/SSR.hx b/hrt/prefab/rfx/SSR.hx index c5c6b568a..61edc2383 100644 --- a/hrt/prefab/rfx/SSR.hx +++ b/hrt/prefab/rfx/SSR.hx @@ -150,7 +150,7 @@ class SSR extends RendererFX { @:s public var support4K : Bool = false; @:s public var batchSample : Bool = false; - function new(?parent, ?shared) { + function new(parent, shared) { super(parent, shared); ssrPass = new h3d.pass.ScreenFx(new SSRShader()); } diff --git a/hrt/prefab/rfx/Sao.hx b/hrt/prefab/rfx/Sao.hx index b7affc757..c6750bacb 100644 --- a/hrt/prefab/rfx/Sao.hx +++ b/hrt/prefab/rfx/Sao.hx @@ -18,7 +18,7 @@ class Sao extends RendererFX { var saoBlur = new h3d.pass.Blur(); var saoCopy = new h3d.pass.Copy(); - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); blur = 5; samples = 30; diff --git a/hrt/prefab/rfx/ScreenShaderGraph.hx b/hrt/prefab/rfx/ScreenShaderGraph.hx index 0281e002b..1f04076a5 100644 --- a/hrt/prefab/rfx/ScreenShaderGraph.hx +++ b/hrt/prefab/rfx/ScreenShaderGraph.hx @@ -28,7 +28,7 @@ class ScreenShaderGraph extends RendererFX { @:s public var renderMode : ScreenShaderGraphMode; - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); renderMode = AfterTonemapping; } diff --git a/hrt/prefab/rfx/TemporalBloom.hx b/hrt/prefab/rfx/TemporalBloom.hx index 4f2d6d348..d6fb89f47 100644 --- a/hrt/prefab/rfx/TemporalBloom.hx +++ b/hrt/prefab/rfx/TemporalBloom.hx @@ -98,7 +98,7 @@ class TemporalBloom extends RendererFX { var tonemap = new Bloom.BloomTonemap(); - function new(?parent, shared: ContextShared) { + function new(parent, shared: ContextShared) { super(parent, shared); size = 0.5; downScaleCount = 5; diff --git a/hrt/prefab/vlm/LightProbe.hx b/hrt/prefab/vlm/LightProbe.hx index b82ffb125..b85734f76 100644 --- a/hrt/prefab/vlm/LightProbe.hx +++ b/hrt/prefab/vlm/LightProbe.hx @@ -215,7 +215,7 @@ class LightProbe extends Object3D { @:s public var debugDisplay : Bool = true; @:s public var sphereRadius : Float = 0.5; - public function new( ?parent : Prefab, shared: ContextShared) { + public function new(parent : Prefab, shared: ContextShared) { super(parent, shared); // Duplicate Name Fix - Prevent baked data conflict diff --git a/hrt/prefab/vlm/VolumetricLightmap.hx b/hrt/prefab/vlm/VolumetricLightmap.hx index 1ae6c7f51..08dfd2d39 100644 --- a/hrt/prefab/vlm/VolumetricLightmap.hx +++ b/hrt/prefab/vlm/VolumetricLightmap.hx @@ -20,7 +20,7 @@ class VolumetricLightmap extends Object3D { var baker : hide.view.l3d.ProbeBakerProcess; #end - public function new(?parent, shared: ContextShared) { + public function new(parent, shared: ContextShared) { super(parent, shared); } diff --git a/hrt/shader/Refraction.hx b/hrt/shader/Refraction.hx index 2dd4d66b3..79aaf7b74 100644 --- a/hrt/shader/Refraction.hx +++ b/hrt/shader/Refraction.hx @@ -20,7 +20,7 @@ class Refraction extends hrt.prefab.Shader { @:s var albedoMult : Float = 1.0; var refractionShader : RefractionPropsShader; - public function new(?parent, shared) { + public function new(parent, shared) { super(parent, shared); refractionShader = new RefractionPropsShader();