Skip to content

Commit

Permalink
Prefab new is now extracted into __newInit()
Browse files Browse the repository at this point in the history
  • Loading branch information
EspeuteClement committed Jan 29, 2024
1 parent 463f210 commit 127016a
Show file tree
Hide file tree
Showing 32 changed files with 124 additions and 42 deletions.
2 changes: 1 addition & 1 deletion hrt/prefab/Curve.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/DynamicShader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/Light.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
74 changes: 74 additions & 0 deletions hrt/prefab/Macros.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -341,9 +342,82 @@ class Macros {
for (f in buildFields2) {
buildFields.push(f);
}

replaceNew(buildFields);

return buildFields;
}

static public function replaceNew(fields: Array<Field>) {
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);
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/Material.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Material extends Prefab {
@:s public var refMatLib : String;
@:s public var overrides : Array<Dynamic> = [];

public function new(?parent, shared: ContextShared) {
public function new(parent, shared: ContextShared) {
super(parent, shared);
props = {};
}
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/Model.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
7 changes: 5 additions & 2 deletions hrt/prefab/Prefab.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/RenderProps.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/Shader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/World.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
}
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/fx/Emitter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/fx/Event.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/l2d/NoiseGenerator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 8 additions & 4 deletions hrt/prefab/l3d/Camera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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) )
Expand Down Expand Up @@ -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(_) {
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/l3d/Instance.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
}
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/l3d/Particles3D.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/l3d/Trail.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 1 addition & 5 deletions hrt/prefab/l3d/Trails.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/pbr/Anisotropy.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/Bloom.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 9 additions & 4 deletions hrt/prefab/rfx/Border.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ class BorderShader extends h3d.shader.ScreenShader {

class Border extends RendererFX {
public var pass : h3d.pass.ScreenFx<BorderShader>;
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 ) {
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/Configurator.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/DistanceBlur.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/GenFog.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/Refraction.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/SSR.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/Sao.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion hrt/prefab/rfx/ScreenShaderGraph.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 127016a

Please sign in to comment.